APIM 通过 SystemAssigned Managed Identity 与 KeyVault 连接失败

问题描述 投票:0回答:1

我正在使用此处描述的以下设置。 https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-properties?WT.mc_id=Portal-fx&tabs=azure-cli

我使用系统管理的身份设置 APIM,然后转到 KV,向 APIM 身份授予 Key Vault Secrets 和证书管理,但是当我尝试通过 terraform 将自定义域和证书创建到 APIM 时,出现以下错误:

错误:创建/更新自定义域:xxx 执行 CreateOrUpdate:xx 意外状态 400,并出现错误:InvalidOperation:无法使用 Api 管理服务的托管服务标识 (http://aka.ms/apimmsi) 访问 KeyVault Secret xxx。检查类型为 SystemAssigned、ClientId: xxx 和 ObjectId: xxx 的托管身份是否对 KeyVault 访问策略中的机密具有 GET 权限。

terraform azure-keyvault apim
1个回答
0
投票

我厌倦了使用 terraform 通过 SystemAssigned Managed Identity 配置与 KeyVault 的 APIM 连接,并且我能够成功配置要求。

在这里,您尝试使用系统托管标识设置 Azure API 管理 (APIM),然后使用此标识访问 Azure Key Vault 中的机密,以便通过 Terraform 配置自定义域和证书。您遇到的错误表明托管服务身份 (MSI) 访问 Key Vault 时存在权限问题。

您收到的错误消息表明与您的 Azure API 管理服务关联的托管服务标识不具备访问 Azure Key Vault 中的机密所需的权限。

  • API 管理的系统托管标识可能对 Key Vault 中的机密不具有正确的权限(特别是 GET 权限)。
  • 有时,设置权限后,在完全传播和生效之前可能会出现延迟。

确保您的 SP 或用户权限符合您的配置要求。由于您需要配置 keyvault 并访问其机密,我们需要 keyvault 管理员、机密官员和贡献者角色来代表您的身份。

我尝试了具有必要权限的配置演示版本,我可以成功地配置要求。

我的地形配置:

provider "azurerm" {
    features {}
}

variable "resource_group_name" {
    description = "Name of the resource group"
    type        = string
}

variable "location" {
    description = "Location for all resources"
    type        = string
}
  
variable "tenant_id" {
    description = "Tenant ID for the Azure resources"
    type        = string
}
  
resource "azurerm_resource_group" "example" {
    name     = var.resource_group_name
    location = var.location
}
  
resource "azurerm_api_management" "example" {
    name                = "examplevk-apim"
    location            = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
    publisher_name      = "example"
    publisher_email     = "[email protected]"
    sku_name            = "Developer_1"
  
    identity {
      type = "SystemAssigned"
    }
}
  
resource "azurerm_key_vault" "example" {
    name                = "examplekvvksb"
    location            = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
    tenant_id           = var.tenant_id
    sku_name            = "standard"
  
    access_policy {
      tenant_id = var.tenant_id
      object_id = azurerm_api_management.example.identity[0].principal_id
  
      secret_permissions = ["Get", "List", "Set", "Delete", "Recover", "Backup", "Restore", "Purge"]
    }
}

输出:

enter image description here

enter image description here

enter image description here

现在将 SSL 证书推送到密钥保管库并继续进行后续配置。

# Assuming the certificate is already in the Key Vault
data "azurerm_key_vault_secret" "example_certificate" {
  name         = "myCertificateName"
  key_vault_id = azurerm_key_vault.example.id
}

# Configure Custom Domain in APIM
resource "azurerm_api_management_custom_domain" "example" {
  api_management_id = azurerm_api_management.example.id

  portal {
    host_name            = "portal.my-custom-domain.com"
    key_vault_id         = azurerm_key_vault.example.id
    key_vault_secret_id  = data.azurerm_key_vault_secret.example_certificate.id
  }

  # Configure additional domains (developer portal, gateway, etc.) as needed
}

此配置将允许您访问认证,因为您的 SP 拥有上述所有相关权限。

© www.soinside.com 2019 - 2024. All rights reserved.