Skip to content

Commit 402c780

Browse files
fix: highlight linked secrets in ls output (gopasspw#3536)
* Initial plan * fix: highlight linked secrets in tree output Co-authored-by: dominikschulz <1449175+dominikschulz@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dominikschulz <1449175+dominikschulz@users.noreply.github.com>
1 parent a86172d commit 402c780

13 files changed

Lines changed: 255 additions & 14 deletions

File tree

‎internal/action/list_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ foo2/bar2
107107
└── zen/ (shadowed)
108108
└── bar
109109
110+
`
111+
assert.Equal(t, want, buf.String())
112+
buf.Reset()
113+
114+
require.NoError(t, act.Store.Link(ctx, "foo/bar", "foo/link"))
115+
require.NoError(t, act.List(ctx, gptest.CliCtx(ctx, t, "foo")))
116+
want = `foo/
117+
├── bar
118+
├── link -> foo/bar
119+
└── zen/ (shadowed)
120+
└── bar
121+
110122
`
111123
assert.Equal(t, want, buf.String())
112124
buf.Reset()

‎internal/backend/storage/cryptfs/crypt.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ func (c *Crypt) Path() string {
124124
return c.path
125125
}
126126

127+
// LinkTarget returns the target of a linked secret, if it can be determined.
128+
func (c *Crypt) LinkTarget(_ context.Context, name string) (string, bool, error) {
129+
c.mux.RLock()
130+
defer c.mux.RUnlock()
131+
132+
h, ok := c.mappings[name]
133+
if !ok {
134+
return "", false, nil
135+
}
136+
137+
if h == c.hash(name) {
138+
return "", false, nil
139+
}
140+
141+
for candidate, candidateHash := range c.mappings {
142+
if candidateHash == h && candidate != name && c.hash(candidate) == h {
143+
return candidate, true, nil
144+
}
145+
}
146+
147+
for candidate, candidateHash := range c.mappings {
148+
if candidateHash == h && candidate != name {
149+
return candidate, true, nil
150+
}
151+
}
152+
153+
return "", true, nil
154+
}
155+
127156
// Version returns the version of the backend.
128157
func (c *Crypt) Version(ctx context.Context) semver.Version {
129158
return semver.Version{Major: 1}

‎internal/backend/storage/fossilfs/storage.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (f *Fossil) Path() string {
5252
return f.fs.Path()
5353
}
5454

55+
// LinkTarget returns the relative target of a symlinked secret.
56+
func (f *Fossil) LinkTarget(ctx context.Context, name string) (string, bool, error) {
57+
return f.fs.LinkTarget(ctx, name)
58+
}
59+
5560
// Fsck checks the storage integrity.
5661
func (f *Fossil) Fsck(ctx context.Context) error {
5762
// ensure sane fossil config.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package fs
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"runtime"
9+
)
10+
11+
// LinkTarget returns the relative target of a symlinked secret.
12+
func (s *Store) LinkTarget(_ context.Context, name string) (string, bool, error) {
13+
if runtime.GOOS == "windows" {
14+
name = filepath.FromSlash(name)
15+
}
16+
17+
path, err := s.safePath(name)
18+
if err != nil {
19+
return "", false, err
20+
}
21+
22+
info, err := os.Lstat(path)
23+
if err != nil {
24+
if os.IsNotExist(err) {
25+
return "", false, nil
26+
}
27+
28+
return "", false, err
29+
}
30+
31+
if info.Mode()&os.ModeSymlink != os.ModeSymlink {
32+
return "", false, nil
33+
}
34+
35+
target, err := os.Readlink(path)
36+
if err != nil {
37+
return "", true, fmt.Errorf("failed to read symlink target for %q: %w", name, err)
38+
}
39+
40+
if !filepath.IsAbs(target) {
41+
target = filepath.Join(filepath.Dir(path), target)
42+
}
43+
44+
target, err = filepath.Rel(s.path, target)
45+
if err != nil {
46+
return "", true, fmt.Errorf("failed to get relative symlink target for %q: %w", name, err)
47+
}
48+
49+
return filepath.ToSlash(filepath.Clean(target)), true, nil
50+
}

‎internal/backend/storage/gitfs/storage.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func (g *Git) Path() string {
5454
return g.fs.Path()
5555
}
5656

57+
// LinkTarget returns the relative target of a symlinked secret.
58+
func (g *Git) LinkTarget(ctx context.Context, name string) (string, bool, error) {
59+
return g.fs.LinkTarget(ctx, name)
60+
}
61+
5762
// Fsck checks the storage integrity.
5863
func (g *Git) Fsck(ctx context.Context) error {
5964
// ensure sane git config.

‎internal/backend/storage/jjfs/jj.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func New(path string) (*JJFS, error) {
3434
nil
3535
}
3636

37+
// LinkTarget returns the relative target of a symlinked secret.
38+
func (j *JJFS) LinkTarget(ctx context.Context, name string) (string, bool, error) {
39+
return j.fs.LinkTarget(ctx, name)
40+
}
41+
3742
// Init initializes this store's jj repo.
3843
func Init(ctx context.Context, path, userName, userEmail string) (*JJFS, error) {
3944
j := &JJFS{

‎internal/store/leaf/link_target.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leaf
2+
3+
import (
4+
"context"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
type linkTargeter interface {
10+
LinkTarget(context.Context, string) (string, bool, error)
11+
}
12+
13+
// LinkTarget returns the target of a linked secret, if the named entry is a link.
14+
func (s *Store) LinkTarget(ctx context.Context, name string) (string, bool, error) {
15+
lt, ok := s.storage.(linkTargeter)
16+
if !ok {
17+
return "", false, nil
18+
}
19+
20+
if s.alias != "" {
21+
name = strings.TrimPrefix(name, s.alias+Sep)
22+
}
23+
24+
target, isLink, err := lt.LinkTarget(ctx, s.passfile(ctx, name))
25+
if err != nil || !isLink {
26+
return "", isLink, err
27+
}
28+
29+
target = strings.TrimSuffix(filepath.ToSlash(target), "."+s.crypto.Ext())
30+
if s.alias != "" {
31+
target = s.alias + Sep + target
32+
}
33+
34+
return target, true, nil
35+
}

‎internal/store/root/list.go‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/gopasspw/gopass/internal/out"
1010
"github.com/gopasspw/gopass/internal/store"
11+
"github.com/gopasspw/gopass/internal/store/leaf"
1112
"github.com/gopasspw/gopass/internal/tree"
1213
"github.com/gopasspw/gopass/pkg/debug"
1314
)
@@ -25,7 +26,7 @@ func (r *Store) List(ctx context.Context, maxDepth int) ([]string, error) {
2526
// Tree returns the tree representation of the entries.
2627
func (r *Store) Tree(ctx context.Context) (*tree.Root, error) {
2728
root := tree.New("gopass")
28-
addFileFunc := func(in ...string) {
29+
addFileFunc := func(sub *leaf.Store, in ...string) {
2930
for _, f := range in {
3031
var ct string
3132

@@ -40,7 +41,19 @@ func (r *Store) Tree(ctx context.Context) (*tree.Root, error) {
4041
ct = "text/plain"
4142
}
4243

43-
if err := root.AddFile(f, ct); err != nil {
44+
linkTarget, isLink, err := sub.LinkTarget(ctx, f)
45+
if err != nil {
46+
out.Errorf(ctx, "Failed to resolve link target for %s: %s", f, err)
47+
48+
continue
49+
}
50+
51+
if isLink {
52+
err = root.AddLink(f, linkTarget)
53+
} else {
54+
err = root.AddFile(f, ct)
55+
}
56+
if err != nil {
4457
out.Errorf(ctx, "Failed to add file %s to tree: %s", f, err)
4558

4659
continue
@@ -63,7 +76,7 @@ func (r *Store) Tree(ctx context.Context) (*tree.Root, error) {
6376
}
6477

6578
debug.V(1).Log("[root] adding files: %q", sf)
66-
addFileFunc(sf...)
79+
addFileFunc(r.store, sf...)
6780
debug.V(1).Log("[root] Tree: %s", root.Format(-1))
6881
addTplFunc(r.store.ListTemplates(ctx, "")...)
6982

@@ -86,7 +99,7 @@ func (r *Store) Tree(ctx context.Context) (*tree.Root, error) {
8699
}
87100

88101
debug.V(1).Log("[%s] adding files: %q", alias, sf)
89-
addFileFunc(sf...)
102+
addFileFunc(substore, sf...)
90103
addTplFunc(substore.ListTemplates(ctx, alias)...)
91104
}
92105

‎internal/store/root/store_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sort"
77
"testing"
88

9+
"github.com/fatih/color"
910
"github.com/gopasspw/gopass/internal/backend"
1011
_ "github.com/gopasspw/gopass/internal/backend/crypto"
1112
_ "github.com/gopasspw/gopass/internal/backend/storage"
@@ -29,6 +30,26 @@ func TestSimpleList(t *testing.T) {
2930
assert.Equal(t, []string{"foo"}, st.List(tree.INF))
3031
}
3132

33+
func TestTreeMarksLinkedSecrets(t *testing.T) {
34+
ctx := config.NewContextInMemory()
35+
36+
u := gptest.NewUnitTester(t)
37+
38+
rs, err := createRootStore(ctx, u)
39+
require.NoError(t, err)
40+
require.NoError(t, rs.Link(ctx, "foo", "bar"))
41+
42+
st, err := rs.Tree(ctx)
43+
require.NoError(t, err)
44+
45+
color.NoColor = true
46+
assert.Equal(t, `gopass
47+
├── bar -> foo
48+
└── foo
49+
`, st.Format(tree.INF))
50+
assert.Equal(t, []string{"bar", "foo"}, st.List(tree.INF))
51+
}
52+
3253
func TestListMulti(t *testing.T) {
3354
ctx := config.NewContextInMemory()
3455
ctx = backend.WithCryptoBackend(ctx, backend.Plain)

‎internal/tree/node.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type Node struct {
1111
Name string
1212
Leaf bool
13+
Link bool
1314
Template bool
1415
Mount bool
1516
Path string
@@ -46,6 +47,10 @@ func (n Node) Equals(other Node) bool {
4647
return false
4748
}
4849

50+
if n.Link != other.Link {
51+
return false
52+
}
53+
4954
if n.Subtree != nil {
5055
if other.Subtree == nil {
5156
return false
@@ -70,6 +75,7 @@ func (n Node) Merge(other Node) *Node {
7075
r := Node{
7176
Name: n.Name,
7277
Leaf: n.Leaf,
78+
Link: n.Link,
7379
Template: n.Template,
7480
Mount: n.Mount,
7581
Path: n.Path,
@@ -89,6 +95,11 @@ func (n Node) Merge(other Node) *Node {
8995
r.Template = true
9096
}
9197

98+
if other.Link {
99+
r.Link = true
100+
r.Path = other.Path
101+
}
102+
92103
// Handling of mounts is a bit more tricky. See the comment above.
93104
// If we're adding a mount to the tree this shadows (erases) everything
94105
// that was on this branch before a replaces it with the mount.
@@ -143,6 +154,12 @@ func (n *Node) format(prefix string, last bool, maxDepth, curDepth int) string {
143154
switch {
144155
case n.Mount:
145156
_, _ = out.WriteString(colMount(n.Name + " (" + n.Path + ")"))
157+
case n.Link:
158+
label := n.Name + " (link)"
159+
if n.Path != "" {
160+
label = n.Name + " -> " + n.Path
161+
}
162+
_, _ = out.WriteString(colLink(label))
146163
case n.Subtree != nil:
147164
_, _ = out.WriteString(colDir(n.Name + sep))
148165
default:

0 commit comments

Comments
 (0)
⌂ 换网址