Skip to content

Commit

Permalink
Modularize secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfontes authored Dec 20, 2023
1 parent 1855a0f commit cccadc9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
19 changes: 1 addition & 18 deletions template/bb/index.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 22 additions & 0 deletions template/bb/lib/secrets.clj
Original file line number Diff line number Diff line change
@@ -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))))
3 changes: 2 additions & 1 deletion template/bb/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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"]})
Expand Down

0 comments on commit cccadc9

Please sign in to comment.