Skip to content

Commit

Permalink
refactor(gcs): Bump kork version and update google credentials to lat…
Browse files Browse the repository at this point in the history
…est style. (#605)
  • Loading branch information
Matt Duftler committed Oct 17, 2019
1 parent 6c3f7d7 commit 0a17e92
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions front50-gcs/front50-gcs.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation "com.netflix.spinnaker.clouddriver:clouddriver-google-common:$clouddriverVersion"
implementation "org.apache.commons:commons-lang3"
implementation "com.google.apis:google-api-services-storage"
implementation 'com.google.auth:google-auth-library-oauth2-http'
implementation "com.netflix.spinnaker.kork:kork-hystrix"
implementation "com.netflix.spinnaker.kork:kork-security"
implementation "net.logstash.logback:logstash-logback-encoder"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.http.ByteArrayContent;
Expand All @@ -36,6 +35,8 @@
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.Objects;
import com.google.api.services.storage.model.StorageObject;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.netflix.spectator.api.Clock;
Expand Down Expand Up @@ -112,22 +113,23 @@ public ObjectMapper getObjectMapper() {
return this.objectMapper;
}

private GoogleCredential loadCredential(
HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException {
GoogleCredential credential;
private GoogleCredentials loadCredential(String jsonPath) throws IOException {
GoogleCredentials credentials = null;

if (!jsonPath.isEmpty()) {
FileInputStream stream = new FileInputStream(jsonPath);
credential =
GoogleCredential.fromStream(stream, transport, factory)
.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
credentials = GoogleCredentials.fromStream(stream);
log.info("Loaded credentials from {}", value("jsonPath", jsonPath));
} else {
log.info(
"spinnaker.gcs.enabled without spinnaker.gcs.jsonPath. "
+ "Using default application credentials. Using default credentials.");
credential = GoogleCredential.getApplicationDefault();
credentials = GoogleCredentials.getApplicationDefault();
}
return credential;

return credentials.createScopedRequired()
? credentials.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL))
: credentials;
}

@VisibleForTesting
Expand Down Expand Up @@ -218,21 +220,20 @@ public GcsStorageService(
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = loadCredential(httpTransport, jsonFactory, credentialsPath);
GoogleCredentials credentials = loadCredential(credentialsPath);
HttpRequestInitializer requestInitializer =
new HttpRequestInitializer() {
new HttpCredentialsAdapter(credentials) {
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
super.initialize(request);
request.setConnectTimeout(connectTimeoutSec * 1000);
request.setReadTimeout(readTimeoutSec * 1000);
}
};

String applicationName = "Spinnaker/" + applicationVersion;
storage =
new Storage.Builder(httpTransport, jsonFactory, credential)
new Storage.Builder(httpTransport, jsonFactory, requestInitializer)
.setApplicationName(applicationName)
.setHttpRequestInitializer(requestInitializer)
.build();
} catch (IOException | java.security.GeneralSecurityException e) {
throw new IllegalStateException(e);
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
clouddriverVersion=5.3.0
fiatVersion=1.5.1
enablePublishing=false
korkVersion=6.12.2
spinnakerGradleVersion=7.0.1
korkVersion=6.12.1
includeProviders=azure,gcs,oracle,redis,s3,swift,sql
Expand Down

0 comments on commit 0a17e92

Please sign in to comment.