Search in sources :

Example 1 with KubeconfigAuthentication

use of io.kubernetes.client.util.credentials.KubeconfigAuthentication in project java by kubernetes-client.

the class ClientBuilder method build.

public ApiClient build() {
    final ApiClient client = new ApiClient();
    client.setHttpClient(client.getHttpClient().newBuilder().protocols(protocols).readTimeout(this.readTimeout).pingInterval(pingInterval).build());
    if (basePath != null) {
        if (basePath.endsWith("/")) {
            basePath = basePath.substring(0, basePath.length() - 1);
        }
        client.setBasePath(basePath);
    }
    client.setVerifyingSsl(verifyingSsl);
    if (authentication != null) {
        if (StringUtils.isNotEmpty(keyStorePassphrase)) {
            if (authentication instanceof KubeconfigAuthentication) {
                if (((KubeconfigAuthentication) authentication).getDelegateAuthentication() instanceof ClientCertificateAuthentication) {
                    ((ClientCertificateAuthentication) (((KubeconfigAuthentication) authentication).getDelegateAuthentication())).setPassphrase(keyStorePassphrase);
                }
            }
        }
        authentication.provide(client);
    }
    // TODO: Add a test to ensure that this works correctly...
    if (caCertBytes != null) {
        client.setSslCaCert(new ByteArrayInputStream(caCertBytes));
    }
    return client;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ApiClient(io.kubernetes.client.openapi.ApiClient) ClientCertificateAuthentication(io.kubernetes.client.util.credentials.ClientCertificateAuthentication) KubeconfigAuthentication(io.kubernetes.client.util.credentials.KubeconfigAuthentication)

Example 2 with KubeconfigAuthentication

use of io.kubernetes.client.util.credentials.KubeconfigAuthentication in project java by kubernetes-client.

the class ClientBuilderTest method testSettingPassphraseForKubeConfigShouldWork.

@Test
public void testSettingPassphraseForKubeConfigShouldWork() throws IOException {
    String expectedPassphrase = "test";
    ClientBuilder builder = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(KUBECONFIG_HTTPS_X509_FILE_PATH))).setKeyStorePassphrase(expectedPassphrase);
    KubeconfigAuthentication receivingAuthn = (KubeconfigAuthentication) builder.getAuthentication();
    builder.build();
    assertEquals(expectedPassphrase, ((ClientCertificateAuthentication) receivingAuthn.getDelegateAuthentication()).getPassphrase());
}
Also used : FileReader(java.io.FileReader) KubeconfigAuthentication(io.kubernetes.client.util.credentials.KubeconfigAuthentication) Test(org.junit.Test)

Example 3 with KubeconfigAuthentication

use of io.kubernetes.client.util.credentials.KubeconfigAuthentication in project java by kubernetes-client.

the class ClientBuilder method kubeconfig.

/**
 * Creates a builder which is pre-configured from a {@link KubeConfig}.
 *
 * <p>To load a <tt>KubeConfig</tt>, see {@link KubeConfig#loadKubeConfig(Reader)}.
 *
 * @param config The {@link KubeConfig} to configure the builder from.
 * @return <tt>ClientBuilder</tt> configured from the provided <tt>KubeConfig</tt>
 * @throws IOException if the files specified in the provided <tt>KubeConfig</tt> are not readable
 */
public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
    final ClientBuilder builder = new ClientBuilder();
    String server = config.getServer();
    if (server == null) {
        throw new IllegalArgumentException("No server in kubeconfig");
    }
    if (!server.contains("://")) {
        if (server.contains(":443")) {
            server = "https://" + server;
        } else {
            server = "http://" + server;
        }
    }
    final byte[] caBytes = config.getDataOrFileRelative(config.getCertificateAuthorityData(), config.getCertificateAuthorityFile());
    if (caBytes != null) {
        builder.setCertificateAuthority(caBytes);
    }
    builder.setVerifyingSsl(config.verifySSL());
    builder.setBasePath(server);
    builder.setAuthentication(new KubeconfigAuthentication(config));
    return builder;
}
Also used : KubeconfigAuthentication(io.kubernetes.client.util.credentials.KubeconfigAuthentication)

Aggregations

KubeconfigAuthentication (io.kubernetes.client.util.credentials.KubeconfigAuthentication)3 ApiClient (io.kubernetes.client.openapi.ApiClient)1 ClientCertificateAuthentication (io.kubernetes.client.util.credentials.ClientCertificateAuthentication)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileReader (java.io.FileReader)1 Test (org.junit.Test)1