Skip to content

Commit af3c300

Browse files
committed
added add-identity overload for passing keys as byte arrays
1 parent 606d878 commit af3c300

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/clj_ssh/ssh.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
127127
(defn has-identity?
128128
"Check if the given identity is present."
129129
([name] (has-identity? *ssh-agent* name))
130-
([agent name] (some #(= name %) (.getIdentityNames agent))))
130+
([#^JSch agent name] (some #(= name %) (.getIdentityNames agent))))
131131

132-
(defn make-identity
132+
(defn #^Identity make-identity
133133
"Create a JSch identity. This can be used to check whether the key is
134134
encrypted."
135135
([private-key-path public-key-path]
@@ -157,7 +157,11 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
157157
(if (instance? Identity private-key)
158158
private-key
159159
(file-path private-key))
160-
(and passphrase (.getBytes passphrase)))))
160+
(and passphrase (.getBytes passphrase))))
161+
([#^JSch agent #^String name #^bytes private-key #^bytes public-key
162+
#^bytes passphrase]
163+
(.addIdentity
164+
agent name private-key public-key passphrase)))
161165

162166
(defn add-identity-with-keychain
163167
"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."
340344
(doto exec
341345
(.setInputStream
342346
(if (string? in)
343-
(java.io.ByteArrayInputStream. (.getBytes in))
347+
(java.io.ByteArrayInputStream. (.getBytes #^String in))
344348
in)
345349
false)
346350
(.setOutputStream out-stream)

test/clj_ssh/ssh_test.clj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ list, Alan Dipert and MeikelBrandmeyer."
2828
(defn private-key-path
2929
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh"))
3030

31+
(defn public-key-path
32+
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh.pub"))
33+
3134
(defn encrypted-private-key-path
3235
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh_pp"))
3336

37+
(defn encrypted-public-key-path
38+
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh_pp.pub"))
39+
3440
(defn username
3541
[] (or (. System getProperty "ssh.username")
3642
(. System getProperty "user.name")))
@@ -111,7 +117,17 @@ list, Alan Dipert and MeikelBrandmeyer."
111117
(with-ssh-agent [false]
112118
(add-identity key)
113119
(is (= 1 (count (.getIdentityNames *ssh-agent*))))
114-
(add-identity *ssh-agent* key))))
120+
(add-identity *ssh-agent* key)))
121+
(testing "passing byte arrays"
122+
(with-ssh-agent [false]
123+
(add-identity
124+
*ssh-agent*
125+
"name"
126+
(.getBytes (slurp (private-key-path)))
127+
(.getBytes (slurp (public-key-path)))
128+
nil)
129+
(is (= 1 (count (.getIdentityNames *ssh-agent*))))
130+
(is (= "name" (first (.getIdentityNames *ssh-agent*)))))))
115131

116132
(deftest has-identity?-test
117133
(let [key (private-key-path)]

0 commit comments

Comments
 (0)