【问题标题】:Vagrant fails to authenticate with GCP service account in provisionVagrant 无法通过提供的 GCP 服务帐户进行身份验证
【发布时间】:2021-08-28 14:05:53
【问题描述】:

我尝试从 GCS 中获取一些文件,但没有按预期工作。

我创建了一个服务帐户并允许它访问目标资源。
当下面的脚本以交互方式执行时,一切正常。但是,在配置中执行时,我收到了以下警告和错误。

  1. 无法打开配置文件:[/root/.config/gcloud/configurations/config_default]。
    我认为这是因为根环境中不存在 .config 目录。
    我做了 .config 目录作为测试,但它并没有解决问题。

  2. 您目前没有选择有效的帐户。
    我不想以用户帐户身份进行身份验证,所以我认为我不必实现“gcloud auth login”。

Stacktrace 和脚本如下。

default: WARNING: Could not open the configuration file: [/root/.config/gcloud/configurations/config_default].
    default: ERROR: (gcloud.iam.service-accounts.keys.create) You do not currently have an active account selected.
    default: Please run:
    default: 
    default:   $ gcloud auth login
    default: 
    default: to obtain new credentials.
    default: 
    default: If you have already logged in with a different account:
    default: 
    default:     $ gcloud config set account ACCOUNT
    default: 
    default: to select an already authenticated account to use.
    default: ERROR: (gcloud.auth.activate-service-account) Could not read json file /root/key.json: Expecting value: line 1 column 1 (char 0)
    default: ServiceException: 401 Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.
    default: /tmp/vagrant-shell: line 45: : command not found
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
# Install Google Cloud SDK
## Add URI to package source list
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list

## Install packages needed
sudo apt install -y apt-transport-https ca-certificates gnupg

## Import Google Cloud public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -

## Update and Install Google Cloud SDK
sudo apt update && sudo apt install -y google-cloud-sdk

## Create service account key
cd ~ && gcloud iam service-accounts keys create key.json --iam-account=example@example.iam.gserviceaccount.com

## Activate service account
gcloud auth activate-service-account \
         example@example.iam.gserviceaccount.com \
         --key-file ~/key.json \
         --project example-project

  • 主机操作系统:MacOS 11.5.1
  • 来宾操作系统:Ubuntu 18.04 LTS(ubuntu/bionic64)
  • 流浪者 2.2.17
  • Virtualbox 6.1.26

【问题讨论】:

标签: linux ubuntu google-cloud-platform vagrant gcloud


【解决方案1】:

IIUC,错误不是gcloud auth activate-service-account 的结果,而是gcloud iam service-accounts keys create 的结果。

当您尝试gcloud iam service-accounts keys create 时,您尚未验证gcloud,因此您无权创建密钥。

您应该在流程之外创建密钥并(安全地)将密钥传输到 Vagrant 进程,以便它可以gcloud auth activate-service-account

【讨论】:

  • 我认为你是对的。但我不知道为什么一开始就不能创建密钥。
  • 因为您没有通过身份验证,所以不允许这样做。在任何gcloud 命令成功之前,您需要gcloud auth login 作为用户帐户或gcloud auth activate-service-account。但是,通常,要创建密钥,您需要在其他地方以用户帐户的身份登录,获取密钥,然后将其复制到您想要 gcloud auth activate-service-account 的机器上。
  • 谢谢。即使我创建了一个 json 密钥文件以使用服务帐户登录,我也不知道我需要作为用户帐户进行身份验证。我认为在提供时以非交互方式创建它是不现实的
  • 不,你没有。但是您的问题中的脚本中有 2 个 gcloud 命令。您尝试在之前您通过身份验证之前创建服务帐户,这是不可能的。如果您已经拥有密钥(按照我的建议在其他地方创建),那么您可以跳过无法在脚本中创建密钥的第一个 gckoud 命令,而只需使用第二个使用您必须已经创建的密钥进行身份验证的命令。
  • 我按照您的建议更改了工作流程,一切正常。谢谢你的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多