Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set #845

Merged
merged 2 commits into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ 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
3. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable

This comment was marked as spam.

4. App Engine project ID
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 = serviceAccountProjectId();
}
if (projectId == null) {
projectId = appEngineProjectId();
}
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)) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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