Search in sources :

Example 1 with BasicBuilder

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;
}
Also used : HttpHeaders(io.fabric8.kubernetes.client.http.HttpHeaders) LinkedHashMap(java.util.LinkedHashMap) BasicBuilder(io.fabric8.kubernetes.client.http.BasicBuilder) Interceptor(io.fabric8.kubernetes.client.http.Interceptor)

Example 2 with BasicBuilder

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;
}
Also used : Config(io.fabric8.kubernetes.client.Config)

Example 3 with BasicBuilder

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();
}
Also used : HttpResponse(io.fabric8.kubernetes.client.http.HttpResponse) Response(okhttp3.Response) Chain(okhttp3.Interceptor.Chain) Builder(okhttp3.Request.Builder) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) BasicBuilder(io.fabric8.kubernetes.client.http.BasicBuilder) Request(okhttp3.Request) BasicBuilder(io.fabric8.kubernetes.client.http.BasicBuilder) Interceptor(io.fabric8.kubernetes.client.http.Interceptor) Test(org.junit.jupiter.api.Test)

Example 4 with BasicBuilder

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());
}
Also used : HttpHeaders(io.fabric8.kubernetes.client.http.HttpHeaders) SSLParameters(javax.net.ssl.SSLParameters) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) BasicBuilder(io.fabric8.kubernetes.client.http.BasicBuilder) Interceptor(io.fabric8.kubernetes.client.http.Interceptor)

Example 5 with BasicBuilder

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");
}
Also used : HttpHeaders(io.fabric8.kubernetes.client.http.HttpHeaders) HashMap(java.util.HashMap) BasicBuilder(io.fabric8.kubernetes.client.http.BasicBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

BasicBuilder (io.fabric8.kubernetes.client.http.BasicBuilder)5 HttpHeaders (io.fabric8.kubernetes.client.http.HttpHeaders)4 Interceptor (io.fabric8.kubernetes.client.http.Interceptor)3 Test (org.junit.jupiter.api.Test)3 Config (io.fabric8.kubernetes.client.Config)1 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)1 RequestConfig (io.fabric8.kubernetes.client.RequestConfig)1 HttpClient (io.fabric8.kubernetes.client.http.HttpClient)1 HttpResponse (io.fabric8.kubernetes.client.http.HttpResponse)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 SSLParameters (javax.net.ssl.SSLParameters)1 Chain (okhttp3.Interceptor.Chain)1 Request (okhttp3.Request)1 Builder (okhttp3.Request.Builder)1 Response (okhttp3.Response)1