Skip to content

Commit

Permalink
added add-identity overload for passing keys as byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoduncan committed Jun 17, 2010
1 parent 606d878 commit af3c300
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/clj_ssh/ssh.clj
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion test/clj_ssh/ssh_test.clj
Expand Up @@ -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")))
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit af3c300

Please sign in to comment.