Skip to content

Commit d17e325

Browse files
mziccardaozarov
authored andcommitted
Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set
* Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set * First look for AE's project id, then GOOGLE_APPLICATION_CREDENTIALS'
1 parent 59cdf37 commit d17e325

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
8484
1. Project ID supplied when building the service options
8585
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
8686
3. App Engine project ID
87-
4. Google Cloud SDK project ID
88-
5. Compute Engine project ID
87+
4. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
88+
5. Google Cloud SDK project ID
89+
6. Compute Engine project ID
8990

9091
Authentication
9192
--------------

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@
3030
import com.google.common.io.Files;
3131
import com.google.gcloud.spi.ServiceRpcFactory;
3232

33+
import org.json.JSONException;
34+
import org.json.JSONObject;
35+
import org.json.JSONTokener;
36+
3337
import java.io.BufferedReader;
3438
import java.io.File;
39+
import java.io.FileInputStream;
3540
import java.io.FileNotFoundException;
3641
import java.io.FileReader;
3742
import java.io.IOException;
@@ -378,7 +383,10 @@ protected String defaultHost() {
378383
protected String defaultProject() {
379384
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
380385
if (projectId == null) {
381-
projectId = getAppEngineProjectId();
386+
projectId = appEngineProjectId();
387+
}
388+
if (projectId == null) {
389+
projectId = serviceAccountProjectId();
382390
}
383391
return projectId != null ? projectId : googleCloudProjectId();
384392
}
@@ -461,7 +469,7 @@ private static boolean isWindows() {
461469
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
462470
}
463471

464-
protected static String getAppEngineProjectId() {
472+
protected static String appEngineProjectId() {
465473
try {
466474
Class<?> factoryClass =
467475
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
@@ -479,6 +487,20 @@ protected static String getAppEngineProjectId() {
479487
}
480488
}
481489

490+
protected static String serviceAccountProjectId() {
491+
String project = null;
492+
String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
493+
if(credentialsPath != null) {
494+
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
495+
JSONObject json = new JSONObject(new JSONTokener(credentialsStream));
496+
project = json.getString("project_id");
497+
} catch (IOException | JSONException ex) {
498+
// ignore
499+
}
500+
}
501+
return project;
502+
}
503+
482504
@SuppressWarnings("unchecked")
483505
public ServiceT service() {
484506
if (service == null) {

0 commit comments

Comments
 (0)