From af3c300c35a600a5d3a70ac7e5dedd0e60a9492b Mon Sep 17 00:00:00 2001 From: Hugo Duncan Date: Thu, 17 Jun 2010 15:47:39 -0400 Subject: [PATCH] added add-identity overload for passing keys as byte arrays --- src/clj_ssh/ssh.clj | 12 ++++++++---- test/clj_ssh/ssh_test.clj | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/clj_ssh/ssh.clj b/src/clj_ssh/ssh.clj index bd0bcbf..e945c0b 100644 --- a/src/clj_ssh/ssh.clj +++ b/src/clj_ssh/ssh.clj @@ -127,9 +127,9 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)" (defn has-identity? "Check if the given identity is present." ([name] (has-identity? *ssh-agent* name)) - ([agent name] (some #(= name %) (.getIdentityNames agent)))) + ([#^JSch agent name] (some #(= name %) (.getIdentityNames agent)))) -(defn make-identity +(defn #^Identity make-identity "Create a JSch identity. This can be used to check whether the key is encrypted." ([private-key-path public-key-path] @@ -157,7 +157,11 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)" (if (instance? Identity private-key) private-key (file-path private-key)) - (and passphrase (.getBytes passphrase))))) + (and passphrase (.getBytes passphrase)))) + ([#^JSch agent #^String name #^bytes private-key #^bytes public-key + #^bytes passphrase] + (.addIdentity + agent name private-key public-key passphrase))) (defn add-identity-with-keychain "Add a private key, only if not already known, using the keychain to obtain @@ -340,7 +344,7 @@ keys. All other option key pairs will be passed as SSH config options." (doto exec (.setInputStream (if (string? in) - (java.io.ByteArrayInputStream. (.getBytes in)) + (java.io.ByteArrayInputStream. (.getBytes #^String in)) in) false) (.setOutputStream out-stream) diff --git a/test/clj_ssh/ssh_test.clj b/test/clj_ssh/ssh_test.clj index 92ce4d7..351c37a 100644 --- a/test/clj_ssh/ssh_test.clj +++ b/test/clj_ssh/ssh_test.clj @@ -28,9 +28,15 @@ list, Alan Dipert and MeikelBrandmeyer." (defn private-key-path [] (str (. System getProperty "user.home") "/.ssh/clj_ssh")) +(defn public-key-path + [] (str (. System getProperty "user.home") "/.ssh/clj_ssh.pub")) + (defn encrypted-private-key-path [] (str (. System getProperty "user.home") "/.ssh/clj_ssh_pp")) +(defn encrypted-public-key-path + [] (str (. System getProperty "user.home") "/.ssh/clj_ssh_pp.pub")) + (defn username [] (or (. System getProperty "ssh.username") (. System getProperty "user.name"))) @@ -111,7 +117,17 @@ list, Alan Dipert and MeikelBrandmeyer." (with-ssh-agent [false] (add-identity key) (is (= 1 (count (.getIdentityNames *ssh-agent*)))) - (add-identity *ssh-agent* key)))) + (add-identity *ssh-agent* key))) + (testing "passing byte arrays" + (with-ssh-agent [false] + (add-identity + *ssh-agent* + "name" + (.getBytes (slurp (private-key-path))) + (.getBytes (slurp (public-key-path))) + nil) + (is (= 1 (count (.getIdentityNames *ssh-agent*)))) + (is (= "name" (first (.getIdentityNames *ssh-agent*))))))) (deftest has-identity?-test (let [key (private-key-path)]