Skip to content

Commit ed07486

Browse files
committed
Use passphrase when adding key to agent, and honour the key name
1 parent e2aafdf commit ed07486

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/clj_ssh/ssh.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@
155155
(if (local-repo? id-repo)
156156
(.addIdentity agent name private-key public-key passphrase)
157157
(let [keypair (KeyPair/load agent private-key-path public-key-path)]
158+
(when passphrase
159+
(.decrypt keypair passphrase))
158160
(.add id-repo (.forSSHAgent keypair)))))
159161

160162
(and public-key-path private-key-path)
@@ -164,13 +166,19 @@
164166
agent
165167
(file-path private-key-path) (file-path public-key-path) passphrase)
166168
(let [keypair (KeyPair/load agent private-key-path public-key-path)]
169+
(when passphrase
170+
(.decrypt keypair passphrase))
171+
(.setPublicKeyComment keypair name)
167172
(.add id-repo (.forSSHAgent keypair)))))
168173

169174
private-key-path
170175
(let [id-repo (id-repository)]
171176
(if (local-repo? id-repo)
172177
(.addIdentity agent (file-path private-key-path) passphrase)
173178
(let [keypair (KeyPair/load agent private-key-path)]
179+
(when passphrase
180+
(.decrypt keypair passphrase))
181+
(.setPublicKeyComment keypair name)
174182
(.add id-repo (.forSSHAgent keypair)))))
175183

176184
:else

test/clj_ssh/cli_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
(with-ssh-agent (ssh-agent {:use-system-ssh-agent false})
7777
(add-identity-with-keychain
7878
:private-key-path (encrypted-private-key-path)
79-
:name "clj-ssh")
79+
:passphrase "clj-ssh")
8080
(let [session (session "localhost")]
8181
(is (instance? com.jcraft.jsch.Session session))
8282
(is (not (connected? session)))

test/clj_ssh/ssh_test.clj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@
6060
(let [agent (ssh-agent {})]
6161
(let [n (count (.getIdentityNames agent))
6262
test-key-comment "key for test clj-ssh"
63-
has-key (some #(= test-key-comment %) (.getIdentityNames agent))]
63+
has-key (some #(= (private-key-path) %) (.getIdentityNames agent))]
6464
(add-identity
6565
agent
66-
{:name "name"
67-
:private-key-path (private-key-path)
66+
{:private-key-path (private-key-path)
6867
:public-key-path (public-key-path)})
6968
(is (or has-key (= (inc n) (count (.getIdentityNames agent)))))
70-
(is (some #(= test-key-comment %) (.getIdentityNames agent)))))))
69+
(is (some #(= (private-key-path) %) (.getIdentityNames agent)))))))
7170

7271
(deftest has-identity?-test
7372
(let [key (private-key-path)
@@ -141,7 +140,7 @@
141140
(add-identity-with-keychain
142141
agent
143142
{:private-key-path (encrypted-private-key-path)
144-
:name "clj-ssh"})
143+
:passphrase "clj-ssh"})
145144
(let [session (session
146145
agent
147146
"localhost"

0 commit comments

Comments
 (0)