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

data.azurerm_key_vault_secret - support specifying the keyvault secret version #21336

Merged
merged 2 commits into from
Apr 13, 2023
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
6 changes: 3 additions & 3 deletions internal/services/keyvault/key_vault_secret_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func dataSourceKeyVaultSecret() *pluginsdk.Resource {

"version": {
Type: pluginsdk.TypeString,
Computed: true,
Optional: true,
},

"versionless_id": {
Expand Down Expand Up @@ -78,6 +78,7 @@ func dataSourceKeyVaultSecretRead(d *pluginsdk.ResourceData, meta interface{}) e
defer cancel()

name := d.Get("name").(string)
version := d.Get("version").(string)
keyVaultId, err := parse.VaultID(d.Get("key_vault_id").(string))
if err != nil {
return err
Expand All @@ -88,8 +89,7 @@ func dataSourceKeyVaultSecretRead(d *pluginsdk.ResourceData, meta interface{}) e
return fmt.Errorf("looking up Secret %q vault url from id %q: %+v", name, *keyVaultId, err)
}

// we always want to get the latest version
resp, err := client.GetSecret(ctx, *keyVaultBaseUri, name, "")
resp, err := client.GetSecret(ctx, *keyVaultBaseUri, name, version)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("KeyVault Secret %q (KeyVault URI %q) does not exist", name, *keyVaultBaseUri)
Expand Down
52 changes: 52 additions & 0 deletions internal/services/keyvault/key_vault_secret_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,40 @@ func TestAccDataSourceKeyVaultSecret_complete(t *testing.T) {
})
}

func TestAccDataSourceKeyVaultSecret_specifyVersion(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_key_vault_secret", "test")
r := KeyVaultSecretDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("value").HasValue("rick-and-morty"),
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("resource_id").MatchesRegex(regexp.MustCompile(`^/subscriptions/[\w-]+/resourceGroups/[\w-]+/providers/Microsoft.KeyVault/vaults/[\w-]+/secrets/[\w-]+/versions/[\w-]+$`)),
check.That(data.ResourceName).Key("resource_versionless_id").MatchesRegex(regexp.MustCompile(`^/subscriptions/[\w-]+/resourceGroups/[\w-]+/providers/Microsoft.KeyVault/vaults/[\w-]+/secrets/[\w-]+$`)),
),
},
{
Config: r.specifyOldVersion(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("value").HasValue("rick-and-morty"),
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("resource_id").MatchesRegex(regexp.MustCompile(`^/subscriptions/[\w-]+/resourceGroups/[\w-]+/providers/Microsoft.KeyVault/vaults/[\w-]+/secrets/[\w-]+/versions/[\w-]+$`)),
check.That(data.ResourceName).Key("resource_versionless_id").MatchesRegex(regexp.MustCompile(`^/subscriptions/[\w-]+/resourceGroups/[\w-]+/providers/Microsoft.KeyVault/vaults/[\w-]+/secrets/[\w-]+$`)),
),
},
})
}

func (KeyVaultSecretDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_key_vault_secret" "test" {
name = azurerm_key_vault_secret.test.name
key_vault_id = azurerm_key_vault.test.id
version = azurerm_key_vault_secret.test.version
}
`, KeyVaultSecretResource{}.basic(data))
}
Expand All @@ -66,3 +93,28 @@ data "azurerm_key_vault_secret" "test" {
}
`, KeyVaultSecretResource{}.complete(data))
}

func (KeyVaultSecretDataSource) specifyOldVersion(data acceptance.TestData) string {
return fmt.Sprintf(`

data "azurerm_key_vault_secret" "test" {
name = "${local.secret_name}"
key_vault_id = azurerm_key_vault.test.id
}

locals {
old_version = "${data.azurerm_key_vault_secret.test.version}"
secret_name = "secret-%s"
}

%s

data "azurerm_key_vault_secret" "test2" {
name = azurerm_key_vault_secret.test.name
key_vault_id = azurerm_key_vault.test.id
version = "${local.old_version}"
depends_on = [data.azurerm_key_vault_secret.test]
}

`, data.RandomString, KeyVaultSecretResource{}.basicUpdated(data))
}
4 changes: 2 additions & 2 deletions website/docs/d/key_vault_secret.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The following arguments are supported:

* `name` - (Required) Specifies the name of the Key Vault Secret.

* `version` - (Optional) Specifies the version of the Key Vault Secret. Defaults to the current version of the Key Vault Secret.

**NOTE:** The vault must be in the same subscription as the provider. If the vault is in another subscription, you must create an aliased provider for that subscription.

## Attributes Reference
Expand All @@ -53,8 +55,6 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `value` - The value of the Key Vault Secret.

* `version` - The current version of the Key Vault Secret.

* `versionless_id` - The Versionless ID of the Key Vault Secret. This can be used to always get latest secret value, and enable fetching automatically rotating secrets.

## Timeouts
Expand Down