Skip to content

Commit

Permalink
Show recipients from subfolder ids (#1453)
Browse files Browse the repository at this point in the history
Fixes #954

RELEASE_NOTES=[ENHANCEMENT] Show recipients from subfolder id files

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz authored Jul 17, 2020
1 parent 0ecb718 commit 2647ce6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/godbus/dbus v0.0.0-20190623212516-8a1682060722
github.com/gokyle/twofactor v1.0.1
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.5.0
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0
Expand Down
38 changes: 35 additions & 3 deletions internal/store/leaf/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package leaf
import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"

"github.com/google/go-cmp/cmp"
"github.com/gopasspw/gopass/internal/backend/crypto/age"
"github.com/gopasspw/gopass/internal/debug"
"github.com/gopasspw/gopass/internal/out"
Expand All @@ -29,6 +32,32 @@ func (s *Store) Recipients(ctx context.Context) []string {
return rs
}

// RecipientsTree returns a mapping of secrets to recipients
func (s *Store) RecipientsTree(ctx context.Context) map[string][]string {
idfs := s.idFiles(ctx)
out := make(map[string][]string, len(idfs))

root := s.Recipients(ctx)
for _, idf := range idfs {
if strings.HasPrefix(idf, ".") {
continue
}
srs, err := s.getRecipients(ctx, idf)
if err != nil {
debug.Log("failed to list recipients: %s", err)
continue
}
if cmp.Equal(out[""], srs) {
debug.Log("root recipients equal secret recipients from %s", idf)
continue
}
dir := filepath.Dir(idf)
out[dir] = srs
}
out[""] = root
return out
}

// AddRecipient adds a new recipient to the list
func (s *Store) AddRecipient(ctx context.Context, id string) error {
rs, err := s.GetRecipients(ctx, "")
Expand Down Expand Up @@ -137,10 +166,13 @@ func (s *Store) OurKeyID(ctx context.Context) string {
// GetRecipients will load all Recipients from the .gpg-id file for the given
// secret path
func (s *Store) GetRecipients(ctx context.Context, name string) ([]string, error) {
idf := s.idFile(ctx, name)
return s.getRecipients(ctx, s.idFile(ctx, name))
}

func (s *Store) getRecipients(ctx context.Context, idf string) ([]string, error) {
buf, err := s.storage.Get(ctx, idf)
if err != nil {
return nil, errors.Wrapf(err, "failed to get recipients for %s", name)
return nil, errors.Wrapf(err, "failed to get recipients from %s", idf)
}

rawRecps := recipients.Unmarshal(buf)
Expand All @@ -152,7 +184,7 @@ func (s *Store) GetRecipients(ctx context.Context, name string) ([]string, error
}
finalRecps = append(finalRecps, fp)
}

sort.Strings(finalRecps)
return finalRecps, nil
}

Expand Down
30 changes: 29 additions & 1 deletion internal/store/leaf/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"

"github.com/gopasspw/gopass/internal/backend"
Expand Down Expand Up @@ -78,7 +79,7 @@ func New(ctx context.Context, alias, path string) (*Store, error) {

// idFile returns the path to the recipient list for this store
// it walks up from the given filename until it finds a directory containing
// a gpg id file or it leaves the scope of this.storage.
// a gpg id file or it leaves the scope of storage.
func (s *Store) idFile(ctx context.Context, name string) string {
if s.crypto == nil {
return ""
Expand All @@ -102,6 +103,33 @@ func (s *Store) idFile(ctx context.Context, name string) string {
return s.crypto.IDFile()
}

// idFiles returns the path to all id files in this store.
func (s *Store) idFiles(ctx context.Context) []string {
if s.crypto == nil {
return nil
}
files, err := s.Storage().List(ctx, "")
if err != nil {
return nil
}
fileSet := make(map[string]struct{}, len(files))
for _, file := range files {
if strings.HasPrefix(filepath.Base(file), ".") {
continue
}
idf := s.idFile(ctx, file)
if s.storage.Exists(ctx, idf) {
fileSet[idf] = struct{}{}
}
}
out := make([]string, 0, len(fileSet))
for file := range fileSet {
out = append(out, file)
}
sort.Strings(out)
return out
}

// Equals returns true if this.storage has the same on-disk path as the other
func (s *Store) Equals(other *Store) bool {
if other == nil {
Expand Down
22 changes: 16 additions & 6 deletions internal/store/root/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ func (r *Store) SaveRecipients(ctx context.Context) error {
func (r *Store) RecipientsTree(ctx context.Context, pretty bool) (*tree.Root, error) {
root := tree.New("gopass")

for _, recp := range r.store.Recipients(ctx) {
if err := r.addRecipient(ctx, "", root, recp, pretty); err != nil {
color.Yellow("Failed to add recipient to tree %s: %s", recp, err)
for name, recps := range r.store.RecipientsTree(ctx) {
if name != "" {
name += "/"
}
for _, recp := range recps {
if err := r.addRecipient(ctx, name, root, recp, pretty); err != nil {
color.Yellow("Failed to add recipient to tree %s: %s", recp, err)
}
}
}

Expand All @@ -97,9 +102,14 @@ func (r *Store) RecipientsTree(ctx context.Context, pretty bool) (*tree.Root, er
if err := root.AddMount(alias, substore.Path()); err != nil {
return nil, errors.Errorf("failed to add mount: %s", err)
}
for _, recp := range substore.Recipients(ctx) {
if err := r.addRecipient(ctx, alias+"/", root, recp, pretty); err != nil {
debug.Log("Failed to add recipient to tree %s: %s", recp, err)
for name, recps := range substore.RecipientsTree(ctx) {
if name != "" {
name += "/"
}
for _, recp := range recps {
if err := r.addRecipient(ctx, alias+"/"+name, root, recp, pretty); err != nil {
debug.Log("Failed to add recipient to tree %s: %s", recp, err)
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/fsutil/fsutil.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fsutil

import (
"fmt"
"io"
"math/rand"
"os"
Expand All @@ -11,6 +10,7 @@ import (
"strings"
"time"

"github.com/gopasspw/gopass/internal/debug"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ func IsDir(path string) bool {
// not found
return false
}
fmt.Printf("failed to check dir %s: %s\n", path, err)
debug.Log("failed to check dir %s: %s\n", path, err)
return false
}

Expand All @@ -61,7 +61,7 @@ func IsFile(path string) bool {
// not found
return false
}
fmt.Printf("failed to check dir %s: %s\n", path, err)
debug.Log("failed to check dir %s: %s\n", path, err)
return false
}

Expand Down

0 comments on commit 2647ce6

Please sign in to comment.