Skip to content

Commit b9f21da

Browse files
committed
Merge pull request #488 from mziccard/bigquery
Merge master into bigquery
2 parents a46fec8 + 683ca95 commit b9f21da

File tree

15 files changed

+303
-246
lines changed

15 files changed

+303
-246
lines changed

java-core/google-cloud-core/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ If you are using Maven, add this to your pom.xml file
1717
<dependency>
1818
<groupId>com.google.gcloud</groupId>
1919
<artifactId>gcloud-java-core</artifactId>
20-
<version>0.0.10</version>
20+
<version>0.1.0</version>
2121
</dependency>
2222
```
2323
If you are using Gradle, add this to your dependencies
2424
```Groovy
25-
compile 'com.google.gcloud:gcloud-java-core:jar:0.0.10'
25+
compile 'com.google.gcloud:gcloud-java-core:jar:0.1.0'
2626
```
2727
If you are using SBT, add this to your dependencies
2828
```Scala
29-
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.10"
29+
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.0"
3030
```
3131

32+
Troubleshooting
33+
---------------
34+
35+
To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting).
36+
3237
Java Versions
3338
-------------
3439

@@ -39,7 +44,9 @@ Contributing
3944

4045
Contributions to this library are always welcome and highly encouraged.
4146

42-
See [CONTRIBUTING] for more information on how to get started.
47+
See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started.
48+
49+
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See [Code of Conduct][code-of-conduct] for more information.
4350

4451
Versioning
4552
----------
@@ -57,5 +64,6 @@ Apache 2.0 - See [LICENSE] for more information.
5764

5865

5966
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
67+
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
6068
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
6169
[cloud-platform]: https://cloud.google.com/

java-core/google-cloud-core/pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.google.gcloud</groupId>
1313
<artifactId>gcloud-java-pom</artifactId>
14-
<version>0.0.11-SNAPSHOT</version>
14+
<version>0.1.1-SNAPSHOT</version>
1515
</parent>
1616
<properties>
1717
<site.installationModule>gcloud-java-core</site.installationModule>
@@ -20,12 +20,18 @@
2020
<dependency>
2121
<groupId>com.google.auth</groupId>
2222
<artifactId>google-auth-library-credentials</artifactId>
23-
<version>0.1.0</version>
23+
<version>0.3.1</version>
2424
</dependency>
2525
<dependency>
2626
<groupId>com.google.auth</groupId>
2727
<artifactId>google-auth-library-oauth2-http</artifactId>
28-
<version>0.1.0</version>
28+
<version>0.3.1</version>
29+
<exclusions>
30+
<exclusion>
31+
<groupId>com.google.guava</groupId>
32+
<artifactId>guava-jdk5</artifactId>
33+
</exclusion>
34+
</exclusions>
2935
</dependency>
3036
<dependency>
3137
<groupId>com.google.http-client</groupId>

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

Lines changed: 83 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,17 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

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-
import com.google.api.client.http.HttpRequestInitializer;
26-
import com.google.api.client.http.HttpTransport;
27-
import com.google.api.client.http.javanet.NetHttpTransport;
28-
import com.google.api.client.json.jackson.JacksonFactory;
29-
import com.google.auth.http.HttpCredentialsAdapter;
21+
import com.google.auth.oauth2.AccessToken;
3022
import com.google.auth.oauth2.GoogleCredentials;
23+
import com.google.auth.oauth2.ServiceAccountCredentials;
3124

3225
import java.io.IOException;
3326
import java.io.InputStream;
3427
import java.io.Serializable;
35-
import java.security.GeneralSecurityException;
28+
import java.lang.reflect.Method;
3629
import java.security.PrivateKey;
30+
import java.util.Collection;
3731
import java.util.Objects;
38-
import java.util.Set;
3932

4033
/**
4134
* Credentials for accessing Google Cloud services.
@@ -45,8 +38,67 @@ public abstract class AuthCredentials implements Restorable<AuthCredentials> {
4538
private static class AppEngineAuthCredentials extends AuthCredentials {
4639

4740
private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials();
48-
private static final AppEngineAuthCredentialsState STATE =
49-
new AppEngineAuthCredentialsState();
41+
private static final AppEngineAuthCredentialsState STATE = new AppEngineAuthCredentialsState();
42+
43+
private static class AppEngineCredentials extends GoogleCredentials {
44+
45+
private final Object appIdentityService;
46+
private final Method getAccessToken;
47+
private final Method getAccessTokenResult;
48+
private final Collection<String> scopes;
49+
50+
AppEngineCredentials() {
51+
try {
52+
Class<?> factoryClass =
53+
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
54+
Method method = factoryClass.getMethod("getAppIdentityService");
55+
this.appIdentityService = method.invoke(null);
56+
Class<?> serviceClass =
57+
Class.forName("com.google.appengine.api.appidentity.AppIdentityService");
58+
Class<?> tokenResultClass = Class.forName(
59+
"com.google.appengine.api.appidentity.AppIdentityService$GetAccessTokenResult");
60+
this.getAccessTokenResult = serviceClass.getMethod("getAccessToken", Iterable.class);
61+
this.getAccessToken = tokenResultClass.getMethod("getAccessToken");
62+
this.scopes = null;
63+
} catch (Exception e) {
64+
throw new RuntimeException("Could not create AppEngineCredentials.", e);
65+
}
66+
}
67+
68+
AppEngineCredentials(Collection<String> scopes, AppEngineCredentials unscoped) {
69+
this.appIdentityService = unscoped.appIdentityService;
70+
this.getAccessToken = unscoped.getAccessToken;
71+
this.getAccessTokenResult = unscoped.getAccessTokenResult;
72+
this.scopes = scopes;
73+
}
74+
75+
/**
76+
* Refresh the access token by getting it from the App Identity service
77+
*/
78+
@Override
79+
public AccessToken refreshAccessToken() throws IOException {
80+
if (createScopedRequired()) {
81+
throw new IOException("AppEngineCredentials requires createScoped call before use.");
82+
}
83+
try {
84+
Object accessTokenResult = getAccessTokenResult.invoke(appIdentityService, scopes);
85+
String accessToken = (String) getAccessToken.invoke(accessTokenResult);
86+
return new AccessToken(accessToken, null);
87+
} catch (Exception e) {
88+
throw new IOException("Could not get the access token.", e);
89+
}
90+
}
91+
92+
@Override
93+
public boolean createScopedRequired() {
94+
return scopes == null || scopes.isEmpty();
95+
}
96+
97+
@Override
98+
public GoogleCredentials createScoped(Collection<String> scopes) {
99+
return new AppEngineCredentials(scopes, this);
100+
}
101+
}
50102

51103
private static class AppEngineAuthCredentialsState
52104
implements RestorableState<AuthCredentials>, Serializable {
@@ -70,9 +122,8 @@ public boolean equals(Object obj) {
70122
}
71123

72124
@Override
73-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
74-
Set<String> scopes) {
75-
return new AppIdentityCredential(scopes);
125+
public GoogleCredentials credentials() {
126+
return new AppEngineCredentials();
76127
}
77128

78129
@Override
@@ -86,8 +137,6 @@ public static class ServiceAccountAuthCredentials extends AuthCredentials {
86137
private final String account;
87138
private final PrivateKey privateKey;
88139

89-
private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials();
90-
91140
private static class ServiceAccountAuthCredentialsState
92141
implements RestorableState<AuthCredentials>, Serializable {
93142

@@ -103,9 +152,6 @@ private ServiceAccountAuthCredentialsState(String account, PrivateKey privateKey
103152

104153
@Override
105154
public AuthCredentials restore() {
106-
if (account == null && privateKey == null) {
107-
return NO_CREDENTIALS;
108-
}
109155
return new ServiceAccountAuthCredentials(account, privateKey);
110156
}
111157

@@ -130,23 +176,9 @@ public boolean equals(Object obj) {
130176
this.privateKey = checkNotNull(privateKey);
131177
}
132178

133-
ServiceAccountAuthCredentials() {
134-
account = null;
135-
privateKey = null;
136-
}
137-
138179
@Override
139-
protected HttpRequestInitializer httpRequestInitializer(
140-
HttpTransport transport, Set<String> scopes) {
141-
GoogleCredential.Builder builder = new GoogleCredential.Builder()
142-
.setTransport(transport)
143-
.setJsonFactory(new JacksonFactory());
144-
if (privateKey != null) {
145-
builder.setServiceAccountPrivateKey(privateKey);
146-
builder.setServiceAccountId(account);
147-
builder.setServiceAccountScopes(scopes);
148-
}
149-
return builder.build();
180+
public ServiceAccountCredentials credentials() {
181+
return new ServiceAccountCredentials(null, account, privateKey, null, null);
150182
}
151183

152184
public String account() {
@@ -163,56 +195,7 @@ public RestorableState<AuthCredentials> capture() {
163195
}
164196
}
165197

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-
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
198+
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216199

217200
private GoogleCredentials googleCredentials;
218201

@@ -250,9 +233,8 @@ public boolean equals(Object obj) {
250233
}
251234

252235
@Override
253-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
254-
Set<String> scopes) {
255-
return new HttpCredentialsAdapter(googleCredentials);
236+
public GoogleCredentials credentials() {
237+
return googleCredentials;
256238
}
257239

258240
@Override
@@ -261,18 +243,12 @@ public RestorableState<AuthCredentials> capture() {
261243
}
262244
}
263245

264-
protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
265-
Set<String> scopes);
246+
public abstract GoogleCredentials credentials();
266247

267248
public static AuthCredentials createForAppEngine() {
268249
return AppEngineAuthCredentials.INSTANCE;
269250
}
270251

271-
public static AuthCredentials createForComputeEngine()
272-
throws IOException, GeneralSecurityException {
273-
return new ComputeEngineAuthCredentials();
274-
}
275-
276252
/**
277253
* Returns the Application Default Credentials.
278254
*
@@ -319,21 +295,15 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
319295
*/
320296
public static ServiceAccountAuthCredentials createForJson(InputStream jsonCredentialStream)
321297
throws IOException {
322-
GoogleCredential tempCredentials = GoogleCredential.fromStream(jsonCredentialStream);
323-
return new ServiceAccountAuthCredentials(tempCredentials.getServiceAccountId(),
324-
tempCredentials.getServiceAccountPrivateKey());
325-
}
326-
327-
public static AuthCredentials noCredentials() {
328-
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
329-
}
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;
298+
GoogleCredentials tempCredentials = GoogleCredentials.fromStream(jsonCredentialStream);
299+
if (tempCredentials instanceof ServiceAccountCredentials) {
300+
ServiceAccountCredentials tempServiceAccountCredentials =
301+
(ServiceAccountCredentials) tempCredentials;
302+
return new ServiceAccountAuthCredentials(
303+
tempServiceAccountCredentials.getClientEmail(),
304+
tempServiceAccountCredentials.getPrivateKey());
305+
}
306+
throw new IOException(
307+
"The given JSON Credentials Stream is not for a service account credential.");
338308
}
339309
}

java-core/google-cloud-core/src/main/java/com/google/gcloud/ExceptionHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ void verifyCaller(Callable<?> callable) {
231231
}
232232
}
233233

234-
public Set<Class<? extends Exception>> getRetriableExceptions() {
234+
public Set<Class<? extends Exception>> retriableExceptions() {
235235
return retriableExceptions;
236236
}
237237

238-
public Set<Class<? extends Exception>> getNonRetriableExceptions() {
238+
public Set<Class<? extends Exception>> nonRetriableExceptions() {
239239
return nonRetriableExceptions;
240240
}
241241

@@ -262,7 +262,7 @@ boolean shouldRetry(Exception ex) {
262262
/**
263263
* Returns an instance which retry any checked exception and abort on any runtime exception.
264264
*/
265-
public static ExceptionHandler getDefaultInstance() {
265+
public static ExceptionHandler defaultInstance() {
266266
return DEFAULT_INSTANCE;
267267
}
268268

0 commit comments

Comments
 (0)