Skip to content

Commit

Permalink
Allow overriding password prompt via (with-password p ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdowney committed Sep 11, 2021
1 parent 82637a1 commit 5043c27
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/io/sixtant/secrets.clj
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,25 @@
;;; Main API: read & update secrets


(def ^:dynamic *password* "See `with-password`." nil)


(defmacro with-password
"Any calls inside `body` which would otherwise prompt for a password instead
use the given `password`."
[password & body]
`(binding [*password* ~password]
~@body))


(def ^:dynamic *secrets* "See `with-secrets`." nil)


(defn read-secrets
"Prefer `with-secrets`."
[]
(if (.isFile (io/file *path*))
(let [p (read-password "Password: ")]
(let [p (or *password* (read-password "Password: "))]
{:data (decrypt-from-disk {:password p :path *path*})
:password p})
{:data {}
Expand Down
57 changes: 50 additions & 7 deletions test/io/sixtant/secrets_test.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
(ns io.sixtant.secrets-test
(:require [clojure.test :refer :all]
[clojure.java.io :as io]
[io.sixtant.secrets :refer :all])
(:import (clojure.lang ExceptionInfo)
(java.io File)))


(def ^:dynamic *temp-files* [])


(defn temp
"Return the path to a temporary file with the given `prefix` and `suffix`."
[prefix suffix]
(let [p (.getCanonicalPath (File/createTempFile prefix suffix))]
(try
(set! *temp-files* (conj *temp-files* p))
(catch IllegalStateException _
(throw (ex-info "`temp` called outside of `with-temp-files`" {}))))
p))


(defmacro with-temp-files
"Ensure the deletion of any temp files created via `temp`."
[& body]
`(binding [*temp-files* []]
(try
(do ~@body)
(finally
(run!
(fn [path#] (.delete (io/file path#)))
*temp-files*)))))


(deftest encrypt-decrypt-test
(let [data "secret data"
pass "pass"]
Expand All @@ -15,10 +42,26 @@


(deftest encrypt-to-disk-test
(let [temp (.getCanonicalPath (File/createTempFile "encrypted" ".edn"))
conf {:password "pass" :path temp}
data {:bitso {:production {:key "foo" :secret "bar"}}}]
(testing "Encryption & persistence of Clojure data structures"
(encrypt-to-disk data conf)
(is (not= (read-string (slurp temp)) data) "Data encrypted on disk")
(is (= (decrypt-from-disk conf) data) "Can be decrypted"))))
(with-temp-files
(let [temp (temp "encrypted" ".edn")
conf {:password "pass" :path temp}
data {:bitso {:production {:key "foo" :secret "bar"}}}]
(testing "Encryption & persistence of Clojure data structures"
(encrypt-to-disk data conf)
(is (not= (read-string (slurp temp)) data) "Data encrypted on disk")
(is (= (decrypt-from-disk conf) data) "Can be decrypted")))))


(deftest high-level-api-test
(with-temp-files
(let [temp (temp "encrypted" ".edn")
data {:bitso {:prod {:key "foo" :secret "bar"}}}]

; Write data to a temporary secrets fil
(with-path temp
(write-secrets {:data data :password "pass"}))

(with-password "pass"
(with-path temp
(with-secrets
(is (= (secrets :bitso :prod :key) "foo"))))))))

0 comments on commit 5043c27

Please sign in to comment.