Skip to content

Commit 614591e

Browse files
committed
---
yaml --- r: 6569 b: refs/heads/tswast-patch-1 c: 0a05851 h: refs/heads/master i: 6567: 14287c0
1 parent d23e414 commit 614591e

9 files changed

Lines changed: 137 additions & 61 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 2c90245497df0c9674829bcfd02ec5adb294b295
60+
refs/heads/tswast-patch-1: 0a05851b3ad9d6138f57c726ea888d4675ef375b
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
6969
1. Project ID supplied when building the service options
7070
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
7171
3. App Engine project ID
72-
4. Google Cloud SDK project ID
73-
5. Compute Engine project ID
72+
4. Compute Engine project ID
73+
5. Google Cloud SDK project ID
7474

7575
Authentication
7676
--------------

branches/tswast-patch-1/gcloud-java-core/pom.xml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@
2020
<dependency>
2121
<groupId>com.google.auth</groupId>
2222
<artifactId>google-auth-library-credentials</artifactId>
23-
<version>0.3.1</version>
23+
<version>0.1.0</version>
2424
</dependency>
2525
<dependency>
2626
<groupId>com.google.auth</groupId>
2727
<artifactId>google-auth-library-oauth2-http</artifactId>
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>
28+
<version>0.1.0</version>
3529
</dependency>
3630
<dependency>
3731
<groupId>com.google.http-client</groupId>

branches/tswast-patch-1/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
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;
2223
import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
24+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
2325
import com.google.api.client.http.HttpRequestInitializer;
2426
import com.google.api.client.http.HttpTransport;
27+
import com.google.api.client.http.javanet.NetHttpTransport;
2528
import com.google.api.client.json.jackson.JacksonFactory;
2629
import com.google.auth.http.HttpCredentialsAdapter;
2730
import com.google.auth.oauth2.GoogleCredentials;
@@ -30,6 +33,7 @@
3033
import java.io.IOException;
3134
import java.io.InputStream;
3235
import java.io.Serializable;
36+
import java.security.GeneralSecurityException;
3337
import java.security.PrivateKey;
3438
import java.util.Objects;
3539
import java.util.Set;
@@ -160,6 +164,55 @@ public RestorableState<AuthCredentials> capture() {
160164
}
161165
}
162166

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

165218
private GoogleCredentials googleCredentials;
@@ -225,6 +278,11 @@ public static AuthCredentials createForAppEngine() {
225278
return AppEngineAuthCredentials.INSTANCE;
226279
}
227280

