Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the KV metadata put CLI, allow delete-version-after to be reset to 0 #8635

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions command/kv_metadata_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *KVMetadataPutCommand) Flags() *FlagSets {
f.DurationVar(&DurationVar{
Name: "delete-version-after",
Target: &c.flagDeleteVersionAfter,
Default: 0,
Default: -1,
EnvVar: "",
Completion: complete.PredictAnything,
Usage: `Specifies the length of time before a version is deleted.
Expand Down Expand Up @@ -141,7 +141,7 @@ func (c *KVMetadataPutCommand) Run(args []string) int {
"cas_required": c.flagCASRequired,
}

if c.flagDeleteVersionAfter > 0 {
if c.flagDeleteVersionAfter >= 0 {
data["delete_version_after"] = c.flagDeleteVersionAfter.String()
}

Expand Down
76 changes: 76 additions & 0 deletions command/kv_metadata_put_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package command

import (
"strings"
"testing"

"github.com/hashicorp/vault/api"
"github.com/mitchellh/cli"
)

func testKVMetadataPutCommand(tb testing.TB) (*cli.MockUi, *KVMetadataPutCommand) {
tb.Helper()

ui := cli.NewMockUi()
return ui, &KVMetadataPutCommand{
BaseCommand: &BaseCommand{
UI: ui,
},
}
}

func TestKvMetadataPutCommandDeleteVersionAfter(t *testing.T) {
client, closer := testVaultServer(t)
defer closer()

if err := client.Sys().Mount("kv/", &api.MountInput{
Type: "kv-v2",
}); err != nil {
t.Fatal(err)
}

ui, cmd := testKVMetadataPutCommand(t)
cmd.client = client

// Set a limit of 1s first.
code := cmd.Run([]string{"-delete-version-after=1s", "kv/secret/my-secret"})
if code != 0 {
t.Errorf("expected %d but received %d", 0, code)
}

combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, "Success! Data written to: kv/metadata/secret/my-secret\n") {
t.Errorf("expected %q but received %q", "Success! Data written to: kv/metadata/secret/my-secret\n", combined)
}

secret, err := client.Logical().Read("kv/metadata/secret/my-secret")
if err != nil {
t.Fatal(err)
}
if secret.Data["delete_version_after"] != "1s" {
t.Fatalf("expected 1s but received %q", secret.Data["delete_version_after"])
}

// Now verify that we can return it to 0s.
ui, cmd = testKVMetadataPutCommand(t)
cmd.client = client

// Set a limit of 1s first.
code = cmd.Run([]string{"-delete-version-after=0", "kv/secret/my-secret"})
if code != 0 {
t.Errorf("expected %d but received %d", 0, code)
}

combined = ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, "Success! Data written to: kv/metadata/secret/my-secret\n") {
t.Errorf("expected %q but received %q", "Success! Data written to: kv/metadata/secret/my-secret\n", combined)
}

secret, err = client.Logical().Read("kv/metadata/secret/my-secret")
if err != nil {
t.Fatal(err)
}
if secret.Data["delete_version_after"] != "0s" {
t.Fatalf("expected 0s but received %q", secret.Data["delete_version_after"])
}
}