-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
55 lines (48 loc) · 1.85 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns aerogrant.core
(:require [aero.core :as aero]
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as string]
[cognitect.aws.client.api :as aws]
[aerogrant.web-ident :as wident]
[integrant.core :as ig]))
(defn read-config
([profile]
(-> "config.edn"
io/resource
(aero/read-config {:profile profile})))
([profile opts]
(-> "config.edn"
io/resource
(aero/read-config {:profile profile} opts))))
(defn sm-client [region]
(try (aws/client {:api :secretsmanager
:region (or region
(System/getenv "AWS_REGION")
(System/getProperty "AWS_REGION")
(System/getenv "aws.region")
(System/getProperty "aws.region"))
:credentials-provider (wident/default-credentials-provider)})
(catch Exception e (str "issue contacting secrets manager " e))))
(defmethod aero/reader 'trim
[_ _ [value]]
(when value (string/trim value)))
(defmethod aero/reader 'ig/ref
[_ _ value]
(ig/ref value))
(defmethod aero/reader 'ig/refset
[_ _ value]
(ig/refset value))
(defmethod aero/reader 'asm
[_ _ [secret key region]]
(let [secrets (:SecretString (aws/invoke (sm-client region)
{:op :GetSecretValue
:request {:SecretId secret}}))]
(try
;; Attempt to parse the string as JSON
(if key
(get-in (json/parse-string secrets) [key])
(json/parse-string secrets))
;; If JSON parsing fails, catch the exception and just return the original string
(catch Exception e
secrets))))