批处理终结点上的授权

批处理终结点支持 Microsoft Entra 身份验证,或 aad_token。 若要调用批处理终结点,必须向批处理终结点 URI 提供有效的“Microsoft Entra”身份验证令牌。 授权在终结点级别强制执行。 以下文章介绍如何正确与批处理终结点和安全要求进行交互。

授权的工作原理

若要调用批处理终结点,必须提供表示安全主体的有效Microsoft Entra令牌。 此 主体可以是用户主体 或服务 主体。 在任何情况下,调用终结点时,会在与令牌关联的标识下创建批处理部署作业。 标识需要以下权限才能成功创建作业:

  • 读取批处理终结点和部署。
  • 在批处理推理终结点和部署中创建作业。
  • 创建试验并运行。
  • 将数据读取和写入数据存储。
  • 列出数据存储机密。

有关 RBAC 权限的详细列表,请参阅 为批处理终结点调用配置 RBAC

重要

根据数据存储的配置方式,可能无法使用标识来调用批处理终结点来读取基础数据。 有关详细信息,请参阅 配置计算群集进行数据访问

如何使用不同类型的凭据运行任务

以下示例演示了使用不同类型的凭据启动批处理部署作业的不同方法:

重要

处理启用了专用链接的工作区时,您无法在 Azure Machine Learning Studio 的 UI 中调用批处理终结点。 请改用 Azure 机器学习 CLI v2 来创建作业。

先决条件

使用凭据运行作业

若要使用当前登录用户的标识执行批处理终结点,请执行以下步骤:

  1. 使用Azure CLI通过交互式身份验证或设备代码身份验证登录:

    az login
    
  2. 进行身份验证后,使用以下命令运行批处理部署作业:

    az ml batch-endpoint invoke --name $ENDPOINT_NAME \
                                --input https://azuremlexampledata.blob.core.chinacloudapi.cn/data/heart-disease-uci
    

使用服务主体运行作业

若要使用已在Microsoft Entra ID中创建的服务主体执行批处理终结点,请创建一个要进行身份验证的机密。 执行以下步骤:

  1. 按照选项 3 中所述创建用于身份验证的机密 :创建新的客户端密码

  2. 若要使用服务主体进行身份验证,请使用以下命令。 有关详细信息,请参阅 Sign with Azure CLI

    az login --service-principal \
             --tenant <tenant> \
             -u <app-id> \
             -p <password-or-cert> 
    
  3. 进行身份验证后,使用以下命令运行批处理部署作业:

    az ml batch-endpoint invoke --name $ENDPOINT_NAME \
                                --input https://azuremlexampledata.blob.core.chinacloudapi.cn/data/heart-disease-uci/
    

使用托管标识运行作业

可以使用托管身份来调用批处理端点和进行部署。 请注意,此管理标识不属于批处理终结点,但它是用于执行终结点的标识,因此会创建批处理作业。 在此方案中,可以使用用户分配标识和系统分配标识。

对于为Azure资源配置的托管标识的资源,可以使用托管标识登录。 使用资源的标识登录是通过 --identity 标志执行的。 有关详细信息,请参阅 Sign with Azure CLI

az login --identity

进行身份验证后,使用以下命令运行批处理部署作业:

az ml batch-endpoint invoke --name $ENDPOINT_NAME \
                            --input https://azuremlexampledata.blob.core.chinacloudapi.cn/data/heart-disease-uci

为批处理终结点调用配置 RBAC

Batch 终结点公开使用者可用于生成作业的持久的 API。 调用程序请求适当的权限,以便能够生成这些作业。 可以使用其中一个 内置安全角色 ,也可以根据目的创建自定义角色。

若要成功调用批处理终结点,需要向用于调用终结点的身份授予以下显式操作。 要获取分配 Azure 角色的说明,请参阅 步骤分配 Azure 角色

