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

azurerm_key_vault_certificate - allow new versions of key vault certs to be imported #18848

Merged
merged 2 commits into from
Oct 24, 2022
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
34 changes: 26 additions & 8 deletions internal/services/keyvault/key_vault_certificate_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func resourceKeyVaultCertificate() *pluginsdk.Resource {
"certificate": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
AtLeastOneOf: []string{
"certificate_policy",
"certificate",
Expand All @@ -76,14 +75,12 @@ func resourceKeyVaultCertificate() *pluginsdk.Resource {
"contents": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"password": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
},
},
Expand Down Expand Up @@ -553,13 +550,34 @@ func resourceKeyVaultCertificateUpdate(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}
patch := keyvault.CertificateUpdateParameters{}
if t, ok := d.GetOk("tags"); ok {
patch.Tags = tags.Expand(t.(map[string]interface{}))

if d.HasChange("certificate") {
if v, ok := d.GetOk("certificate"); ok {
// Import new version of certificate
certificate := expandKeyVaultCertificate(v)
importParameters := keyvault.CertificateImportParameters{
Base64EncodedCertificate: utils.String(certificate.CertificateData),
Password: utils.String(certificate.CertificatePassword),
}
resp, err := client.ImportCertificate(ctx, id.KeyVaultBaseUrl, id.Name, importParameters)
if err != nil {
return err
}
if resp.ID != nil {
d.SetId(*resp.ID)
}
}
}

if _, err = client.UpdateCertificate(ctx, id.KeyVaultBaseUrl, id.Name, id.Version, patch); err != nil {
return err
if d.HasChange("tags") {
patch := keyvault.CertificateUpdateParameters{}
if t, ok := d.GetOk("tags"); ok {
patch.Tags = tags.Expand(t.(map[string]interface{}))
}

if _, err = client.UpdateCertificate(ctx, id.KeyVaultBaseUrl, id.Name, "", patch); err != nil {
return err
}
}
return resourceKeyVaultCertificateRead(d, meta)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,32 @@ func TestAccKeyVaultCertificate_unorderedKeyUsage(t *testing.T) {
})
}

func TestAccKeyVaultCertificate_updatedImportedCertificate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_key_vault_certificate", "test")
r := KeyVaultCertificateResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicImportPFX(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("certificate_data").Exists(),
check.That(data.ResourceName).Key("certificate_data_base64").Exists(),
),
},
data.ImportStep("certificate"),
{
Config: r.basicImportPFX_ECDSA(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("certificate_data").Exists(),
check.That(data.ResourceName).Key("certificate_data_base64").Exists(),
),
},
data.ImportStep("certificate"),
})
}

func (t KeyVaultCertificateResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
keyVaultsClient := clients.KeyVault
client := clients.KeyVault.ManagementClient
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/key_vault_certificate.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ The following arguments are supported:

`certificate` supports the following:

* `contents` - (Required) The base64-encoded certificate contents. Changing this forces a new resource to be created.
* `password` - (Optional) The password associated with the certificate. Changing this forces a new resource to be created.
* `contents` - (Required) The base64-encoded certificate contents.
* `password` - (Optional) The password associated with the certificate.

`certificate_policy` supports the following:

Expand Down