Skip to content

Commit b1d703b

Browse files
committed
Add default search for .secrets.edn file in the home directory
1 parent 5043c27 commit b1d703b

5 files changed

Lines changed: 38 additions & 22 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ Any one secrets file contains an encrypted version of a single
1010
[EDN](https://github.com/edn-format/edn) map, with arbitrary levels of
1111
labeled nesting.
1212

13-
By default, the secrets file lives at .secrets.edn in the working directory,
14-
but this path can be changed explicitly via `with-path` (or via the `:path`
15-
flag at the command line).
13+
The path used for the secrets file, in priority order, is one of:
14+
15+
- the one explicitly specified via `with-path` (or `:path` at the command line),
16+
- the `.secrets.edn` file in the working directory, or
17+
- the `.secrets.edn` file in the home directory.
1618

1719

1820
## Contents

secrets

-10.5 MB
Binary file not shown.

src/io/sixtant/secrets.clj

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
[EDN](https://github.com/edn-format/edn) map, with arbitrary levels of
1010
labeled nesting.
1111
12-
By default, the secrets file lives at .secrets.edn in the working directory,
13-
but this path can be changed explicitly via `with-path` (or via the `:path`
14-
flag at the command line)."
12+
The path used for the secrets file, in priority order, is one of:
13+
14+
- the one explicitly specified via `with-path` (or `:path` at the command
15+
line),
16+
- the `.secrets.edn` file in the working directory, or
17+
- the `.secrets.edn` file in the home directory."
1518
(:require [buddy.core.codecs :as codecs]
1619
[buddy.core.nonce :as nonce]
1720
[buddy.core.crypto :as crypto]
@@ -27,18 +30,27 @@
2730
(set! *warn-on-reflection* true)
2831

2932

30-
(def ^:dynamic *path*
31-
"Location of saved secrets on disk."
32-
".secrets.edn")
33+
(defn default-secrets-path []
34+
(if (.exists (io/file ".secrets.edn"))
35+
".secrets.edn"
36+
(let [home (System/getProperty "user.home")]
37+
(.getCanonicalPath (io/file home ".secrets.edn")))))
3338

3439

40+
(def ^:dynamic *path* "Explicitly bound path." nil)
3541
(defmacro with-path
3642
"Set the file path used for saved secrets."
3743
[path & body]
3844
`(binding [*path* ~path]
3945
~@body))
4046

4147

48+
(defn secrets-path
49+
"Location of saved secrets on disk."
50+
[]
51+
(or *path* (default-secrets-path)))
52+
53+
4254
;;; Primitives for key stretching & encryption
4355

4456

@@ -185,22 +197,24 @@
185197
(defn read-secrets
186198
"Prefer `with-secrets`."
187199
[]
188-
(if (.isFile (io/file *path*))
189-
(let [p (or *password* (read-password "Password: "))]
190-
{:data (decrypt-from-disk {:password p :path *path*})
191-
:password p})
192-
{:data {}
193-
:password nil}))
200+
(let [path (secrets-path)]
201+
(if (.isFile (io/file path))
202+
(let [p (or *password* (read-password (str "Password for " path ": ")))]
203+
{:data (decrypt-from-disk {:password p :path path})
204+
:password p})
205+
{:data {}
206+
:password nil})))
194207

195208

196209
(defn write-secrets
197210
"Prefer `swap-secrets!`."
198211
[{:keys [data password]}]
199212
(print "Encrypting data for writing...")
200213
(flush)
201-
(let [enc (encrypt-to-disk data {:password password :path *path*})]
214+
(let [path (secrets-path)
215+
enc (encrypt-to-disk data {:password password :path path})]
202216
(println " Done.")
203-
(println "Wrote" (count (.getBytes (prn-str enc))) "bytes.")
217+
(println "Wrote" (count (.getBytes (prn-str enc))) "bytes to" (str path "."))
204218
enc))
205219

206220

@@ -259,7 +273,7 @@
259273
(defn rand-n [coll n] (repeatedly n #(nth coll (rng-int (count coll)))))
260274
(def passphrase
261275
(let [words (string/split-lines (slurp "/usr/share/dict/words"))]
262-
(string/join " " (rand-n words 6))))
276+
(string/join " " (rand-n words 5))))
263277

264278
;; Encrypt some data with the passphrase to send to somebody else
265279
(encrypt (prn-str {:foo :bar}) passphrase)

src/io/sixtant/secrets/main.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"Given all of the leftover `args`, parse out a :path parameter if any,
3333
otherwise use the default path for secrets."
3434
[args]
35-
(or (get (apply hash-map args) ":path") s/*path*))
35+
(or (get (apply hash-map args) ":path") (s/secrets-path)))
3636

3737

3838
(defcommand inspect
@@ -111,7 +111,7 @@
111111
(let [args (rest args)
112112
[path args] (if (= (first args) :path)
113113
[(second args) (rest (rest args))]
114-
[s/*path* args])
114+
[(s/secrets-path) args])
115115
[vname->path & args] args]
116116
(assert (string? vname->path) "first parameter is an EDN string")
117117
(let [vname->path (read-string vname->path)]
@@ -138,7 +138,7 @@
138138
(flush)
139139
(= (string/lower-case (read-line)) "y")))]
140140
(if change?
141-
(let [p (s/read-password "Set password: ")
141+
(let [p (s/read-password (str "Set password for " (s/secrets-path) ": "))
142142
p' (s/read-password "Confirm password: ")]
143143
(if (= p p')
144144
(assoc x :password p)

test/io/sixtant/secrets_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
(let [temp (temp "encrypted" ".edn")
5858
data {:bitso {:prod {:key "foo" :secret "bar"}}}]
5959

60-
; Write data to a temporary secrets fil
60+
; Write data to a temporary secrets file
6161
(with-path temp
6262
(write-secrets {:data data :password "pass"}))
6363

0 commit comments

Comments
 (0)