|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull;
|
20 | 20 |
|
21 | 21 | 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; |
25 | 22 | import com.google.api.client.http.HttpRequestInitializer;
|
26 | 23 | import com.google.api.client.http.HttpTransport;
|
27 |
| -import com.google.api.client.http.javanet.NetHttpTransport; |
28 | 24 | import com.google.api.client.json.jackson.JacksonFactory;
|
29 | 25 | import com.google.auth.http.HttpCredentialsAdapter;
|
30 | 26 | import com.google.auth.oauth2.GoogleCredentials;
|
31 | 27 |
|
32 | 28 | import java.io.IOException;
|
33 | 29 | import java.io.InputStream;
|
34 | 30 | import java.io.Serializable;
|
35 |
| -import java.security.GeneralSecurityException; |
36 | 31 | import java.security.PrivateKey;
|
37 | 32 | import java.util.Objects;
|
38 | 33 | import java.util.Set;
|
|
42 | 37 | */
|
43 | 38 | public abstract class AuthCredentials implements Restorable<AuthCredentials> {
|
44 | 39 |
|
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 |
| - |
84 | 40 | public static class ServiceAccountAuthCredentials extends AuthCredentials {
|
85 | 41 |
|
86 | 42 | private final String account;
|
@@ -163,55 +119,6 @@ public RestorableState<AuthCredentials> capture() {
|
163 | 119 | }
|
164 | 120 | }
|
165 | 121 |
|
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 |
| - |
215 | 122 | private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
|
216 | 123 |
|
217 | 124 | private GoogleCredentials googleCredentials;
|
@@ -264,21 +171,12 @@ public RestorableState<AuthCredentials> capture() {
|
264 | 171 | protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
|
265 | 172 | Set<String> scopes);
|
266 | 173 |
|
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 |
| - |
276 | 174 | /**
|
277 | 175 | * Returns the Application Default Credentials.
|
278 | 176 | *
|
279 | 177 | * <p>Returns the Application Default Credentials which are credentials that identify and
|
280 | 178 | * 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 |
282 | 180 | * variable GOOGLE_APPLICATION_CREDENTIALS.
|
283 | 181 | * </p>
|
284 | 182 | *
|
@@ -327,13 +225,4 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
|
327 | 225 | public static AuthCredentials noCredentials() {
|
328 | 226 | return ServiceAccountAuthCredentials.NO_CREDENTIALS;
|
329 | 227 | }
|
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 |
| - } |
339 | 228 | }
|
0 commit comments