Skip to content

Commit 60870c9

Browse files
authored
Merge pull request #122 from neozwu/use-gce-meta-for-gae8
use metadata server to get credentials for GAE 8 standard environment
2 parents 3c5fabd + 2ac95c1 commit 60870c9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class DefaultCredentialsProvider {
6969
static final String CLOUD_SHELL_ENV_VAR = "DEVSHELL_CLIENT_PORT";
7070

7171
static final String SKIP_APP_ENGINE_ENV_VAR = "GOOGLE_APPLICATION_CREDENTIALS_SKIP_APP_ENGINE";
72+
static final String SPECIFICATION_VERSION = System.getProperty("java.specification.version");
73+
static final String GAE_RUNTIME_VERSION = System.getProperty("com.google.appengine.runtime.version");
74+
static final String RUNTIME_JETTY_LOGGER = System.getProperty("org.eclipse.jetty.util.log.class");
7275

7376
static final String NO_GCE_CHECK_ENV_VAR = "NO_GCE_CHECK";
7477
static final String GCE_METADATA_HOST_ENV_VAR = "GCE_METADATA_HOST";
@@ -175,8 +178,8 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
175178
}
176179
}
177180

178-
// Then try App Engine
179-
if (credentials == null && !skipAppEngineCredentialsCheck()) {
181+
// Then try GAE 7 standard environment
182+
if (credentials == null && isOnGAEStandard7() && !skipAppEngineCredentialsCheck()) {
180183
credentials = tryGetAppEngineCredential();
181184
}
182185

@@ -186,7 +189,7 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
186189
credentials = tryGetCloudShellCredentials();
187190
}
188191

189-
// Then try Compute Engine
192+
// Then try Compute Engine and GAE 8 standard environment
190193
if (credentials == null) {
191194
credentials = tryGetComputeCredentials(transportFactory);
192195
}
@@ -283,6 +286,11 @@ private boolean skipAppEngineCredentialsCheck() {
283286
return skip;
284287
}
285288

289+
protected boolean isOnGAEStandard7() {
290+
return GAE_RUNTIME_VERSION != null
291+
&& (SPECIFICATION_VERSION.equals("1.7") || RUNTIME_JETTY_LOGGER == null);
292+
}
293+
286294
/*
287295
* Start of methods to allow overriding in the test code to isolate from the environment.
288296
*/

oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public void getDefaultCredentials_appEngineClassWithoutRuntime_NotFoundError() {
187187
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
188188
testProvider.addType(DefaultCredentialsProvider.APP_ENGINE_SIGNAL_CLASS,
189189
MockOffAppEngineSystemProperty.class);
190+
testProvider.setProperty("isOnGAEStandard7", "true");
190191

191192
try {
192193
testProvider.getDefaultCredentials(transportFactory);
@@ -203,6 +204,7 @@ public void getDefaultCredentials_appEngineRuntimeWithoutClass_throwsHelpfulLoad
203204
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
204205
testProvider.addType(DefaultCredentialsProvider.APP_ENGINE_SIGNAL_CLASS,
205206
MockAppEngineSystemProperty.class);
207+
testProvider.setProperty("isOnGAEStandard7", "true");
206208

207209
try {
208210
testProvider.getDefaultCredentials(transportFactory);
@@ -223,6 +225,7 @@ public void getDefaultCredentials_appEngineSkipWorks_retrievesCloudShellCredenti
223225
MockOffAppEngineSystemProperty.class);
224226
testProvider.setEnv(DefaultCredentialsProvider.CLOUD_SHELL_ENV_VAR,"9090");
225227
testProvider.setEnv(DefaultCredentialsProvider.SKIP_APP_ENGINE_ENV_VAR, "true");
228+
testProvider.setProperty("isOnGAEStanadard7", "true");
226229
GoogleCredentials credentials = testProvider.getDefaultCredentials(transportFactory);
227230
assertNotNull(credentials);
228231
assertTrue(credentials instanceof CloudShellCredentials);
@@ -578,6 +581,11 @@ Class<?> forName(String className) throws ClassNotFoundException {
578581
throw new ClassNotFoundException("TestDefaultCredentialProvider: Class not found.");
579582
}
580583

584+
@Override
585+
protected boolean isOnGAEStandard7() {
586+
return getProperty("isOnGAEStandard7", "false").equals("true");
587+
}
588+
581589
int getForNameCallCount() {
582590
return forNameCallCount;
583591
}

0 commit comments

Comments
 (0)