use of io.fabric8.kubernetes.client.http.BasicBuilder in project kubernetes-client by fabric8io.
the class HttpClientUtils method createApplicableInterceptors.
public static Map<String, io.fabric8.kubernetes.client.http.Interceptor> createApplicableInterceptors(Config config, HttpClient.Factory factory) {
Map<String, io.fabric8.kubernetes.client.http.Interceptor> interceptors = new LinkedHashMap<>();
// Header Interceptor
interceptors.put(HEADER_INTERCEPTOR, new Interceptor() {
@Override
public void before(BasicBuilder builder, HttpHeaders headers) {
if (Utils.isNotNullOrEmpty(config.getUsername()) && Utils.isNotNullOrEmpty(config.getPassword())) {
builder.header("Authorization", basicCredentials(config.getUsername(), config.getPassword()));
} else if (Utils.isNotNullOrEmpty(config.getOauthToken())) {
builder.header("Authorization", "Bearer " + config.getOauthToken());
}
if (config.getCustomHeaders() != null && !config.getCustomHeaders().isEmpty()) {
for (Map.Entry<String, String> entry : config.getCustomHeaders().entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
if (config.getUserAgent() != null && !config.getUserAgent().isEmpty()) {
builder.setHeader("User-Agent", config.getUserAgent());
}
}
});
// Impersonator Interceptor
interceptors.put(ImpersonatorInterceptor.NAME, new ImpersonatorInterceptor(config));
// Token Refresh Interceptor
interceptors.put(TokenRefreshInterceptor.NAME, new TokenRefreshInterceptor(config, factory));
// Backwards Compatibility Interceptor
String shouldDisableBackwardsCompatibilityInterceptor = Utils.getSystemPropertyOrEnvVar(KUBERNETES_BACKWARDS_COMPATIBILITY_INTERCEPTOR_DISABLE, "false");
if (!Boolean.parseBoolean(shouldDisableBackwardsCompatibilityInterceptor)) {
interceptors.put(BackwardsCompatibilityInterceptor.NAME, new BackwardsCompatibilityInterceptor());
}
return interceptors;
}
use of io.fabric8.kubernetes.client.http.BasicBuilder in project kubernetes-client by fabric8io.
the class TokenRefreshInterceptor method afterFailure.
@Override
public boolean afterFailure(BasicBuilder headerBuilder, HttpResponse<?> response) {
boolean resubmit = false;
if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
String currentContextName = null;
String newAccessToken = null;
if (config.getCurrentContext() != null) {
currentContextName = config.getCurrentContext().getName();
}
Config newestConfig = Config.autoConfigure(currentContextName);
if (newestConfig.getAuthProvider() != null && newestConfig.getAuthProvider().getName().equalsIgnoreCase("oidc")) {
newAccessToken = OpenIDConnectionUtils.resolveOIDCTokenFromAuthConfig(newestConfig.getAuthProvider().getConfig(), factory.newBuilder());
} else {
newAccessToken = newestConfig.getOauthToken();
}
if (newAccessToken != null) {
// Delete old Authorization header and append new one
headerBuilder.setHeader("Authorization", "Bearer " + newAccessToken);
config.setOauthToken(newAccessToken);
resubmit = true;
}
}
return resubmit;
}
use of io.fabric8.kubernetes.client.http.BasicBuilder in project kubernetes-client by fabric8io.
the class OkHttpClientFactoryTest method inteceptorClosure.
@Test
void inteceptorClosure() throws IOException {
OkHttpClientBuilderImpl.InteceptorAdapter adapter = new OkHttpClientBuilderImpl.InteceptorAdapter(new Interceptor() {
@Override
public boolean afterFailure(BasicBuilder builder, HttpResponse<?> response) {
return true;
}
}, "name");
Chain chain = Mockito.mock(Chain.class);
Mockito.when(chain.request()).thenReturn(Mockito.mock(Request.class));
Mockito.when(chain.request().newBuilder()).thenReturn(Mockito.mock(Builder.class));
Response response = Mockito.mock(Response.class);
Mockito.when(chain.proceed(Mockito.any())).thenReturn(response);
adapter.intercept(chain);
Mockito.verify(response).close();
}
use of io.fabric8.kubernetes.client.http.BasicBuilder in project kubernetes-client by fabric8io.
the class JdkHttpClientBuilderImpl method build.
@Override
public HttpClient build() {
if (httpClient != null) {
return new JdkHttpClientImpl(this, httpClient);
}
java.net.http.HttpClient.Builder builder = clientFactory.createNewHttpClientBuilder();
if (connectTimeout != null) {
builder.connectTimeout(connectTimeout);
}
if (sslContext != null) {
builder.sslContext(sslContext);
}
if (followRedirects) {
builder.followRedirects(Redirect.ALWAYS);
}
if (proxyAddress != null) {
builder.proxy(ProxySelector.of(proxyAddress));
} else {
builder.proxy(java.net.http.HttpClient.Builder.NO_PROXY);
}
if (proxyAuthorization != null) {
this.interceptors.put("PROXY-AUTH", new Interceptor() {
@Override
public void before(BasicBuilder builder, HttpHeaders headers) {
builder.setHeader("Proxy-Authorization", proxyAuthorization);
}
});
}
if (preferHttp11) {
builder.version(Version.HTTP_1_1);
}
if (tlsVersions != null && tlsVersions.length > 0) {
builder.sslParameters(new SSLParameters(null, Arrays.asList(tlsVersions).stream().map(TlsVersion::javaName).toArray(String[]::new)));
}
clientFactory.additionalConfig(builder);
return new JdkHttpClientImpl(this, builder.build());
}
use of io.fabric8.kubernetes.client.http.BasicBuilder in project kubernetes-client by fabric8io.
the class DefaultKubernetesClientTest method testInitClientWithCustomHeaderConfiguration.
@Test
void testInitClientWithCustomHeaderConfiguration() {
final Map<String, String> customHeaders = new HashMap<>();
customHeaders.put("user-id", "test-user");
customHeaders.put("cluster-id", "test-cluster");
final Config configWithCustomerHeaders = new ConfigBuilder().withCustomHeaders(customHeaders).build();
BasicBuilder basicBuilder = Mockito.mock(BasicBuilder.class);
HttpClientUtils.createApplicableInterceptors(configWithCustomerHeaders, null).get(HttpClientUtils.HEADER_INTERCEPTOR).before(basicBuilder, Mockito.mock(HttpHeaders.class));
Mockito.verify(basicBuilder, Mockito.times(1)).header("user-id", "test-user");
}
Aggregations