Skip to content

Commit d6e8dd5

Browse files
authored
Adding back the global yes flag and bugfix insert (gopasspw#1596)
Fixes gopasspw#1595 RELEASE_NOTES=[BUGFIX] Re-adding the global --yes flag RELEASE_NOTES=[BUGFIX] Insert is not resetting the pw now if a key:value pair is specified inline This is just adding --yes back since it was mistakingly removed. Signed-off-by: Yolan Romailler <yolan@romailler.ch>
1 parent 10f89fe commit d6e8dd5

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

docs/commands/gopass.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ Flag | Aliases | Description
2525
---- | ------- | -----------
2626
`--clip` | `-c` | Copy the password value into the clipboard and don't show the content.
2727
`--unsafe` | `-u` | Display unsafe content (e.g. the password) even when the `safecontent` option is set. No-op when `safecontent` is `false`.
28+
`--yes` | | Assume yes on all yes/no questions or use the default on all others.
2829

internal/action/insert.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,14 @@ func (s *Action) insertSingle(ctx context.Context, name, pw string, kvps map[str
126126
}
127127
}
128128
}
129+
129130
setMetadata(sec, kvps)
130-
sec.Set("password", pw)
131-
audit.Single(ctx, pw)
131+
132+
// we only update the pw if the kvps were not set or if it's non-empty, because otherwise we were updating the kvps
133+
if pw != "" || len(kvps) == 0 {
134+
sec.Set("password", pw)
135+
audit.Single(ctx, pw)
136+
}
132137

133138
if err := s.Store.Set(ctxutil.WithCommitMessage(ctx, "Inserted user supplied password"), name, sec); err != nil {
134139
return ExitError(ExitEncrypt, err, "failed to write secret '%s': %s", name, err)

internal/action/insert_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ func TestInsert(t *testing.T) {
9595
assert.NoError(t, act.Insert(gptest.CliCtxWithFlags(ctx, t, map[string]string{"multiline": "true"}, "bar", "baz")))
9696
buf.Reset()
9797
})
98+
99+
t.Run("insert key:value", func(t *testing.T) {
100+
assert.NoError(t, act.Insert(gptest.CliCtxWithFlags(ctx, t, nil, "keyvaltest", "baz:val")))
101+
assert.NoError(t, act.show(ctx, gptest.CliCtx(ctx, t), "keyvaltest", false))
102+
assert.Equal(t, "Baz: val\n", buf.String())
103+
buf.Reset()
104+
})
98105
}
99106

100107
func TestInsertStdin(t *testing.T) {

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func setupApp(ctx context.Context, sv semver.Version) (context.Context, *cli.App
150150
Aliases: []string{"u", "force", "f"},
151151
Usage: "Display unsafe content (e.g. the password) even if safecontent is enabled",
152152
},
153+
&cli.BoolFlag{
154+
Name: "yes",
155+
Usage: "Assume yes on all yes/no questions or use the default on all others",
156+
},
153157
}
154158
app.Action = func(c *cli.Context) error {
155159
if err := action.Initialized(c); err != nil {

tests/insert_test.go

+61-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,78 @@ func TestInsert(t *testing.T) {
2626
t.Run("Regression test for #1573 without actual pipes", func(t *testing.T) {
2727
out, err = ts.run("show -f some/secret")
2828
assert.NoError(t, err)
29-
assert.Equal(t, out, "Password: moar")
29+
assert.Equal(t, "Password: moar", out)
3030

3131
out, err = ts.run("show -f some/newsecret")
3232
assert.NoError(t, err)
33-
assert.Equal(t, out, "Password: and\n\nmoar")
33+
assert.Equal(t, "Password: and\n\nmoar", out)
3434

3535
_, err := ts.run("config mime false")
3636
assert.NoError(t, err)
3737

3838
out, err = ts.run("show -f some/secret")
3939
assert.NoError(t, err)
40-
assert.Equal(t, out, "moar")
40+
assert.Equal(t, "moar", out)
4141

4242
out, err = ts.run("show -f some/newsecret")
4343
assert.NoError(t, err)
44-
assert.Equal(t, out, "and\n\nmoar")
44+
assert.Equal(t, "and\n\nmoar", out)
45+
})
46+
47+
t.Run("Regression test for #1595", func(t *testing.T) {
48+
_, err = ts.runCmd([]string{ts.Binary, "insert", "some/other"}, []byte("nope"))
49+
assert.NoError(t, err)
50+
51+
out, err = ts.run("insert some/other")
52+
assert.Error(t, err)
53+
assert.Equal(t, "\nError: not overwriting your current secret\n", out)
54+
55+
out, err = ts.run("show -o some/other")
56+
assert.NoError(t, err)
57+
assert.Equal(t, "nope", out)
58+
59+
out, err = ts.run("--yes insert some/other")
60+
assert.NoError(t, err)
61+
assert.Equal(t, "Warning: Password is empty or all whitespace", out)
62+
63+
out, err = ts.run("insert -f some/other")
64+
assert.NoError(t, err)
65+
assert.Equal(t, "Warning: Password is empty or all whitespace", out)
66+
67+
out, err = ts.run("show -o some/other")
68+
assert.Error(t, err)
69+
assert.Equal(t, "\nError: empty secret\n", out)
70+
71+
_, err = ts.runCmd([]string{ts.Binary, "insert", "-f", "some/other"}, []byte("final"))
72+
assert.NoError(t, err)
73+
74+
out, err = ts.run("show -o some/other")
75+
assert.NoError(t, err)
76+
assert.Equal(t, "final", out)
77+
78+
// This is arguably not a good behaviour: it should not overwrite the password when we are only working on a key:value.
79+
out, err = ts.run("insert -f some/other test:inline")
80+
assert.NoError(t, err)
81+
assert.Equal(t, "", out)
82+
83+
out, err = ts.run("show some/other Test")
84+
assert.NoError(t, err)
85+
assert.Equal(t, "inline", out)
86+
87+
out, err = ts.run("insert some/other test:inline2")
88+
assert.Error(t, err)
89+
assert.Equal(t, "\nError: not overwriting your current secret\n", out)
90+
91+
out, err = ts.run("show some/other Test")
92+
assert.NoError(t, err)
93+
assert.Equal(t, "inline", out)
94+
95+
out, err = ts.run("--yes insert some/other test:inline2")
96+
assert.NoError(t, err)
97+
assert.Equal(t, "", out)
98+
99+
out, err = ts.run("show some/other Test")
100+
assert.NoError(t, err)
101+
assert.Equal(t, "inline2", out)
45102
})
46103
}

0 commit comments

Comments
 (0)