Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicate keys on GPG key ring #17242

Merged
merged 5 commits into from
Oct 7, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions models/gpg_key_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,46 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro
verified = true
}

if len(ekeys) > 1 {
id2key := map[string]*openpgp.Entity{}
newKeys := make([]*openpgp.Entity, 0, len(ekeys))
zeripath marked this conversation as resolved.
Show resolved Hide resolved
for _, ekey := range ekeys {
id := ekey.PrimaryKey.KeyIdString()
if original, has := id2key[id]; has {
// Coalesce this with the other one
for _, subkey := range ekey.Subkeys {
if subkey.PublicKey == nil {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
continue
}
found := false

for _, originalSubkey := range original.Subkeys {
if originalSubkey.PublicKey == nil {
continue
}
if originalSubkey.PublicKey.KeyId == subkey.PublicKey.KeyId {
found = true
break
}
}
if !found {
original.Subkeys = append(original.Subkeys, subkey)
}
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}
for name, identity := range ekey.Identities {
if _, has := original.Identities[name]; has {
continue
}
original.Identities[name] = identity
}
continue
}
id2key[id] = ekey
newKeys = append(newKeys, ekey)
}
ekeys = newKeys
}

for _, ekey := range ekeys {
// Key ID cannot be duplicated.
has, err := db.GetEngine(ctx).Where("key_id=?", ekey.PrimaryKey.KeyIdString()).
Expand Down