背景
- 我想使用一个带有顶点AI自定义作业的服务帐户,并创建一个身份令牌
- 我正在尝试通知一个云运行API一旦顶点AI自定义作业完成
- 我已经创建了一个具有
roles/run.invoker
角色的服务帐户,并确认该服务帐户可以访问云运行的API。- 我使用了服务帐户中的凭据,并创建了一个身份令牌,其中访问者设置为云运行的API URL
问题
- 使用
gcloud ai custom-jobs create
或通过golang客户端库创建顶点AI自定义作业时,无法为自定义服务帐户获取身份令牌。gcloud auth print-identity-token
导致错误:(gcloud.auth.print-identity-token) No identity token can be obtained from the current credentials.
- 使用元数据服务器URL直接给出
Not Found
响应。- 命令:
curl -s --get --data-urlencode "audience=$CLOUD_RUN_API_URL" --data-urlencode "format=full" -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/service-accounts/default/identity
- 回复:
Not Found
- 命令:
- 我需要一些关于调用自定义作业的设置/方式将允许我访问服务帐户的身份令牌的指导
详细信息
- 为了测试创建身份令牌,使用下面的docker文件指定自定义容器:
FROM alpine:3.6
RUN apk add --update python curl which bash
RUN curl -sSL https://sdk.cloud.google.com | bash -s -- --disable-prompts
ENV PATH $PATH:/root/google-cloud-sdk/bin
CMD ["gcloud", "auth", "print-identity-token", "--verbosity", "debug"]
以下生成一个工作身份令牌:
- ✅在本地运行此映像
- ✅使用";创建";按钮,并将服务帐户设置为我为这个用例创建的自定义帐户
- ✅使用
gcloud
运行此映像,不设置任何服务帐户(使用项目的顶点ai默认服务帐户)
以下结果导致了一个错误(这个问题促使我问这个问题):
❌使用以下设置的
gcloud
运行此图像时,我会出现错误:- 命令(已编辑图像名称/服务帐户):
gcloud ai custom-jobs create --region=asia-northeast1 --display-name=cli_identity_test --worker-pool-spec=machine-type=n2-standard-4,replica-count=1,container-image-uri=<IMAGE NAME> --service-account=<SERVICE ACCOUNT EMAIL>
- 错误:
DEBUG: (gcloud.auth.print-identity-token) No identity token can be obtained from the current credentials. Traceback (most recent call last): File "/root/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 987, in Execute resources = calliope_command.Run(cli=self, args=args) File "/root/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 809, in Run resources = command_instance.Run(args) File "/root/google-cloud-sdk/lib/googlecloudsdk/calliope/exceptions.py", line 129, in TryFunc return func(*args, **kwargs) File "/root/google-cloud-sdk/lib/surface/auth/print_identity_token.py", line 130, in Run credential = _Run(args) File "/root/google-cloud-sdk/lib/surface/auth/print_identity_token.py", line 78, in _Run 'No identity token can be obtained from the current credentials.') InvalidIdentityTokenError: No identity token can be obtained from the current credentials. ERROR: (gcloud.auth.print-identity-token) No identity token can be obtained from the current credentials.
- 命令(已编辑图像名称/服务帐户):
❌使用自定义服务帐户运行图像,并使用顶点ai golang API客户端库启动作业
关于角色的注释
- 我也尝试过扩展与我的服务帐户关联的角色,但这些并不能修复错误:
- 以匹配默认的顶点ai服务帐户(
roles/aiplatform.customCodeServiceAgent
) - 服务帐户令牌创建者(
roles/iam.serviceAccountTokenCreator
) - 服务帐户用户(
roles/iam.serviceAccountUser
)
- 以匹配默认的顶点ai服务帐户(
我写了
我相信您可以重用API和访问令牌来生成和标识令牌。我曾经在云构建方面遇到过类似的问题(我认为现在已经解决了)。
您可以使用
import google.auth.transport.requests
import google.oauth2.id_token
auth_req = google.auth.transport.requests.Request()
token = google.oauth2.id_token.fetch_id_token(auth_req, <AUDIENCE>)
示例:云功能的受众看起来像https://<REGION>-<PROJECT_ID>.cloudfunctions.net/<FUNCTION_NAME>