Daniel Watrous
Daniel Watrous

Reputation: 3831

gcloud auth activate-service-account from JSON to set current project

I created a service account and got a JSON formatted credential file back, which looks like this.

{
  "type": "service_account",
  "project_id": "myproject",
  "private_key_id": "db0cd27ba7d2acad610b854c04f0aa8ad67ad5fc",
  "private_key": "REDACTED",
  "client_email": "[email protected]",
  "client_id": "558691835510594518717",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gitlab-ci%40myproject.iam.gserviceaccount.com"
}

I can then activate this service account as follows

# gcloud auth activate-service-account --key-file myproject-5ddb0cd20b85.json
Activated service account credentials for: [[email protected]]

Since the JSON file includes the project_id, I expected this to be set in the active configuration, but it isn't.

# gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT                                             PROJECT  DEFAULT_ZONE  DEFAULT_REGION
default  True       [email protected]

Is there some way to set the project from the JSON file when I activate the service account?

Upvotes: 2

Views: 1904

Answers (2)

Maxim
Maxim

Reputation: 4441

The project_id from the Service Account JSON file is not supposed to be automatically set as the default project.

Having this functionality depends on the use case. Some API calls are done by using the project_id from the Service Account, and some use the default project currently set, so it wouldn’t be appropriate to generalize this.

As a workaround, I made a bash script that achieves what you’re looking for:

gcloud config set project $(cat 'key.json' | jq '.project_id' | sed -e 's/^"//' -e 's/"$//')

Upvotes: 1

Arkanil Dutta
Arkanil Dutta

Reputation: 45

Since the JSON file is actually used for authenticating and the project_id is used for authentication purposes, it is not kept in the active configuration.

Upvotes: 0

Related Questions