How to authenticate google APIs with different service account credentials?

一个人想着一个人 提交于 2019-12-04 14:54:05
Mark

Configuration in the Cloud SDK is global for the user, but you can specify what aspects of that config to use on a per command basis. To accomplish what you are trying to do you can:

gcloud auth activate-service-account foo@developer.gserviceaccount.com --key-file ...
gcloud auth activate-service-account bar@developer.gserviceaccount.com --key-file ...

At this point, both sets of credentials are in your global credentials store. Now you can run:

gcloud --account foo@developer.gserviceaccount.com some-command
gcloud --account bar@developer.gserviceaccount.com some-command

in parallel, and each will use the given account without interfering.


A larger extension of this is 'configurations' which do the same thing, but for your entire set of config (including settings like account and project).

# Create first configuration
gcloud config configurations create myconfig
gcloud config configurations activate myconfig
gcloud config set account foo@developer.gserviceaccount.com
gcloud config set project foo

# Create second configuration
gcloud config configurations create anotherconfig
gcloud config configurations activate anotherconfig
gcloud config set account bar@developer.gserviceaccount.com
gcloud config set project bar

And you can say which configuration to use on a per command basis.

gcloud --configuration myconfig some-command
gcloud --configuration anotherconfig some-command

You can read more about configurations by running: gcloud topic configurations


All properties have corresponding environment variables that allow you to set that particular property for a single command invocation or for a terminal session. They take the form:

CLOUDSDK_<SECTION>_<PROPERTY>

for example: CLOUDSDK_CORE_ACCOUNT

You can see all the available config settings by running: gcloud help config

The equivalent of the --configuration flag is: CLOUDSDK_ACTIVE_CONFIG_NAME


If you really want complete isolation, you can also change the Cloud SDK's config directory by setting CLOUDSDK_CONFIG to a directory of your choosing. Note that if you do this, the config is completely separate including the credential store, all configurations, logs, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!