Skip to content

Commit a625ab9

Browse files
authored
fetch project id on app engine platforms (#2304)
* fetch project id on app engine platforms * resolve dependency conflict * reorder the sequence of fetching projectId * use appId to infer projectId for GAE J7 * remove appengine API reflection
1 parent 8f1eb48 commit a625ab9

2 files changed

Lines changed: 66 additions & 23 deletions

File tree

google-cloud-core/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@
3939
<version>20160810</version>
4040
<scope>compile</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.google.http-client</groupId>
44+
<artifactId>google-http-client</artifactId>
45+
<exclusions>
46+
<exclusion>
47+
<groupId>com.google.code.findbugs</groupId>
48+
<artifactId>jsr305</artifactId>
49+
</exclusion>
50+
</exclusions>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>com.google.code.findbugs</groupId>
55+
<artifactId>jsr305</artifactId>
56+
<version>3.0.0</version>
57+
</dependency>
4258
<dependency>
4359
<groupId>org.easymock</groupId>
4460
<artifactId>easymock</artifactId>

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

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
import static com.google.common.base.Preconditions.checkArgument;
2121
import static com.google.common.base.Preconditions.checkNotNull;
2222

23+
import com.google.api.client.http.GenericUrl;
24+
import com.google.api.client.http.HttpHeaders;
25+
import com.google.api.client.http.HttpRequest;
26+
import com.google.api.client.http.HttpRequestFactory;
27+
import com.google.api.client.http.HttpResponse;
28+
import com.google.api.client.http.HttpTransport;
29+
import com.google.api.client.http.javanet.NetHttpTransport;
2330
import com.google.api.core.ApiClock;
2431
import com.google.api.core.CurrentMillisClock;
2532
import com.google.api.core.InternalApi;
@@ -39,7 +46,6 @@
3946
import java.io.InputStream;
4047
import java.io.ObjectInputStream;
4148
import java.io.Serializable;
42-
import java.lang.reflect.Method;
4349
import java.nio.charset.Charset;
4450
import java.util.Locale;
4551
import java.util.Objects;
@@ -381,30 +387,51 @@ private static boolean isWindows() {
381387
}
382388

383389
protected static String getAppEngineProjectId() {
384-
try {
385-
Class<?> factoryClass =
386-
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
387-
Class<?> serviceClass =
388-
Class.forName("com.google.appengine.api.appidentity.AppIdentityService");
389-
Method method = factoryClass.getMethod("getAppIdentityService");
390-
Object appIdentityService = method.invoke(null);
391-
method = serviceClass.getMethod("getServiceAccountName");
392-
String serviceAccountName = (String) method.invoke(appIdentityService);
393-
int indexOfAtSign = serviceAccountName.indexOf('@');
394-
return serviceAccountName.substring(0, indexOfAtSign);
395-
} catch (ClassNotFoundException exception) {
396-
if (System.getProperty("com.google.appengine.runtime.version") != null) {
397-
// Could not resolve appengine classes under GAE environment.
398-
throw new RuntimeException("Google App Engine runtime detected "
399-
+ "(the environment variable \"com.google.appengine.runtime.version\" is set), "
400-
+ "but unable to resolve appengine-sdk classes. "
401-
+ "For more details see "
402-
+ "https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/APPENGINE.md");
390+
String projectId = null;
391+
if (PlatformInformation.isOnGAEStandard7()) {
392+
projectId = getAppEngineProjectIdFromAppId();
393+
} else {
394+
//for GAE flex and standard Java 8 environment
395+
projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
396+
if (projectId == null) {
397+
projectId = System.getenv("GCLOUD_PROJECT");
398+
}
399+
if (projectId == null) {
400+
projectId = getAppEngineProjectIdFromAppId();
401+
}
402+
if (projectId == null) {
403+
try {
404+
projectId = getAppEngineProjectIdFromMetadataServer();
405+
} catch (IOException ignore) {
406+
projectId = null;
407+
}
403408
}
404-
return null;
405-
} catch (Exception ignore) {
406-
return null;
407409
}
410+
return projectId;
411+
}
412+
413+
protected static String getAppEngineProjectIdFromAppId() {
414+
String projectId = getAppEngineAppId();
415+
if (projectId != null && projectId.contains(":")) {
416+
int colonIndex = projectId.indexOf(":");
417+
projectId = projectId.substring(colonIndex + 1);
418+
}
419+
return projectId;
420+
}
421+
422+
private static String getAppEngineProjectIdFromMetadataServer() throws IOException {
423+
String metadata = "http://metadata.google.internal";
424+
String projectIdURL = "/computeMetadata/v1/project/project-id";
425+
GenericUrl url = new GenericUrl(metadata + projectIdURL);
426+
427+
HttpTransport netHttpTransport = new NetHttpTransport();
428+
HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory();
429+
HttpRequest request = requestFactory.buildGetRequest(url)
430+
.setConnectTimeout(500)
431+
.setReadTimeout(500)
432+
.setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"));
433+
HttpResponse response = request.execute();
434+
return response.parseAsString();
408435
}
409436

410437
protected static String getServiceAccountProjectId() {

0 commit comments

Comments
 (0)