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

Add data field to KV data sources #1577

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion vault/data_source_kv_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func kvSecretDataSource() *schema.Resource {
consts.FieldDataJSON: {
Type: schema.TypeString,
Computed: true,
Description: "JSON-encoded secret data read from Vault.",
Sensitive: true,
},
consts.FieldData: {
Type: schema.TypeMap,
Computed: true,
Description: "Map of strings read from Vault.",
Sensitive: true,
},
Expand Down Expand Up @@ -70,7 +76,8 @@ func kvSecretDataSourceRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf("no secret found at %q", path)
}

jsonData, err := json.Marshal(secret.Data)
data := secret.Data
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
jsonData, err := json.Marshal(data)
if err != nil {
return diag.Errorf("error marshaling JSON for %q: %s", path, err)
}
Expand All @@ -79,6 +86,10 @@ func kvSecretDataSourceRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

if err := d.Set(consts.FieldData, serializeDataMapToString(data)); err != nil {
return diag.FromErr(err)
}

if err := d.Set(consts.FieldLeaseID, secret.LeaseID); err != nil {
return diag.FromErr(err)
}
Expand Down
8 changes: 8 additions & 0 deletions vault/data_source_kv_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestDataSourceKVSecret(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, consts.FieldPath, fmt.Sprintf("%s/%s", mount, name)),
resource.TestCheckResourceAttr(resourceName, consts.FieldLeaseRenewable, "false"),
resource.TestCheckResourceAttr(resourceName, "data.zip", "zap"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to check the length of data.%

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in latest commit

resource.TestCheckResourceAttr(resourceName, "data.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "data.test", "false"),
resource.TestCheckResourceAttr(resourceName, "data.baz", "{\"riff\":\"raff\"}"),
testutil.CheckJSONData(resourceName, consts.FieldDataJSON, expectedSubkeys),
),
},
Expand All @@ -35,6 +39,10 @@ func TestDataSourceKVSecret(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, consts.FieldPath, fmt.Sprintf("%s/%s-updated", mount, name)),
resource.TestCheckResourceAttr(resourceName, consts.FieldLeaseRenewable, "false"),
resource.TestCheckResourceAttr(resourceName, "data.zip", "zap"),
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "data.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "data.test", "false"),
resource.TestCheckResourceAttr(resourceName, "data.baz", "{\"riff\":\"raff\"}"),
testutil.CheckJSONData(resourceName, consts.FieldDataJSON, expectedSubkeys),
),
},
Expand Down
13 changes: 12 additions & 1 deletion vault/data_source_kv_secret_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ func kvSecretV2DataSource() *schema.Resource {
consts.FieldDataJSON: {
Type: schema.TypeString,
Computed: true,
Description: "Map of strings read from Vault",
Description: "JSON-encoded secret data read from Vault.",
Sensitive: true,
},

consts.FieldData: {
Type: schema.TypeMap,
Computed: true,
Description: "Map of strings read from Vault.",
Sensitive: true,
},

Expand Down Expand Up @@ -121,6 +128,10 @@ func kvSecretV2DataSourceRead(_ context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

if err := d.Set(consts.FieldData, serializeDataMapToString(data.(map[string]interface{}))); err != nil {
return diag.FromErr(err)
}

if v, ok := secret.Data["metadata"]; ok {
metadata := v.(map[string]interface{})

Expand Down
4 changes: 4 additions & 0 deletions vault/data_source_kv_secret_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func TestDataSourceKVV2Secret(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, consts.FieldName, name),
resource.TestCheckResourceAttr(resourceName, consts.FieldPath, fmt.Sprintf("%s/data/%s", mount, name)),
resource.TestCheckResourceAttr(resourceName, "destroyed", "false"),
resource.TestCheckResourceAttr(resourceName, "data.zip", "zap"),
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "data.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "data.test", "false"),
resource.TestCheckResourceAttr(resourceName, "data.baz", "{\"riff\":\"raff\"}"),
testutil.CheckJSONData(resourceName, consts.FieldDataJSON, expectedSubkeys),
),
},
Expand Down
10 changes: 10 additions & 0 deletions vault/data_source_kv_subkeys_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func kvSecretSubkeysV2DataSource() *schema.Resource {
// cleanly support nested values
Description: "Subkeys for the KV-V2 secret read from Vault.",
},
consts.FieldData: {
Type: schema.TypeMap,
Computed: true,
Description: "Subkeys stored as a map of strings.",
Sensitive: true,
},
},
}
}
Expand Down Expand Up @@ -103,6 +109,10 @@ func kvSecretSubkeysDataSourceRead(_ context.Context, d *schema.ResourceData, me
if err := d.Set(consts.FieldDataJSON, string(jsonData)); err != nil {
return diag.FromErr(err)
}

if err := d.Set(consts.FieldData, serializeDataMapToString(data.(map[string]interface{}))); err != nil {
return diag.FromErr(err)
}
}

d.SetId(path)
Expand Down
3 changes: 3 additions & 0 deletions vault/data_source_kv_subkeys_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func TestDataSourceKVSubkeys(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, consts.FieldPath, fmt.Sprintf("%s/subkeys/%s", mount, secretPath)),
resource.TestCheckResourceAttrSet(resourceName, consts.FieldDataJSON),
resource.TestCheckResourceAttr(resourceName, "data.zip", "null"),
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "data.foo", "null"),
resource.TestCheckResourceAttr(resourceName, "data.baz", "{\"riff\":null}"),
testutil.CheckJSONData(resourceName, consts.FieldDataJSON, expectedSubkeys),
),
},
Expand Down
5 changes: 4 additions & 1 deletion website/docs/d/kv_secret.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ Use of this resource requires the `read` capability on the given path.

The following attributes are exported:

* `data_json` - A mapping whose keys are the top-level data keys returned from
* `data` - A mapping whose keys are the top-level data keys returned from
Vault and whose values are the corresponding values. This map can only
represent string data, so any non-string values returned from Vault are
serialized as JSON.

* `data_json` - String containing a JSON-encoded object that that is
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
read as the secret data at the given path.

* `lease_id` - The lease identifier assigned by Vault, if any.

* `lease_duration` - The duration of the secret lease, in seconds. Once
Expand Down
5 changes: 4 additions & 1 deletion website/docs/d/kv_secret_v2.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ The following attributes are exported:

* `path` - Full path where the KVV2 secret is written.

* `data_json` - A mapping whose keys are the top-level data keys returned from
* `data` - A mapping whose keys are the top-level data keys returned from
Vault and whose values are the corresponding values. This map can only
represent string data, so any non-string values returned from Vault are
serialized as JSON.

* `data_json` - String containing a JSON-encoded object that that is
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
read as the secret data at the given path.

* `created_time` - Time at which secret was created.

* `custom_metadata` - Custom metadata for the secret.
Expand Down
4 changes: 3 additions & 1 deletion website/docs/d/kv_subkeys_v2.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ The following attributes are exported:

* `path` - Full path where the KV-V2 secrets are listed.

* `data_json` - Subkeys for the KV-V2 secret read from Vault..
* `data_json` - Subkeys for the KV-V2 secret read from Vault.

* `data` - Subkeys for the KV-V2 secret stored as a serialized map of strings.