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

libsecret: Keys() unlocks collection first #61

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/keyring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
listBackends := flag.Bool("list-backends", false, "Whether to list backends")

// actions to take
actionListKeys := flag.Bool("list-keys", false, "Whether to list backends")
actionListKeys := flag.Bool("list-keys", false, "Whether to list keys")
actionSetValue := flag.String("set", "", "The value to set")

// keychain
Expand Down
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)
}