forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
64 lines (52 loc) · 1.68 KB
/
init.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
package sub
import (
"context"
"strings"
"github.com/gopasspw/gopass/pkg/out"
"github.com/pkg/errors"
)
// Initialized returns true if the store is properly initialized
func (s *Store) Initialized(ctx context.Context) bool {
if s == nil || s.storage == nil {
return false
}
return s.storage.Exists(ctx, s.idFile(ctx, ""))
}
// Init tries to initialize a new password store location matching the object
func (s *Store) Init(ctx context.Context, path string, ids ...string) error {
if s.Initialized(ctx) {
return errors.Errorf(`Found already initialized store at %s.
You can add secondary stores with gopass init --path <path to secondary store> --store <mount name>`, path)
}
// initialize recipient list
recipients := make([]string, 0, len(ids))
for _, id := range ids {
if id == "" {
continue
}
kl, err := s.crypto.FindPublicKeys(ctx, id)
if err != nil {
out.Error(ctx, "Failed to fetch public key for '%s': %s", id, err)
continue
}
if len(kl) < 1 {
out.Error(ctx, "No useable keys for '%s'", id)
continue
}
recipients = append(recipients, kl[0])
}
if len(recipients) < 1 {
return errors.Errorf("failed to initialize store: no valid recipients given in %+v", ids)
}
kl, err := s.crypto.FindPrivateKeys(ctx, recipients...)
if err != nil {
return errors.Errorf("Failed to get available private keys: %s", err)
}
if len(kl) < 1 {
return errors.Errorf("None of the recipients has a secret key. You will not be able to decrypt the secrets you add")
}
if err := s.saveRecipients(ctx, recipients, "Initialized Store for "+strings.Join(recipients, ", "), true); err != nil {
return errors.Wrapf(err, "failed to initialize store: %s", err)
}
return nil
}