Skip to content

Commit 89f1704

Browse files
committed
---
yaml --- r: 2491 b: refs/heads/update-datastore c: a4c4273 h: refs/heads/master i: 2489: 23f7802 2487: ef50b33
1 parent 2ea473f commit 89f1704

7 files changed

Lines changed: 46 additions & 48 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3
66
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b
77
refs/heads/resource-manager: ebf4adc5ee835cd2086c4ac5b4e78d01a5a005a7
8-
refs/heads/update-datastore: 1e7fe54e8cef571608ab4992c2769b5cad5c615b
8+
refs/heads/update-datastore: a4c42736e359a52c0c43c46cb88a1d8860e2c297
99
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444
1010
refs/tags/v0.0.10: 207ebd2a3472fddee69fe1298eb90429e3306efd
1111
refs/tags/v0.0.11: ffbfba48a6426ff63c08ff2117e58681f251fbf2

branches/update-datastore/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ import com.google.gcloud.storage.StorageOptions;
164164
import java.nio.ByteBuffer;
165165
import java.nio.channels.WritableByteChannel;
166166
167-
StorageOptions options = StorageOptions.builder().projectId("project").build();
168-
Storage storage = options.service();
167+
Storage storage = StorageOptions.defaultInstance().service();
169168
BlobId blobId = BlobId.of("bucket", "blob_name");
170169
Blob blob = Blob.load(storage, blobId);
171170
if (blob == null) {

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.client.json.jackson.JacksonFactory;
2929
import com.google.auth.http.HttpCredentialsAdapter;
3030
import com.google.auth.oauth2.GoogleCredentials;
31+
import com.google.auth.oauth2.ServiceAccountCredentials;
3132

3233
import java.io.IOException;
3334
import java.io.InputStream;
@@ -212,7 +213,7 @@ public RestorableState<AuthCredentials> capture() {
212213
}
213214
}
214215

215-
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216+
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216217

217218
private GoogleCredentials googleCredentials;
218219

@@ -255,6 +256,15 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
255256
return new HttpCredentialsAdapter(googleCredentials.createScoped(scopes));
256257
}
257258

