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 all commits
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
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)) {

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