Skip to content

Commit 5d53adb

Browse files
author
Ajay Kannan
committed
Auth cleanup
1 parent 74a39cc commit 5d53adb

File tree

4 files changed

+42
-152
lines changed

4 files changed

+42
-152
lines changed

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

+1-112
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@
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.compute.ComputeCredential;
23-
import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
24-
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
2522
import com.google.api.client.http.HttpRequestInitializer;
2623
import com.google.api.client.http.HttpTransport;
27-
import com.google.api.client.http.javanet.NetHttpTransport;
2824
import com.google.api.client.json.jackson.JacksonFactory;
2925
import com.google.auth.http.HttpCredentialsAdapter;
3026
import com.google.auth.oauth2.GoogleCredentials;
3127

3228
import java.io.IOException;
3329
import java.io.InputStream;
3430
import java.io.Serializable;
35-
import java.security.GeneralSecurityException;
3631
import java.security.PrivateKey;
3732
import java.util.Objects;
3833
import java.util.Set;
@@ -42,45 +37,6 @@
4237
*/
4338
public abstract class AuthCredentials implements Restorable<AuthCredentials> {
4439

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

8642
private final String account;
@@ -163,55 +119,6 @@ public RestorableState<AuthCredentials> capture() {
163119
}
164120
}
165121

166-
private static class ComputeEngineAuthCredentials extends AuthCredentials {
167-
168-
private ComputeCredential computeCredential;
169-
170-
private static final ComputeEngineAuthCredentialsState STATE =
171-
new ComputeEngineAuthCredentialsState();
172-
173-
private static class ComputeEngineAuthCredentialsState
174-
implements RestorableState<AuthCredentials>, Serializable {
175-
176-
private static final long serialVersionUID = -6168594072854417404L;
177-
178-
@Override
179-
public AuthCredentials restore() {
180-
try {
181-
return new ComputeEngineAuthCredentials();
182-
} catch (IOException | GeneralSecurityException e) {
183-
throw new IllegalStateException(
184-
"Could not restore " + ComputeEngineAuthCredentials.class.getSimpleName(), e);
185-
}
186-
}
187-
188-
@Override
189-
public int hashCode() {
190-
return getClass().getName().hashCode();
191-
}
192-
193-
@Override
194-
public boolean equals(Object obj) {
195-
return obj instanceof ComputeEngineAuthCredentialsState;
196-
}
197-
}
198-
199-
ComputeEngineAuthCredentials() throws IOException, GeneralSecurityException {
200-
computeCredential = getComputeCredential();
201-
}
202-
203-
@Override
204-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
205-
Set<String> scopes) {
206-
return computeCredential;
207-
}
208-
209-
@Override
210-
public RestorableState<AuthCredentials> capture() {
211-
return STATE;
212-
}
213-
}
214-
215122
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216123

217124
private GoogleCredentials googleCredentials;
@@ -264,21 +171,12 @@ public RestorableState<AuthCredentials> capture() {
264171
protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
265172
Set<String> scopes);
266173

