适用范围:
Azure CLI ml 扩展 v2(最新版)
Python SDK azure-ai-ml v2(最新版)
本文介绍如何对客户端进行身份验证,以便在联机终结点上执行控制平面和数据平面操作。
控制平面操作可控制终结点并对其进行更改。 控制平面操作包括在联机终结点和联机部署上进行的创建、读取、更新和删除 (CRUD) 操作。
数据平面操作使用数据与联机终结点交互,而无需更改终结点。 例如,数据平面操作可能包括向联机终结点发送评分请求并获取响应。
先决条件
Azure 机器学习工作区。 有关创建工作区的说明,请参阅 “创建工作区”。
Azure CLI 和
ml扩展或 Azure 机器学习 Python SDK v2:若要安装 Azure CLI 和
ml扩展,请参阅安装和设置 CLI(v2)。本文中的示例假定使用 Bash shell 或兼容的 shell。 例如,可以在 Linux 系统或 适用于 Linux 的 Windows 子系统上使用 shell。
准备用户标识
需要用户身份才能在在线端点上进行控制平面操作(例如 CRUD 操作)和数据平面操作(例如发送评分请求)。 可以对控制平面操作和数据平面操作使用相同的用户标识,也可使用不同的用户标识。 在本文中,你将对控制平面和数据平面操作使用相同的用户标识。
有关在 Microsoft Entra ID 中创建用户标识的信息,请参阅 “设置身份验证”。 稍后需要标识 ID。
向标识分配权限
在本部分中,将为用于与终结点交互的用户标识分配权限。 首先使用内置角色或创建自定义角色。 然后,将角色分配给用户标识。
使用内置角色
可以使用 AzureML Data Scientist内置角色 来管理和使用终结点和部署。 它还使用通配符,用于包括以下控制平面的RBAC操作:
Microsoft.MachineLearningServices/workspaces/onlineEndpoints/writeMicrosoft.MachineLearningServices/workspaces/onlineEndpoints/deleteMicrosoft.MachineLearningServices/workspaces/onlineEndpoints/readMicrosoft.MachineLearningServices/workspaces/onlineEndpoints/token/actionMicrosoft.MachineLearningServices/workspaces/onlineEndpoints/listKeys/actionMicrosoft.MachineLearningServices/workspaces/onlineEndpoints/regenerateKeys/action
它使用通配符来包括以下数据平面RBAC操作。
Microsoft.MachineLearningServices/workspaces/onlineEndpoints/score/action
(可选)可以使用 Azure Machine Learning Workspace Connection Secrets Reader 内置角色访问来自工作区连接的密钥。 它包括以下控制平面的 RBAC 操作:
Microsoft.MachineLearningServices/workspaces/connections/listsecrets/actionMicrosoft.MachineLearningServices/workspaces/metadata/secrets/read
如果使用这些内置角色,则无需在此步骤中执行任何操作。
(可选)创建自定义角色
如果使用内置角色或其他预制自定义角色,可以跳过此步骤。
通过创建角色的 JSON 定义来定义自定义角色的范围和操作。 例如,以下角色定义 custom-role-for-control-plane.json允许用户对指定工作区中的在线端点执行 CRUD 操作。
{ "Name": "Custom role for control plane operations - online endpoint", "IsCustom": true, "Description": "Can CRUD against online endpoints.", "Actions": [ "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/write", "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/delete", "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/read", "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/token/action", "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/listKeys/action", "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/regenerateKeys/action" ], "NotActions": [ ], "AssignableScopes": [ "/subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>" ] }以下角色定义 custom-role-for-scoring.json允许用户将评分请求发送到指定工作区中的联机终结点。
{ "Name": "Custom role for scoring - online endpoint", "IsCustom": true, "Description": "Can score against online endpoints.", "Actions": [ "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/*/action" ], "NotActions": [ ], "AssignableScopes": [ "/subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>" ] }使用 JSON 定义创建自定义角色:
az role definition create --role-definition custom-role-for-control-plane.json --subscription <subscriptionID> az role definition create --role-definition custom-role-for-scoring.json --subscription <subscriptionID>注意
若要创建自定义角色,需要具有下面三个角色之一:
- 所有者
- 用户访问管理员
- 具有
Microsoft.Authorization/roleDefinitions/write权限的自定义角色(用于创建/更新/删除自定义角色)和Microsoft.Authorization/roleDefinitions/read权限(查看自定义角色)。
若要详细了解如何创建自定义角色,请参阅 Azure 自定义角色。
验证角色定义:
az role definition list --custom-role-only -o table az role definition list -n "Custom role for control plane operations - online endpoint" az role definition list -n "Custom role for scoring - online endpoint" export role_definition_id1=`(az role definition list -n "Custom role for control plane operations - online endpoint" --query "[0].id" | tr -d '"')` export role_definition_id2=`(az role definition list -n "Custom role for scoring - online endpoint" --query "[0].id" | tr -d '"')`
将角色分配给标识
如果使用
AzureML Data Scientist内置角色,请使用以下代码将角色分配给用户标识。az role assignment create --assignee <identityID> --role "AzureML Data Scientist" --scope /subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>/providers/Microsoft.MachineLearningServices/workspaces/<workspaceName>(可选)如果使用
Azure Machine Learning Workspace Connection Secrets Reader内置角色,请使用以下代码将角色分配给用户标识。az role assignment create --assignee <identityID> --role "Azure Machine Learning Workspace Connection Secrets Reader" --scope /subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>/providers/Microsoft.MachineLearningServices/workspaces/<workspaceName>如果使用自定义角色,请使用以下代码将角色分配给用户标识。
az role assignment create --assignee <identityID> --role "Custom role for control plane operations - online endpoint" --scope /subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>/providers/Microsoft.MachineLearningServices/workspaces/<workspaceName> az role assignment create --assignee <identityID> --role "Custom role for scoring - online endpoint" --scope /subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>/providers/Microsoft.MachineLearningServices/workspaces/<workspaceName>注意
若要将自定义角色分配给用户标识,需要具有下面三个角色之一:
- 所有者
- 用户访问管理员
- 允许
Microsoft.Authorization/roleAssignments/write权限(分配自定义角色)和Microsoft.Authorization/roleAssignments/read(查看角色分配)的自定义角色。
有关 Azure 角色及其权限的详细信息,请参阅 Azure 角色 , 并使用 Azure 门户分配 Azure 角色。
确认角色分配:
az role assignment list --scope /subscriptions/<subscriptionID>/resourcegroups/<resourceGroupName>/providers/Microsoft.MachineLearningServices/workspaces/<workspaceName>
获取用于控制平面操作的 Microsoft Entra 令牌
如果计划通过直接使用令牌的 REST API 来执行控制平面操作,请完成此步骤。
如果计划将其他方法(如 Azure CLI 与 ml 扩展 v2、Python SDK v2 或 Azure 机器学习工作室)配合使用,则无需手动获取 Microsoft Entra 令牌。 您的用户身份将在登录时进行身份验证,系统将自动检索并为您传递令牌。
可以从 Azure 资源终结点检索用于控制平面操作的 Microsoft Entra 令牌:https://management.chinacloudapi.cn
登录 Azure。
az login如果要使用特定标识,请通过以下代码使用标识进行登录:
az login --identity --username <identityID>使用此上下文获取令牌:
export CONTROL_PLANE_TOKEN=`(az account get-access-token --resource https://management.chinacloudapi.cn --query accessToken | tr -d '"')`
(可选)验证 Microsoft Entra 令牌的资源端点和客户端 ID
检索Microsoft Entra 令牌后,可以通过通过 management.chinacloudapi.cn 解码令牌来验证令牌是否适用于正确的 Azure 资源终结点()和正确的客户端 ID,这会返回包含以下信息的 JSON 响应:
{
"aud": "https://management.chinacloudapi.cn",
"oid": "<your-object-id>"
}
创建终结点
以下示例创建一个终结点,其终结点标识为系统分配的标识。 系统分配的标识是终结点托管标识的默认标识类型。 系统分配的标识会自动分配一些基本角色。 若要详细了解系统分配标识的角色分配,请参阅终结点标识的自动角色分配。
CLI 不要求显式提供控制平面令牌。 相反,CLI az login 命令会在登录期间对你进行身份验证,并且会自动检索并传递令牌。
创建名为 endpoint.yml的终结点定义 YAML 文件:
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineEndpoint.schema.json name: my-endpoint auth_mode: aad_token可以将
auth_mode设置为key以进行密钥身份验证或设置为aml_token以进行Azure机器学习令牌身份验证。 此示例使用aad_token进行 Microsoft Entra 令牌身份验证。创建终结点:
az ml online-endpoint create -f endpoint.yml检查终结点的状态:
az ml online-endpoint show -n my-endpoint如果要在创建终结点时替代
auth_mode(例如,替代为aad_token),请运行以下代码:az ml online-endpoint create -n my-endpoint --auth-mode aad_token如果要更新现有终结点并指定
auth_mode(例如,如),aad_token请运行以下代码:az ml online-endpoint update -n my-endpoint --set auth_mode=aad_token
创建部署
若要创建部署,请参阅 使用联机终结点部署机器学习模型 ,或使用 REST 将模型部署为联机终结点。 为不同的身份验证模式创建部署的方式没有区别。
以下代码是关于如何创建部署的示例。 有关部署联机终结点的详细信息,请参阅 使用联机终结点(通过 CLI)部署机器学习模型。
创建名为 blue-deployment.yml的部署定义 YAML 文件:
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json name: blue endpoint_name: my-aad-auth-endp1 model: path: ../../model-1/model/ code_configuration: code: ../../model-1/onlinescoring/ scoring_script: score.py environment: conda_file: ../../model-1/environment/conda.yml image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest instance_type: Standard_DS3_v2 instance_count: 1使用 YAML 文件创建部署。 对于此示例,请将所有流量设置为新部署。
az ml online-deployment create -f blue-deployment.yml --all-traffic
获取终结点的评分 URI
如果使用 az ml online-endpoint invoke 调用终结点,CLI 会自动解析评分 URI,因此无需手动检索它。
但是,如果需要用于其他工具(如 REST API 或自定义 HTTP 客户端)的评分 URI,可以使用以下命令检索它:
scoringUri=$(az ml online-endpoint show -n my-endpoint --query "scoring_uri")
获取数据平面操作的密钥或令牌
即使获取密钥或令牌的过程是控制平面操作,也可以对数据平面操作使用密钥或令牌。 换句话说,可以使用控制平面令牌获取稍后用于执行数据平面操作的密钥或令牌。
若要获取密钥或 Azure 机器学习令牌,请求它的用户身份需要为其分配相应的角色,如 控制平面操作的授权中所述。 用户标识不需要任何额外的角色来获取 Microsoft Entra 令牌。
如果计划使用 CLI 调用终结点,则无需显式获取数据平面操作的密钥或令牌,因为 CLI 会为你提供这些信息。 但是,仍然可以使用 CLI 来获取密钥或令牌,用于数据平面操作,这样可以与其他通道一起使用,例如 REST API。
若要获取数据平面操作的密钥或令牌,请使用 az ml online-endpoint get-credentials 命令。 此命令返回包含密钥、令牌和/或其他信息的 JSON 输出。
提示
在以下命令中,该 --query 参数用于从 JSON 输出中提取特定信息。 但是,可以将任何合适的工具用于该目的。
当终结点的 auth_mode 为 key
密钥在
primaryKey和secondaryKey字段中返回。export DATA_PLANE_TOKEN=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query primaryKey) export DATA_PLANE_TOKEN2=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query secondaryKey)
当终结点的 auth_mode 为 aml_token
令牌在
accessToken字段中返回。令牌过期时间在
expiryTimeUtc字段中返回。令牌刷新时间在
refreshAfterTimeUtc字段中返回。export DATA_PLANE_TOKEN=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query accessToken) export EXPIRY_TIME_UTC=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query expiryTimeUtc) export REFRESH_AFTER_TIME_UTC=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query refreshAfterTimeUtc)
当终结点的 auth_mode 为 aad_token
令牌在
accessToken字段中返回。令牌过期时间在
expiryTimeUtc字段中返回。export DATA_PLANE_TOKEN=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query accessToken) export EXPIRY_TIME_UTC=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -g $RESOURCE_GROUP -w $WORKSPACE_NAME -o tsv --query expiryTimeUtc)
验证 Microsoft Entra 令牌的资源终结点和客户端 ID
获取 Entra 令牌后,可以通过 studio.ml.azure.cn 解码令牌来验证令牌是否适用于正确的 Azure 资源终结点,以及正确的客户端 ID,这将返回包含以下信息的 JSON 响应:
{
"aud": "https://studio.ml.azure.cn",
"oid": "<your-object-id>"
}
使用密钥或令牌对数据评分
可以将 az ml online-endpoint invoke 用于具有密钥或 Azure 机器学习令牌或 Microsoft Entra 令牌的终结点。 CLI 自动提供密钥或令牌,因此无需显式传递密钥或令牌。
az ml online-endpoint invoke -n my-endpoint -r request.json
日志和监视器流量
若要在终结点的诊断设置中启用流量日志记录,请完成 “启用日志”中的步骤。
如果启用了诊断设置,可以查看 AmlOnlineEndpointTrafficLogs 表以查看身份验证模式和用户标识。