gpt4 book ai didi

google-cloud-platform - 在 Docker Container 内的 Container OS 上访问 Google Cloud 服务帐户凭据

转载 作者:行者123 更新时间:2023-12-04 01:08:01 28 4
gpt4 key购买 nike

使用 Container Optimized OS (COS) 在 Google Cloud Compute 上,访问 default service account 凭据的最佳方式是什么?用于 Docker 容器中的 VM 项目?

$ gcloud compute instances create test-instance \
--image=cos-stable --image-project=cos-cloud

$ ssh (ip of the above)
# gcloud ...
Command not found

# docker run -ti google/cloud-sdk:alpine /bin/sh
# gcloud auth activate-service-account
... --key-file: Must be specified.

如果凭据在 VM 上,那么 Docker 可以挂载这些凭据。通常凭据将位于 .config/gcloud/ , 并使用 docker run -v ~/.config/gcloud:~/.config/gcloud image 执行此操作.目前尚不清楚容器操作系统中是否以及在何处提供此类凭据,特别是因为缺少 gcloud .

如果凭证在 VM 上且可挂载失败,选项似乎包括:
  • 将凭证放入容器元数据/环境变量中;
  • 创建 .json服务帐户的凭据文件,然后
  • 上传到虚拟机,然后挂载;或
  • 添加 .json到容器;
  • 运行一个 Docker 容器(例如 cloud-sdk-docker ),它获取凭据并通过例如与主机共享它们。共享挂载分区。理想情况下,这将是 gcloud auth activate-service-account

  • 是否有规范或最佳实践方法为 Docker 容器提供 VM 项目的服务帐户凭据?

    谷歌云已经有一个安全策略模型,这是理想的模型:项目中的虚拟机应该具有服务帐户提供的访问权限。为了避免复杂性和错误配置或事故的可能性,正确的解决方案将采用这种现有的安全模型,即不涉及创建、下载、分发和维护凭证文件。

    感觉这将是一个需要使用 COS、Docker 和 Kubernetes 解决的常规问题,所以我认为我错过了一些简单的东西——但是从文档中我看不到解决方案。

    编辑 — 注意 set-service-account API——这个问题可以简化为“你如何在容器操作系统中使用 set-service-account API?”如果不可能(因为容器操作系统缺少 gcloudgsutil ),我认为应该注意这一点,以便 VM 用户可以做出相应的计划。

    编辑 对于接下来的人来说:

    为了复制这个问题,我使用了:
    [local] $ ssh test-instance-ip
    [test-instance] $ docker run -it gcr.io/google-appengine/python /bin/bash
    [test-instance] $ pip install --upgrade google-cloud-datastore
    [test-instance] $ python

    >>> from google.cloud import datastore
    >>> datastore_client = datastore.Client()
    >>> q = datastore.query.Query(datastore_client, kind='MODEL-KIND')
    >>> list(q.fetch())
    [... results]

    问题确实是在 API 中为 VM 实例设置的范围,尤其是 datastore API 已为默认帐户禁用(在标题 云 API 访问范围 下用于 VM)。可以找到范围和必要的 datastore行如下:
    > gcloud compute instances describe test-instance
    ...
    serviceAccounts:
    - email: *****-compute@developer.gserviceaccount.com
    scopes:
    - https://www.googleapis.com/auth/datastore
    ...
    ...

    请注意,服务帐户本身有权访问数据存储(因此通常可以使用服务 key 的 json 凭证 key 访问数据存储)。服务帐户权限受 VM 范围的限制。

    最佳答案

    通常的身份验证方式是出现在 Google cloud SDK Docker readme 上的方式。 .

    在 COS 实例中运行一次:

    docker run -ti --name gcloud-config google/cloud-sdk gcloud auth login

    这会将您的凭据存储在 gcloud-config 中容器体积。

    这个卷应该只安装有你想要访问你的凭证的容器,它可能不会是任何不是 cloud-sdk 的东西。
    docker run --rm -ti --volumes-from gcloud-config google/cloud-sdk:alpine gcloud compute instances create test-docker --project [PROJECT]  


    Created [https://www.googleapis.com/compute/v1/projects/project/zones/us-east1-b/instances/test-docker].
    NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
    test-docker us-east1-b n1-standard-1 10.142.0.8 X.X.X.X RUNNING

    服务帐户通常意味着使用他们自己的一组凭据,他们必须从某个地方获取这些凭据,作为 key 文件,以及环境变量或 token :

    gcloud auth activate-service-account

    If you want gcloud (and other tools in the Cloud SDK) to use service account credentials to make requests, use this command to import these credentials from a file that contains a private authorization key, and activate them for use in gcloud. This command serves the same function as gcloud auth login but for using a service account rather than your Google user credentials.



    此外, best practice就是为不同的实例创建不同的服务账号,而不是获取默认服务账号的key并使用:

    In general, Google recommends that each instance that needs to call a Google API should run as a service account with the minimum permissions necessary for that instance to do its job. In practice, this means you should configure service accounts for your instances with the following process:

    1 - Create a new service account rather than using the Compute Engine default service account.
    2 - Grant IAM roles to that service account for only the resources that it needs.
    3 - Configure the instance to run as that service account.
    4 - Grant the instance the https://www.googleapis.com/auth/cloud-platform scope.
    5 - Avoid granting more access than necessary and regularly check your service account permissions to make sure they are up-to-date.



    更新

    我不确定 set-service-account 是否可以满足您的需要/想要的。使用它,您可以更改实例使用的服务帐户(但必须停止该实例,因此您不能使用它来更改服务帐户,以免更改实例)。但是,您可以在其他情况下正常使用它,请参阅:
    jordim@cos ~ $ docker run --rm -ti --volumes-from gcloud-config google/cloud-sdk:alpine gcloud compute instances set-service-account instance-1 --service-account xx-compute@developer.gserviceaccount.com
    Did you mean zone [us-east1-b] for instance: [instance-1] (Y/n)?

    Updated [https://www.googleapis.com/compute/v1/projects/XX/zones/us-east1-b/instances/instance-1].

    关于google-cloud-platform - 在 Docker Container 内的 Container OS 上访问 Google Cloud 服务帐户凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50292668/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com