How to specify the GCP Credential Location in application.properties file (for using the Pub/Sub in GCP)?

Viewed 7085

This seems straightforward to do that passing the Service Account key file (generated from the GCP console) by specifying the file location in the application.properties file. However, I tried all the following options:

1. spring.cloud.gcp.credentials.location=file:/home/my_user_id/mp6key.json
2. spring.cloud.gcp.credentials.location=file:src/main/resources/mp6key.json
3. spring.cloud.gcp.credentials.location=file:./main/resources/mp6key.json
4. spring.cloud.gcp.credentials.location=file:/src/main/resources/mp6key.json

It all ended up with the same error:

java.io.FileNotFoundException: /home/my_user_id/mp6key.json (No such file or directory)

Could anyone advise where I should put the key file and then how should I specify the path to the file properly?

The same programs run successfully in Ecplise with messages published and subscribed using the Pub/Sub processing from GCP (using the Project Id/Service Account key generated in GCP), but now stuck with the above issue after deployed to run on GCP.

5 Answers

As mentioned in the official documentation, the credentials file can be obtained from a number of different locations such as the file system, classpath, URL, etc.

for example, if the service account key file is stored in the classpath as src/main/resources/key.json, pass the following property

spring.cloud.gcp.credentials.location=classpath:key.json

if the key file is stored somewhere else in your local file system, use the file prefix in the property value

spring.cloud.gcp.credentials.location=file:<path to key file>

My line looks like this:

spring.cloud.gcp.credentials.location=file:src/main/resources/[my_json_file]

And this works.

The following also works if I put it in the root of the project directory:

spring.cloud.gcp.credentials.location=file:./[my_json_file]

Have you tried to follow this quickstart? Please, try to follow it thoughtfully and explain if you get any error finishing the quickstart.

Anyway, before running your Java script, try running on the console the following (please modify with the exact path where you store your key):

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/mp6key.json"

How are you authenticating your credentials in your Java script?

My answer is easy: if you run you code on GCP, you don't have to use service account key file. Problem eliminated, problem solved!

More seriously, have a look on service identity. I don't know what is your current service (Compute? Function? Cloud Run?). Anyway, you can attach any service account on GCP components. Then, when you code, simply use the default credential. Automatically the component identity is loaded. No key to manage, no key to store securely, no key to rotate!

If you provide more detail on your target platform, I could provide your some guidance to achieve this.

Keep in mind that the service account key file are designed to be used by automatic apps (w/o user account involved) hosted outside GCP (on prem, other Cloud Provider, a CI/CD, Apigee,...)

UPDATE

When you use your personal account, you can also use the default credential.

  • Install gcloud SDK on your computer
  • Use the command gcloud auth application-default login
  • Follow the instructions
  • Enjoy!

If it doesn't work, get the <path> displayed after the login command and set this value in the environment variable named GOOGLE_APPLICATION_CREDENTIALS.

If you definitively want to use service account key file (which are a security issue for the previous reason, but...), you can use it locally

  • Either set the json key file path into the GOOGLE_APPLICATION_CREDENTIALS environment variable
  • Or run this command gcloud auth activate-service-account --key-file=<path to your json key file>

Provided your file is in the resources folder try

file://mp6key.json

using file:// instead of file:/ works for me at least

Related