267-
public static AuthCredentials createForAppEngine() {
268-
return AppEngineAuthCredentials.INSTANCE;
269-
}
270-
271-
public static AuthCredentials createForComputeEngine()
272-
throws IOException, GeneralSecurityException {
273-
return new ComputeEngineAuthCredentials();
274-
}
275-
276174
/**
277175
* Returns the Application Default Credentials.
278176
*
279177
* <p>Returns the Application Default Credentials which are credentials that identify and
280178
* authorize the whole application. This is the built-in service account if running on
281-
* Google Compute Engine or the credentials file can be read from the path in the environment
179+
* Google App/Compute Engine or the credentials file can be read from the path in the environment
282180
* variable GOOGLE_APPLICATION_CREDENTIALS.
283181
* </p>
284182
*
@@ -327,13 +225,4 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
327225
public static AuthCredentials noCredentials() {
328226
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
329227
}
330-
331-
static ComputeCredential getComputeCredential() throws IOException, GeneralSecurityException {
332-
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
333-
// Try to connect using Google Compute Engine service account credentials.
334-
ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
335-
// Force token refresh to detect if we are running on Google Compute Engine.
336-
credential.refreshToken();
337-
return credential;
338-
}
339228
}

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

+16-19
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import static java.nio.charset.StandardCharsets.UTF_8;
2222

2323
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
24+
import com.google.api.client.googleapis.compute.ComputeCredential;
25+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
2426
import com.google.api.client.http.HttpRequest;
2527
import com.google.api.client.http.HttpRequestInitializer;
2628
import com.google.api.client.http.HttpTransport;
2729
import com.google.api.client.http.javanet.NetHttpTransport;
30+
import com.google.api.client.json.jackson.JacksonFactory;
2831
import com.google.common.collect.Iterables;
2932
import com.google.gcloud.spi.ServiceRpcFactory;
3033

@@ -41,6 +44,7 @@
4144
import java.lang.reflect.Method;
4245
import java.net.HttpURLConnection;
4346
import java.net.URL;
47+
import java.security.GeneralSecurityException;
4448
import java.util.Enumeration;
4549
import java.util.Locale;
4650
import java.util.Objects;
@@ -111,12 +115,22 @@ public HttpTransport create() {
111115
}
112116
// Consider Compute
113117
try {
114-
return AuthCredentials.getComputeCredential().getTransport();
118+
return getComputeHttpTransport();
115119
} catch (Exception e) {
116120
// Maybe not on GCE
117121
}
118122
return new NetHttpTransport();
119123
}
124+
125+
private static HttpTransport getComputeHttpTransport()
126+
throws IOException, GeneralSecurityException {
127+
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
128+
// Try to connect using Google Compute Engine service account credentials.
129+
ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
130+
// Force token refresh to detect if we are running on Google Compute Engine.
131+
credential.refreshToken();
132+
return transport;
133+
}
120134
}
121135

122136
/**
@@ -326,28 +340,11 @@ protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> ser
326340
}
327341

328342
private static AuthCredentials defaultAuthCredentials() {
329-
// Consider App Engine. This will not be needed once issue #21 is fixed.
330-
if (appEngineAppId() != null) {
331-
try {
332-
return AuthCredentials.createForAppEngine();
333-
} catch (Exception ignore) {
334-
// Maybe not on App Engine
335-
}
336-
}
337-
338343
try {
339344
return AuthCredentials.createApplicationDefaults();
340345
} catch (Exception ex) {
341-
// fallback to old-style
342-
}
343-
344-
// Consider old-style Compute. This will not be needed once issue #21 is fixed.
345-
try {
346-
return AuthCredentials.createForComputeEngine();
347-
} catch (Exception ignore) {
348-
// Maybe not on GCE
346+
return AuthCredentials.noCredentials();
349347
}
350-
return AuthCredentials.noCredentials();
351348
}
352349

353350
protected static String appEngineAppId() {

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,22 @@ public class SerializationTest {
133133

134134
@Test
135135
public void testServiceOptions() throws Exception {
136-
DatastoreOptions options = DatastoreOptions.builder()
137-
.authCredentials(AuthCredentials.createForAppEngine())
138-
.normalizeDataset(false)
139-
.projectId("ds1")
140-
.build();
136+
DatastoreOptions options =
137+
DatastoreOptions.builder()
138+
.authCredentials(AuthCredentials.createApplicationDefaults())
139+
.normalizeDataset(false)
140+
.projectId("ds1")
141+
.build();
141142
DatastoreOptions serializedCopy = serializeAndDeserialize(options);
142143
assertEquals(options, serializedCopy);
143144

144-
options = options.toBuilder()
145-
.namespace("ns1")
146-
.retryParams(RetryParams.getDefaultInstance())
147-
.authCredentials(AuthCredentials.noCredentials())
148-
.force(true)
149-
.build();
145+
options =
146+
options.toBuilder()
147+
.namespace("ns1")
148+
.retryParams(RetryParams.getDefaultInstance())
149+
.authCredentials(AuthCredentials.noCredentials())
150+
.force(true)
151+
.build();
150152
serializedCopy = serializeAndDeserialize(options);
151153
assertEquals(options, serializedCopy);
152154
}

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,21 @@ public class SerializationTest {
7373

7474
@Test
7575
public void testServiceOptions() throws Exception {
76-
StorageOptions options = StorageOptions.builder()
77-
.projectId("p1")
78-
.authCredentials(AuthCredentials.createForAppEngine())
79-
.build();
76+
StorageOptions options =
77+
StorageOptions.builder()
78+
.projectId("p1")
79+
.authCredentials(AuthCredentials.createApplicationDefaults())
80+
.build();
8081
StorageOptions serializedCopy = serializeAndDeserialize(options);
8182
assertEquals(options, serializedCopy);
8283

83-
options = options.toBuilder()
84-
.projectId("p2")
85-
.retryParams(RetryParams.getDefaultInstance())
86-
.authCredentials(AuthCredentials.noCredentials())
87-
.pathDelimiter(":")
88-
.build();
84+
options =
85+
options.toBuilder()
86+
.projectId("p2")
87+
.retryParams(RetryParams.getDefaultInstance())
88+
.authCredentials(AuthCredentials.noCredentials())
89+
.pathDelimiter(":")
90+
.build();
8991
serializedCopy = serializeAndDeserialize(options);
9092
assertEquals(options, serializedCopy);
9193
}

0 commit comments

Comments
 (0)