/
move.go
217 lines (170 loc) · 5.86 KB
/
move.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
217
package leaf
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/queue"
"github.com/gopasspw/gopass/internal/store"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
)
// Copy will copy one entry to another location. Multi-store copies are
// supported. Each entry has to be decoded and encoded for the destination
// to make sure it's encrypted for the right set of recipients.
func (s *Store) Copy(ctx context.Context, from, to string) error {
// recursive copy?
if s.IsDir(ctx, from) {
return fmt.Errorf("recursive operations are not supported")
}
// try direct copy first
err := s.directMove(ctx, from, to, false)
if err == nil {
debug.Log("direct copy %s -> %s successful", from, to)
return nil
}
debug.Log("direct copy failed: %v", err)
content, err := s.Get(ctx, from)
if err != nil {
return fmt.Errorf("failed to get %q from store: %w", from, err)
}
if err := s.Set(ctxutil.WithCommitMessage(ctx, fmt.Sprintf("Copied from %s to %s", from, to)), to, content); err != nil {
if !errors.Is(err, store.ErrMeaninglessWrite) {
return fmt.Errorf("failed to save secret %q to store: %w", to, err)
}
out.Warningf(ctx, "No need to write: the secret is already there and with the right value")
}
return nil
}
// Move will move one entry from one location to another.
// Moving an entry will decode it from the old location, encode it
// for the destination store with the right set of recipients and remove it
// from the old location afterwards.
func (s *Store) Move(ctx context.Context, from, to string) error {
// recursive move?
if s.IsDir(ctx, from) {
return fmt.Errorf("recursive operations are not supported")
}
// try direct move first
err := s.directMove(ctx, from, to, true)
if err == nil {
debug.Log("direct move %s -> %s successful", from, to)
return nil
}
debug.Log("direct move failed: %v", err)
// fall back to copy and delete
content, err := s.Get(ctx, from)
if err != nil {
return fmt.Errorf("failed to decrypt %q: %w", from, err)
}
if err := s.Set(ctxutil.WithCommitMessage(ctx, fmt.Sprintf("Move from %s to %s", from, to)), to, content); err != nil {
if !errors.Is(err, store.ErrMeaninglessWrite) {
return fmt.Errorf("failed to save secret %q to store: %w", to, err)
}
out.Warningf(ctx, "No need to write: the secret is already there and with the right value")
}
if err := s.Delete(ctx, from); err != nil {
return fmt.Errorf("failed to delete %q: %w", from, err)
}
return nil
}
func (s *Store) directMove(ctx context.Context, from, to string, del bool) error {
ctx = ctxutil.WithCommitMessage(ctx, fmt.Sprintf("Move from %s to %s", from, to))
pFrom := s.Passfile(from)
pTo := s.Passfile(to)
debug.Log("directMove %s (%q) -> %s (%q)", from, to, pFrom, pTo)
if err := s.storage.Move(ctx, pFrom, pTo, del); err != nil {
return fmt.Errorf("failed to move %q to %q: %w", from, to, err)
}
// It is not possible to perform concurrent git add and git commit commands
// so we need to skip this step when using concurrency and perform them
// at the end of the batch processing.
if IsNoGitOps(ctx) {
debug.Log("sub.directMove(%q -> %q) - skipping git ops (disabled)", from, to)
return nil
}
if err := s.storage.TryAdd(ctx, pFrom, pTo); err != nil {
return fmt.Errorf("failed to add %q and %q to git: %w", pFrom, pTo, err)
}
if !ctxutil.IsGitCommit(ctx) {
return nil
}
// try to enqueue this task, if the queue is not available
// it will return the task and we will execute it inline
t := queue.GetQueue(ctx).Add(func(_ context.Context) (context.Context, error) {
return nil, s.gitCommitAndPush(ctx, to)
})
_, err := t(ctx)
return err
}
// Delete will remove an single entry from the store.
func (s *Store) Delete(ctx context.Context, name string) error {
return s.delete(ctx, name, false)
}
// Prune will remove a subtree from the Store.
func (s *Store) Prune(ctx context.Context, tree string) error {
return s.delete(ctx, tree, true)
}
// delete will either delete one file or an directory tree depending on the
// recurse flag.
func (s *Store) delete(ctx context.Context, name string, recurse bool) error {
path := s.Passfile(name)
if recurse {
if err := s.deleteRecurse(ctx, name, path); err != nil {
return err
}
}
if err := s.deleteSingle(ctx, path); err != nil {
// might fail if we deleted the root of a tree which isn't a secret
// itself
if !recurse {
return err
}
}
if !ctxutil.IsGitCommit(ctx) {
return nil
}
if err := s.storage.TryCommit(ctx, fmt.Sprintf("Remove %s from store.", name)); err != nil {
return fmt.Errorf("failed to commit changes to git: %w", err)
}
if !config.Bool(ctx, "core.autopush") {
debug.Log("not pushing to git remote, core.autopush is false")
return nil
}
if err := s.storage.TryPush(ctx, "", ""); err != nil {
return fmt.Errorf("failed to push change to git remote: %w", err)
}
return nil
}
func (s *Store) deleteRecurse(ctx context.Context, name, path string) error {
if !s.storage.IsDir(ctx, name) && !s.storage.Exists(ctx, path) {
return store.ErrNotFound
}
name = strings.TrimPrefix(name, string(filepath.Separator))
debug.Log("Pruning %s", name)
if err := s.storage.Prune(ctx, name); err != nil {
debug.Log("storage.Prune(%v) failed", name)
return err
}
if err := s.storage.TryAdd(ctx, name); err != nil {
return fmt.Errorf("failed to add %q to git: %w", path, err)
}
debug.Log("pruned")
return nil
}
func (s *Store) deleteSingle(ctx context.Context, path string) error {
if !s.storage.Exists(ctx, path) {
return store.ErrNotFound
}
debug.Log("Deleting %s", path)
if err := s.storage.Delete(ctx, path); err != nil {
return err
}
if err := s.storage.TryAdd(ctx, path); err != nil {
return fmt.Errorf("failed to add %q to git: %w", path, err)
}
return nil
}