Skip to content

Commit 4ecd840

Browse files
committed
Merge pull request #339 from mziccard/integration-test-env-vars
Use GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT vars in RemoteGcsHelper
2 parents 315a0eb + 82ebc71 commit 4ecd840

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

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;

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
@@ -43,6 +43,8 @@
4343
import com.google.common.hash.Hashing;
4444
import com.google.common.io.BaseEncoding;
4545
import com.google.common.primitives.Ints;
46+
import com.google.gcloud.AuthCredentials;
47+
import com.google.gcloud.AuthCredentials.ApplicationDefaultAuthCredentials;
4648
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
4749
import com.google.gcloud.PageImpl;
4850
import com.google.gcloud.BaseService;
@@ -584,9 +586,15 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
584586
ServiceAccountAuthCredentials cred =
585587
(ServiceAccountAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
586588
if (cred == null) {
587-
checkArgument(options().authCredentials() instanceof ServiceAccountAuthCredentials,
588-
"Signing key was not provided and could not be derived");
589-
cred = (ServiceAccountAuthCredentials) this.options().authCredentials();
589+
AuthCredentials serviceCred = this.options().authCredentials();
590+
if (serviceCred instanceof ServiceAccountAuthCredentials) {
591+
cred = (ServiceAccountAuthCredentials) serviceCred;
592+
} else {
593+
if (serviceCred instanceof ApplicationDefaultAuthCredentials) {
594+
cred = ((ApplicationDefaultAuthCredentials) serviceCred).toServiceAccountCredentials();
595+
}
596+
}
597+
checkArgument(cred != null, "Signing key was not provided and could not be derived");
590598
}
591599
// construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs
592600
StringBuilder stBuilder = new StringBuilder();

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> {

utilities/integration_test_env.sh

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)