forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
175 lines (153 loc) · 4.69 KB
/
mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package root
import (
"context"
"fmt"
"sort"
"strings"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/store"
"github.com/gopasspw/gopass/internal/store/leaf"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/fsutil"
"github.com/fatih/color"
)
// AddMount adds a new mount
func (r *Store) AddMount(ctx context.Context, alias, path string, keys ...string) error {
if err := r.addMount(ctx, alias, path, keys...); err != nil {
return fmt.Errorf("failed to add mount: %w", err)
}
// check for duplicate mounts
return r.checkMounts()
}
func (r *Store) addMount(ctx context.Context, alias, path string, keys ...string) error {
if alias == "" {
return fmt.Errorf("alias must not be empty")
}
if r.mounts == nil {
r.mounts = make(map[string]*leaf.Store, 1)
}
if _, found := r.mounts[alias]; found {
return AlreadyMountedError(alias)
}
fullPath := fsutil.CleanPath(path)
debug.Log("addMount - Path: %s - Full: %s", path, fullPath)
// initialize sub store
s, err := r.initSub(ctx, alias, fullPath, keys)
if err != nil {
return fmt.Errorf("failed to init sub store %q at %q: %w", alias, fullPath, err)
}
r.mounts[alias] = s
if r.cfg.Mounts == nil {
r.cfg.Mounts = make(map[string]string, 1)
}
r.cfg.Mounts[alias] = path
debug.Log("Added mount %s -> %s (%s)", alias, path, fullPath)
return nil
}
func (r *Store) initSub(ctx context.Context, alias, path string, keys []string) (*leaf.Store, error) {
// init regular sub store
s, err := leaf.New(ctx, alias, path)
if err != nil {
return nil, fmt.Errorf("failed to initialize store %q at %q: %w", alias, path, err)
}
if s.IsInitialized(ctx) {
return s, nil
}
debug.Log("[%s] Mount %s is not initialized", alias, path)
if len(keys) < 1 {
debug.Log("[%s] No keys available", alias)
return s, NotInitializedError{alias, path}
}
debug.Log("[%s] Trying to initialize at %s for %+v", alias, path, keys)
if err := s.Init(ctx, path, keys...); err != nil {
return s, fmt.Errorf("failed to initialize store %q at %q: %w", alias, path, err)
}
out.Printf(ctx, "Password store %s initialized for:", path)
for _, r := range s.Recipients(ctx) {
color.Yellow(r)
}
return s, nil
}
// RemoveMount removes and existing mount
func (r *Store) RemoveMount(ctx context.Context, alias string) error {
if _, found := r.mounts[alias]; !found {
out.Warningf(ctx, "%s is not mounted", alias)
}
if _, found := r.mounts[alias]; !found {
out.Warningf(ctx, "%s is not initialized", alias)
}
delete(r.mounts, alias)
delete(r.cfg.Mounts, alias)
return nil
}
// Mounts returns a map of mounts with their paths
func (r *Store) Mounts() map[string]string {
m := make(map[string]string, len(r.mounts))
for alias, sub := range r.mounts {
m[alias] = sub.Path()
}
return m
}
// MountPoints returns a sorted list of mount points. It encodes the logic that
// the longer a mount point the more specific it is. This allows to "shadow" a
// shorter mount point by a longer one.
func (r *Store) MountPoints() []string {
mps := make([]string, 0, len(r.mounts))
for k := range r.mounts {
mps = append(mps, k)
}
sort.Sort(sort.Reverse(store.ByPathLen(mps)))
return mps
}
// MountPoint returns the most-specific mount point for the given key
func (r *Store) MountPoint(name string) string {
for _, mp := range r.MountPoints() {
if strings.HasPrefix(name+"/", mp+"/") {
return mp
}
}
return ""
}
// Lock drops all cached credentials
func (r *Store) Lock() error {
for _, sub := range r.mounts {
if err := sub.Lock(); err != nil {
return err
}
}
return r.store.Lock()
}
// getStore returns the Store object at the most-specific mount point for the
// given key. returns sub store reference, truncated path to secret
func (r *Store) getStore(name string) (*leaf.Store, string) {
name = strings.TrimSuffix(name, "/")
mp := r.MountPoint(name)
if sub, found := r.mounts[mp]; found {
return sub, strings.TrimPrefix(name, sub.Alias())
}
return r.store, name
}
// GetSubStore returns an exact match for a mount point or an error if this
// mount point does not exist
func (r *Store) GetSubStore(name string) (*leaf.Store, error) {
if name == "" {
return r.store, nil
}
if sub, found := r.mounts[name]; found {
return sub, nil
}
debug.Log("mounts available: %+v", r.mounts)
return nil, fmt.Errorf("no such mount point %q", name)
}
// checkMounts performs some sanity checks on our mounts. At the moment it
// only checks if some path is mounted twice.
func (r *Store) checkMounts() error {
paths := make(map[string]string, len(r.mounts))
for k, v := range r.mounts {
if _, found := paths[v.Path()]; found {
return fmt.Errorf("doubly mounted path at %s: %s", v.Path(), k)
}
paths[v.Path()] = k
}
return nil
}