【问题标题】:Azure AD - create a new Service Principal programmaticallyAzure AD - 以编程方式创建新的服务主体
【发布时间】:2019-10-17 21:15:42
【问题描述】:

我正在编写一个 python 脚本来预配和配置 Azure 服务。我想在我的脚本中提供一个新的服务主体,但存在权限问题。如果我在终端中运行此命令(在azure login 之后),它将创建服务主体:

az ad sp create-for-rbac --name Testapp

我想实现相同的目标,但在我的 python 脚本中,我使用现有的服务主体进行资源配置。我正在拨打的电话如下:

call("az login --service-principal -u '%s' -p '%s' --tenant '%s'" % (args.client_id, args.client_sec, args.tenant_id), shell=True)
call("az ad sp create-for-rbac --name TestServicePrincipal", shell=True)

参数是现有服务主体的凭据。该服务主体已经是该订阅的OWNER,您可以在此处看到:

在运行我的 python 脚本时,我可以很好地登录(也可以使用这些凭据通过脚本提供一堆其他资源),但是在创建 SP 帐户时会出现权限错误,如您在此处看到的:

synergies git:(master) ✗ python test.py -c 'testcustomer' -l 'eastus' -sid '1234' -cs '1234' -cid '1234' -tid '1234'
[
  {
    "cloudName": "AzureCloud",
    "id": "1234",
    "isDefault": true,
    "name": "Free Trial",
    "state": "Enabled",
    "tenantId": "1234",
    "user": {
      "name": "1234",
      "type": "servicePrincipal"
    }
  }
]
Changing "TestServicePrincipal" to a valid URI of "http://TestServicePrincipal", which is the required format used for service principal names
Insufficient privileges to complete the operation.

我们将不胜感激任何建议!

【问题讨论】:

  • 如果您对此有任何其他疑虑,请告诉我。
  • 非常感谢@TonyJu 的详细回答,非常感谢您花时间提供帮助!你是对的,我没有授予Application.ReadWrite.All 权限的 SP,添加它确实很有意义,我认为事情会奏效,但是,我仍然遇到完全相同的错误:Insufficient privileges to complete the operation.。我什至创建了一个新的 SP 应用程序并设置了所需的权限,但仍然出现相同的权限问题错误。然后我尝试为 Graph 添加更多权限,但没有区别。不确定还有什么?

标签: azure azure-active-directory service-principal


【解决方案1】:

我已经解决了。您需要将 Global administrator 角色分配给您的 sp。 注意:这需要几分钟才能生效

【讨论】:

  • 谢谢!它对你有用没有任何错误?我按照你的建议做了 - 创建了一个新的 SP 应用程序并分配了应用程序管理员角色。现在,当我运行脚本时,它仍然会抛出相同的权限错误,但具有更多上下文,但同时它会创建 SP 应用程序 - 尝试了几次并且总是相同的问题 - 错误但创建了 SP。 Creating service principal failed for appid 'http://petristesting2'. Trace followed: {'Cache-Control': 'no-cache', 'Pragma':....Insufficient privileges to complete the operation -> 跟踪继续,只是评论空间不足。
  • @PetrHecko 分配 sp 全局管理员角色将解决此问题。
  • 非常感谢,全球管理员修复了最后一个问题! @托尼朱
猜你喜欢
  • 2023-02-22
  • 2018-06-06
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
相关资源
最近更新 更多