Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(artifacts): update to okhttp3 and add connection timeout config for github artifacts. #5093

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions clouddriver-artifacts/clouddriver-artifacts.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies {
implementation "com.google.apis:google-api-services-storage:v1-rev141-1.25.0"
implementation 'com.google.auth:google-auth-library-oauth2-http'
implementation "com.netflix.frigga:frigga"
implementation "com.netflix.spinnaker.kork:kork-core"
implementation "com.netflix.spinnaker.kork:kork-web"
implementation "com.netflix.spinnaker.kork:kork-artifacts"
implementation "com.netflix.spinnaker.kork:kork-annotations"
implementation "com.netflix.spinnaker.kork:kork-exceptions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package com.netflix.spinnaker.clouddriver.artifacts.bitbucket;

import com.squareup.okhttp.OkHttpClient;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.clouddriver.artifacts.config.SimpleHttpArtifactCredentials;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.squareup.okhttp.OkHttpClient;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;

@NonnullByDefault
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
package com.netflix.spinnaker.clouddriver.artifacts.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.squareup.okhttp.*;
import java.io.IOException;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

@Slf4j
public abstract class BaseHttpArtifactCredentials<T extends ArtifactAccount> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import com.netflix.spinnaker.clouddriver.artifacts.exceptions.FailedDownloadException;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.*;

@NonnullByDefault
public abstract class SimpleHttpArtifactCredentials<T extends ArtifactAccount>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import javax.annotation.ParametersAreNullableByDefault;
import lombok.Builder;
import lombok.Setter;
import lombok.Value;
import org.springframework.boot.context.properties.ConstructorBinding;

Expand All @@ -35,6 +36,7 @@ final class GitRepoArtifactAccount implements ArtifactAccount {
private final String sshPrivateKeyPassphrase;
private final String sshKnownHostsFilePath;
private final boolean sshTrustUnknownHosts;
@Setter private final int timeout;

@Builder
@ConstructorBinding
Expand All @@ -47,7 +49,8 @@ public GitRepoArtifactAccount(
String sshPrivateKeyFilePath,
String sshPrivateKeyPassphrase,
String sshKnownHostsFilePath,
boolean sshTrustUnknownHosts) {
boolean sshTrustUnknownHosts,
int timeout) {
this.name = Strings.nullToEmpty(name);
this.username = Strings.nullToEmpty(username);
this.password = Strings.nullToEmpty(password);
Expand All @@ -56,5 +59,6 @@ public GitRepoArtifactAccount(
this.sshPrivateKeyPassphrase = Strings.nullToEmpty(sshPrivateKeyPassphrase);
this.sshKnownHostsFilePath = Strings.nullToEmpty(sshKnownHostsFilePath);
this.sshTrustUnknownHosts = sshTrustUnknownHosts;
this.timeout = timeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ final class GitRepoArtifactCredentials implements ArtifactCredentials {
private final String sshKnownHostsFilePath;
private final boolean sshTrustUnknownHosts;
private final AuthType authType;
private final int timeout;

private enum AuthType {
HTTP,
Expand All @@ -80,6 +81,7 @@ public GitRepoArtifactCredentials(GitRepoArtifactAccount account) {
this.sshPrivateKeyPassphrase = account.getSshPrivateKeyPassphrase();
this.sshKnownHostsFilePath = account.getSshKnownHostsFilePath();
this.sshTrustUnknownHosts = account.isSshTrustUnknownHosts();
this.timeout = account.getTimeout();

if (!username.isEmpty() && !password.isEmpty()) {
authType = AuthType.HTTP;
Expand Down Expand Up @@ -126,11 +128,16 @@ public InputStream download(Artifact artifact) throws IOException {
private Git clone(Artifact artifact, Path stagingPath, String remoteRef) throws GitAPIException {
// TODO(ethanfrogers): add support for clone history depth once jgit supports it

return addAuthentication(Git.cloneRepository())
.setURI(artifact.getReference())
.setDirectory(stagingPath.toFile())
.setBranch(remoteRef)
.call();
CloneCommand cloneCommand =
addAuthentication(Git.cloneRepository())
.setURI(artifact.getReference())
.setDirectory(stagingPath.toFile())
.setBranch(remoteRef);

if (timeout > 0) {
cloneCommand.setTimeout(timeout);
}
return cloneCommand.call();
}

private void archiveToOutputStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
package com.netflix.spinnaker.clouddriver.artifacts.github;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.okhttp.OkHttpClient;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -44,7 +45,26 @@ List<? extends GitHubArtifactCredentials> gitHubArtifactCredentials(OkHttpClient
.map(
a -> {
try {
return new GitHubArtifactCredentials(a, okHttpClient, objectMapper);
if (gitHubArtifactProviderProperties.getConnectTimeoutMs() > 0
|| gitHubArtifactProviderProperties.getReadTimeoutMs() > 0) {

OkHttpClient.Builder extendedTimeoutClientBuilder = okHttpClient.newBuilder();
if (gitHubArtifactProviderProperties.getConnectTimeoutMs() > 0) {
extendedTimeoutClientBuilder.connectTimeout(
gitHubArtifactProviderProperties.getConnectTimeoutMs(),
TimeUnit.MILLISECONDS);
}

if (gitHubArtifactProviderProperties.getReadTimeoutMs() > 0) {
extendedTimeoutClientBuilder.readTimeout(
gitHubArtifactProviderProperties.getReadTimeoutMs(), TimeUnit.MILLISECONDS);
}
return new GitHubArtifactCredentials(
a, extendedTimeoutClientBuilder.build(), objectMapper);
} else {
return new GitHubArtifactCredentials(a, okHttpClient, objectMapper);
}

} catch (Exception e) {
log.warn("Failure instantiating GitHub artifact account {}: ", a, e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
import com.netflix.spinnaker.clouddriver.artifacts.exceptions.FailedDownloadException;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.ResponseBody;
import java.io.IOException;
import javax.annotation.Nullable;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;

@NonnullByDefault
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
@ConfigurationProperties("artifacts.github")
final class GitHubArtifactProviderProperties implements ArtifactProvider<GitHubArtifactAccount> {
private boolean enabled;
long connectTimeoutMs;
long readTimeoutMs;
private List<GitHubArtifactAccount> accounts = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.netflix.spinnaker.clouddriver.artifacts.gitlab;

import com.squareup.okhttp.OkHttpClient;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import com.netflix.spinnaker.clouddriver.artifacts.config.SimpleHttpArtifactCredentials;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import java.util.Optional;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

@NonnullByDefault
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package com.netflix.spinnaker.clouddriver.artifacts.helm;

import com.squareup.okhttp.OkHttpClient;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.web.exceptions.NotFoundException;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.ResponseBody;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;

@NonnullByDefault
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package com.netflix.spinnaker.clouddriver.artifacts.http;

import com.squareup.okhttp.OkHttpClient;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.clouddriver.artifacts.config.SimpleHttpArtifactCredentials;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.squareup.okhttp.OkHttpClient;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;

@NonnullByDefault
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.netflix.spinnaker.clouddriver.artifacts.jenkins;

import com.squareup.okhttp.OkHttpClient;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import com.netflix.spinnaker.clouddriver.artifacts.config.SimpleHttpArtifactCredentials;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

@NonnullByDefault
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2017 Google, Inc.
* Copyright 2020 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
Expand All @@ -12,11 +12,14 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.spinnaker.config;

import com.squareup.okhttp.OkHttpClient;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -25,13 +28,22 @@
import org.springframework.stereotype.Component;

@Configuration
@EnableConfigurationProperties
@EnableScheduling
@Component
@EnableConfigurationProperties(ArtifactProviderProperties.class)
@ComponentScan("com.netflix.spinnaker.clouddriver.artifacts")
@Slf4j
public class ArtifactConfiguration {

@Bean
OkHttpClient okHttpClient() {
return new OkHttpClient();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base client is already configured here kork-web and we should be able to inject this and customize further for timeout settings etc using the newBuilder api.

OkHttpClient okHttpClient(
OkHttp3ClientConfiguration okHttp3ClientConfiguration,
ArtifactProviderProperties properties) {
log.info("Initializing for Artifact provider okhttp client");
OkHttpClient.Builder builder = okHttp3ClientConfiguration.create();
builder.readTimeout(properties.getReadTimeoutMs(), TimeUnit.MILLISECONDS);
builder.connectTimeout(properties.getConnectTimeoutMs(), TimeUnit.MILLISECONDS);
builder.retryOnConnectionFailure(properties.isRetryOnConnectionFailure());
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.spinnaker.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties("artifacts")
@Slf4j
class ArtifactProviderProperties {
long connectTimeoutMs;
long readTimeoutMs;
boolean retryOnConnectionFailure = true;
}
Loading