Gcloud - 无法使用一个服务帐户配置多个虚拟机

Ada*_*wka 5 cloud google-cloud-platform gcloud google-iam

我正在使用 Gcloud 运行 Prow(持续集成服务器)。我的工作之一是创建一个虚拟机,执行一些测试,然后删除该实例。我使用服务帐户来创建虚拟机,运行测试。

#!/bin/bash

set -o errexit

cleanup() {
    gcloud compute instances delete kyma-integration-test-${RANDOM_ID}
}


gcloud config set project ...
gcloud auth activate-service-account --key-file ...

gcloud compute instances create <vm_name> \
    --metadata enable-oslogin=TRUE \
    --image debian-9-stretch-v20181009 \
    --image-project debian-cloud --machine-type n1-standard-4 --boot-disk-size 20 \

trap cleanup exit

gcloud compute scp --strict-host-key-checking=no --quiet <script.sh> <vm_name>:~/<script.sh>

gcloud compute ssh --quiet <vm_name> -- ./<script.sh>
Run Code Online (Sandbox Code Playgroud)

一段时间后,我收到以下错误:

ERROR: (gcloud.compute.scp) INVALID_ARGUMENT: Login profile size exceeds 32 KiB. Delete profile values to make additional space.
Run Code Online (Sandbox Code Playgroud)

事实上,对于该服务帐户,describe命令返回大量数据,例如sshPublicKeys节中约 70 个条目。

gcloud auth activate-service-account --key-file ... gcloud compute os-login describe-profile

大多数公钥引用已删除的虚拟机实例。如何执行此列表的清理?或者是否有可能根本不存储该公钥?

Jer*_*iah 6

永久的解决方案是使用--ssh-key-expire-after 30s. 您仍然需要使用上面的解决方案或更多一点像这样的命令功夫来清理现有的密钥(不使用 grep)。

for i in $(gcloud compute os-login ssh-keys list --format="table[no-heading](value.fingerprint)"); do 
  echo $i; 
  gcloud compute os-login ssh-keys remove --key $i || true; 
done
Run Code Online (Sandbox Code Playgroud)

注意:您必须使用有问题的帐户。gcloud config account activate ACCOUNT和/或gcloud auth activate-service-account --key-file=FILEgcloud auth login

脚本中需要一个新的 ssh 密钥:

# KEYNAME should be something like $HOME/.ssh/google_compute_engine
ssh-keygen -t rsa -N "" -f "${KEYNAME}" -C "${USERNAME}" || true
chmod 400 ${KEYNAME}*

cat > ssh-keys <<EOF
${USERNAME}:$(cat ${KEYNAME}.pub)
EOF
Run Code Online (Sandbox Code Playgroud)

测试这个解决方案:

while :; do
  USERNAME=testing@test-project.iam.gserviceaccount.com
  KEYNAME=~/.ssh/google_compute_engine
  rm -f ~/.ssh/google_compute_engine*
  ssh-keygen -t rsa -N "" -f "${KEYNAME}" -C "${USERNAME}" || true
  chmod 400 ${KEYNAME}*
  cat > ssh-keys <<EOF
  ${USERNAME}:$(cat ${KEYNAME}.pub)
EOF
  gcloud --project=test-project compute ssh --ssh-key-expire-after 30s one-bastion-to-rule-them-all -- date
  gcloud --project=test-project compute os-login ssh-keys list --format="table[no-heading](value.fingerprint)" \
    |wc -l
done
Run Code Online (Sandbox Code Playgroud)


Ali*_*oua 0

这些密钥存储在您的项目元数据中,您可以通过 Google Console UI 删除来删除它们