Skip to content

Commit 51f4f8c

Browse files
zeripath6543
andauthored
Handle duplicate keys on GPG key ring (#17242) (#17284)
Backport #17242 It is possible that a keyring can contain duplicate keys on a keyring due to jpegs or other layers. This currently leads to a confusing error for the user - where we report a duplicate key insertion. This PR simply coalesces keys into one key if there are duplicates. Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent f5845e6 commit 51f4f8c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

models/gpg_key_add.go

+40
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,46 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro
9999
verified = true
100100
}
101101

102+
if len(ekeys) > 1 {
103+
id2key := map[string]*openpgp.Entity{}
104+
newEKeys := make([]*openpgp.Entity, 0, len(ekeys))
105+
for _, ekey := range ekeys {
106+
id := ekey.PrimaryKey.KeyIdString()
107+
if original, has := id2key[id]; has {
108+
// Coalesce this with the other one
109+
for _, subkey := range ekey.Subkeys {
110+
if subkey.PublicKey == nil {
111+
continue
112+
}
113+
found := false
114+
115+
for _, originalSubkey := range original.Subkeys {
116+
if originalSubkey.PublicKey == nil {
117+
continue
118+
}
119+
if originalSubkey.PublicKey.KeyId == subkey.PublicKey.KeyId {
120+
found = true
121+
break
122+
}
123+
}
124+
if !found {
125+
original.Subkeys = append(original.Subkeys, subkey)
126+
}
127+
}
128+
for name, identity := range ekey.Identities {
129+
if _, has := original.Identities[name]; has {
130+
continue
131+
}
132+
original.Identities[name] = identity
133+
}
134+
continue
135+
}
136+
id2key[id] = ekey
137+
newEKeys = append(newEKeys, ekey)
138+
}
139+
ekeys = newEKeys
140+
}
141+
102142
for _, ekey := range ekeys {
103143
// Key ID cannot be duplicated.
104144
has, err := sess.Where("key_id=?", ekey.PrimaryKey.KeyIdString()).

0 commit comments

Comments
 (0)