-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
fsck.go
105 lines (90 loc) · 2.45 KB
/
fsck.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
package sub
import (
"context"
"sort"
"github.com/justwatchcom/gopass/pkg/out"
"github.com/pkg/errors"
)
// Fsck checks all entries matching the given prefix
func (s *Store) Fsck(ctx context.Context, path string) error {
// first let the storage backend check itself
if err := s.storage.Fsck(ctx); err != nil {
return errors.Wrapf(err, "storage backend found errors: %s", err)
}
// then we'll make sure all the secrets are readable by us and every
// valid recipient
names, err := s.List(ctx, path)
if err != nil {
return errors.Wrapf(err, "failed to list entries: %s", err)
}
sort.Strings(names)
for _, name := range names {
if err := s.fsckCheckEntry(ctx, name); err != nil {
return errors.Wrapf(err, "failed to check %s: %s", name, err)
}
}
return nil
}
func (s *Store) fsckCheckEntry(ctx context.Context, name string) error {
// make sure we can actually decode this secret
// if this fails there is no way we could fix this
_, err := s.Get(ctx, name)
if err != nil {
return errors.Wrapf(err, "failed to decode secret %s: %s", name, err)
}
// now compare the recipients this secret was encoded for and fix it if
// if doesn't match
ciphertext, err := s.storage.Get(ctx, s.passfile(name))
if err != nil {
return err
}
itemRecps, err := s.crypto.RecipientIDs(ctx, ciphertext)
if err != nil {
return err
}
perItemStoreRecps, err := s.GetRecipients(ctx, name)
if err != nil {
return err
}
// check itemRecps matches storeRecps
missing, extra := compareStringSlices(perItemStoreRecps, itemRecps)
if len(missing) > 0 {
out.Red(ctx, "Missing recipients on %s: %+v", name, missing)
}
if len(extra) > 0 {
out.Red(ctx, "Extra recipients on %s: %+v", name, extra)
}
if len(missing) > 0 || len(extra) > 0 {
sec, err := s.Get(ctx, name)
if err != nil {
return err
}
if err := s.Set(WithReason(ctx, "fsck fix recipients"), name, sec); err != nil {
return err
}
}
return nil
}
func compareStringSlices(want, have []string) ([]string, []string) {
missing := []string{}
extra := []string{}
wantMap := make(map[string]struct{}, len(want))
haveMap := make(map[string]struct{}, len(have))
for _, w := range want {
wantMap[w] = struct{}{}
}
for _, h := range have {
haveMap[h] = struct{}{}
}
for k := range wantMap {
if _, found := haveMap[k]; !found {
missing = append(missing, k)
}
}
for k := range haveMap {
if _, found := wantMap[k]; !found {
extra = append(extra, k)
}
}
return missing, extra
}