-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
wizard.go
421 lines (359 loc) · 11.6 KB
/
wizard.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package create
import (
"context"
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/cui"
"github.com/gopasspw/gopass/internal/editor"
"github.com/gopasspw/gopass/internal/hook"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/set"
"github.com/gopasspw/gopass/internal/store/root"
"github.com/gopasspw/gopass/internal/tpl"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/fsutil"
"github.com/gopasspw/gopass/pkg/gopass/secrets"
"github.com/gopasspw/gopass/pkg/pwgen"
"github.com/gopasspw/gopass/pkg/pwgen/pwrules"
"github.com/gopasspw/gopass/pkg/termio"
"github.com/martinhoefling/goxkcdpwgen/xkcdpwgen"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
const (
tplPath = ".gopass/create/"
)
// Attribute is a credential attribute that is being asked for
// when populating a template.
type Attribute struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Prompt string `yaml:"prompt"`
Charset string `yaml:"charset"`
Min int `yaml:"min"`
Max int `yaml:"max"`
AlwaysPrompt bool `yaml:"always_prompt"` // always prompt for the crendentials
}
// Template is an action template for the create wizard.
type Template struct {
Name string `yaml:"name"`
Priority int `yaml:"priority"`
Prefix string `yaml:"prefix"`
NameFrom []string `yaml:"name_from"`
Welcome string `yaml:"welcome"`
Attributes []Attribute `yaml:"attributes"`
}
// Wizard is the templateable credential creation wizard.
type Wizard struct {
Templates []Template
}
// New creates a new instance of the wizard. It will parse the user
// supplied templates and add the default templates.
func New(ctx context.Context, s backend.Storage) (*Wizard, error) {
w := &Wizard{}
tpls, err := w.parseTemplates(ctx, s)
if err != nil {
return w, fmt.Errorf("could not parse templates: %w", err)
}
if len(tpls) < 1 {
// no templates found, write default templates
if err := w.writeTemplates(ctx, s); err != nil {
return w, fmt.Errorf("could not write default templates: %w", err)
}
// then re-parse them
tpls, err = w.parseTemplates(ctx, s)
if err != nil {
return w, fmt.Errorf("could not parse templates: %w", err)
}
}
// fallback to the default templates if no templates were found
// even after writing them. this indicates a problem with writing
// and should be investigated.
if len(tpls) < 1 {
out.Error(ctx, "No templates found, falling back to default templates")
tpls, err = w.parseTemplatesFallback(ctx)
if err != nil {
return w, fmt.Errorf("could not parse fallback templates: %w", err)
}
}
w.Templates = tpls
return w, nil
}
func (w *Wizard) parseTemplatesFallback(ctx context.Context) ([]Template, error) {
parsed := []Template{}
for _, tpl := range defaultTemplates {
t := Template{}
if err := yaml.Unmarshal([]byte(tpl), &t); err != nil {
return nil, err
}
parsed = append(parsed, t)
}
return parsed, nil
}
func (w *Wizard) parseTemplates(ctx context.Context, s backend.Storage) ([]Template, error) {
tpls, err := s.List(ctx, tplPath)
if err != nil {
return nil, err
}
parsedTpls := []Template{}
for _, f := range tpls {
if !strings.HasSuffix(f, ".yml") && !strings.HasSuffix(f, ".yaml") {
debug.Log("ignoring unknown file extension: %s", f)
continue
}
buf, err := s.Get(ctx, f)
if err != nil {
debug.Log("failed to read template %s: %s", f, err)
continue
}
tpl := Template{}
if err := yaml.Unmarshal(buf, &tpl); err != nil {
debug.Log("failed to parse template %s: %s", f, err)
out.Errorf(ctx, "Bad template %s: %s\n%s", f, err, string(buf))
continue
}
parsedTpls = append(parsedTpls, tpl)
}
sort.Slice(parsedTpls, func(i, j int) bool {
return parsedTpls[i].Priority < parsedTpls[j].Priority
})
return parsedTpls, nil
}
// ActionCallback is the callback for the creation calls to print and copy the credentials.
type ActionCallback func(context.Context, *cli.Context, string, string, bool) error
// Actions returns a list of actions that can be performed on the wizard. The actions directly
// interact with the underlying storage.
func (w *Wizard) Actions(s *root.Store, cb ActionCallback) cui.Actions {
sort.Slice(w.Templates, func(i, j int) bool {
return w.Templates[i].Priority < w.Templates[j].Priority
})
acts := make(cui.Actions, 0, len(w.Templates))
for _, tpl := range w.Templates {
acts = append(acts, cui.Action{
Name: tpl.Name,
Fn: mkActFunc(tpl, s, cb),
})
}
return acts
}
func mkActFunc(tpl Template, s *root.Store, cb ActionCallback) func(context.Context, *cli.Context) error { //nolint:cyclop
debug.Log("creating action func for %+v, cb: %p", tpl, cb)
return func(ctx context.Context, c *cli.Context) error {
name := c.Args().First()
store := c.String("store")
// select store.
if store == "" {
store = cui.AskForStore(ctx, s)
}
ctx = config.WithMount(ctx, store)
force := c.Bool("force")
if err := hook.Invoke(ctx, "create.pre-hook", name); err != nil {
return err
}
sec := secrets.NewAKV()
out.Print(ctx, tpl.Welcome)
// genPW is needed for the callback
var genPw bool
// password is needed for the callback
var password string
// hostname is needed in later iterations (e.g. password rule lookup)
var hostname string
// wantForName is a list of attributes that will be used to build the name
wantForName := set.Map(tpl.NameFrom)
// nameParts are the components the name will be built from
var nameParts []string
// step is only used for printing the progress
var step int
for _, v := range tpl.Attributes {
step++
k := v.Name
// if no prompt is set default to the key
if v.Prompt == "" {
v.Prompt = strings.ToTitle(k)
}
switch v.Type {
case "string":
sv, err := termio.AskForString(ctx, fmtfn(2, strconv.Itoa(step), v.Prompt), "")
if err != nil {
return err
}
if v.Min > 0 && len(sv) < v.Min {
return fmt.Errorf("%s is too short (needs %d)", v.Name, v.Min)
}
if v.Max > 0 && len(sv) > v.Min {
return fmt.Errorf("%s is too long (at most %d)", v.Name, v.Max)
}
if wantForName[k] {
nameParts = append(nameParts, sv)
}
_ = sec.Set(k, sv)
case "multiline":
ed := editor.Path(c)
content, err := renderTemplate(ctx, k, s)
if err != nil {
debug.Log("failed to render template %q: %s", k, err)
}
content, err = editor.Invoke(ctx, ed, content)
if err != nil {
return exit.Error(exit.Unknown, err, "failed to invoke editor: %s", err)
}
n, err := sec.Write(content)
if err != nil {
return fmt.Errorf("failed to write %d bytes to %s: %w", n, k, err)
}
case "hostname":
var def string
if k == "username" {
def = config.String(ctx, "create.default-username")
}
sv, err := termio.AskForString(ctx, fmtfn(2, strconv.Itoa(step), v.Prompt), def)
if err != nil {
return err
}
hostname = extractHostname(sv)
if hostname == "" {
return fmt.Errorf("can not parse URL %s", sv)
}
if wantForName[k] {
nameParts = append(nameParts, hostname)
}
if u := pwrules.LookupChangeURL(ctx, hostname); u != "" {
_ = sec.Set("password-change-url", u)
}
_ = sec.Set(k, sv)
case "password":
var err error
if !v.AlwaysPrompt {
genPw, err = termio.AskForBool(ctx, fmtfn(2, strconv.Itoa(step), "Generate Password?"), true)
if err != nil {
return err
}
}
if genPw { //nolint:nestif
password, err = generatePassword(ctx, hostname, v.Charset)
if err != nil {
return err
}
} else {
password, err = termio.AskForPassword(ctx, v.Prompt, true)
if err != nil {
return err
}
if v.Min > 0 && len(password) < v.Min {
return fmt.Errorf("%s is too short (needs %d)", v.Name, v.Min)
}
if v.Max > 0 && len(password) > v.Min {
return fmt.Errorf("%s is too long (at most %d)", v.Name, v.Max)
}
}
sec.SetPassword(password)
}
}
// now we can generate a name. If it's already take we can the user for an alternative
// name.
// make sure the store is properly separated from the name.
if store != "" {
store += "/"
}
// by default create will generate a name for the secret based on the user
// input. Only when the force flag is given it will accept a secrets path
// as the first argument.
if name == "" || !force {
for i, s := range nameParts {
nameParts[i] = fsutil.CleanFilename(s)
}
name = fmt.Sprintf("%s%s/%s", store, tpl.Prefix, filepath.Join(nameParts...))
}
if force && !strings.HasPrefix(name, store) {
out.Warningf(ctx, "User supplied secret name %q does not match requested mount %q. Ignoring store flag.", name, store)
}
// force will also override the check for existing entries.
if s.Exists(ctx, name) && !force {
step++
var err error
name, err = termio.AskForString(ctx, fmtfn(2, strconv.Itoa(step), "Secret already exists. Choose another path or enter to overwrite"), name)
if err != nil {
return err
}
}
if err := s.Set(ctxutil.WithCommitMessage(ctx, "Created new entry"), name, sec); err != nil {
return fmt.Errorf("failed to set %q: %w", name, err)
}
out.OKf(ctx, "Credentials saved to %q", name)
return cb(ctx, c, name, password, genPw)
}
}
func renderTemplate(ctx context.Context, name string, s *root.Store) ([]byte, error) {
tName, tmpl, found := s.LookupTemplate(ctx, name)
if !found {
debug.Log("No template found for %s", name)
return nil, nil
}
tmplStr := strings.TrimSpace(string(tmpl))
if tmplStr == "" {
debug.Log("Skipping empty template %q, for %s", tName, name)
return nil, nil
}
nc, err := tpl.Execute(ctx, string(tmpl), name, nil, s)
if err != nil {
return nil, fmt.Errorf("failed to execute template %q: %w", tName, err)
}
return nc, nil
}
// generatePasssword will walk through the password generation steps.
func generatePassword(ctx context.Context, hostname, charset string) (string, error) {
defaultLength, _ := config.DefaultPasswordLengthFromEnv(ctx)
if charset != "" {
length, err := termio.AskForInt(ctx, fmtfn(4, "a", "How long?"), 4)
if err != nil {
return "", err
}
return pwgen.GeneratePasswordCharset(length, charset), nil
}
if _, found := pwrules.LookupRule(ctx, hostname); found {
out.Noticef(ctx, "Using password rules for %s ...", hostname)
length, err := termio.AskForInt(ctx, fmtfn(4, "b", "How long?"), defaultLength)
if err != nil {
return "", err
}
return pwgen.NewCrypticForDomain(ctx, length, hostname).Password(), nil
}
xkcd, err := termio.AskForBool(ctx, fmtfn(4, "a", "Human-pronounceable passphrase?"), false)
if err != nil {
return "", err
}
if xkcd {
length, err := termio.AskForInt(ctx, fmtfn(4, "b", "How many words?"), config.DefaultXKCDLength)
if err != nil {
return "", err
}
g := xkcdpwgen.NewGenerator()
g.SetNumWords(length)
g.SetDelimiter(" ")
g.SetCapitalize(true)
return string(g.GeneratePassword()), nil
}
length, err := termio.AskForInt(ctx, fmtfn(4, "b", "How long?"), defaultLength)
if err != nil {
return "", err
}
symbols, err := termio.AskForBool(ctx, fmtfn(4, "c", "Include symbols?"), false)
if err != nil {
return "", err
}
corp, err := termio.AskForBool(ctx, fmtfn(4, "d", "Strict rules?"), false)
if err != nil {
return "", err
}
if corp {
return pwgen.GeneratePasswordWithAllClasses(length, symbols)
}
return pwgen.GeneratePassword(length, symbols), nil
}