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

Fix bug where vault_kv_secret_v2 resource's cas field had no effect #1729

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ const (
FieldUpgradeInfoJSON = "upgrade_info_json"
FieldMaxVersions = "max_versions"
FieldCASRequired = "cas_required"
FieldCAS = "cas"
FieldOptions = "options"
FieldDeleteVersionAfter = "delete_version_after"
FieldCustomMetadata = "custom_metadata"
FieldCustomMetadataJSON = "custom_metadata_json"
Expand Down
11 changes: 6 additions & 5 deletions vault/resource_kv_secret_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,14 @@ func kvSecretV2Write(ctx context.Context, d *schema.ResourceData, meta interface
return diag.Errorf("data_json %#v syntax error: %s", d.Get(consts.FieldDataJSON), err)
}

data := map[string]interface{}{
"data": secretData,
options := d.Get(consts.FieldOptions).(map[string]interface{})
if _, isSet := options["cas"]; !isSet {
options["cas"] = d.Get(consts.FieldCAS)
}

kvFields := []string{"cas", "options"}
for _, k := range kvFields {
data[k] = d.Get(k)
data := map[string]interface{}{
"data": secretData,
"options": options,
}

if _, err := client.Logical().Write(path, data); err != nil {
Expand Down
18 changes: 16 additions & 2 deletions vault/resource_kv_secret_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestAccKVSecretV2(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "data.zip", "zap"),
resource.TestCheckResourceAttr(resourceName, "data.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "data.flag", "false"),
resource.TestCheckResourceAttr(resourceName, "metadata.version", "1"),
),
},
{
Expand All @@ -48,6 +49,7 @@ func TestAccKVSecretV2(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "custom_metadata.0.data.pizza", "please"),
resource.TestCheckResourceAttr(resourceName, "custom_metadata.0.delete_version_after", "0"),
resource.TestCheckResourceAttr(resourceName, "custom_metadata.0.max_versions", "5"),
resource.TestCheckResourceAttr(resourceName, "metadata.version", "2"),
),
},
{
Expand All @@ -71,10 +73,16 @@ func testKVSecretV2Config_initial(mount, name string) string {
`, kvV2MountConfig(mount))

ret += fmt.Sprintf(`
resource "vault_kv_secret_backend_v2" "test" {
mount = vault_mount.kvv2.path
cas_required = true
}

resource "vault_kv_secret_v2" "test" {
mount = vault_mount.kvv2.path
mount = vault_kv_secret_backend_v2.test.mount
name = "%s"
delete_all_versions = true
cas = 0
data_json = jsonencode(
{
zip = "zap",
Expand All @@ -94,10 +102,16 @@ func testKVSecretV2Config_updated(mount, name string) string {
`, kvV2MountConfig(mount))

ret += fmt.Sprintf(`
resource "vault_kv_secret_backend_v2" "test" {
mount = vault_mount.kvv2.path
cas_required = true
}

resource "vault_kv_secret_v2" "test" {
mount = vault_mount.kvv2.path
mount = vault_kv_secret_backend_v2.test.mount
name = "%s"
delete_all_versions = true
cas = 1
data_json = jsonencode(
{
zip = "zoop",
Expand Down
Loading