你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 Azure CLI 和 REST API 配置 Azure RBAC 角色
本文介绍如何使用 Azure 命令行接口 (CLI) 和 REST API 向客户端应用程序和用户授予访问 Azure Health Data Services 的权限。 此步骤称为角色分配或 Azure 基于角色的访问控制 (RBAC)。 有关详细信息,请参阅“配置 Azure RBAC 角色”。
从 Azure Health Data Services 示例查看和下载 CLI 脚本和 REST API 脚本。
备注
要执行角色分配操作,必须授予用户(或客户端应用程序)RBAC 权限。 请联系 Azure 订阅管理员以获取帮助。
可以使用角色名称或 GUID ID 列出应用程序角色。 在包含空格时,在双引号中包含角色名称。 有关详细信息,请参阅列出 Azure 角色定义。
az role definition list --name "FHIR Data Contributor"
az role definition list --name 5a1fc7df-4bf1-4951-a576-89034ee01acd
az role definition list --name "DICOM Data Owner"
az role definition list --name 58a3b984-7adf-4c20-983a-32417c86fbc8
Azure Health Data Services 的角色分配需要以下值:
- 应用程序角色名称或 GUID ID。
- 用户或客户端应用程序的服务主体 ID。
- 角色分配的范围,即 Azure Health Data Services 服务实例。 它包括订阅、资源组、工作区名称和 FHIR 或 DICOM 服务名称。 可以对范围使用绝对 URL 或相对 URL。 请注意,不要在相对 URL 的开头添加“/”。
#Azure Health Data Services role assignment
fhirrole="FHIR Data Contributor"
dicomrole="DICOM Data Owner"
clientid=xxx
subscriptionid=xxx
resourcegroupname=xxx
workspacename=xxx
fhirservicename=xxx
dicomservicename=xxx
fhirrolescope="subscriptions/$subscriptionid/resourceGroups/$resourcegroupname/providers/Microsoft.HealthcareApis/workspaces/$workspacename/fhirservices/$fhirservicename"
dicomrolescope="subscriptions/$subscriptionid/resourceGroups/$resourcegroupname/providers/Microsoft.HealthcareApis/workspaces/$workspacename/dicomservices/$dicomservicename"
#find client app service principal id
spid=$(az ad sp show --id $clientid --query objectId --output tsv)
#assign the specified role
az role assignment create --assignee-object-id $spid --assignee-principal-type ServicePrincipal --role "$fhirrole" --scope $fhirrolescope
az role assignment create --assignee-object-id $spid --assignee-principal-type ServicePrincipal --role "$dicomrole" --scope $dicomrolescope
可以从命令行响应或在 Azure 门户中验证角色分配状态。
适用于 FHIR 的 Azure API 的角色分配的工作方式类似。 区别在于范围仅包含 FHIR 服务,不需要工作区名称。
#azure api for fhir role assignment
fhirrole="FHIR Data Contributor"
clientid=xxx
subscriptionid=xxx
resourcegroupname=xxx
fhirservicename=xxx
fhirrolescope="subscriptions/$subscriptionid/resourceGroups/$resourcegroupname/providers/Microsoft.HealthcareApis/services/$fhirservicename"
#find client app service principal id
spid=$(az ad sp show --id $clientid --query objectId --output tsv)
#assign the specified role
az role assignment create --assignee-object-id $spid --assignee-principal-type ServicePrincipal --role "$fhirrole" --scope $fhirrolescope
或者,可以直接将 Put 请求发送到角色分配 REST API。 有关详细信息,请参阅使用 REST API 分配 Azure 角色。
备注
本文中的 REST API 脚本是基于 REST 客户端扩展的。 如果处于其他环境中,则需要修改变量。
API 需要以下值:
- 分配 ID,它是唯一标识事务的 GUID 值。 可以使用 Visual Studio 或 Visual Studio Code 扩展等工具获取 GUID 值。 此外,还可以使用联机工具(例如 UUID 生成器)来获取它。
- API 支持的 API 版本。
- 向其授予访问权限的 Azure Health Data Services 的范围。 它包括订阅 ID、资源组名称和 FHIR 或 DICOM 服务实例名称。
- FHIR 数据参与者或 DICOM 数据所有者等角色的角色定义 ID。 使用
az role definition list --name "<role name>"
列出角色定义 ID。 - 用户或客户端应用程序的服务主体 ID。
- Microsoft Entra 访问令牌到
https://management.azure.com/
,而不是 Azure Health Data Services。 可以使用现有工具或使用 Azure CLI 命令获取访问令牌,az account get-access-token --resource "https://management.azure.com/"
- 对于 Azure Health Data Services,范围包括工作区名称和 FHIR/DICOM 服务实例名称。
### Create a role assignment - Azure Health Data Services (DICOM)
@roleassignmentid=xxx
@roleapiversion=2021-04-01
@roledefinitionid=58a3b984-7adf-4c20-983a-32417c86fbc8
dicomservicename-xxx
@scope=/subscriptions/{{subscriptionid}}/resourceGroups/{{resourcegroupname}}/providers/Microsoft.HealthcareApis/workspaces/{{workspacename}}/dicomservices/{{dicomservicename}}
#get service principal id
@spid=xxx
#get access token
@token=xxx
PUT https://management.azure.com/{{scope}}/providers/Microsoft.Authorization/roleAssignments/{{roleassignmentid}}?api-version={{roleapiversion}}
Authorization: Bearer {{token}}
Content-Type: application/json
Accept: application/json
{
"properties": {
"roleDefinitionId": "/subscriptions/{{subscriptionid}}/providers/Microsoft.Authorization/roleDefinitions/{{roledefinitionid}}",
"principalId": "{{spid}}"
}
}
对于 Azure API for FHIR,范围的定义有所不同,因为它仅支持 FHIR 服务,不需要工作区名称。
### Create a role assignment - Azure API for FHIR
@roleassignmentid=xxx
@roleapiversion=2021-04-01
@roledefinitionid=5a1fc7df-4bf1-4951-a576-89034ee01acd
fhirservicename-xxx
@scope=/subscriptions/{{subscriptionid}}/resourceGroups/{{resourcegroupname}}/providers/Microsoft.HealthcareApis/services/{{fhirservicename}}
#get service principal id
@spid=xxx
#get access token
@token=xxx
PUT https://management.azure.com/{{scope}}/providers/Microsoft.Authorization/roleAssignments/{{roleassignmentid}}?api-version={{roleapiversion}}
Authorization: Bearer {{token}}
Content-Type: application/json
Accept: application/json
{
"properties": {
"roleDefinitionId": "/subscriptions/{{subscriptionid}}/providers/Microsoft.Authorization/roleDefinitions/{{roledefinitionid}}",
"principalId": "{{spid}}"
}
}
(可选)可以获取 Azure Health Data Services 服务或 Azure API for FHIR 的列表。 API 版本是基于 Azure Health Data Services 的,而不是基于角色分配 REST API 的版本。
对于 Azure Health Data Services,请指定订阅 ID、资源组名称、工作区名称、FHIR 或 DICOM 服务和 API 版本。
### Get Azure Health Data Services DICOM services
@apiversion=2021-06-01
@subscriptionid=xxx
@resourcegroupname=xxx
@workspacename=xxx
GET https://management.azure.com/subscriptions/{{subscriptionid}}/resourceGroups/{{resourcegroupname}}/providers/Microsoft.HealthcareApis/workspaces/{{workspacename}}/dicomservices?api-version={{apiversion}}
Authorization: Bearer {{token}}
Content-Type: application/json
Accept: application/json
对于 Azure API for FHIR,请指定订阅 ID 和 API 版本。
### Get a list of Azure API for FHIR services
@apiversion=2021-06-01
@subscriptionid=xxx
GET https://management.azure.com/subscriptions/{{subscriptionid}}/providers/Microsoft.HealthcareApis/services?api-version={{apiversion}}
Authorization: Bearer {{token}}
Content-Type: application/json
Accept: application/json
向客户端应用程序授予适当的权限后,可以在应用程序中访问 Azure Health Data Services。