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_eventhub_namespace_customer_managed_key - validating that the User Assigned Identity used for accessing the Key Vault is assigned to the EventHub Namespace #25809

Merged
merged 10 commits into from
May 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -135,6 +136,10 @@ func resourceEventHubNamespaceCustomerManagedKeyCreateUpdate(d *pluginsdk.Resour

userAssignedIdentity := d.Get("user_assigned_identity_id").(string)
if userAssignedIdentity != "" && keyVaultProps != nil {
userAssignedIdentityId, err := commonids.ParseUserAssignedIdentityID(userAssignedIdentity)
if err != nil {
return err
}

// this provides a more helpful error message than the API response
if namespace.Identity == nil {
Expand All @@ -143,7 +148,11 @@ func resourceEventHubNamespaceCustomerManagedKeyCreateUpdate(d *pluginsdk.Resour

isIdentityAssignedToParent := false
for item := range namespace.Identity.IdentityIds {
if item == userAssignedIdentity {
parentEhnUaiId, err := commonids.ParseUserAssignedIdentityIDInsensitively(item)
if err != nil {
return fmt.Errorf("parsing %q as a User Assigned Identity ID: %+v", item, err)
}
if resourceids.Match(parentEhnUaiId, userAssignedIdentityId) {
isIdentityAssignedToParent = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ resource "azurerm_eventhub_namespace" "test" {
data "azurerm_client_config" "current" {}

resource "azurerm_key_vault" "test" {
name = "acctestkv%[2]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
name = "acctestkv%[3]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
purge_protection_enabled = true
}

resource "azurerm_key_vault_access_policy" "test" {
Expand Down Expand Up @@ -263,7 +264,7 @@ resource "azurerm_key_vault_access_policy" "test2" {
}

resource "azurerm_key_vault_key" "test" {
name = "acctestkvkey%[2]s"
name = "acctestkvkey%[3]s"
key_vault_id = azurerm_key_vault.test.id
key_type = "RSA"
key_size = 2048
Expand Down Expand Up @@ -310,11 +311,12 @@ resource "azurerm_eventhub_namespace" "test" {
data "azurerm_client_config" "current" {}

resource "azurerm_key_vault" "test" {
name = "acctestkv%[3]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
name = "acctestkv%[3]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
purge_protection_enabled = true
}

resource "azurerm_key_vault_access_policy" "test" {
Expand Down