Skip to content

Commit

Permalink
refactor(google): Update to latest google credentials style. (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Duftler committed Oct 23, 2019
1 parent 2729ab2 commit 015b701
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 42 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Mon Jun 17 22:31:26 UTC 2019
orcaVersion=7.52.0
orcaVersion=7.56.1
kotlinVersion=1.3.20
enablePublishing=false
spinnakerGradleVersion=7.0.1
Expand Down
2 changes: 2 additions & 0 deletions kayenta-google/kayenta-google.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ dependencies {

api "com.google.apis:google-api-services-monitoring"
api "com.google.apis:google-api-services-storage"

implementation "com.google.auth:google-auth-library-oauth2-http"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.netflix.kayenta.google.config;

import com.netflix.kayenta.google.security.GoogleCredentials;
import com.netflix.kayenta.google.security.GoogleJsonCredentials;
import com.netflix.kayenta.google.security.GoogleClientFactory;
import com.netflix.kayenta.google.security.GoogleJsonClientFactory;
import com.netflix.kayenta.google.security.GoogleNamedAccountCredentials;
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
Expand Down Expand Up @@ -62,21 +62,21 @@ boolean registerGoogleCredentials(

try {
String jsonKey = googleManagedAccount.getJsonKey();
GoogleCredentials googleCredentials =
GoogleClientFactory googleClientFactory =
StringUtils.hasLength(jsonKey)
? new GoogleJsonCredentials(project, jsonKey)
: new GoogleCredentials(project);
? new GoogleJsonClientFactory(project, jsonKey)
: new GoogleClientFactory(project);

GoogleNamedAccountCredentials.GoogleNamedAccountCredentialsBuilder
googleNamedAccountCredentialsBuilder =
GoogleNamedAccountCredentials.builder()
.name(name)
.project(project)
.credentials(googleCredentials);
.credentials(googleClientFactory);

if (!CollectionUtils.isEmpty(supportedTypes)) {
if (supportedTypes.contains(AccountCredentials.Type.METRICS_STORE)) {
googleNamedAccountCredentialsBuilder.monitoring(googleCredentials.getMonitoring());
googleNamedAccountCredentialsBuilder.monitoring(googleClientFactory.getMonitoring());
}

if (supportedTypes.contains(AccountCredentials.Type.OBJECT_STORE)) {
Expand All @@ -97,7 +97,7 @@ boolean registerGoogleCredentials(
googleNamedAccountCredentialsBuilder.bucketLocation(
googleManagedAccount.getBucketLocation());
googleNamedAccountCredentialsBuilder.rootFolder(rootFolder);
googleNamedAccountCredentialsBuilder.storage(googleCredentials.getStorage());
googleNamedAccountCredentialsBuilder.storage(googleClientFactory.getStorage());
}

googleNamedAccountCredentialsBuilder.supportedTypes(supportedTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.netflix.kayenta.google.security;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
Expand All @@ -27,6 +26,8 @@
import com.google.api.services.monitoring.v3.MonitoringScopes;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Collection;
import java.util.Optional;
Expand All @@ -36,55 +37,51 @@

@ToString
@Slf4j
public class GoogleCredentials {
public class GoogleClientFactory {

private static String applicationVersion =
Optional.ofNullable(GoogleCredentials.class.getPackage().getImplementationVersion())
Optional.ofNullable(GoogleClientFactory.class.getPackage().getImplementationVersion())
.orElse("Unknown");

@Getter private String project;

public GoogleCredentials(String project) {
public GoogleClientFactory(String project) {
this.project = project;
}

public Monitoring getMonitoring() throws IOException {
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = buildHttpTransport();
GoogleCredential credential = getCredential(httpTransport, jsonFactory, MonitoringScopes.all());
HttpRequestInitializer reqInit = setHttpTimeout(credential);
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredentials credentials = getCredentials(MonitoringScopes.all());
HttpRequestInitializer reqInit = setHttpTimeout(credentials);
String applicationName = "Spinnaker/" + applicationVersion;

return new Monitoring.Builder(httpTransport, jsonFactory, credential)
return new Monitoring.Builder(httpTransport, jsonFactory, reqInit)
.setApplicationName(applicationName)
.setHttpRequestInitializer(reqInit)
.build();
}

public Storage getStorage() throws IOException {
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = buildHttpTransport();
GoogleCredential credential = getCredential(httpTransport, jsonFactory, StorageScopes.all());
HttpRequestInitializer reqInit = setHttpTimeout(credential);
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredentials credentials = getCredentials(StorageScopes.all());
HttpRequestInitializer reqInit = setHttpTimeout(credentials);
String applicationName = "Spinnaker/" + applicationVersion;

return new Storage.Builder(httpTransport, jsonFactory, credential)
return new Storage.Builder(httpTransport, jsonFactory, reqInit)
.setApplicationName(applicationName)
.setHttpRequestInitializer(reqInit)
.build();
}

protected GoogleCredential getCredential(
HttpTransport httpTransport, JsonFactory jsonFactory, Collection<String> scopes)
throws IOException {
protected GoogleCredentials getCredentials(Collection<String> scopes) throws IOException {
log.debug(
"Loading credentials for project {} using application default credentials, with scopes {}.",
project,
scopes);

// No JSON key was specified in matching config on key server, so use application default
// credentials.
return GoogleCredential.getApplicationDefault().createScoped(scopes);
return GoogleCredentials.getApplicationDefault().createScoped(scopes);
}

protected HttpTransport buildHttpTransport() {
Expand All @@ -95,11 +92,11 @@ protected HttpTransport buildHttpTransport() {
}
}

static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
return new HttpRequestInitializer() {
static HttpRequestInitializer setHttpTimeout(final GoogleCredentials credentials) {
return new HttpCredentialsAdapter(credentials) {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
requestInitializer.initialize(httpRequest);
super.initialize(httpRequest);
httpRequest.setConnectTimeout(2 * 60000); // 2 minutes connect timeout
httpRequest.setReadTimeout(2 * 60000); // 2 minutes read timeout
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.netflix.kayenta.google.security;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -29,26 +27,23 @@

@ToString(callSuper = true)
@Slf4j
public class GoogleJsonCredentials extends GoogleCredentials {
public class GoogleJsonClientFactory extends GoogleClientFactory {

@Getter private final String jsonKey;

public GoogleJsonCredentials(String project, String jsonKey) {
public GoogleJsonClientFactory(String project, String jsonKey) {
super(project);

this.jsonKey = jsonKey;
}

@Override
protected GoogleCredential getCredential(
HttpTransport httpTransport, JsonFactory jsonFactory, Collection<String> scopes)
throws IOException {
protected GoogleCredentials getCredentials(Collection<String> scopes) throws IOException {
log.debug(
"Loading credentials for project {} from json key, with scopes {}.", getProject(), scopes);

InputStream credentialStream = new ByteArrayInputStream(jsonKey.getBytes("UTF-8"));

return GoogleCredential.fromStream(credentialStream, httpTransport, jsonFactory)
.createScoped(scopes);
return GoogleCredentials.fromStream(credentialStream).createScoped(scopes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

@Builder
@Data
public class GoogleNamedAccountCredentials implements AccountCredentials<GoogleCredentials> {
public class GoogleNamedAccountCredentials implements AccountCredentials<GoogleClientFactory> {

@NotNull private String name;

@NotNull @Singular private List<Type> supportedTypes;

@NotNull private GoogleCredentials credentials;
@NotNull private GoogleClientFactory credentials;

@NotNull private String project;

Expand Down

0 comments on commit 015b701

Please sign in to comment.