Skip to content

Commit

Permalink
adding default client construction
Browse files Browse the repository at this point in the history
this allows moving openid logic away from okhttp
  • Loading branch information
shawkins committed Nov 2, 2021
1 parent c19b903 commit acce11c
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@

import io.fabric8.kubernetes.client.Config;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public interface HttpClient {
public interface HttpClient extends AutoCloseable {

public interface Factory {

HttpClient createHttpClient(Config config);

HttpClient.Builder newBuilder();

}

Expand All @@ -45,8 +50,11 @@ public interface Builder {
Builder addOrReplaceInterceptor(String name, Interceptor interceptor);

Builder authenticatorNone();

Builder sslContext(SSLContext context, TrustManager[] trustManagers);
}

@Override
void close();

Builder newBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
package io.fabric8.kubernetes.client.http;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.stream.Collectors;

public interface HttpRequest extends HttpHeaders {

Expand Down Expand Up @@ -61,8 +66,22 @@ default Builder patch(String contentType, String patchForUpdate) {

@Override
Builder setHeader(String k, String v);

default Builder post(Map<String, String> formData) {
return post("application/x-www-form-urlencoded", formData.entrySet().stream().map((e) -> {
return formURLEncode(e.getKey()) + "=" + formURLEncode(e.getValue());
}).collect(Collectors.joining("&")));
}

}

static String formURLEncode(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.displayName());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

URI uri();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
Expand All @@ -56,6 +54,11 @@ public class OkHttpClientFactory implements HttpClient.Factory {
public HttpClient createHttpClient(Config config) {
return createHttpClient(config, builder -> {});
}

@Override
public Builder newBuilder() {
return OkHttpClientImpl.builder();
}

/**
* Creates an HTTP client configured to access the Kubernetes API.
Expand All @@ -76,20 +79,6 @@ public static io.fabric8.kubernetes.client.http.HttpClient createHttpClient(fina
httpClientBuilder.hostnameVerifier((s, sslSession) -> true);
}

TrustManager[] trustManagers = SSLUtils.trustManagers(config);
KeyManager[] keyManagers = SSLUtils.keyManagers(config);

try {
SSLContext sslContext = SSLUtils.sslContext(keyManagers, trustManagers);
X509TrustManager trustManager = null;
if (trustManagers != null && trustManagers.length == 1) {
trustManager = (X509TrustManager) trustManagers[0];
}
httpClientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
} catch (GeneralSecurityException e) {
throw new AssertionError(e); // The system has no TLS. Just give up.
}

Logger reqLogger = LoggerFactory.getLogger(HttpLoggingInterceptor.class);
if (reqLogger.isTraceEnabled()) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
Expand Down Expand Up @@ -159,7 +148,15 @@ public static io.fabric8.kubernetes.client.http.HttpClient createHttpClient(fina
additionalConfig.accept(httpClientBuilder);
}

Builder builderWrapper = new OkHttpClientImpl(httpClientBuilder.build()).newBuilder();
Builder builderWrapper = new OkHttpClientImpl.BuilderImpl(httpClientBuilder);

// TODO: the rest can be moved to common logic
TrustManager[] trustManagers = SSLUtils.trustManagers(config);
KeyManager[] keyManagers = SSLUtils.keyManagers(config);

SSLContext sslContext = SSLUtils.sslContext(keyManagers, trustManagers);
builderWrapper.sslContext(sslContext, trustManagers);

HttpClientUtils.createApplicableInterceptors(config).forEach((s, i) -> builderWrapper.addOrReplaceInterceptor(s, i));
return builderWrapper.build();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -147,7 +151,7 @@ public String getName() {
}
}

static class BuilderImpl implements Builder {
public static class BuilderImpl implements Builder {

private boolean streaming;
private OkHttpClient.Builder builder;
Expand Down Expand Up @@ -227,6 +231,17 @@ public Builder authenticatorNone() {
builder.authenticator(Authenticator.NONE);
return this;
}

@Override
public Builder sslContext(SSLContext context, TrustManager[] trustManagers) {
X509TrustManager trustManager = null;
if (trustManagers != null && trustManagers.length == 1) {
trustManager = (X509TrustManager) trustManagers[0];
}
builder.sslSocketFactory(context.getSocketFactory(), trustManager);
return null;
}

}

private final okhttp3.OkHttpClient httpClient;
Expand Down Expand Up @@ -304,5 +319,5 @@ public okhttp3.OkHttpClient getOkHttpClient() {
public HttpRequest.Builder newHttpRequestBuilder() {
return new OkHttpRequestImpl.BuilderImpl();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.fabric8.kubernetes.client.http.HttpHeaders;
import io.fabric8.kubernetes.client.http.Interceptor;
import io.fabric8.kubernetes.client.internal.okhttp.OkHttpClientFactory;
import io.fabric8.kubernetes.client.internal.okhttp.OkHttpClientImpl;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -126,6 +127,11 @@ public static String basicCredentials(String username, String password) {

public static HttpClient createHttpClient(Config config) {
// TODO: replace with reflection / service load and factory interface
return OkHttpClientFactory.createHttpClient(config, builder -> {});
return new OkHttpClientFactory().createHttpClient(config);
}

public static HttpClient.Builder createHttpClientBuilder() {
// TODO: replace with reflection / service load and factory interface
return new OkHttpClientFactory().newBuilder();
}
}
Loading

0 comments on commit acce11c

Please sign in to comment.