From 8919e8a2cf399fcf775a48c5a4818f034b71d6e0 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Thu, 10 Jun 2021 15:46:26 +0200 Subject: Rename variables and add shared config validation --- .../main/java/ai/vespa/feed/client/CliClient.java | 2 +- .../java/ai/vespa/feed/client/ApacheCluster.java | 8 +++---- .../ai/vespa/feed/client/FeedClientBuilder.java | 26 ++++++++++++---------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/vespa-feed-client-cli/src/main/java/ai/vespa/feed/client/CliClient.java b/vespa-feed-client-cli/src/main/java/ai/vespa/feed/client/CliClient.java index 9d4f3525c32..0d4c92cf483 100644 --- a/vespa-feed-client-cli/src/main/java/ai/vespa/feed/client/CliClient.java +++ b/vespa-feed-client-cli/src/main/java/ai/vespa/feed/client/CliClient.java @@ -84,7 +84,7 @@ public class CliClient { builder.setHostnameVerifier(AcceptAllHostnameVerifier.INSTANCE); } cliArgs.certificateAndKey().ifPresent(c -> builder.setCertificate(c.certificateFile, c.privateKeyFile)); - cliArgs.caCertificates().ifPresent(builder::setCaCertificates); + cliArgs.caCertificates().ifPresent(builder::setCaCertificatesFile); cliArgs.headers().forEach(builder::addRequestHeader); return builder.build(); } diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/ApacheCluster.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/ApacheCluster.java index 672f5f080b5..7939e884551 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/ApacheCluster.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/ApacheCluster.java @@ -145,11 +145,11 @@ class ApacheCluster implements Cluster { private static SSLContext constructSslContext(FeedClientBuilder builder) throws IOException { if (builder.sslContext != null) return builder.sslContext; SslContextBuilder sslContextBuilder = new SslContextBuilder(); - if (builder.certificate != null && builder.privateKey != null) { - sslContextBuilder.withCertificateAndKey(builder.certificate, builder.privateKey); + if (builder.certificateFile != null && builder.privateKeyFile != null) { + sslContextBuilder.withCertificateAndKey(builder.certificateFile, builder.privateKeyFile); } - if (builder.caCertificates != null) { - sslContextBuilder.withCaCertificates(builder.caCertificates); + if (builder.caCertificatesFile != null) { + sslContextBuilder.withCaCertificates(builder.caCertificatesFile); } return sslContextBuilder.build(); } diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java index d0a221ed358..bc9fa6b7e36 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java @@ -35,9 +35,9 @@ public class FeedClientBuilder { int maxStreamsPerConnection = 128; FeedClient.RetryStrategy retryStrategy = defaultRetryStrategy; FeedClient.CircuitBreaker circuitBreaker = new GracePeriodCircuitBreaker(Duration.ofSeconds(1), Duration.ofMinutes(10)); - Path certificate; - Path privateKey; - Path caCertificates; + Path certificateFile; + Path privateKeyFile; + Path caCertificatesFile; public static FeedClientBuilder create(URI endpoint) { return new FeedClientBuilder(Collections.singletonList(endpoint)); } @@ -81,9 +81,6 @@ public class FeedClientBuilder { } public FeedClientBuilder setSslContext(SSLContext context) { - if (certificate != null || caCertificates != null || privateKey != null) { - throw new IllegalArgumentException("Cannot set both SSLContext and certificate / CA certificates"); - } this.sslContext = requireNonNull(context); return this; } @@ -113,24 +110,29 @@ public class FeedClientBuilder { } public FeedClientBuilder setCertificate(Path certificatePemFile, Path privateKeyPemFile) { - if (sslContext != null) throw new IllegalArgumentException("Cannot set both SSLContext and certificate"); - this.certificate = certificatePemFile; - this.privateKey = privateKeyPemFile; + this.certificateFile = certificatePemFile; + this.privateKeyFile = privateKeyPemFile; return this; } - public FeedClientBuilder setCaCertificates(Path caCertificatesFile) { - if (sslContext != null) throw new IllegalArgumentException("Cannot set both SSLContext and CA certificate"); - this.caCertificates = caCertificatesFile; + public FeedClientBuilder setCaCertificatesFile(Path caCertificatesFile) { + this.caCertificatesFile = caCertificatesFile; return this; } public FeedClient build() { try { + validateConfiguration(); return new HttpFeedClient(this); } catch (IOException e) { throw new UncheckedIOException(e); } } + private void validateConfiguration() { + if (sslContext != null && (certificateFile != null || caCertificatesFile != null || privateKeyFile != null)) { + throw new IllegalArgumentException("Cannot set both SSLContext and certificate / CA certificates"); + } + } + } -- cgit v1.2.3