授予托管标识权限以通过 ARM 模板部署脚本在 Azure AD 中创建应用程序注册

Give Managed Identity permission to create app registration in Azure AD via ARM Template Deployment Script

我们要实现的目标:

我们正在使用 Azure ARM 模板来部署新的应用程序。当我们部署这些新应用程序时,我们需要将它们注册到我们的 Azure AD 以进行身份​​验证。我们希望在我们的模板中包含此应用程序注册以及应用程序资源的部署。

看起来 Azure 部署脚本是在我们的 ARM 模板中向 Azure AD 注册新应用程序的方式。在我们的部署脚本中,我尝试 运行 的“scriptContent”只是 az ad app create --display-name ${appName}

问题

权限。我们得到 DeploymentScriptError: Insufficient privileges to complete the operation。我继续创建一个托管身份并在脚本的开头添加 az login --identity -u ${managedIdentityId} 但同样的错误仍然存​​在。托管身份似乎没有创建应用程序注册的权限,我不确定如何授予它此权限

我发现 this article 它提供了一个 PowerShell 脚本,用于向托管身份授予必要的权限,但是,作者没有解释“GraphAppId”是什么或它来自哪里。

如有任何帮助,我们将不胜感激

我们对 ARM 模板还很陌生,但这是我们目前拥有的模板:

main.bicep


targetScope = 'subscription'

param location string = 'eastus'


resource myResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: 'rg-test1'
  location: location
}

resource managedId 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' existing = {
  name: 'mi-deployscripttest'
  scope: resourceGroup('DefaultResourceGroup-EUS')
}

module deploymentScript 'modules/deploymentScript.bicep' = {
  scope: myResourceGroup
  name: 'deploymentScript'
  params: {
    appName: 'testApp1'
    location: location
    managedIdentityId: managedId.id
    managedIdentityPrincipalId: managedId.properties.principalId
  }
}

deploymentScript.bicep

param location string 
param appName string
param managedIdentityId string
param managedIdentityPrincipalId string

var scriptContent = format('''
  az login --identity -u {0}
  az ad app create --display-name {1}
''', managedIdentityId, appName)


resource deploymentScriptRoleDefinition 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = {
  name: guid('basicDeploymentScriptDefinition')
  properties: {
    roleName: 'deployment-script-minimum-privilege-for-deployment-principal'
    description: 'Configure least privilege for the deployment principal in deployment script'
    type: 'customRole'
    permissions: [
      {
        actions: [
          'Microsoft.Storage/storageAccounts/*'
          'Microsoft.ContainerInstance/containerGroups/*'
          'Microsoft.Resources/deployments/*'
          'Microsoft.Resources/deploymentScripts/*'
          'Microsoft.Storage/register/action'
        ]
      }
    ]
    assignableScopes: [
      resourceGroup().id
    ]
  }
}

resource deploymentScriptRoleAssignment 'Microsoft.Authorization/roleAssignments@2015-07-01' = {
  name: guid('basicDeploymentScriptAssignment')
  properties: {
    principalId: managedIdentityPrincipalId
    roleDefinitionId: deploymentScriptRoleDefinition.id
  }
}

resource deploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: 'deploymentScriptTest1'
  location: location
  kind: 'AzureCLI'
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${managedIdentityId}': {}
    }
  }
  properties: {
    azCliVersion: '2.9.1'
    retentionInterval: 'P1D'
    scriptContent: scriptContent
    cleanupPreference: 'Always'
  }
  dependsOn: [ 
    deploymentScriptRoleAssignment 
  ] 
}

要找到 ClientID/AppID,您需要检查发送到 https://login.microsoftonline.com 的请求以进行身份​​验证。 ClientID/AppID 作为请求 url 中的参数发送,如下面 示例请求 中突出显示的那样:

https://login.microsoftonline.com/xxxxxx.onmicrosoft.com/oauth2/v2.0/authorize?**client_id=d736a5a0-xxxx-xxxx-xxxx-d192b45e4aa7**&response_type=code&redirect_uri=https://jwt.ms&state=1234&response_mode=query&scope=openid

要在令牌中添加所需的权限,您需要先复制您在获取访问令牌的请求中使用的 客户端 ID(又名应用程序 ID),然后导航至:

Azure 门户 > Azure Active Directory > 应用程序注册 > 所有应用程序 > 使用之前复制的 ClientID/AppID 搜索

在该应用程序中导航至:

Api 权限 > 添加权限 > Microsoft Graph > 委派权限 > 展开用户 > Select 所需权限,如下所示。添加权限后,单击授予管理员同意 your_tenant 按钮。

可以参考Insufficient privileges to complete the operation" while using Graph API, Calling your APIs with Azure AD Managed Service Identity using application permissions and