259+
public ServiceAccountAuthCredentials toServiceAccountCredentials() {
260+
if (googleCredentials instanceof ServiceAccountCredentials) {
261+
ServiceAccountCredentials credentials = (ServiceAccountCredentials) googleCredentials;
262+
return new ServiceAccountAuthCredentials(credentials.getClientEmail(),
263+
credentials.getPrivateKey());
264+
}
265+
return null;
266+
}
267+
258268
@Override
259269
public RestorableState<AuthCredentials> capture() {
260270
return STATE;

branches/update-datastore/gcloud-java-storage/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import com.google.gcloud.storage.StorageOptions;
6868
import java.nio.ByteBuffer;
6969
import java.nio.channels.WritableByteChannel;
7070

71-
Storage storage = StorageOptions.getDefaultInstance().service();
71+
Storage storage = StorageOptions.defaultInstance().service();
7272
Blob blob = new Blob(storage, "bucket", "blob_name");
7373
if (!blob.exists()) {
7474
storage2.create(blob.info(), "Hello, Cloud Storage!".getBytes(UTF_8));

branches/update-datastore/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import com.google.common.hash.Hashing;
4343
import com.google.common.io.BaseEncoding;
4444
import com.google.common.primitives.Ints;
45+
import com.google.gcloud.AuthCredentials;
46+
import com.google.gcloud.AuthCredentials.ApplicationDefaultAuthCredentials;
4547
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
4648
import com.google.gcloud.PageImpl;
4749
import com.google.gcloud.BaseService;
@@ -564,9 +566,15 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
564566
ServiceAccountAuthCredentials cred =
565567
(ServiceAccountAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
566568
if (cred == null) {
567-
checkArgument(options().authCredentials() instanceof ServiceAccountAuthCredentials,
568-
"Signing key was not provided and could not be derived");
569-
cred = (ServiceAccountAuthCredentials) this.options().authCredentials();
569+
AuthCredentials serviceCred = this.options().authCredentials();
570+
if (serviceCred instanceof ServiceAccountAuthCredentials) {
571+
cred = (ServiceAccountAuthCredentials) serviceCred;
572+
} else {
573+
if (serviceCred instanceof ApplicationDefaultAuthCredentials) {
574+
cred = ((ApplicationDefaultAuthCredentials) serviceCred).toServiceAccountCredentials();
575+
}
576+
}
577+
checkArgument(cred != null, "Signing key was not provided and could not be derived");
570578
}
571579
// construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs
572580
StringBuilder stBuilder = new StringBuilder();

branches/update-datastore/gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public class RemoteGcsHelper {
4545

4646
private static final Logger log = Logger.getLogger(RemoteGcsHelper.class.getName());
4747
private static final String BUCKET_NAME_PREFIX = "gcloud-test-bucket-temp-";
48-
private static final String PROJECT_ID_ENV_VAR = "GCLOUD_TESTS_PROJECT_ID";
49-
private static final String PRIVATE_KEY_ENV_VAR = "GCLOUD_TESTS_KEY";
5048
private final StorageOptions options;
5149

5250
private RemoteGcsHelper(StorageOptions options) {
@@ -107,13 +105,7 @@ public static RemoteGcsHelper create(String projectId, InputStream keyStream)
107105
StorageOptions storageOptions = StorageOptions.builder()
108106
.authCredentials(AuthCredentials.createForJson(keyStream))
109107
.projectId(projectId)
110-
.retryParams(RetryParams.builder()
111-
.retryMaxAttempts(10)
112-
.retryMinAttempts(6)
113-
.maxRetryDelayMillis(30000)
114-
.totalRetryPeriodMillis(120000)
115-
.initialRetryDelayMillis(250)
116-
.build())
108+
.retryParams(retryParams())
117109
.connectTimeout(60000)
118110
.readTimeout(60000)
119111
.build();
@@ -145,41 +137,30 @@ public static RemoteGcsHelper create(String projectId, String keyPath)
145137
log.log(Level.WARNING, ex.getMessage());
146138
}
147139
throw GcsHelperException.translate(ex);
148-
} catch (IOException ex) {
149-
if (log.isLoggable(Level.WARNING)) {
150-
log.log(Level.WARNING, ex.getMessage());
151-
}
152-
throw GcsHelperException.translate(ex);
153140
}
154141
}
155142

156143
/**
157-
* Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two
158-
* environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}.
159-
*
160-
* @return A {@code RemoteGcsHelper} object for the provided options.
161-
* @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment
162-
* variables {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if
163-
* the file pointed by {@code GCLOUD_TESTS_KEY} does not exist
144+
* Creates a {@code RemoteGcsHelper} object using default project id and authentication
145+
* credentials.
164146
*/
165147
public static RemoteGcsHelper create() throws GcsHelperException {
166-
String projectId = System.getenv(PROJECT_ID_ENV_VAR);
167-
String keyPath = System.getenv(PRIVATE_KEY_ENV_VAR);
168-
if (projectId == null) {
169-
String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set";
170-
if (log.isLoggable(Level.WARNING)) {
171-
log.log(Level.WARNING, message);
172-
}
173-
throw new GcsHelperException(message);
174-
}
175-
if (keyPath == null) {
176-
String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set";
177-
if (log.isLoggable(Level.WARNING)) {
178-
log.log(Level.WARNING, message);
179-
}
180-
throw new GcsHelperException(message);
181-
}
182-
return create(projectId, keyPath);
148+
StorageOptions storageOptions = StorageOptions.builder()
149+
.retryParams(retryParams())
150+
.connectTimeout(60000)
151+
.readTimeout(60000)
152+
.build();
153+
return new RemoteGcsHelper(storageOptions);
154+
}
155+
156+
private static RetryParams retryParams() {
157+
return RetryParams.builder()
158+
.retryMaxAttempts(10)
159+
.retryMinAttempts(6)
160+
.maxRetryDelayMillis(30000)
161+
.totalRetryPeriodMillis(120000)
162+
.initialRetryDelayMillis(250)
163+
.build();
183164
}
184165

185166
private static class DeleteBucketTask implements Callable<Boolean> {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Export test env variables
2-
export GCLOUD_TESTS_PROJECT_ID="gcloud-devel"
3-
export GCLOUD_TESTS_KEY=$TRAVIS_BUILD_DIR/signing-tools/gcloud-devel-travis.json
2+
export GCLOUD_PROJECT="gcloud-devel"
3+
export GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/signing-tools/gcloud-devel-travis.json

0 commit comments

Comments
 (0)