Azure 服务主体调用 New-AzSubscriptionAlias PowerShell 函数需要哪些权限?

What permissions does an Azure service principal need to call the New-AzSubscriptionAlias PowerShell function?

我正在尝试通过托管代理 运行 上的 Azure DevOps 管道作为服务主体创建订阅,但收到错误消息:

New-AzSubscriptionAlias: /home/vsts/work/1/s/Azure.Automation/Subscriptions/Create-AzureSubscription.ps1:190
Line |
 190 |  $subscription = New-AzSubscriptionAlias `
     |                  ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Operation returned an invalid status code 'Unauthorized'

如何授予服务主体所需的访问权限?

如上所写here in Programmatically create Azure subscriptions with the latest APIs

You must have an Owner role on an Enrollment Account to create a subscription. There are two ways to get the role:

  • The Enterprise Administrator of your enrollment can make you an Account Owner (sign in required) which makes you an Owner of the Enrollment Account.
  • An existing Owner of the Enrollment Account can grant you access. Similarly, to use a service principal to create an EA subscription, you must grant that service principal the ability to create subscriptions.

如果您需要使用服务主体来创建订阅,则必须向服务主体授予 Azure RBAC owner 角色。

您可以使用 New-AzRoleAssignment 命令将所有者角色分配给您的服务主体。

要指定 用户,请使用 SignInName 或 Azure AD ObjectId 参数。 要指定 安全组 ,请使用 Azure AD ObjectId 参数。要指定 Azure AD 应用程序,请使用 ApplicationId 或 ObjectId 参数。必须使用 RoleDefinitionName 参数指定要分配的角色。

您需要使用 AzureRmRoleAssignment cmdlet 为该服务主体分配一个 所有者 角色。

我们追踪到任务调用的底层 New-AzureRmRoleAssignment cmdlet 所需的两个缺失权限 -

  • Azure DevOps 用于连接到您的服务主体 (SPN) Azure 订阅需要所有者角色
  • 相同的 SPN 还需要读取目录数据权限 Azure AD

还有一个第 3 方 Azure Role Based Access Control 任务可用于您的 azure devops 管道。

更多一步一步的教程,请参考这篇博客--Service Principal considerations when using Azure DevOps to manage RBAC on Azure Resource Groups