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

switch keychain Set() to update existing items instead of delete/add #43

Merged
merged 1 commit into from
Jul 4, 2019
Merged
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: 21 additions & 10 deletions keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package keyring

import (
"errors"
"fmt"

gokeychain "github.com/keybase/go-keychain"
Expand Down Expand Up @@ -128,22 +129,32 @@ func (k *keychain) Set(item Item) error {
debugf("Adding service=%q, label=%q, account=%q, trusted=%v to osx keychain %q", k.service, item.Label, item.Key, isTrusted, k.path)

if err := gokeychain.AddItem(kcItem); err == gokeychain.ErrorDuplicateItem {
debugf("Item already exists, deleting")
delItem := gokeychain.NewItem()
delItem.SetSecClass(gokeychain.SecClassGenericPassword)
delItem.SetService(k.service)
delItem.SetAccount(item.Key)
debugf("Item already exists, updating")
queryItem := gokeychain.NewItem()
queryItem.SetSecClass(gokeychain.SecClassGenericPassword)
queryItem.SetService(k.service)
queryItem.SetAccount(item.Key)
queryItem.SetMatchLimit(gokeychain.MatchLimitOne)
queryItem.SetReturnAttributes(true)

if k.path != "" {
delItem.SetMatchSearchList(kc)
queryItem.SetMatchSearchList(kc)
}

if err = gokeychain.DeleteItem(delItem); err != nil {
return fmt.Errorf("Error deleting existing item: %v", err)
results, err := gokeychain.QueryItem(queryItem)
if err != nil {
return fmt.Errorf("Failed to query keychain: %v", err)
}
if len(results) == 0 {
return errors.New("no results")
}

debugf("Adding item again")
return gokeychain.AddItem(kcItem)
// Don't call SetAccess() as this will cause multiple prompts on update, even when we are not updating the AccessList
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We nil out the ACL here before calling update. If we don't do this, macOS will prompt a number of times and say incorrectly that we are changing ownership and changing ACLs even if the access list is identical to the currently stored item.

kcItem.SetAccess(nil)

if err := gokeychain.UpdateItem(queryItem, kcItem); err != nil {
return fmt.Errorf("Failed to update item in keychain: %v", err)
}
}

return nil
Expand Down