控制对 Vertex AI 端点的访问权限

本页面介绍了如何通过对 Vertex AI 端点设置 IAM 政策来控制其访问权限。本教程假定您已熟悉 IAM 概念,例如政策、角色、权限和主账号,如使用 IAM 进行 Vertex AI 访问权限控制以及与访问管理相关的概念中所述。

IAM 政策包含一个或多个角色绑定,用于定义哪些 IAM 角色与哪些主账号关联。一个角色对应授予主账号的一组权限。Vertex AI 提供了可在政策中使用的预定义角色。或者,您也可以创建自己的自定义角色。

获取 IAM 政策

您可以使用 REST API 在 Vertex AI 端点上查看当前 IAM 政策。为此,您必须拥有端点或项目的 endpoints.getIamPolicy 权限。Vertex AI Administrator 角色 (roles/aiplatform.admin) 会授予此权限。

REST

如需从资源中获取 IAM 政策,请发送使用 getIamPolicy 方法的 POST 请求。

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION_ID:端点所在的区域,例如 us-central1
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • ENDPOINT_ID:端点的 ID。

HTTP 方法和网址:

POST http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:getIamPolicy

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:getIamPolicy"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:getIamPolicy" | Select-Object -Expand Content

您应该会收到包含当前 IAM 政策的 JSON 响应:

{
  "version": 1,
  "etag": "BwXTmICm7mI",
  "bindings": [
    {
      "role": "roles/aiplatform.user",
      "members": [
        "user:[email protected]"
      ]
    }
  ]
}

设置 IAM 政策

您可以使用 REST API 在端点上设置 IAM 政策。为此,您必须拥有端点或项目的 endpoints.setIamPolicy 权限。Vertex AI Administrator 角色 (roles/aiplatform.admin) 会授予此权限。

REST

如需对资源设置 IAM 政策,请发送使用 setIamPolicy 方法的 POST 请求。

设置 IAM 政策会替换任何现有政策;不会附加更改。如需修改资源的现有政策,请使用 getIamPolicy 方法获取其现有政策,然后进行修改。在 setIamPolicy 请求中添加修改后的政策以及 etag

如果您收到 409 错误代码,则表示并发 setIamPolicy 请求已更新该政策。使用 getIamPolicy 方法获取政策更新后的 etag,然后使用新的 etag 重试 setIamPolicy 请求。

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION_ID:端点所在的区域,例如 us-central1
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • ENDPOINT_ID:端点的 ID。
  • ROLE:包含要授予的权限的 IAM 角色,例如 roles/aiplatform.user
  • PRINCIPAL:被授予角色权限的主账号,例如 user:[email protected]
  • ETAG:用于防止政策的同时更新相互覆盖的字符串值。此值作为 getIamPolicy 响应的一部分返回。

HTTP 方法和网址:

POST http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:setIamPolicy

请求 JSON 正文:

{
  "policy": {
    "bindings": [
      {
        "role": "ROLE",
        "members": [
          "PRINCIPAL"
        ]
      },
      ...
    ],
    "etag": "ETAG"
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:setIamPolicy"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:setIamPolicy" | Select-Object -Expand Content

您应该会收到包含当前 IAM 政策的 JSON 响应:

{
  "version": 1,
  "etag": "BwXTmICm7mI",
  "bindings": [
    {
      "role": "roles/aiplatform.user",
      "members": [
        "user:[email protected]"
      ]
    }
  ]
}

验证用户对端点的 IAM 权限

您可以验证当前通过身份验证的用户是否具有端点的特定 IAM 权限。

REST

如需验证用户是否具有某项资源的特定 IAM 权限,请发送使用 testIamPermissions 方法的 POST 请求。以下示例可让您测试当前通过身份验证的用户是否具有端点的一组 IAM 权限。

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION_ID:端点所在的区域,例如 us-central1
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • ENDPOINT_ID:端点的 ID。

HTTP 方法和网址:

POST http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:testIamPermissions -d

请求 JSON 正文:

{
  "permissions": [
    "aiplatform.googleapis.com/aiplatform.endpoints.get",
    "aiplatform.googleapis.com/aiplatform.endpoints.predict"
  ]
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:testIamPermissions -d"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "http://LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID:testIamPermissions -d" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。响应仅包含来自当前经过身份验证的用户可用的请求 JSON 正文中的权限。
{
  "permissions": [
    "aiplatform.googleapis.com/aiplatform.endpoints.get",
    "aiplatform.googleapis.com/aiplatform.endpoints.predict"
  ]
}