From cccadc9a3d805fa017f2ea1b1c3f18f2ff20848c Mon Sep 17 00:00:00 2001 From: Carlos Fontes Date: Wed, 20 Dec 2023 19:14:59 +0900 Subject: [PATCH] Modularize secrets --- template/bb/index.clj | 19 +------------------ template/bb/lib/secrets.clj | 22 ++++++++++++++++++++++ template/bb/tests.clj | 3 ++- 3 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 template/bb/lib/secrets.clj diff --git a/template/bb/index.clj b/template/bb/index.clj index 2663fb7..471a65b 100755 --- a/template/bb/index.clj +++ b/template/bb/index.clj @@ -9,27 +9,10 @@ [ring.middleware.text :refer [wrap-text-body]] [ring.middleware.headers :refer [wrap-lowercase-headers wrap-friendly-headers]] [ring.util.walk :as ring-walk] - [ring.util.string :as ring-string] + [secrets :refer [->secrets]] [compojure.response :as response] - [babashka.fs :as fs] - [clojure.edn :as edn] [handler :as function])) -(defn ->secret [filepath secret-raw] - (let [secret (edn/read-string secret-raw)] - (if (map? secret) - secret - (let [secret-key (-> filepath fs/file-name keyword)] - {secret-key (ring-string/read-string secret-raw)})))) - -(defn ->secrets [] - (when (fs/exists? "/var/openfaas/secrets") - (->> (fs/list-dir "/var/openfaas/secrets") - (map #(let [filepath (-> % str) - secret-raw (slurp filepath)] - (->secret filepath secret-raw))) - (apply merge)))) - (def keywords? #(if (nil? %) true %)) (def fn-arg-cnt #(some-> % meta :arglists first count)) diff --git a/template/bb/lib/secrets.clj b/template/bb/lib/secrets.clj new file mode 100644 index 0000000..1365981 --- /dev/null +++ b/template/bb/lib/secrets.clj @@ -0,0 +1,22 @@ +(ns secrets + (:require + [ring.util.string :as ring-string] + [babashka.fs :as fs] + [clojure.edn :as edn])) + +(def secrets-dir "/var/openfaas/secrets") + +(defn ->secret [filepath secret-raw] + (let [secret (edn/read-string secret-raw)] + (if (map? secret) + secret + (let [secret-key (-> filepath fs/file-name keyword)] + {secret-key (ring-string/read-string secret-raw)})))) + +(defn ->secrets [] + (when (fs/exists? secrets-dir) + (->> (fs/list-dir secrets-dir) + (map #(let [filepath (-> % str) + secret-raw (slurp filepath)] + (->secret filepath secret-raw))) + (apply merge)))) diff --git a/template/bb/tests.clj b/template/bb/tests.clj index 0689873..db4d7d8 100644 --- a/template/bb/tests.clj +++ b/template/bb/tests.clj @@ -9,6 +9,7 @@ [ring.middleware.text :as middleware-text] [ring.util.walk :as ring-walk] [ring.util.string :as ring-string] + [secrets] [index])) (eg middleware-text/text-request? @@ -33,7 +34,7 @@ "asd" => "asd" :qwer => "qwer") -(eg index/->secret +(eg secrets/->secret [_ "{:foo \"bar\"}"] => {:foo "bar"} ["foo" "bar"] => {:foo "bar"} ["foo" "[1 \"baz\"]"] => {:foo [1 "baz"]})