Skip to content

Commit

Permalink
Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set
Browse files Browse the repository at this point in the history
* Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set

* First look for AE's project id, then GOOGLE_APPLICATION_CREDENTIALS'
  • Loading branch information
mziccard authored and aozarov committed Apr 4, 2016
1 parent 59cdf37 commit d17e325
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
1. Project ID supplied when building the service options
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
3. App Engine project ID
4. Google Cloud SDK project ID
5. Compute Engine project ID
4. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
5. Google Cloud SDK project ID
6. Compute Engine project ID

Authentication
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
import com.google.common.io.Files;
import com.google.gcloud.spi.ServiceRpcFactory;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -378,7 +383,10 @@ protected String defaultHost() {
protected String defaultProject() {
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
if (projectId == null) {
projectId = getAppEngineProjectId();
projectId = appEngineProjectId();
}
if (projectId == null) {
projectId = serviceAccountProjectId();
}
return projectId != null ? projectId : googleCloudProjectId();
}
Expand Down Expand Up @@ -461,7 +469,7 @@ private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
}

protected static String getAppEngineProjectId() {
protected static String appEngineProjectId() {
try {
Class<?> factoryClass =
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
Expand All @@ -479,6 +487,20 @@ protected static String getAppEngineProjectId() {
}
}

protected static String serviceAccountProjectId() {
String project = null;
String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
if(credentialsPath != null) {
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
JSONObject json = new JSONObject(new JSONTokener(credentialsStream));
project = json.getString("project_id");
} catch (IOException | JSONException ex) {
// ignore
}
}
return project;
}

@SuppressWarnings("unchecked")
public ServiceT service() {
if (service == null) {
Expand Down

0 comments on commit d17e325

Please sign in to comment.