Skip to content

Commit

Permalink
Modularize secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfontes committed Dec 20, 2023
1 parent 1855a0f commit 8cd3dc2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 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))))

0 comments on commit 8cd3dc2

Please sign in to comment.