Skip to content

Commit 329bccb

Browse files
authored
Merge pull request #67 from quorumcontrol/fix/check-other-err-values
Check for other errors from gokeychain.AddItem
2 parents c2a5408 + e16c51d commit 329bccb

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

keychain.go

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ func (k *keychain) GetMetadata(key string) (Metadata, error) {
112112
return md, nil
113113
}
114114

115+
func (k *keychain) updateItem(kc gokeychain.Keychain, kcItem gokeychain.Item, account string) error {
116+
queryItem := gokeychain.NewItem()
117+
queryItem.SetSecClass(gokeychain.SecClassGenericPassword)
118+
queryItem.SetService(k.service)
119+
queryItem.SetAccount(account)
120+
queryItem.SetMatchLimit(gokeychain.MatchLimitOne)
121+
queryItem.SetReturnAttributes(true)
122+
123+
if k.path != "" {
124+
queryItem.SetMatchSearchList(kc)
125+
}
126+
127+
results, err := gokeychain.QueryItem(queryItem)
128+
if err != nil {
129+
return fmt.Errorf("Failed to query keychain: %v", err)
130+
}
131+
if len(results) == 0 {
132+
return errors.New("no results")
133+
}
134+
135+
// Don't call SetAccess() as this will cause multiple prompts on update, even when we are not updating the AccessList
136+
kcItem.SetAccess(nil)
137+
138+
if err := gokeychain.UpdateItem(queryItem, kcItem); err != nil {
139+
return fmt.Errorf("Failed to update item in keychain: %v", err)
140+
}
141+
142+
return nil
143+
}
144+
115145
func (k *keychain) Set(item Item) error {
116146
var kc gokeychain.Keychain
117147

@@ -162,33 +192,15 @@ func (k *keychain) Set(item Item) error {
162192

163193
debugf("Adding service=%q, label=%q, account=%q, trusted=%v to osx keychain %q", k.service, item.Label, item.Key, isTrusted, k.path)
164194

165-
if err := gokeychain.AddItem(kcItem); err == gokeychain.ErrorDuplicateItem {
166-
debugf("Item already exists, updating")
167-
queryItem := gokeychain.NewItem()
168-
queryItem.SetSecClass(gokeychain.SecClassGenericPassword)
169-
queryItem.SetService(k.service)
170-
queryItem.SetAccount(item.Key)
171-
queryItem.SetMatchLimit(gokeychain.MatchLimitOne)
172-
queryItem.SetReturnAttributes(true)
173-
174-
if k.path != "" {
175-
queryItem.SetMatchSearchList(kc)
176-
}
177-
178-
results, err := gokeychain.QueryItem(queryItem)
179-
if err != nil {
180-
return fmt.Errorf("Failed to query keychain: %v", err)
181-
}
182-
if len(results) == 0 {
183-
return errors.New("no results")
184-
}
195+
err := gokeychain.AddItem(kcItem)
185196

186-
// Don't call SetAccess() as this will cause multiple prompts on update, even when we are not updating the AccessList
187-
kcItem.SetAccess(nil)
197+
if err == gokeychain.ErrorDuplicateItem {
198+
debugf("Item already exists, updating")
199+
err = k.updateItem(kc, kcItem, item.Key)
200+
}
188201

189-
if err := gokeychain.UpdateItem(queryItem, kcItem); err != nil {
190-
return fmt.Errorf("Failed to update item in keychain: %v", err)
191-
}
202+
if err != nil {
203+
return err
192204
}
193205

194206
return nil

0 commit comments

Comments
 (0)