【问题标题】:Assign ManagedID to KeyVault Access Policy将 ManagedID 分配给 KeyVault 访问策略
【发布时间】:2021-10-14 21:45:12
【问题描述】:

我有一个二头肌模板,可创建 2 个 webApp 和一个 KeyVault。每个 WebApp 都是使用 managedID 创建的,我需要将其添加到 Keyvault 中,以便 webapp 可以提取机密。

但是在创建 2 个 web 应用程序时,我不知道如何将两个 ManagedID 分配给 KeyVault。

二头肌模板正在使用模块

  name: 'ciKeyVault'
  params: {
    keyVaultName: keyVaultName
    aclBypass: keyVaultSettings.aclBypass
    aclDefaultAction: keyVaultSettings.aclDefaultAction
    enabledForDeployment: keyVaultSettings.enabledForDeployment
    enabledForDiskEncryption: keyVaultSettings.enabledForDiskEncryption
    enabledForTemplateDeployment: keyVaultSettings.enabledForTemplateDeployment
    keyPermissions: keyVaultSettings.keyPermissions
    keyVaultSettings: keyVaultSettings
    secretsPermissions: keyVaultSettings.secretsPermissions
    skuFamily: keyVaultSettings.skuFamily
    skuName: keyVaultSettings.skuName
    tenantId: subscription().tenantId
    objectId: 'b71e61c4-7cff-41d0-8370-a7d9c01dde84'
  }
}

并且需要从 AppService 部署中检索 objectId。使用这个模块:

module AppService '../../../Modules/Azure.App.Service.template.bicep' = [for i in range(0, length(webAppSettings.webApps)): { 
  name: webAppSettings.webApps[i].Name
  dependsOn: [
    frontEndAppServicePlan
  ]
  params: {
    webAppName: webAppSettings.webApps[i].appServiceType == 'functionApp' ? toLower('fnc-${webAppSettings.webApps[i].name}-${resourceGroupNameSuffix}') : toLower('web-${webAppSettings.webApps[i].name}-${resourceGroupNameSuffix}')
    hostingPlan:  frontEndAppServicePlan.outputs.hostingPlanId
    virtualNetworkResourceGroup: virtualNetworkResourceGroup
    environmentName:environmentName
    webAppSettings:webAppSettings
    appServiceType: webAppSettings.webApps[i].appServiceType
    LinuxFX:webAppSettings.webApps[i].LinuxFX
    appSettings:webAppSettings.webapps[i].appSettings
  }
}]

当它是单个 appService 时很好,因为我可以使用 output usid string = AppServices.identity.principalId 引用 ID

但是当我有 2 个 appServices 时,我不知道如何传入这两个 ID

有什么想法吗?

干杯

【问题讨论】:

  • 所以首先你使用系统分配的身份创建 webapp,然后你想在 kv 中为托管身份创建访问策略?
  • 嗨,Thomas,是的,这就是我想要做的。有什么想法吗?
  • 我不知道二头肌,但让我试着理解一下:第一个sn-p底部的objectid是指一个webapp?让我感到困惑的是,此设置没有上下文:似乎会有一个数组和/或多个位置来为多个 Web 应用程序添加访问策略。实际执行这些参数是什么?

标签: azure azure-resource-manager azure-keyvault azure-bicep


【解决方案1】:

假设你有一个模块Azure.App.Service.template.bicep,看起来像这样:

param webAppName string
...

// Create the web app
resource webApp 'Microsoft.Web/sites@2020-09-01' = {
  name: webAppName
  location: resourceGroup().location
  identity: {
    type: 'SystemAssigned'
  }
  ...
}

output usid string = webApp.identity.principalId 

在父模板中,您可以创建一个模块数组来创建您的 web 应用程序(与您执行此操作的方式相同),然后创建一个访问策略资源以向所有 web 应用程序授予对 key Vault 的访问权限。

...
// Create the app services
module AppServices '../../../Modules/Azure.App.Service.template.bicep' = [for webApp in webAppSettings.webApps: {
  name: webApp.Name
  params: {
    webAppName: webApp.Name
    ...
  }
}]

// Granting the app services access ot key vault
resource appServicesKeyVaultAccessPolicies 'Microsoft.KeyVault/vaults/accessPolicies@2019-09-01' = {
  name: '${keyVaultName}/add'
  properties: {
    accessPolicies: [for i in range(0, length(webAppSettings.webApps)): {
      tenantId: subscription().tenantId
      objectId: AppServices[i].outputs.usid
      permissions: {
        secrets: keyVaultSettings.secretsPermissions
        keys: keyVaultSettings.keyPermissions
      }
    }]
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2021-11-11
    • 1970-01-01
    • 2021-08-15
    • 2021-12-23
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多