-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
mount.go
216 lines (191 loc) · 6.43 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package root
import (
"context"
"sort"
"strings"
"github.com/gopasspw/gopass/pkg/backend"
"github.com/gopasspw/gopass/pkg/config"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/store"
"github.com/gopasspw/gopass/pkg/store/sub"
"github.com/gopasspw/gopass/pkg/store/vault"
"github.com/fatih/color"
"github.com/pkg/errors"
)
// 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, nil, keys...); err != nil {
return errors.Wrapf(err, "failed to add mount")
}
// check for duplicate mounts
return r.checkMounts()
}
func (r *Store) addMount(ctx context.Context, alias, path string, sc *config.StoreConfig, keys ...string) error {
if alias == "" {
return errors.Errorf("alias must not be empty")
}
if r.mounts == nil {
r.mounts = make(map[string]store.Store, 1)
}
if _, found := r.mounts[alias]; found {
return errors.Errorf("%s is already mounted", alias)
}
out.Debug(ctx, "addMount - Path: %s - StoreConfig: %+v", path, sc)
// propagate our config settings to the sub store
if sc != nil {
if !backend.HasCryptoBackend(ctx) {
ctx = backend.WithCryptoBackend(ctx, sc.Path.Crypto)
out.Debug(ctx, "addMount - Using crypto backend %s", backend.CryptoBackendName(sc.Path.Crypto))
}
if !backend.HasRCSBackend(ctx) {
ctx = backend.WithRCSBackend(ctx, sc.Path.RCS)
out.Debug(ctx, "addMount - Using RCS backend %s", backend.RCSBackendName(sc.Path.RCS))
}
}
// parse backend URL
pathURL, err := backend.ParseURL(path)
if err != nil {
return errors.Wrapf(err, "failed to parse backend URL '%s': %s", path, err)
}
// initialize sub store
s, err := r.initSub(ctx, sc, alias, pathURL, keys)
if err != nil {
return errors.Wrapf(err, "failed to init sub store")
}
r.mounts[alias] = s
if r.cfg.Mounts == nil {
r.cfg.Mounts = make(map[string]*config.StoreConfig, 1)
}
if sc == nil {
// imporant: copy root config to avoid overwriting it with sub store
// values
cp := *r.cfg.Root
sc = &cp
}
sc.Path = pathURL
if backend.HasCryptoBackend(ctx) {
sc.Path.Crypto = backend.GetCryptoBackend(ctx)
}
if backend.HasRCSBackend(ctx) {
sc.Path.RCS = backend.GetRCSBackend(ctx)
}
if backend.HasStorageBackend(ctx) {
sc.Path.Storage = backend.GetStorageBackend(ctx)
}
r.cfg.Mounts[alias] = sc
out.Debug(ctx, "Added mount %s -> %s", alias, sc.Path.String())
return nil
}
func (r *Store) initSubVault(ctx context.Context, alias string, path *backend.URL) (store.Store, error) {
return vault.New(ctx, alias, path, r.cfg.Directory(), r.agent)
}
func (r *Store) initSub(ctx context.Context, sc *config.StoreConfig, alias string, path *backend.URL, keys []string) (store.Store, error) {
// init vault sub store
if backend.GetCryptoBackend(ctx) == backend.Vault || path.Crypto == backend.Vault {
out.Debug(ctx, "Initializing Vault Store at %s -> %s", alias, path.String())
return r.initSubVault(ctx, alias, path)
}
// init regular sub store
s, err := sub.New(ctx, r.cfg, alias, path, r.cfg.Directory(), r.agent)
if err != nil {
return nil, errors.Wrapf(err, "failed to initialize store '%s' at '%s': %s", alias, path, err)
}
if s.Initialized(ctx) {
return s, nil
}
out.Debug(ctx, "[%s] Mount %s is not initialized", alias, path)
if len(keys) < 1 {
return s, errors.Errorf("password store %s is not initialized. Try gopass init --store %s --path %s", alias, alias, path)
}
if err := s.Init(ctx, path.String(), keys...); err != nil {
return s, errors.Wrapf(err, "failed to initialize store '%s' at '%s'", alias, path)
}
out.Green(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 {
return errors.Errorf("%s is not mounted", alias)
}
if _, found := r.mounts[alias]; !found {
out.Yellow(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 ""
}
// getStore returns the Store object at the most-specific mount point for the
// given key
// context with sub store options set, sub store reference, truncated path to secret
func (r *Store) getStore(ctx context.Context, name string) (context.Context, store.Store, string) {
name = strings.TrimSuffix(name, "/")
mp := r.MountPoint(name)
if sub, found := r.mounts[mp]; found {
return r.cfg.Mounts[mp].WithContext(ctx), sub, strings.TrimPrefix(name, sub.Alias())
}
return r.cfg.Root.WithContext(ctx), r.store, name
}
// WithConfig populates the context with the substore config
func (r *Store) WithConfig(ctx context.Context, name string) context.Context {
name = strings.TrimSuffix(name, "/")
mp := r.MountPoint(name)
if _, found := r.mounts[mp]; found {
return r.cfg.Mounts[mp].WithContext(ctx)
}
return r.cfg.Root.WithContext(ctx)
}
// 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) (store.Store, error) {
if name == "" {
return r.store, nil
}
if sub, found := r.mounts[name]; found {
return sub, nil
}
return nil, errors.Errorf("no such mount point '%s'", 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 errors.Errorf("Doubly mounted path at %s: %s", v.Path(), k)
}
paths[v.Path()] = k
}
return nil
}