Description
Wrong gcloud command configuration is loaded
Problem
My ~/.config/gcloud/configurations/config_default
contents:
[core]
account = ***REDACTED***@gmail.com
project = gcpnext2016-swast
[compute]
zone = us-central1-a
region = us-central1
My active configuration:
$ gcloud config list
Your active configuration is: [swast-test-flexible]
[app]
suppress_change_warning = true
[compute]
region = us-central1
zone = us-central1-b
[core]
account = [email protected]
disable_usage_reporting = False
project = swast-test-flexible
[metrics]
command_name = gcloud.config.list
When I run the Bookshelf application, which uses Application Default Credentials locally with mvn -Plocal clean jetty:run-exploded
and I connect to http://localhost:8080, I see data from my gcpnext2016-swast
project, not the swast-test-flexible
project like I expect.
Steps to reproduce
- Run the Bookshelf application, which uses Application Default Credentials locally with
mvn -Plocal clean jetty:run-exploded
and I connect to http://localhost:8080 - Add a few books.
- Run
gcloud init
. - Create a new configuration instead of modifying the default, and select a different project this time.
- Run the Bookshelf application again.
- Notice that you still see the same data from the project selected in the default configuration, not the new configuration that you just activated.
Suspected root cause
gcloud-java uses default configuration rather than the currently selected one to figure out which project to send RPCs to. https://github.com/GoogleCloudPlatform/gcloud-java/blob/d03e5a7d80dc17d9012476557015e4f44be64687/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java#L394
Project ID is not extracted from service keys
Problem
When I use a service key, with the Bookshelf app command
GOOGLE_APPLICATION_CREDENTIALS=$HOME/src/service-keys/swast-test-flexible.json mvn -Plocal clean jetty:run-exploded
I get an exception when my default gcloud command configuration has a different project ID than the service key.
Caused by: com.google.gcloud.datastore.DatastoreException: Unauthorized.
Steps to reproduce
- Create a JSON service key for a project (a different project than what the default gcloud command configuration is set to).
- Run the Bookshelf application using this service key.
GOOGLE_APPLICATION_CREDENTIALS=path/to/key.json mvn -Plocal clean jetty:run-exploded
- Observe an unexpected
com.google.gcloud.datastore.DatastoreException
.
Suspected root cause
The defaultProjectId is not extracting the project information from the service key, as I would expect it to. Note that a key is of the format:
{
"type": "service_account",
"project_id": "swast-test-flexible", <-- gcloud-java should be using this, but it's not.
"private_key_id": "abcdedfghijklmnop",
"private_key": "-----BEGIN PRIVATE KEY-----\nBLAHblahBLAH\n-----END PRIVATE KEY-----\n",
"client_email": "getting-started-java-laptop@swast-test-flexible.iam.gserviceaccount.com",
"client_id": "1234567890",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/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/getting-started-java-laptop%40swast-test-flexible.iam.gserviceaccount.com"
}