我尝试使用 api 将订阅的贡献者角色分配给 serviceprincipal。
资料如下:
订阅 ID:b59c6b1b-xxxxxxxxxx
服务主体ID:73eb9e1e-xxxxxxxx
贡献者 ID:b24988ac-6180-42a0-ab88-20f7382dd24c(Azure 内置贡献者角色 ID,我得到它使用 'az 角色定义列表 --query "[].{name:name, roleType:roleType, roleName:roleName} " --output tsv' by az cli.是吗?)
我是这样使用api
POST https://graph.microsoft.com/v1.0/servicePrincipals/b59c6b1b-xxxxxxxxxx/appRoleAssignedTo
Content-Type: application/json
Content-Length: 110
{
"principalId": "73eb9e1e-xxxxxxxx",
"resourceId": "b59c6b1b-xxxxxxxxxx",
"appRoleId": "b24988ac-6180-42a0-ab88-20f7382dd24c"
}
但是我得到了这样的错误响应
{
"error": {
"code": "Request_ResourceNotFound",
"innerError": {
"client-request-id": "4fed54c4-xxxxxxxxx",
"date": "2021-01-11T12:00:08",
"request-id": "4fed54c4-xxxxxxxxx"
},
"message": "Resource \u0027b59c6b1b-xxxxxxxxxxx\u0027 does not exist or one of its queried reference-property objects are not present."
}
}
这意味着没有通过ID找到订阅,在这个例子中,我使用订阅ID,我想可能是我应该像其他示例资源一样使用订阅ObjectID。但我没有找到通过订阅获取订阅ObjectID的方法使用门户或api。或者我可能将错误信息修复到资源 ID 中。所以我想知道我需要知道哪些信息才能将订阅的贡献者角色分配给 serviceprincipal
答案 0 :(得分:0)
POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignedTo
Content-Type: application/json
Content-Length: 110
{
"principalId": "principalId-value",
"resourceId": "resourceId-value",
"appRoleId": "appRoleId-value"
}
这里的 {id} 和“resourceId”是服务主体的对象 ID。
您可以通过以下两种方式之一获得此信息:
选项 1:
来自门户:
Azure Active Directory >> 应用注册 >> 从下拉菜单中选择所有应用 >> 找到您的应用并点击它。
选项 2:
来自图表
使用 GET /servicePrincipals
https://graph.microsoft.com/beta/serviceprincipals?$filter=startswith(displayName, 'Application-Name')
您必须替换上面的应用程序名称。 上述请求的输出将在 id 键下具有 guid 值。
答案 1 :(得分:0)
你用错了API,这个MS Graph API是把AAD App role分配给服务主体,你需要的是把RBAC role分配给订阅中的服务主体,你需要要使用此 API - Role Assignments - Create
,您可以点击此页面中的 Try it
按钮,登录您的帐户并直接试用。
确保您用于获取令牌的客户端/用户具有创建角色分配的权限 Microsoft.Authorization/roleAssignments/write
,例如User Access Administrator
或 Owner
。
示例:
PUT https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId}?api-version=2015-07-01
{
"properties": {
"roleDefinitionId": "/subscriptions/xxxxx/providers/Microsoft.Authorization/roleDefinitions/de139f84-1756-47ae-9be6-808fbbe84772",
"principalId": "xxxxxxx"
}
}
有关详细信息,请按照此 doc 中的每个步骤进行操作。
此外,如果您可以接受使用 Azure CLI,则可以使用此命令 az role assignment create
。