Skip to content

Commit

Permalink
Merge pull request #53 from psanford/fix-password-store-keys-with-sla…
Browse files Browse the repository at this point in the history
…shes

Fix password store to handle keys with '/' in them
  • Loading branch information
mtibben authored Oct 27, 2019
2 parents 6caaf19 + f28e2cd commit 90a8c37
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
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

0 comments on commit 90a8c37

Please sign in to comment.