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 password store to handle keys with '/' in them #53

Merged
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
23 changes: 13 additions & 10 deletions pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -142,16 +141,20 @@ func (k *passKeyring) Keys() ([]string, error) {
return keys, fmt.Errorf("%s is not a directory", path)
}

files, err := ioutil.ReadDir(path)
if err != nil {
return keys, err
}
err = filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}

for _, f := range files {
if filepath.Ext(f.Name()) == ".gpg" {
name := filepath.Base(f.Name())
if !info.IsDir() && filepath.Ext(p) == ".gpg" {
name := strings.TrimPrefix(p, path)
if name[0] == os.PathSeparator {
name = name[1:]
}
keys = append(keys, name[:len(name)-4])
}
}
return keys, nil
return nil
})

return keys, err
}
34 changes: 18 additions & 16 deletions pass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"reflect"
"testing"
)

Expand Down Expand Up @@ -101,33 +101,35 @@ func TestPassKeyringKeysWhenNotEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)

item := Item{Key: "llamas", Data: []byte("llamas are great")}

if err := k.Set(item); err != nil {
t.Fatal(err)
items := []Item{
{Key: "llamas", Data: []byte("llamas are great")},
{Key: "alpacas", Data: []byte("alpacas are better")},
{Key: "africa/elephants", Data: []byte("who doesn't like elephants")},
}

item = Item{Key: "alpacas", Data: []byte("alpacas are better")}

if err := k.Set(item); err != nil {
t.Fatal(err)
for _, item := range items {
if err := k.Set(item); err != nil {
t.Fatal(err)
}
}

keys, err := k.Keys()
if err != nil {
t.Fatal(err)
}

if len(keys) != 2 {
t.Fatalf("Expected 2 keys, got %d", len(keys))
if len(keys) != len(items) {
t.Fatalf("Expected %d keys, got %d", len(items), len(keys))
}

sort.Strings(keys)
if keys[0] != "alpacas" {
t.Fatalf("Expected alpacas")
expectedKeys := []string{
"africa/elephants",
"alpacas",
"llamas",
}
if keys[1] != "llamas" {
t.Fatalf("Expected llamas")

if !reflect.DeepEqual(keys, expectedKeys) {
t.Fatalf("Expected keys %v, got %v", expectedKeys, keys)
}
}

Expand Down