googleapis / googleapis/google-cloud-java

[google-auth-library-java] Add support for reading GOOGLE_APPLICATION_CREDENTIALS as a property

Open
#12,600 1 comment 0 reactions 1 assignee Claimed by @TimurSadykov View on GitHub
type: feature request
Dominant language
Java
Stars
2.1k
Forks
1.2k
Avg merge
1d 23h
Merged PRs (30d)
154

Description

In Java, we can have "system properties" as well as "environment variables" that keep our map between keys and string values.
We access system properties using the method **System.getProperty** while to access the environment variables we use **System.getenv**.
Different systems may use different approaches to pass configurations. For instance, Amazon Elastibewnsatck passes our variables to **Tomcat** as properties that will have to be accessed using System.getProperty (see link [here](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-cfg-softwaresettings.html)).

Currently, **DefaultCredentialsProvider** reads GOOGLE_APPLICATION_CREDENTIALS only using System.getenv (apart from using getWellKnownCredentialsFile).
It would be nice if it could also try to read the credentials from properties.

I was unable to send a pull request (permission denied), so here goes the draft code I think would easily fix the issue:
New method for DefaultCredentialsProvider:
```
String getEnvOrProperty(String name) {
String envValue = getEnv(name);
if (envValue == null || envValue.length() == 0) {
envValue = getProperty(name, "");
}
return envValue;
}
```
Change line 135 of DefaultCredentialsProvider#getDefaultCredentialsUnsynchronized from:
`String credentialsPath = getEnv(CREDENTIAL_ENV_VAR);`
to:
`String credentialsPath = getEnvOrProperty(CREDENTIAL_ENV_VAR);`

Another approach would be to modify **GoogleAuthUtils#getWellKnownCredentialsFile** to add a new clause based on getProperty and the default CREDENTIAL_ENV_VAR.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.