forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone.go
278 lines (227 loc) · 8.43 KB
/
clone.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
package action
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/backend/crypto/age"
"github.com/gopasspw/gopass/internal/backend/crypto/gpg"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/cui"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/set"
"github.com/gopasspw/gopass/internal/store/root"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/fsutil"
"github.com/gopasspw/gopass/pkg/termio"
"github.com/urfave/cli/v2"
)
// Clone will fetch and mount a new password store from a git repo.
func (s *Action) Clone(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
if c.IsSet("crypto") {
ctx = backend.WithCryptoBackendString(ctx, c.String("crypto"))
}
if c.IsSet("storage") {
ctx = backend.WithStorageBackendString(ctx, c.String("storage"))
}
path := c.String("path")
if c.Args().Len() < 1 {
return exit.Error(exit.Usage, nil, "Usage: %s clone repo [mount]", s.Name)
}
// gopass clone [--crypto=foo] [--path=/some/store] git://foo/bar team0.
repo := c.Args().Get(0)
mount := ""
if c.Args().Len() > 1 {
mount = c.Args().Get(1)
}
out.Printf(ctx, logo)
out.Printf(ctx, "🌟 Welcome to gopass!")
out.Printf(ctx, "🌟 Cloning an existing password store from %q ...", repo)
if name := termio.DetectName(ctx, c); name != "" {
ctx = ctxutil.WithUsername(ctx, name)
}
if email := termio.DetectEmail(ctx, c); email != "" {
ctx = ctxutil.WithEmail(ctx, email)
}
// age: only native keys
// "[ssh] types should only be used for compatibility with existing keys,
// and native X25519 keys should be preferred otherwise."
// https://pkg.go.dev/filippo.io/[email protected]/agessh#pkg-overview.
ctx = age.WithOnlyNative(ctx, true)
// gpg: only trusted keys
// only list "usable" / properly trused and signed GPG keys by requesting
// always trust is false. Ignored for other backends. See
// https://www.gnupg.org/gph/en/manual/r1554.html.
ctx = gpg.WithAlwaysTrust(ctx, false)
if err := s.clone(ctx, repo, mount, path); err != nil {
return err
}
// need to re-initialize the root store or it's already initialized
// and won't properly set up crypto according to our context.
s.Store = root.New(s.cfg)
inited, err := s.Store.IsInitialized(ctx)
if err != nil {
return exit.Error(exit.Unknown, err, "Failed to check store status: %s", err)
}
if !inited {
out.Errorf(ctx, "Failed to clone")
return nil
}
if !c.Bool("check-keys") {
return nil
}
return s.cloneCheckDecryptionKeys(ctx, mount)
}
// storageBackendOrDefault will return a storage backend that can be clone,
// i.e. specifically backend.FS can't be cloned.
func storageBackendOrDefault(ctx context.Context, repo string) backend.StorageBackend {
// first try to get it from the context.
if be := backend.GetStorageBackend(ctx); be != backend.FS {
return be
}
if strings.HasSuffix(repo, ".fossil") {
return backend.FossilFS
}
if strings.HasSuffix(repo, ".git") {
return backend.GitFS
}
debug.Log("falling back to the default storage backend for clone (GitFS)")
return backend.GitFS
}
func (s *Action) clone(ctx context.Context, repo, mount, path string) error {
if path == "" {
path = config.PwStoreDir(mount)
}
inited, err := s.Store.IsInitialized(ctxutil.WithGitInit(ctx, false))
if err != nil {
return exit.Error(exit.Unknown, err, "Failed to initialized stores: %s", err)
}
if mount == "" && inited {
return exit.Error(exit.AlreadyInitialized, nil, "Can not clone %s to the root store, as this store is already initialized. Please try cloning to a submount: `%s clone %s sub`", repo, s.Name, repo)
}
// make sure the parent directory exists.
if parentPath := filepath.Dir(path); !fsutil.IsDir(parentPath) {
if err := os.MkdirAll(parentPath, 0o700); err != nil {
return exit.Error(exit.Unknown, err, "Failed to create parent directory for clone: %s", err)
}
}
// clone repo.
sb := storageBackendOrDefault(ctx, repo)
out.Noticef(ctx, "Cloning %s repository %q to %q ...", sb, repo, path)
if _, err := backend.Clone(ctx, sb, repo, path); err != nil {
return exit.Error(exit.Git, err, "failed to clone repo %q to %q: %s", repo, path, err)
}
// add mount.
debug.Log("Mounting cloned repo %q at %q", path, mount)
if err := s.cloneAddMount(ctx, mount, path); err != nil {
return err
}
// try to init repo config.
out.Noticef(ctx, "Configuring %s repository ...", sb)
// ask for config values.
username, email, err := s.cloneGetGitConfig(ctx, mount)
if err != nil {
return err
}
// initialize repo config.
if err := s.Store.RCSInitConfig(ctx, mount, username, email); err != nil {
debug.Log("Stacktrace: %+v\n", err)
out.Errorf(ctx, "Failed to configure %s: %s", sb, err)
}
if mount != "" {
mount = " " + mount
}
out.Printf(ctx, "Your password store is ready to use! Have a look around: `%s list%s`\n", s.Name, mount)
return nil
}
func (s *Action) cloneCheckDecryptionKeys(ctx context.Context, mount string) error {
crypto := s.getCryptoFor(ctx, mount)
if crypto == nil {
return fmt.Errorf("can not continue without crypto")
}
debug.Log("Crypto Backend initialized as: %s", crypto.Name())
// check for existing GPG/Age keypairs (private/secret keys). We need at least
// one useable key pair. If none exists try to create one.
if !s.initHasUseablePrivateKeys(ctx, crypto) {
out.Printf(ctx, "🔐 No useable cryptographic keys. Generating new key pair")
if crypto.Name() == "gpgcli" {
out.Printf(ctx, "🕰 Key generation may take up to a few minutes")
}
if err := s.initGenerateIdentity(ctx, crypto, ctxutil.GetUsername(ctx), ctxutil.GetEmail(ctx)); err != nil {
return fmt.Errorf("failed to create new private key: %w", err)
}
out.Printf(ctx, "🔐 Cryptographic keys generated")
}
debug.Log("We have useable private keys")
recpSet := set.New(s.Store.ListRecipients(ctx, mount)...)
ids, err := crypto.ListIdentities(ctx)
if err != nil {
out.Warningf(ctx, "Failed to check decryption keys: %s", err)
return nil
}
idSet := set.New(ids...)
if idSet.IsSubset(recpSet) {
out.Noticef(ctx, "Found valid decryption keys. You can now decrypt your passwords.")
return nil
}
var exported bool
if sub, err := s.Store.GetSubStore(mount); err == nil {
debug.Log("exporting public keys: %v", idSet.Elements())
exported, err = sub.UpdateExportedPublicKeys(ctx, idSet.Elements())
if err != nil {
debug.Log("failed to export missing public keys: %w", err)
}
} else {
debug.Log("failed to get sub store: %s", err)
}
out.Noticef(ctx, "Please ask the owner of the password store to add one of your keys: %s", strings.Join(idSet.Elements(), ", "))
if exported {
out.Noticef(ctx, "The missing keys were exported to the password store. Run `gopass sync` to push them.")
}
return nil
}
func (s *Action) cloneAddMount(ctx context.Context, mount, path string) error {
if mount == "" {
return nil
}
inited, err := s.Store.IsInitialized(ctx)
if err != nil {
return exit.Error(exit.Unknown, err, "Failed to initialize store: %s", err)
}
if !inited {
return exit.Error(exit.NotInitialized, nil, "Root-Store is not initialized. Clone or init root store first")
}
if err := s.Store.AddMount(ctx, mount, path); err != nil {
return exit.Error(exit.Mount, err, "Failed to add mount: %s", err)
}
out.Printf(ctx, "Mounted password store %s at mount point `%s` ...", path, mount)
return nil
}
func (s *Action) cloneGetGitConfig(ctx context.Context, name string) (string, string, error) {
out.Printf(ctx, "🎩 Gathering information for the git repository ...")
// for convenience, set defaults to user-selected values from available private keys.
// NB: discarding returned error since this is merely a best-effort look-up for convenience.
username, email, _ := cui.AskForGitConfigUser(ctx, s.Store.Crypto(ctx, name))
if username == "" {
username = termio.DetectName(ctx, nil)
var err error
username, err = termio.AskForString(ctx, "🚶 What is your name?", username)
if err != nil {
return "", "", exit.Error(exit.IO, err, "Failed to read user input: %s", err)
}
}
if email == "" {
email = termio.DetectEmail(ctx, nil)
var err error
email, err = termio.AskForString(ctx, "📧 What is your email?", email)
if err != nil {
return "", "", exit.Error(exit.IO, err, "Failed to read user input: %s", err)
}
}
return username, email, nil
}