-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
sync.go
289 lines (234 loc) · 6.96 KB
/
sync.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package action
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"time"
"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/diff"
"github.com/gopasspw/gopass/internal/notify"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/store"
"github.com/gopasspw/gopass/internal/store/leaf"
"github.com/gopasspw/gopass/internal/tree"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/urfave/cli/v2"
"github.com/xhit/go-str2duration"
)
var (
autosyncInterval = time.Duration(3*24) * time.Hour
autosyncLastRun time.Time
)
func init() {
sv := os.Getenv("GOPASS_AUTOSYNC_INTERVAL")
if sv == "" {
return
}
debug.Log("GOPASS_AUTOSYNC_INTERVAL is deprecated. Please use autosync.interval")
iv, err := strconv.Atoi(sv)
if err != nil {
return
}
autosyncInterval = time.Duration(iv*24) * time.Hour
}
// Sync all stores with their remotes.
func (s *Action) Sync(c *cli.Context) error {
return s.sync(ctxutil.WithGlobalFlags(c), c.String("store"))
}
func (s *Action) autoSync(ctx context.Context) error {
if !ctxutil.IsInteractive(ctx) {
return nil
}
if !ctxutil.IsTerminal(ctx) {
return nil
}
if sv := os.Getenv("GOPASS_NO_AUTOSYNC"); sv != "" {
out.Warning(ctx, "GOPASS_NO_AUTOSYNC is deprecated. Please set core.autosync = false.")
return nil
}
if !config.Bool(ctx, "core.autosync") {
return nil
}
ls := s.rem.LastSeen("autosync")
debug.Log("autosync - last seen: %s", ls)
syncInterval := autosyncInterval
if intervalStr := s.cfg.Get("autosync.interval"); intervalStr != "" {
if _, err := strconv.Atoi(intervalStr); err == nil {
intervalStr += "d"
}
if duration, err := str2duration.Str2Duration(intervalStr); err != nil {
out.Warningf(ctx, "failed to parse autosync.interval %q: %q", intervalStr, err)
} else {
syncInterval = duration
}
}
debug.Log("autosync - interval: %s", syncInterval)
if time.Since(ls) > syncInterval {
err := s.sync(ctx, "")
if err != nil {
autosyncLastRun = time.Now()
}
return err
}
return nil
}
func (s *Action) sync(ctx context.Context, store string) error {
// we just did a full sync, no need to run it again
if time.Since(autosyncLastRun) < 10*time.Second {
debug.Log("skipping sync. last sync %ds ago", time.Since(autosyncLastRun))
return nil
}
out.Printf(ctx, "🚥 Syncing with all remotes ...")
numEntries := 0
if l, err := s.Store.Tree(ctx); err == nil {
numEntries = len(l.List(tree.INF))
}
numMPs := 0
mps := s.Store.MountPoints()
mps = append([]string{""}, mps...)
// sync all stores (root and all mounted sub stores).
for _, mp := range mps {
if store != "" {
if store != "<root>" && mp != store {
continue
}
if store == "<root>" && mp != "" {
continue
}
}
numMPs++
_ = s.syncMount(ctx, mp)
}
out.OKf(ctx, "All done")
// If we just sync'ed all stores we can reset the auto-sync interval
if store == "" {
_ = s.rem.Reset("autosync")
}
// Calculate number of changed entries.
// This is a rough estimate as additions and deletions.
// might cancel each other out.
if l, err := s.Store.Tree(ctx); err == nil {
numEntries = len(l.List(tree.INF)) - numEntries
}
diff := ""
if numEntries > 0 {
diff = fmt.Sprintf(" Added %d entries", numEntries)
} else if numEntries < 0 {
diff = fmt.Sprintf(" Removed %d entries", -1*numEntries)
}
if numEntries != 0 {
ctx = config.WithMount(ctx, store)
_ = notify.Notify(ctx, "gopass - sync", fmt.Sprintf("Finished. Synced %d remotes.%s", numMPs, diff))
}
return nil
}
// syncMount syncs a single mount.
func (s *Action) syncMount(ctx context.Context, mp string) error {
// using GetM here to get the value for this mount, it might be different
// than the global value
if as := s.cfg.GetM(mp, "core.autosync"); as == "false" {
debug.Log("not syncing %s, autosync is disabled for this mount", mp)
return nil
}
ctxno := out.WithNewline(ctx, false)
name := mp
if mp == "" {
name = "<root>"
}
out.Printf(ctxno, color.GreenString("[%s] ", name))
sub, err := s.Store.GetSubStore(mp)
if err != nil {
out.Errorf(ctx, "Failed to get sub store %q: %s", name, err)
return fmt.Errorf("failed to get sub stores (%w)", err)
}
if sub == nil {
out.Errorf(ctx, "Failed to get sub stores '%s: nil'", name)
return fmt.Errorf("failed to get sub stores (nil)")
}
l, err := sub.List(ctx, "")
if err != nil {
out.Errorf(ctx, "Failed to list store: %s", err)
}
out.Printf(ctxno, "\n "+color.GreenString("%s pull and push ... ", sub.Storage().Name()))
switch err := sub.Storage().Push(ctx, "", ""); {
case err == nil:
debug.Log("Push succeeded")
out.Printf(ctxno, color.GreenString("OK"))
case errors.Is(err, store.ErrGitNoRemote):
out.Printf(ctx, "Skipped (no remote)")
debug.Log("Failed to push %q to its remote: %s", name, err)
return err
case errors.Is(err, backend.ErrNotSupported):
out.Printf(ctxno, "Skipped (not supported)")
case errors.Is(err, store.ErrGitNotInit):
out.Printf(ctxno, "Skipped (no Git repo)")
default: // any other error
out.Errorf(ctx, "Failed to push %q to its remote: %s", name, err)
return err
}
ln, err := sub.List(ctx, "")
if err != nil {
out.Errorf(ctx, "Failed to list store: %s", err)
}
syncPrintDiff(ctxno, l, ln)
exportKeys := s.cfg.GetBoolM(mp, "core.exportkeys")
debug.Log("Syncing Mount %s. Exportkeys: %t", mp, exportKeys)
if err := syncImportKeys(ctxno, sub, name); err != nil {
return err
}
if exportKeys {
if err := syncExportKeys(ctxno, sub, name); err != nil {
return err
}
}
out.Printf(ctx, "\n "+color.GreenString("done"))
return nil
}
func syncImportKeys(ctx context.Context, sub *leaf.Store, name string) error {
// import keys.
if err := sub.ImportMissingPublicKeys(ctx); err != nil {
out.Errorf(ctx, "Failed to import missing public keys for %q: %s", name, err)
return err
}
return nil
}
func syncExportKeys(ctx context.Context, sub *leaf.Store, name string) error {
// export keys.
rs, err := sub.GetRecipients(ctx, "")
if err != nil {
out.Errorf(ctx, "Failed to load recipients for %q: %s", name, err)
return err
}
exported, err := sub.UpdateExportedPublicKeys(ctx, rs.IDs())
if err != nil {
out.Errorf(ctx, "Failed to export missing public keys for %q: %s", name, err)
return err
}
// only run second push if we did export any keys.
if !exported {
return nil
}
if err := sub.Storage().Push(ctx, "", ""); err != nil {
out.Errorf(ctx, "Failed to push %q to its remote: %s", name, err)
return err
}
return nil
}
func syncPrintDiff(ctxno context.Context, l, r []string) {
added, removed := diff.Stat(l, r)
debug.Log("diff - added: %d - removed: %d", added, removed)
if added > 0 {
out.Printf(ctxno, color.GreenString(" (Added %d entries)", added))
}
if removed > 0 {
out.Printf(ctxno, color.GreenString(" (Removed %d entries)", removed))
}
if added < 1 && removed < 1 {
out.Printf(ctxno, color.GreenString(" (no changes)"))
}
}