"actions": [
    "Microsoft.MachineLearningServices/workspaces/read",
    "Microsoft.MachineLearningServices/workspaces/data/versions/write",
    "Microsoft.MachineLearningServices/workspaces/datasets/registered/read",
    "Microsoft.MachineLearningServices/workspaces/datasets/registered/write",
    "Microsoft.MachineLearningServices/workspaces/datasets/unregistered/read",
    "Microsoft.MachineLearningServices/workspaces/datasets/unregistered/write",
    "Microsoft.MachineLearningServices/workspaces/datastores/read",
    "Microsoft.MachineLearningServices/workspaces/datastores/write",
    "Microsoft.MachineLearningServices/workspaces/datastores/listsecrets/action",
    "Microsoft.MachineLearningServices/workspaces/listStorageAccountKeys/action",
    "Microsoft.MachineLearningServices/workspaces/batchEndpoints/read",
    "Microsoft.MachineLearningServices/workspaces/batchEndpoints/write",
    "Microsoft.MachineLearningServices/workspaces/batchEndpoints/deployments/read",
    "Microsoft.MachineLearningServices/workspaces/batchEndpoints/deployments/write",
    "Microsoft.MachineLearningServices/workspaces/batchEndpoints/deployments/jobs/write",
    "Microsoft.MachineLearningServices/workspaces/batchEndpoints/jobs/write",
    "Microsoft.MachineLearningServices/workspaces/computes/read",
    "Microsoft.MachineLearningServices/workspaces/computes/listKeys/action",
    "Microsoft.MachineLearningServices/workspaces/metadata/secrets/read",
    "Microsoft.MachineLearningServices/workspaces/metadata/snapshots/read",
    "Microsoft.MachineLearningServices/workspaces/metadata/artifacts/read",
    "Microsoft.MachineLearningServices/workspaces/metadata/artifacts/write",
    "Microsoft.MachineLearningServices/workspaces/experiments/read",
    "Microsoft.MachineLearningServices/workspaces/experiments/runs/submit/action",
    "Microsoft.MachineLearningServices/workspaces/experiments/runs/read",
    "Microsoft.MachineLearningServices/workspaces/experiments/runs/write",
    "Microsoft.MachineLearningServices/workspaces/metrics/resource/write",
    "Microsoft.MachineLearningServices/workspaces/modules/read",
    "Microsoft.MachineLearningServices/workspaces/models/read",
    "Microsoft.MachineLearningServices/workspaces/endpoints/pipelines/read",
    "Microsoft.MachineLearningServices/workspaces/endpoints/pipelines/write",
    "Microsoft.MachineLearningServices/workspaces/environments/read",
    "Microsoft.MachineLearningServices/workspaces/environments/write",
    "Microsoft.MachineLearningServices/workspaces/environments/build/action",
    "Microsoft.MachineLearningServices/workspaces/environments/readSecrets/action"
]

为数据访问配置计算群集

Batch 终结点确保只有经过授权的用户能够调用批处理部署并生成作业。 但是,根据输入数据的配置方式,其他凭据可用于读取基础数据。 使用下表了解使用了哪些凭据:

数据输入类型 存储中的凭据 使用的凭据 授予访问权限的主体
数据存储 是的 工作区中的数据存储凭据 访问密钥或共享访问签名 (SAS)
数据资产 是的 工作区中的数据存储凭据 访问密钥或 SAS
数据存储 作业标识 + 计算群集的托管标识 RBAC
数据资产 作业标识 + 计算群集的托管标识 RBAC
Azure Blob 存储 不适用 作业标识 + 计算群集的托管标识 RBAC

对于表中显示“作业标识 + 计算群集托管标识”的项目,将使用计算群集的托管标识来装载和配置存储帐户。 但是,作业的标识仍用于读取基础数据,以便实现精细访问控制。 这意味着,若要从存储成功读取数据,运行部署的计算群集的托管标识必须至少具有对存储帐户的 存储 Blob 数据读取器 访问权限。

若要配置计算群集进行数据访问,请执行以下步骤:

  1. 转到 Azure 机器学习工作室

  2. 导航到 “计算”,然后 导航到“计算群集”。

  3. 选择部署正在使用的计算群集。 此作将打开计算群集的 “详细信息 ”页。

  4. 将托管标识分配给计算群集:

    1. 转到页面的 “托管标识 ”部分,验证计算是否分配了托管标识。 如果没有,请选择铅笔图标以编辑托管标识。

    2. 选择 “分配托管标识 ”旁边的滑块以根据需要启用和配置它。 可以使用 System-Assigned 托管标识或 User-Assigned 托管标识。 如果使用 System-Assigned 托管标识,则会将其命名为“[工作区名称]/computes/[计算群集名称]”。

    3. 保存更改。

  5. 转到 Azure 门户并导航到数据所在的关联存储帐户。 如果数据输入是数据资产或数据存储,请查找放置这些资产的存储帐户。

  6. 在存储帐户中分配存储 Blob 数据读取器访问级别:

    1. 转到 访问控制(IAM)部分。

    2. 选择“ 角色分配”选项卡,然后单击“ 添加>角色分配”。

    3. 查找名为 “存储 Blob 数据读取者”的角色,选择它,然后单击“ 下一步”。

    4. 单击“选择成员”。

    5. 查找已创建的托管标识。 如果使用 System-Assigned 托管标识,则会将其命名为“[工作区名称]/computes/[计算群集名称]”。

    6. 添加帐户并完成向导。

  7. 您的端点已准备好接收来自所选存储帐户的作业和输入数据。

后续步骤