diff --git a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java b/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java index f7b91855f6..888690be88 100644 --- a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java +++ b/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java @@ -12,6 +12,7 @@ */ package io.kubernetes.client.util; +import io.kubernetes.client.util.credentials.CredentialProvider; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -40,42 +41,17 @@ public class ConfigBuilder { private boolean defaultClientMode = false; private boolean verifyingSsl = false; private String basePath = null; - private File certificateAuthorityFile = null; - private String certificateAuthorityData = null; - private String apiKey = null; - private String userName = null; - private String password = null; - private KeyManager[] keyMgrs = null; - private String accessToken = null; - private String apiKeyPrefix = null; - private KubeConfig kubeConfig = null; + private KubeConfig kubeConfig; + private CredentialProvider credentialProvider; private static final Logger log = Logger.getLogger(Config.class); - public String getUserName() { - return userName; + public CredentialProvider getCredentialProvider() { + return credentialProvider; } - public ConfigBuilder setUserName(String userName) { - this.userName = userName; - return this; - } - - public String getPassword() { - return password; - } - - public ConfigBuilder setPassword(String password) { - this.password = password; - return this; - } - - public String getApiKey() { - return apiKey; - } - - public ConfigBuilder setApiKey(String apiKey) { - this.apiKey = apiKey; + public ConfigBuilder setCredentialProvider(final CredentialProvider credentialProvider) { + this.credentialProvider = credentialProvider; return this; } @@ -88,26 +64,6 @@ public ConfigBuilder setBasePath(String basePath) { return this; } - public File getCertificateAuthorityFile() { - return certificateAuthorityFile; - } - - public ConfigBuilder setCertificateAuthority(File certificateAuthorityFile) { - this.certificateAuthorityFile = certificateAuthorityFile; - this.verifyingSsl = true; - return this; - } - - public String getCertificateAuthorityData() { - return certificateAuthorityData; - } - - public ConfigBuilder setCertificateAuthority(String certificateAuthorityData) { - this.certificateAuthorityData = certificateAuthorityData; - this.verifyingSsl = true; - return this; - } - public ConfigBuilder setClusterMode() { this.clusterMode = true; return this; @@ -138,15 +94,6 @@ public ConfigBuilder setKubeConfig(InputStream stream) { return this; } - public KeyManager[] getKeyMgrs() { - return keyMgrs; - } - - public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) { - this.keyMgrs = keyMgrs; - return this; - } - public boolean isVerifyingSsl() { return verifyingSsl; } @@ -165,15 +112,6 @@ public ConfigBuilder setDefaultClientMode() { return this; } - public String getApiKeyPrefix() { - return apiKeyPrefix; - } - - public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) { - this.apiKeyPrefix = apiKeyPrefix; - return this; - } - public ApiClient build() { ApiClient client = new ApiClient(); @@ -197,7 +135,7 @@ public ApiClient build() { } } - if(defaultClientMode ==true ) { + if(defaultClientMode ==true) { try { client = Config.defaultClient(); } catch (IOException e) { @@ -216,57 +154,13 @@ public ApiClient build() { } } - if(keyMgrs != null) { - client.setKeyManagers(keyMgrs); - } - - if(userName != null){ - client.setUsername(userName); - } - - if(password != null){ - client.setPassword(password); - } - - if(( userName != null )&&(password != null)) { - final String usernameAndPassword = userName + ":" + password; - client.setApiKeyPrefix("Basic"); - client.setApiKey(ByteString.of(usernameAndPassword.getBytes(Charset.forName("ISO-8859-1"))).base64()); - } - - if(accessToken != null) { - if (apiKeyPrefix == null){ - client.setApiKeyPrefix("Bearer"); - } - client.setAccessToken(accessToken); - } - - if(apiKeyPrefix != null) { - client.setApiKeyPrefix(apiKeyPrefix); - } - - if(apiKey != null) { - if (apiKeyPrefix == null){ - client.setApiKeyPrefix("Bearer"); - } - client.setApiKey(apiKey); - } - client.setVerifyingSsl(verifyingSsl); - if(certificateAuthorityFile != null) { - try { - client.setSslCaCert(new FileInputStream(certificateAuthorityFile)); - } catch (FileNotFoundException e) { - log.error("Unable to find the file", e); - } - } - - if(certificateAuthorityData != null) { - byte[] bytes = Base64.decodeBase64(certificateAuthorityData); - client.setSslCaCert(new ByteArrayInputStream(bytes)); + if (credentialProvider != null) { + credentialProvider.provide(client); } return client; } + } \ No newline at end of file diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/ApiKeyCredentialProvider.java b/util/src/main/java/io/kubernetes/client/util/credentials/ApiKeyCredentialProvider.java new file mode 100644 index 0000000000..ce2451892c --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/credentials/ApiKeyCredentialProvider.java @@ -0,0 +1,17 @@ +package io.kubernetes.client.util.credentials; + +import io.kubernetes.client.ApiClient; + +public class ApiKeyCredentialProvider implements CredentialProvider { + + private String accessToken; + + public ApiKeyCredentialProvider(final String accessToken) { + this.accessToken = accessToken; + } + + @Override public void provide(ApiClient client) { + client.setApiKeyPrefix("Bearer"); + client.setApiKey(accessToken); + } +} diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/CertificateAuthorityCredentialProvider.java b/util/src/main/java/io/kubernetes/client/util/credentials/CertificateAuthorityCredentialProvider.java new file mode 100644 index 0000000000..d943c00bd4 --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/credentials/CertificateAuthorityCredentialProvider.java @@ -0,0 +1,38 @@ +package io.kubernetes.client.util.credentials; + +import io.kubernetes.client.ApiClient; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import javax.net.ssl.KeyManager; +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; + +public class CertificateAuthorityCredentialProvider implements CredentialProvider { + private static final Logger log = Logger.getLogger(CertificateAuthorityCredentialProvider.class); + + private KeyManager[] keyManagers; + private InputStream inputStream; + + public CertificateAuthorityCredentialProvider(final KeyManager[] keyManagers, final File caFile) { + this.keyManagers = keyManagers; + try { + this.inputStream = new FileInputStream(caFile); + } catch(final FileNotFoundException e) { + log.error("Unable to find Certificate Authority file", e); + } + } + + public CertificateAuthorityCredentialProvider(final KeyManager[] keyManagers, final String caData) { + this.keyManagers = keyManagers; + this.inputStream = new ByteArrayInputStream(Base64.decodeBase64(caData)); + } + + @Override public void provide(ApiClient client) { + client.setSslCaCert(inputStream); + client.setKeyManagers(keyManagers); + client.setVerifyingSsl(true); + } +} diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/CredentialProvider.java b/util/src/main/java/io/kubernetes/client/util/credentials/CredentialProvider.java new file mode 100644 index 0000000000..77b3ac6899 --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/credentials/CredentialProvider.java @@ -0,0 +1,9 @@ +package io.kubernetes.client.util.credentials; + +import io.kubernetes.client.ApiClient; + +public interface CredentialProvider { + + void provide(final ApiClient client); + +} diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/KeyManagerCredentialProvider.java b/util/src/main/java/io/kubernetes/client/util/credentials/KeyManagerCredentialProvider.java new file mode 100644 index 0000000000..96629b581d --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/credentials/KeyManagerCredentialProvider.java @@ -0,0 +1,17 @@ +package io.kubernetes.client.util.credentials; + +import io.kubernetes.client.ApiClient; +import javax.net.ssl.KeyManager; + +public class KeyManagerCredentialProvider implements CredentialProvider{ + + private KeyManager[] keyManagers; + + public KeyManagerCredentialProvider(final KeyManager[] keyManagers) { + this.keyManagers = keyManagers; + } + + @Override public void provide(ApiClient client) { + client.setKeyManagers(keyManagers); + } +} diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/UsernamePasswordProvider.java b/util/src/main/java/io/kubernetes/client/util/credentials/UsernamePasswordProvider.java new file mode 100644 index 0000000000..c7860450d3 --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/credentials/UsernamePasswordProvider.java @@ -0,0 +1,21 @@ +package io.kubernetes.client.util.credentials; + +import io.kubernetes.client.ApiClient; +import java.nio.charset.Charset; +import okio.ByteString; + +public class UsernamePasswordProvider implements CredentialProvider { + private final String username; + private final String password; + + public UsernamePasswordProvider(final String username, final String password) { + this.username = username; + this.password = password; + } + + @Override public void provide(ApiClient client) { + final String usernameAndPassword = username + ":" + password; + client.setApiKeyPrefix("Basic"); + client.setApiKey(ByteString.of(usernameAndPassword.getBytes(Charset.forName("ISO-8859-1"))).base64()); + } +} diff --git a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java b/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java index 5295abf80c..f10affbe46 100644 --- a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java +++ b/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java @@ -15,8 +15,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import io.kubernetes.client.util.credentials.ApiKeyCredentialProvider; +import io.kubernetes.client.util.credentials.CertificateAuthorityCredentialProvider; +import io.kubernetes.client.util.credentials.UsernamePasswordProvider; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.Charset; @@ -166,8 +168,7 @@ public void testUserNamePasswordConfigBuilder() { try { ApiClient client = (new ConfigBuilder()) .setBasePath(basePath) - .setUserName(userName) - .setPassword(password) + .setCredentialProvider(new UsernamePasswordProvider(userName, password)) .build(); assertEquals(userName, ((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentication("BasicAuth")).getUsername()); assertEquals(password, ((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentication("BasicAuth")).getPassword()); @@ -187,8 +188,7 @@ public void testApiKeyConfigbuilder() { ApiClient client = null; client = (new ConfigBuilder()) .setBasePath(basePath) - .setApiKeyPrefix(apiKeyPrefix) - .setApiKey(apiKey) + .setCredentialProvider(new ApiKeyCredentialProvider(apiKey)) .build(); assertEquals(basePath, client.getBasePath()); assertEquals(false, client.isVerifyingSsl()); @@ -205,9 +205,7 @@ public void testKeyMgrANDCertConfigBUilder() { //by default verify ssl is false ApiClient client = (new ConfigBuilder()) .setBasePath(basePath) - .setKeyMgrs(keyMgrs) - .setCertificateAuthority(certificateAuthorityData) - .setVerifyingSsl(true) + .setCredentialProvider(new CertificateAuthorityCredentialProvider(keyMgrs, certificateAuthorityData)) .build(); assertEquals(basePath, client.getBasePath()); assertEquals(true, client.isVerifyingSsl()); @@ -224,7 +222,7 @@ public void testBasePathIllegalArgumentException() throws IOException { ApiClient client = null ; try { client = (new ConfigBuilder()) - .setUserName("user") + .setCredentialProvider(new UsernamePasswordProvider(userName, null)) .build(); } catch(IllegalArgumentException ie) { @@ -233,7 +231,7 @@ public void testBasePathIllegalArgumentException() throws IOException { environmentVariables.set("HOME", "/non-existent"); client = (new ConfigBuilder()) .setDefaultClientMode() - .setUserName("user") + .setCredentialProvider(new UsernamePasswordProvider(userName, null)) .build(); assertEquals("http://localhost:8080", client.getBasePath()); environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath());