Skip to content

Commit

Permalink
Add secrets to :context map
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfontes authored Dec 19, 2023
1 parent 05d7374 commit 3f04622
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
31 changes: 29 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Why Babashka for OpenFaaS instead of Clojure proper?:
** that Function cold start times are kept to a minimum.
** a rapid development feedback loop.
** a simple REPL workflow - just start everything all over again, it's fast.
* Babashka is designed for minimal memory usage, allowing you to scale up your single-purpose functions with comparable RAM consumption.
* Babashka is designed for minimal memory usage, allowing you to scale up your single-purpose Functions with comparable RAM consumption.

== Prerequisites

Expand Down Expand Up @@ -103,6 +103,31 @@ my-function:
:my-env2 2}
----

If you declare secrets in the `stack.yml` file, these will be available in `:context` map as well. Let's start with an example:
[source, yml]
----
my-function:
lang: bb
handler: ./anything/my-function
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/my-function
secrets:
- foo
- baz
----
A secret value that is an EDN string, when internally parsed as a Clojure map, will have its content spliced into the `:context` map. Otherwise, the secret will be available in the `:context` map as `{:secret-name <secret-value>}`, with `<secret-value>` parsed as a Clojure data structure other than a map.

Following up from the definition of `my-function` above, we define the following secrets:
[source, bash]
----
echo 'bar' | faas-cli secret create foo
echo '{:spam "eggs"}' | faas-cli secret create baz
----
becomes this in the `:context` map:
[source, clojure]
----
{:foo "bar" :spam "eggs"}
----

In `bb-streaming` language:
[source, clojure]
----
Expand All @@ -113,7 +138,9 @@ The `event` is extracted from the HTTP payload body, and the function return is

=== Function tests

To test the code of a Function, a `test` directory in the Function's top-level directory is provided, containing:
Tests for your Function run when you build the Function image (`faas-cli build`).

A `test` directory in the Function's top-level directory is provided, containing:

* `run_tests.clj`, with the namespace that will be used to run the tests. You can use any test library and test runner you like.
* a `bb.edn` file where test dependencies can be added. Remove this file if you don't need to add any dependencies.
Expand Down
21 changes: 20 additions & 1 deletion template/bb/index.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@
[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]
[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 All @@ -36,7 +54,8 @@

(defn -main []
(let [env (ring-walk/format-context (System/getenv))
context (merge env (->secrets))
faas-fn (wrap-arg #'function/handler)]
(run-server (->app faas-fn env)
(run-server (->app faas-fn context)
{:port 8082})
@(promise)))
5 changes: 5 additions & 0 deletions template/bb/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"asd" => "asd"
:qwer => "qwer")

(eg index/->secret
[_ "{:foo \"bar\"}"] => {:foo "bar"}
["foo" "bar"] => {:foo "bar"}
["foo" "[1 \"baz\"]"] => {:foo [1 "baz"]})

(eg index/keywords?
true => true
false => false
Expand Down

0 comments on commit 3f04622

Please sign in to comment.