Skip to content

Commit b2d3a6a

Browse files
drakkangopherbot
authored andcommitted
ssh/agent: ensure to not add duplicated keys
When adding a new key, if we already have a Signer with the same public key, we now replace it with the new one instead of duplicating it. Before this change we had this: $ ssh-add -l 3072 SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es nicola@p1 (RSA) 3072 SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es nicola@p1 (RSA-CERT) $ ssh-add /home/nicola/ssh_certs/id_rsa Identity added: /home/nicola/ssh_certs/id_rsa (nicola@p1) Certificate added: /home/nicola/ssh_certs/id_rsa-cert.pub (myid) $ ssh-add -l 3072 SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es nicola@p1 (RSA) 3072 SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es nicola@p1 (RSA-CERT) 3072 SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es nicola@p1 (RSA) 3072 SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es nicola@p1 (RSA-CERT) Change-Id: Iad1b1a6dc94f68f53f05d7d1172f0017839976fc Reviewed-on: https://go-review.googlesource.com/c/crypto/+/602955 Reviewed-by: Filippo Valsorda <[email protected]> Auto-Submit: Nicola Murino <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5bcd010 commit b2d3a6a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

ssh/agent/keyring.go

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ func (r *keyring) Add(key AddedKey) error {
175175
p.expire = &t
176176
}
177177

178+
// If we already have a Signer with the same public key, replace it with the
179+
// new one.
180+
for idx, k := range r.keys {
181+
if bytes.Equal(k.signer.PublicKey().Marshal(), p.signer.PublicKey().Marshal()) {
182+
r.keys[idx] = p
183+
return nil
184+
}
185+
}
186+
178187
r.keys = append(r.keys, p)
179188

180189
return nil

ssh/agent/keyring_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func validateListedKeys(t *testing.T, a Agent, expectedKeys []string) {
2929
t.Fatalf("failed to list keys: %v", err)
3030
return
3131
}
32+
if len(listedKeys) != len(expectedKeys) {
33+
t.Fatalf("expeted %d key, got %d", len(expectedKeys), len(listedKeys))
34+
return
35+
}
3236
actualKeys := make(map[string]bool)
3337
for _, key := range listedKeys {
3438
actualKeys[key.Comment] = true
@@ -74,3 +78,45 @@ func TestKeyringAddingAndRemoving(t *testing.T) {
7478
}
7579
validateListedKeys(t, k, []string{})
7680
}
81+
82+
func TestAddDuplicateKey(t *testing.T) {
83+
keyNames := []string{"rsa", "user"}
84+
85+
k := NewKeyring()
86+
for _, keyName := range keyNames {
87+
addTestKey(t, k, keyName)
88+
}
89+
validateListedKeys(t, k, keyNames)
90+
// Add the keys again.
91+
for _, keyName := range keyNames {
92+
addTestKey(t, k, keyName)
93+
}
94+
validateListedKeys(t, k, keyNames)
95+
// Add an existing key with an updated comment.
96+
keyName := keyNames[0]
97+
addedKey := AddedKey{
98+
PrivateKey: testPrivateKeys[keyName],
99+
Comment: "comment updated",
100+
}
101+
err := k.Add(addedKey)
102+
if err != nil {
103+
t.Fatalf("failed to add key %q: %v", keyName, err)
104+
}
105+
// Check the that key is found and the comment was updated.
106+
keys, err := k.List()
107+
if err != nil {
108+
t.Fatalf("failed to list keys: %v", err)
109+
}
110+
if len(keys) != len(keyNames) {
111+
t.Fatalf("expected %d keys, got %d", len(keyNames), len(keys))
112+
}
113+
isFound := false
114+
for _, key := range keys {
115+
if key.Comment == addedKey.Comment {
116+
isFound = true
117+
}
118+
}
119+
if !isFound {
120+
t.Fatal("key with the updated comment not found")
121+
}
122+
}

0 commit comments

Comments
 (0)