Skip to content

Commit 1322fc9

Browse files
author
Ajay Kannan
committed
Add back AppEngineCredentials
1 parent f995b12 commit 1322fc9

File tree

6 files changed

+59
-15
lines changed

6 files changed

+59
-15
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ There are multiple ways to authenticate to use Google Cloud services.
9696
`gcloud-java` looks for credentials in the following order, stopping once it finds credentials:
9797
9898
1. Credentials supplied when building the service options
99-
2. Key file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
100-
3. Google Cloud SDK credentials
101-
4. App Engine credentials
99+
2. App Engine credentials
100+
3. Key file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
101+
4. Google Cloud SDK credentials
102102
5. Compute Engine credentials
103103
104104
Google Cloud Datastore

gcloud-java-core/pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,6 @@
3333
</exclusion>
3434
</exclusions>
3535
</dependency>
36-
<dependency>
37-
<groupId>com.google.auth</groupId>
38-
<artifactId>google-auth-library-appengine</artifactId>
39-
<version>0.3.1</version>
40-
<exclusions>
41-
<exclusion>
42-
<groupId>com.google.guava</groupId>
43-
<artifactId>guava-jdk5</artifactId>
44-
</exclusion>
45-
</exclusions>
46-
</dependency>
4736
<dependency>
4837
<groupId>com.google.http-client</groupId>
4938
<artifactId>google-http-client</artifactId>

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

2121
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
22+
import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
2223
import com.google.api.client.http.HttpRequestInitializer;
2324
import com.google.api.client.http.HttpTransport;
2425
import com.google.api.client.json.jackson.JacksonFactory;
@@ -38,6 +39,45 @@
3839
*/
3940
public abstract class AuthCredentials implements Restorable<AuthCredentials> {
4041

42+
private static class AppEngineAuthCredentials extends AuthCredentials {
43+
44+
private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials();
45+
private static final AppEngineAuthCredentialsState STATE =
46+
new AppEngineAuthCredentialsState();
47+
48+
private static class AppEngineAuthCredentialsState
49+
implements RestorableState<AuthCredentials>, Serializable {
50+
51+
private static final long serialVersionUID = 3558563960848658928L;
52+
53+
@Override
54+
public AuthCredentials restore() {
55+
return INSTANCE;
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return getClass().getName().hashCode();
61+
}
62+
63+
@Override
64+
public boolean equals(Object obj) {
65+
return obj instanceof AppEngineAuthCredentialsState;
66+
}
67+
}
68+
69+
@Override
70+
protected HttpRequestInitializer httpRequestInitializer(
71+
HttpTransport transport, Set<String> scopes) {
72+
return new AppIdentityCredential(scopes);
73+
}
74+
75+
@Override
76+
public RestorableState<AuthCredentials> capture() {
77+
return STATE;
78+
}
79+
}
80+
4181
public static class ServiceAccountAuthCredentials extends AuthCredentials {
4282

4383
private final String account;
@@ -181,12 +221,16 @@ public RestorableState<AuthCredentials> capture() {
181221
protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
182222
Set<String> scopes);
183223

224+
public static AuthCredentials createForAppEngine() {
225+
return AppEngineAuthCredentials.INSTANCE;
226+
}
227+
184228
/**
185229
* Returns the Application Default Credentials.
186230
*
187231
* <p>Returns the Application Default Credentials which are credentials that identify and
188232
* authorize the whole application. This is the built-in service account if running on
189-
* Google App/Compute Engine or the credentials file can be read from the path in the environment
233+
* Google Compute Engine or the credentials file can be read from the path in the environment
190234
* variable GOOGLE_APPLICATION_CREDENTIALS.
191235
* </p>
192236
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ protected boolean projectIdRequired() {
356356
}
357357

358358
private static AuthCredentials defaultAuthCredentials() {
359+
// Consider App Engine.
360+
if (appEngineAppId() != null) {
361+
try {
362+
return AuthCredentials.createForAppEngine();
363+
} catch (Exception ignore) {
364+
// Maybe not on App Engine
365+
}
366+
}
367+
359368
try {
360369
return AuthCredentials.createApplicationDefaults();
361370
} catch (Exception ex) {

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public void testServiceOptions() throws Exception {
142142

143143
options = options.toBuilder()
144144
.namespace("ns1")
145+
.authCredentials(AuthCredentials.createForAppEngine())
145146
.retryParams(RetryParams.defaultInstance())
146147
.authCredentials(AuthCredentials.noCredentials())
147148
.force(true)

gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class SerializationTest {
7575
public void testServiceOptions() throws Exception {
7676
StorageOptions options = StorageOptions.builder()
7777
.projectId("p1")
78+
.authCredentials(AuthCredentials.createForAppEngine())
7879
.build();
7980
StorageOptions serializedCopy = serializeAndDeserialize(options);
8081
assertEquals(options, serializedCopy);

0 commit comments

Comments
 (0)