Skip to content

Commit

Permalink
libsecret: Keys() unlocks collection first
Browse files Browse the repository at this point in the history
Otherwise it will return empty strings if the collection is locked.
  • Loading branch information
pda committed Feb 11, 2020
1 parent e8b7900 commit f3045fa
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions libsecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,17 @@ func (k *secretsKeyring) Set(item Item) error {
k.collection = collection
}

// create the new item
data, err := json.Marshal(item)
if err != nil {
if err := k.ensureCollectionUnlocked(); err != nil {
return err
}

secret := libsecret.NewSecret(k.session, []byte{}, data, "application/json")

// unlock the collection first
locked, err := k.collection.Locked()
// create the new item
data, err := json.Marshal(item)
if err != nil {
return err
}

if locked {
if err := k.service.Unlock(k.collection); err != nil {
return err
}
}
secret := libsecret.NewSecret(k.session, []byte{}, data, "application/json")

if _, err := k.collection.CreateItem(item.Key, secret, true); err != nil {
return err
Expand Down Expand Up @@ -243,23 +235,24 @@ func (k *secretsKeyring) Keys() ([]string, error) {
if err == errCollectionNotFound {
return []string{}, nil
}
return []string{}, err
return nil, err
}
if err := k.ensureCollectionUnlocked(); err != nil {
return nil, err
}

items, err := k.collection.Items()
if err != nil {
return []string{}, err
return nil, err
}

keys := []string{}

for _, item := range items {
label, err := item.Label()
if err == nil {
keys = append(keys, label)
} else {
// err is being silently ignored here, not sure if that's good or bad
}
}

return keys, nil
}

Expand All @@ -270,3 +263,15 @@ func (k *secretsKeyring) deleteCollection() error {
}
return k.collection.Delete()
}

// unlock the collection if it's locked
func (k *secretsKeyring) ensureCollectionUnlocked() error {
locked, err := k.collection.Locked()
if err != nil {
return err
}
if !locked {
return nil
}
return k.service.Unlock(k.collection)
}

0 comments on commit f3045fa

Please sign in to comment.