-
-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use gpg binary for signing and reading credentials from encrypted file #311
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,14 @@ | |
[cemerick.pomegranate.aether :as aether] | ||
[boot.util :as util] | ||
[boot.pod :as pod] | ||
[boot.gpg :as gpg] | ||
[boot.from.io.aviso.ansi :as ansi] | ||
[boot.kahnsort :as ksort]) | ||
(:import | ||
[boot App] | ||
[java.io File] | ||
[java.util.jar JarFile] | ||
[java.util.regex Pattern] | ||
[org.sonatype.aether.resolution DependencyResolutionException])) | ||
|
||
(def offline? (atom false)) | ||
|
@@ -59,13 +62,75 @@ | |
:password password | ||
:non-proxy-hosts (get-non-proxy-hosts)})))) | ||
|
||
(defn ^{:boot/from :technomancy/leiningen} credentials-fn | ||
"Decrypt map from credentials.clj.gpg in Boot home if present." | ||
([] (let [cred-file (io/file (App/bootdir) "credentials.clj.gpg")] | ||
(if (.exists cred-file) | ||
(credentials-fn cred-file)))) | ||
([file] | ||
(let [{:keys [out err exit]} (gpg/gpg "--quiet" "--batch" | ||
"--decrypt" "--" (str file))] | ||
(if (pos? exit) | ||
(binding [*out* *err*] | ||
(util/warn (format (str "Could not decrypt credentials from %s\n" | ||
"%s\n" | ||
"See `boot gpg --help` for how to install gpg.") | ||
(str file) err))) | ||
(read-string out))))) | ||
|
||
(def credentials (memoize credentials-fn)) | ||
|
||
(defn- ^{:boot/from :technomancy/leiningen} match-credentials [settings auth-map] | ||
(get auth-map (:url settings) | ||
(first (for [[re? cred] auth-map | ||
:when (and (instance? Pattern re?) | ||
(re-find re? (:url settings)))] | ||
cred)))) | ||
|
||
(defn- ^{:boot/from :technomancy/leiningen} resolve-credential | ||
"Resolve key-value pair from result into a credential, updating result." | ||
[source-settings result [k v]] | ||
(letfn [(resolve [v] | ||
(cond (= :env v) | ||
(System/getenv (str "BOOT_" (string/upper-case (name k)))) | ||
|
||
(and (keyword? v) (= "env" (namespace v))) | ||
(System/getenv (string/upper-case (name v))) | ||
|
||
(= :gpg v) | ||
(get (match-credentials source-settings (credentials)) k) | ||
|
||
(coll? v) ;; collection of places to look | ||
(->> (map resolve v) | ||
(remove nil?) | ||
first) | ||
|
||
:else v))] | ||
(if (#{:username :password :passphrase :private-key-file} k) | ||
(assoc result k (resolve v)) | ||
(assoc result k v)))) | ||
|
||
(defn ^{:boot/from :technomancy/leiningen} resolve-credentials | ||
"Applies credentials from the environment or ~/.boot/credentials.clj.gpg | ||
as they are specified and available." | ||
[settings] | ||
(let [gpg-creds (if (= :gpg (:creds settings)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if Perhaps only useful for Leiningen compatibility. |
||
(match-credentials settings (credentials))) | ||
resolved (reduce (partial resolve-credential settings) | ||
(empty settings) | ||
settings)] | ||
(if gpg-creds | ||
(dissoc (merge gpg-creds resolved) :creds) | ||
resolved))) | ||
|
||
(defn resolve-dependencies* | ||
[env] | ||
(try | ||
(aether/resolve-dependencies | ||
:coordinates (:dependencies env) | ||
:repositories (->> (or (:repositories env) @default-repositories) | ||
(map (juxt first (fn [[x y]] (if (map? y) y {:url y})))) | ||
(map (juxt first (fn [[x y]] (resolve-credentials y)))) | ||
(map (juxt first (fn [[x y]] (update-in y [:update] #(or % @update?)))))) | ||
:local-repo (or (:local-repo env) @local-repo nil) | ||
:offline? (or @offline? (:offline? env)) | ||
|
@@ -167,17 +232,18 @@ | |
:local-repo (or (:local-repo env) @local-repo nil)))) | ||
|
||
(defn deploy | ||
[env repo jarfile & [artifact-map]] | ||
[env [repo-id repo-settings] jarfile & [artifact-map]] | ||
(let [{:keys [project version]} | ||
(-> jarfile pod/pom-properties pod/pom-properties-map) | ||
pomfile (doto (File/createTempFile "pom" ".xml") | ||
.deleteOnExit (spit (pod/pom-xml jarfile)))] | ||
.deleteOnExit (spit (pod/pom-xml jarfile))) | ||
repo-settings (resolve-credentials repo-settings)] | ||
(aether/deploy | ||
:coordinates [project version] | ||
:jar-file (io/file jarfile) | ||
:pom-file (io/file pomfile) | ||
:artifact-map artifact-map | ||
:repository [repo] | ||
:repository {repo-id repo-settings} | ||
:local-repo (or (:local-repo env) @local-repo nil)))) | ||
|
||
(def ^:private wagon-files (atom #{})) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(ns boot.gpg | ||
(:require | ||
[clojure.java.io :as io] | ||
[clojure.java.shell :as shell] | ||
[boot.pod :as pod] | ||
[boot.util :as util]) | ||
(:import [java.io StringReader File])) | ||
|
||
(defn ^{:boot/from :technomancy/leiningen} gpg-program | ||
"Lookup the gpg program to use, defaulting to 'gpg'" | ||
[] | ||
(or (System/getenv "BOOT_GPG") "gpg")) | ||
|
||
(defn- ^{:boot/from :technomancy/leiningen} get-english-env [] | ||
"Returns environment variables as a map with clojure keywords and LANGUAGE set to 'en'" | ||
(let [env (System/getenv) | ||
keywords (map #(keyword %) (keys env))] | ||
(merge (zipmap keywords (vals env)) | ||
{:LANGUAGE "en"}))) | ||
|
||
(defn ^{:boot/from :technomancy/leiningen} gpg | ||
"Shells out to (gpg-program) with the given arguments" | ||
[& args] | ||
(let [env (get-english-env)] | ||
(try | ||
(shell/with-sh-env env | ||
(apply shell/sh (gpg-program) args)) | ||
(catch java.io.IOException e | ||
{:exit 1 :err (.getMessage e)})))) | ||
|
||
(defn ^{:boot/from :technomancy/leiningen} signing-args | ||
"Produce GPG arguments for signing a file." | ||
[file opts] | ||
(let [key-spec (if-let [key (:gpg-key opts)] | ||
["--default-key" key]) | ||
passphrase-spec (if-let [pass (:gpg-passphrase opts)] | ||
["--passphrase-fd" "0"]) | ||
passphrase-in-spec (if-let [pass (:gpg-passphrase opts)] | ||
[:in (StringReader. pass)])] | ||
`["--yes" "-ab" ~@key-spec ~@passphrase-spec "--" ~file ~@passphrase-in-spec])) | ||
|
||
(defn ^{:boot/from :technomancy/leiningen} sign | ||
"Create a detached signature and return the signature file name." | ||
[file opts] | ||
(let [{:keys [err exit]} (apply gpg (signing-args file opts))] | ||
(when-not (zero? exit) | ||
(util/fail (str "Could not sign " file "\n" err | ||
"\n\nIf you don't expect people to need to verify the " | ||
"authorship of your jar, don't set :gpg-sign option of push task to true.\n"))) | ||
(str file ".asc"))) | ||
|
||
(defn sign-jar | ||
[outdir jarfile opts] | ||
(shell/with-sh-dir | ||
outdir | ||
(let [jarname (.getName jarfile) | ||
jarout (io/file outdir (str jarname ".asc")) | ||
pomfile (doto (File/createTempFile "pom" ".xml") | ||
(.deleteOnExit) | ||
(spit (pod/pom-xml jarfile))) | ||
pomout (io/file outdir (.replaceAll jarname "\\.jar$" ".pom.asc")) | ||
sign-it #(sign (.getPath %) opts)] | ||
(spit pomout (sign-it pomfile)) | ||
(spit jarout (sign-it jarfile)) | ||
{[:extension "jar.asc"] (.getPath jarout) | ||
[:extension "pom.asc"] (.getPath pomout)}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dropped suport for defining repositories as string instead of map with
:url
property. Could be still added back.