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

Fix synchronization error for read/write keyring.keys #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
31 changes: 18 additions & 13 deletions keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ func (k *Keyring) AddKey(key []byte) error {
}

// No-op if key is already installed
for _, installedKey := range k.keys {
if bytes.Equal(installedKey, key) {
return nil
}
}

keys := append(k.keys, key)
keys := k.GetKeys()
for _, installedKey := range keys {
if bytes.Equal(installedKey, key) {
return nil
}
}

keys = append(keys, key)
primaryKey := k.GetPrimaryKey()
if primaryKey == nil {
primaryKey = key
Expand All @@ -99,9 +100,10 @@ func (k *Keyring) AddKey(key []byte) error {
// UseKey changes the key used to encrypt messages. This is the only key used to
// encrypt messages, so peers should know this key before this method is called.
func (k *Keyring) UseKey(key []byte) error {
for _, installedKey := range k.keys {
keys := k.GetKeys()
for _, installedKey := range keys {
if bytes.Equal(key, installedKey) {
k.installKeys(k.keys, key)
k.installKeys(keys, key)
return nil
}
}
Expand All @@ -111,13 +113,16 @@ func (k *Keyring) UseKey(key []byte) error {
// RemoveKey drops a key from the keyring. This will return an error if the key
// requested for removal is currently at position 0 (primary key).
func (k *Keyring) RemoveKey(key []byte) error {
if bytes.Equal(key, k.keys[0]) {
primaryKey := k.GetPrimaryKey()
if bytes.Equal(key, primaryKey) {
return fmt.Errorf("Removing the primary key is not allowed")
}
for i, installedKey := range k.keys {

keys := k.GetKeys()
for i, installedKey := range keys {
if bytes.Equal(key, installedKey) {
keys := append(k.keys[:i], k.keys[i+1:]...)
k.installKeys(keys, k.keys[0])
keys := append(keys[:i], keys[i+1:]...)
k.installKeys(keys, primaryKey)
}
}
return nil
Expand Down