Я пытался воспроизвести вашу проблему и не смог. Мне удалось подключиться к service-account-b-instance
из service_account_a_instance
в качестве учетной записи службы и выполнять команды в качестве этой учетной записи службы.
Посмотрите на мои шаги ниже:
- создать учетную запись службы
service_account_a
создать экземпляр виртуальной машины, связанный с service_account_a_instance
:
$ gcloud compute instances create service-account-a-instance --zone=europe-west3-a --machine-type=n1-standard-1 --service-account=service-account-a@test-prj.iam.gserviceaccount.com --scopes=https://www.googleapis.com/auth/cloud-platform --metadata enable-oslogin=TRUE
Created [https://www.googleapis.com/compute/v1/projects/test-prj/zones/europe-west3-a/instances/service-account-a-instance].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
service-account-a-instance europe-west3-a n1-standard-1 10.156.0.14 35.XXX.75.XXX RUNNING
создать учетную запись службы service_account_b
создать экземпляр виртуальной машины service_account_b_instance
, связанный с service_account_b
:
$ gcloud compute instances create service-account-b-instance --zone=europe-west3-a --machine-type=n1-standard-1 --service-account=service-account-b@test-prj.iam.gserviceaccount.com --scopes=https://www.googleapis.com/auth/cloud-platform --metadata enable-oslogin=TRUE
Created [https://www.googleapis.com/compute/v1/projects/test-prj/zones/europe-west3-a/instances/service-account-b-instance].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
service-account-b-instance europe-west3-a n1-standard-1 10.156.0.16 35.XXX.255.XXX RUNNING
сгенерировать s sh ключей:
$ ssh-keygen
Generating public/private rsa key pair.
import s sh key:
$ gcloud compute os-login ssh-keys add --key-file id_rsa.pub
подключиться к экземпляру service-account-a-instance
:
$ gcloud compute ssh service-account-a-instance
Linux service-account-a-instance 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1 (2020-01-20) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Mar 2 14:34:51 2020 from 104.132.189.65
user_domain_com@service-account-a-instance:~$
подключиться к экземпляру service-account-b-instance
из service-account-a-instance
:
user_domain_com@service-account-a-instance:~$ gcloud compute ssh service-account-b-instance --project test-prj --zone europe-west3-a
...
ssh: connect to host 35.242.255.44 port 22: Connection timed out
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
включить s sh подключение по VPC network
-> Firewall
и повторите попытку:
user_domain_com@service-account-a-instance:~$ gcloud compute ssh service-account-b-instance --project test-prj --zone europe-west3-a
Permission denied (publickey).
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255].
добавьте роль roles/iam.serviceAccountUser
в service-account-a
и повторите попытку:
user_domain_com@service-account-a-instance:~$ gcloud compute ssh service-account-b-instance --project test-prj --zone europe-west3-a
Linux service-account-b-instance 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1 (2020-01-20) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Mar 2 16:52:28 2020 from 35.198.75.226
sa_116762935227008431464@service-account-b-instance:~$
sa_116762935227008431464@service-account-b-instance:~$ uname -a
Linux service-account-b-instance 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1 (2020-01-20) x86_64 GNU/Linux
и наконец это работает.