281+
public static AuthCredentials createForComputeEngine()
282+
throws IOException, GeneralSecurityException {
283+
return new ComputeEngineAuthCredentials();
284+
}
285+
228286
/**
229287
* Returns the Application Default Credentials.
230288
*
@@ -279,4 +337,13 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
279337
public static AuthCredentials noCredentials() {
280338
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
281339
}
340+
341+
static ComputeCredential getComputeCredential() throws IOException, GeneralSecurityException {
342+
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
343+
// Try to connect using Google Compute Engine service account credentials.
344+
ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
345+
// Force token refresh to detect if we are running on Google Compute Engine.
346+
credential.refreshToken();
347+
return credential;
348+
}
282349
}

branches/tswast-patch-1/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ public HttpTransport create() {
109109
// Maybe not on App Engine
110110
}
111111
}
112+
// Consider Compute
113+
try {
114+
return AuthCredentials.getComputeCredential().getTransport();
115+
} catch (Exception e) {
116+
// Maybe not on GCE
117+
}
112118
return new NetHttpTransport();
113119
}
114120
}
@@ -336,7 +342,7 @@ protected boolean projectIdRequired() {
336342
}
337343

338344
private static AuthCredentials defaultAuthCredentials() {
339-
// Consider App Engine.
345+
// Consider App Engine. This will not be needed once issue #21 is fixed.
340346
if (appEngineAppId() != null) {
341347
try {
342348
return AuthCredentials.createForAppEngine();
@@ -348,8 +354,16 @@ private static AuthCredentials defaultAuthCredentials() {
348354
try {
349355
return AuthCredentials.createApplicationDefaults();
350356
} catch (Exception ex) {
351-
return AuthCredentials.noCredentials();
357+
// fallback to old-style
352358
}
359+
360+
// Consider old-style Compute. This will not be needed once issue #21 is fixed.
361+
try {
362+
return AuthCredentials.createForComputeEngine();
363+
} catch (Exception ignore) {
364+
// Maybe not on GCE
365+
}
366+
return AuthCredentials.noCredentials();
353367
}
354368

355369
protected static String appEngineAppId() {
@@ -369,6 +383,19 @@ protected String defaultProject() {
369383
}
370384

371385
protected static String googleCloudProjectId() {
386+
try {
387+
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
388+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
389+
connection.setRequestProperty("X-Google-Metadata-Request", "True");
390+
InputStream input = connection.getInputStream();
391+
if (connection.getResponseCode() == 200) {
392+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
393+
return reader.readLine();
394+
}
395+
}
396+
} catch (IOException ignore) {
397+
// ignore
398+
}
372399
File configDir;
373400
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
374401
configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
@@ -377,52 +404,38 @@ protected static String googleCloudProjectId() {
377404
} else {
378405
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
379406
}
380-
FileReader fileReader = null;
407+
FileReader fileReader;
381408
try {
382409
fileReader = new FileReader(new File(configDir, "configurations/config_default"));
383410
} catch (FileNotFoundException newConfigFileNotFoundEx) {
384411
try {
385412
fileReader = new FileReader(new File(configDir, "properties"));
386413
} catch (FileNotFoundException oldConfigFileNotFoundEx) {
387-
// ignore
414+
// return null if we can't find config file
415+
return null;
388416
}
389417
}
390-
if (fileReader != null) {
391-
try (BufferedReader reader = new BufferedReader(fileReader)) {
392-
String line;
393-
String section = null;
394-
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
395-
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
396-
while ((line = reader.readLine()) != null) {
397-
if (line.isEmpty() || line.startsWith(";")) {
398-
continue;
399-
}
400-
line = line.trim();
401-
Matcher matcher = sectionPattern.matcher(line);
418+
try (BufferedReader reader = new BufferedReader(fileReader)) {
419+
String line;
420+
String section = null;
421+
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
422+
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
423+
while ((line = reader.readLine()) != null) {
424+
if (line.isEmpty() || line.startsWith(";")) {
425+
continue;
426+
}
427+
line = line.trim();
428+
Matcher matcher = sectionPattern.matcher(line);
429+
if (matcher.matches()) {
430+
section = matcher.group(1);
431+
} else if (section == null || section.equals("core")) {
432+
matcher = projectPattern.matcher(line);
402433
if (matcher.matches()) {
403-
section = matcher.group(1);
404-
} else if (section == null || section.equals("core")) {
405-
matcher = projectPattern.matcher(line);
406-
if (matcher.matches()) {
407-
return matcher.group(1);
408-
}
434+
return matcher.group(1);
409435
}
410436
}
411-
} catch (IOException ex) {
412-
// ignore
413-
}
414-
}
415-
try {
416-
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
417-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
418-
connection.setRequestProperty("X-Google-Metadata-Request", "True");
419-
InputStream input = connection.getInputStream();
420-
if (connection.getResponseCode() == 200) {
421-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
422-
return reader.readLine();
423-
}
424437
}
425-
} catch (IOException ignore) {
438+
} catch (IOException ex) {
426439
// ignore
427440
}
428441
// return null if can't determine

branches/tswast-patch-1/gcloud-java-datastore/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
<artifactId>google-api-services-datastore-protobuf</artifactId>
2828
<version>v1beta2-rev1-2.1.2</version>
2929
<scope>compile</scope>
30-
<exclusions>
31-
<exclusion>
32-
<groupId>com.google.api-client</groupId>
33-
<artifactId>google-api-client</artifactId>
34-
</exclusion>
35-
</exclusions>
3630
</dependency>
3731
<dependency>
3832
<groupId>junit</groupId>

branches/tswast-patch-1/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.json.JSONTokener;
4343

4444
import java.net.InetAddress;
45+
import java.net.SocketTimeoutException;
4546
import java.net.URL;
4647
import java.util.HashMap;
4748
import java.util.Map;
@@ -121,9 +122,15 @@ private static DatastoreRpcException translate(DatastoreException exception) {
121122
if (reason == null) {
122123
reason = HTTP_STATUS_TO_REASON.get(exception.getCode());
123124
}
124-
return reason != null
125-
? new DatastoreRpcException(reason)
126-
: new DatastoreRpcException("Unknown", exception.getCode(), false, message);
125+
if (reason != null) {
126+
return new DatastoreRpcException(reason);
127+
} else {
128+
boolean retryable = false;
129+
if (exception.getCause() instanceof SocketTimeoutException) {
130+
retryable = true;
131+
}
132+
return new DatastoreRpcException("Unknown", exception.getCode(), retryable, message);
133+
}
127134
}
128135

129136
@Override

branches/tswast-patch-1/gcloud-java-storage/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
<groupId>com.google.guava</groupId>
3333
<artifactId>guava-jdk5</artifactId>
3434
</exclusion>
35-
<exclusion>
36-
<groupId>com.google.api-client</groupId>
37-
<artifactId>google-api-client</artifactId>
38-
</exclusion>
3935
</exclusions>
4036
</dependency>
4137
<dependency>

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.io.ByteArrayOutputStream;
6969
import java.io.IOException;
7070
import java.io.InputStream;
71+
import java.net.SocketTimeoutException;
7172
import java.util.ArrayList;
7273
import java.util.Iterator;
7374
import java.util.List;
@@ -101,7 +102,11 @@ private static StorageException translate(IOException exception) {
101102
&& ((GoogleJsonResponseException) exception).getDetails() != null) {
102103
translated = translate(((GoogleJsonResponseException) exception).getDetails());
103104
} else {
104-
translated = new StorageException(0, exception.getMessage(), false);
105+
boolean retryable = false;
106+
if (exception instanceof SocketTimeoutException) {
107+
retryable = true;
108+
}
109+
translated = new StorageException(0, exception.getMessage(), retryable);
105110
}
106111
translated.initCause(exception);
107112
return translated;

0 commit comments

Comments
 (0)