【问题标题】:Using Google Cloud Source Repositories with service account通过服务帐号使用 Google Cloud Source Repositories
【发布时间】:2015-11-11 11:16:44
【问题描述】:

是否可以自动访问Google Cloud Source Repository,即使用service account 从GCE 实例?

我在文档中看到的唯一身份验证方法是使用 gcloud auth login 命令,该命令将对我的个人用户进行身份验证以访问 repo,而不是我正在运行命令的机器。

【问题讨论】:

    标签: google-cloud-platform service-accounts gcloud-cli google-cloud-source-repos


    【解决方案1】:

    在 GCE 虚拟机上运行

    gcloud source repos clone default ~/my_repo
    

    应该自动工作,无需额外的身份验证步骤,因为它将使用虚拟机服务帐户。

    如果您在其他机器上运行,您可以从https://console.cloud.google.com 服务帐户.json 密钥文件下载并激活它

    gcloud auth activate-service-account --key-file KEY_FILE
    

    然后运行上面的克隆命令。

    【讨论】:

    【解决方案2】:

    如果你想用git克隆而不是通过gcloud运行,你可以运行:

    git config --global credential.helper gcloud.sh
    

    ...然后这将起作用:

    git clone https://source.developers.google.com/p/$PROJECT/r/$REPO
    

    【讨论】:

      【解决方案3】:

      如果像我这样的人试图将其作为 Dockerfile 的一部分,经过一段时间的努力,我只能设法让它像这样工作:

      RUN gcloud auth activate-service-account --key-file KEY_FILE ; \
          gcloud source repos clone default ~/my_repo
      

      正如你所看到的,让它成为同一个 RUN 命令的一部分是关键,否则它会一直失败

      ERROR: (gcloud.source.repos.clone) You do not currently have an active account selected.
      

      【讨论】:

        【解决方案4】:
        1. 为实例启用对“Cloud Source Repositories”云 API 的访问。您应该在创建或编辑 Admin console 中的实例时执行此操作
        2. 从实例内部的外壳,执行gcloud source repos clone <repo_name_in_cloud_source> <target_path_to_clone_into>

        【讨论】:

          【解决方案5】:

          如果您在 GCE 上运行,请利用需要更少代码行的新身份验证方法。

          创建 VM 实例时,在“访问和安全”下,将“云平台”设置为“启用”。

          那么验证码就是这么简单:

          from oauth2client.client import GoogleCredentials
          credentials = GoogleCredentials.get_application_default()
          http = credentials.authorize(httplib2.Http())
          

          https://developers.google.com/identity/protocols/application-default-credentials

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-12-17
            • 1970-01-01
            • 2022-11-08
            • 2020-05-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多