From 58f364b8ec4432b57b65737700b4dfd7446bb427 Mon Sep 17 00:00:00 2001 From: kondapally karthik Date: Mon, 22 Jan 2018 23:25:32 +0530 Subject: [PATCH 1/4] adding custom ConfigBuilder --- .../kubernetes/client/util/ConfigBuilder.java | 285 ++++++++++++++++++ .../client/util/ConfigBuilderTest.java | 219 ++++++++++++++ 2 files changed, 504 insertions(+) create mode 100644 util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java create mode 100644 util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java diff --git a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java b/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java new file mode 100644 index 0000000000..8ef3cce266 --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java @@ -0,0 +1,285 @@ +/* +Copyright 2017 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + */ +package io.kubernetes.client.util; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; + +import javax.net.ssl.KeyManager; + +import okio.ByteString; +import io.kubernetes.client.ApiClient; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.KubeConfig; +import io.kubernetes.client.util.SSLUtils; + +public class ConfigBuilder { + + private boolean trustCerts = false; + private boolean clusterMode = false; + private boolean defaultKubeConfigMode = false; + private boolean defaultClientMode = false; + private boolean verifyingSsl = false; + private String basePath = null; + private String 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 InputStream sslCaCert = null; + private KubeConfig kubeConfig = null; + + public String getUserName() { + return userName; + } + + 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; + return this; + } + + public boolean isTrustCerts() { + return trustCerts; + } + + public ConfigBuilder setTrustCerts(boolean trustCerts) { + this.trustCerts = trustCerts; + return this; + } + + public String getbasePath() { + return basePath; + } + + public ConfigBuilder setbasePath(String basePath) { + this.basePath = basePath; + return this; + } + + public String getCertificateAuthorityFile() { + return certificateAuthorityFile; + } + + public ConfigBuilder setCertificateAuthorityFile(String certificateAuthorityFile) { + this.certificateAuthorityFile = certificateAuthorityFile; + return this; + + } + + public String getCertificateAuthorityData() { + return certificateAuthorityData; + } + + public ConfigBuilder setCertificateAuthorityData(String certificateAuthorityData) { + this.certificateAuthorityData = certificateAuthorityData; + return this; + } + + public ConfigBuilder setClusterMode(boolean clusterMode) { + this.clusterMode = clusterMode; + return this; + } + + public ConfigBuilder setKubeConfig(KubeConfig config) { + this.kubeConfig = config; + return this; + } + + public ConfigBuilder setDefaultKubeConfigMode(boolean defaultKubeConfigMode) { + this.defaultKubeConfigMode = defaultKubeConfigMode; + return this; + } + + public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException { + this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(fileName)); + return this; + } + + public ConfigBuilder setKubeConfig(Reader input) { + this.kubeConfig = KubeConfig.loadKubeConfig(input); + return this; + } + + public ConfigBuilder setKubeConfig(InputStream stream) { + this.kubeConfig = KubeConfig.loadKubeConfig(new InputStreamReader(stream)); + return this; + } + + public KeyManager[] getKeyMgrs() { + return keyMgrs; + } + + public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) { + this.keyMgrs = keyMgrs; + return this; + } + + public boolean isVerifyingSsl() { + return verifyingSsl; + } + + public ConfigBuilder setVerifyingSsl(boolean verifyingSsl) { + this.verifyingSsl = verifyingSsl; + return this; + } + + public boolean isDefaultClientMode() { + return defaultClientMode; + } + + public ConfigBuilder setDefaultClientMode(boolean defaultClientMode) { + this.defaultClientMode = defaultClientMode; + return this; + } + + public String getApiKeyPrefix() { + return apiKeyPrefix; + } + + public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) { + this.apiKeyPrefix = apiKeyPrefix; + return this; + } + + public ApiClient build() { + + ApiClient client = new ApiClient(); + + if( kubeConfig !=null) { + client = Config.fromConfig(kubeConfig); + return client; + } + + if(defaultKubeConfigMode == true) { + try { + client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return client; + } + + if(clusterMode == true) { + try { + client = Config.fromCluster(); + } catch (IOException e) { + e.printStackTrace(); + } + return client; + } + + if(defaultClientMode ==true ) { + try { + client = Config.defaultClient(); + } catch (IOException e) { + e.printStackTrace(); + } + return client; + + } + + if (basePath != null ) { + if(basePath.endsWith("/")) { + basePath = basePath.substring(0, basePath.length() - 1); + } + client.setBasePath(basePath) + .setVerifyingSsl(verifyingSsl); + } + + else { + try { + throw new Exception("set kubernetes URL. example: http://localhost"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + 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); + } + + if(sslCaCert != null) { + client.setSslCaCert(sslCaCert); + } + + if(verifyingSsl){ + if((certificateAuthorityData != null) || (certificateAuthorityFile != null)){ + try { + client.setSslCaCert(SSLUtils.getInputStreamFromDataOrFile(certificateAuthorityData, certificateAuthorityFile)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + + return client; + } +} \ No newline at end of file diff --git a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java b/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java new file mode 100644 index 0000000000..b0910c76e1 --- /dev/null +++ b/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java @@ -0,0 +1,219 @@ +/* +Copyright 2017 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + */ +package io.kubernetes.client.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.Charset; + +import javax.net.ssl.KeyManager; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.EnvironmentVariables; +import org.junit.rules.TemporaryFolder; + +import io.kubernetes.client.ApiClient; +import okio.ByteString; + +/** + * Tests for the ConfigBuilder helper class + */ +public class ConfigBuilderTest { + String basePath = "http://localhost"; + String apiKey = "ABCD"; + String userName = "userName"; + String password = "password"; + String apiKeyPrefix = "Bearer"; + String certificateAuthorityData = null; + String certificateAuthorityFile = "/home/user/.minikube/ca.crt"; + String clientCertData = null; + String clientCertFile = "/home/user/.minikube/apiserver.crt"; + String clientKeyData = null; + String clientKeyFile = "/home/user/.minikube/apiserver.key"; + String algo = "RSA"; + String passphrase = ""; + String keyStoreFile = null ; + String keyStorePassphrase = null; + KeyManager[] keyMgrs =null; + + @Rule + public final EnvironmentVariables environmentVariables + = new EnvironmentVariables(); + + @Rule + public TemporaryFolder folder= new TemporaryFolder(); + + + @Test + public void testDefaultClientNothingPresent() { + environmentVariables.set("HOME", "/non-existent"); + ApiClient client = (new ConfigBuilder()) + .setClusterMode(true) + .build(); + assertEquals("http://localhost:8080", client.getBasePath()); + } + + public static String HOME_CONFIG = + "apiVersion: v1\n" + + "clusters:\n" + + "- cluster:\n" + + " server: http://home.dir.com\n" + + " name: foo\n" + + "contexts:\n" + + "- context:\n" + + " cluster: foo\n" + + " name: foo-context\n" + + "current-context: foo-context\n"; + + public static String KUBECONFIG = + "apiVersion: v1\n" + + "clusters:\n" + + "- cluster:\n" + + " server: http://kubeconfig.dir.com\n" + + " name: foo\n" + + "contexts:\n" + + "- context:\n" + + " cluster: foo\n" + + " name: foo-context\n" + + "current-context: foo-context\n"; + + File config = null; + File dir = null; + File kubedir = null; + File configFile = null; + + @Before + public void setUp() throws IOException { + dir = folder.newFolder(); + kubedir = new File(dir, ".kube"); + kubedir.mkdir(); + config = new File(kubedir, "config"); + FileWriter writer = new FileWriter(config); + writer.write(HOME_CONFIG); + writer.flush(); + writer.close(); + + configFile = folder.newFile("config"); + writer = new FileWriter(configFile); + writer.write(KUBECONFIG); + writer.flush(); + writer.close(); + } + + @Test + public void testDefaultClientHomeDir() { + try { + environmentVariables.set("HOME", dir.getCanonicalPath()); + ApiClient client = new ConfigBuilder() + .setDefaultClientMode(true) + .build(); + assertEquals("http://home.dir.com", client.getBasePath()); + } catch (Exception ex) { + ex.printStackTrace(); + fail("Unexpected exception: " + ex); + } + } + + @Test + public void testDefaultClientKubeConfig() { + try { + environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); + ApiClient client = new ConfigBuilder() + .setDefaultKubeConfigMode(true) + .build(); + assertEquals("http://kubeconfig.dir.com", client.getBasePath()); + } catch (Exception ex) { + ex.printStackTrace(); + fail("Unexpected exception: " + ex); + } + } + + @Test + public void testDefaultClientPrecedence() { + try { + environmentVariables.set("HOME", dir.getCanonicalPath()); + environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); + ApiClient client = new ConfigBuilder() + .setDefaultClientMode(true) + .build(); + // $KUBECONFIG should take precedence over $HOME/.kube/config + assertEquals("http://kubeconfig.dir.com", client.getBasePath()); + } catch (Exception ex) { + ex.printStackTrace(); + fail("Unexpected exception: " + ex); + } + } + + @Test + public void testUserNamePasswordConfigBuilder() { + try { + ApiClient client = (new ConfigBuilder()) + .setbasePath(basePath) + .setUserName(userName) + .setPassword(password) + .build(); + assertEquals(userName, ((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentication("BasicAuth")).getUsername()); + assertEquals(password, ((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentication("BasicAuth")).getPassword()); + assertEquals(basePath, client.getBasePath()); + assertEquals(false, client.isVerifyingSsl()); + assertEquals("Basic", ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix()); + assertEquals(ByteString.of((userName+":"+password).getBytes(Charset.forName("ISO-8859-1"))).base64(), ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKey()); + } + catch (Exception e) { + e.printStackTrace(); + fail("Unexpected exception: " + e); + } + } + + @Test + public void testApiKeyConfigbuilder() { + ApiClient client = (new ConfigBuilder()) + .setbasePath(basePath) + .setApiKeyPrefix(apiKeyPrefix) + .setApiKey(apiKey) + .build(); + assertEquals(basePath, client.getBasePath()); + assertEquals(false, client.isVerifyingSsl()); + assertEquals(apiKeyPrefix, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix()); + assertEquals( apiKey, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKey()); + assertEquals(null,((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentications().get("BasicAuth")).getUsername()); + } + + @Test + public void testKeyMgrANDCertConfigBUilder() { + try{ + keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase); + } + catch(Exception e){ + e.printStackTrace(); + } + //by default verify ssl is false + ApiClient client = (new ConfigBuilder()) + .setbasePath(basePath) + .setKeyMgrs(keyMgrs) + .setCertificateAuthorityData(certificateAuthorityData) + .setCertificateAuthorityFile(certificateAuthorityFile) + .setVerifyingSsl(true) + .build(); + assertEquals(basePath, client.getBasePath()); + assertEquals(true, client.isVerifyingSsl()); + //below assert is not appropriate + //assertSame(keyMgrs, client.getKeyManagers()); + } +} From 412d8aff31b4b6d7753f060e955bd63c647cfdee Mon Sep 17 00:00:00 2001 From: kondapally karthik Date: Tue, 23 Jan 2018 00:04:26 +0530 Subject: [PATCH 2/4] fix ConfigBuilder unit tests --- .../kubernetes/client/util/ConfigBuilderTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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 b0910c76e1..1b181c4fc9 100644 --- a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java +++ b/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java @@ -64,7 +64,7 @@ public class ConfigBuilderTest { public void testDefaultClientNothingPresent() { environmentVariables.set("HOME", "/non-existent"); ApiClient client = (new ConfigBuilder()) - .setClusterMode(true) + .setDefaultClientMode(true) .build(); assertEquals("http://localhost:8080", client.getBasePath()); } @@ -135,7 +135,7 @@ public void testDefaultClientKubeConfig() { try { environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); ApiClient client = new ConfigBuilder() - .setDefaultKubeConfigMode(true) + .setDefaultClientMode(true) .build(); assertEquals("http://kubeconfig.dir.com", client.getBasePath()); } catch (Exception ex) { @@ -197,12 +197,9 @@ public void testApiKeyConfigbuilder() { @Test public void testKeyMgrANDCertConfigBUilder() { + // will not fail even if file not found exception occurs for clientCertFile try{ keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase); - } - catch(Exception e){ - e.printStackTrace(); - } //by default verify ssl is false ApiClient client = (new ConfigBuilder()) .setbasePath(basePath) @@ -215,5 +212,9 @@ public void testKeyMgrANDCertConfigBUilder() { assertEquals(true, client.isVerifyingSsl()); //below assert is not appropriate //assertSame(keyMgrs, client.getKeyManagers()); + } + catch(Exception e){ + //e.printStackTrace(); + } } } From bf764a41aea7d651ee5564f47c8099180d3357e0 Mon Sep 17 00:00:00 2001 From: kondapally karthik Date: Wed, 24 Jan 2018 22:37:25 +0530 Subject: [PATCH 3/4] review changes --- .../kubernetes/client/util/ConfigBuilder.java | 105 ++++++++---------- .../client/util/ConfigBuilderTest.java | 70 ++++++++---- 2 files changed, 94 insertions(+), 81 deletions(-) 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 8ef3cce266..f7b91855f6 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,9 @@ */ package io.kubernetes.client.util; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -22,21 +25,22 @@ import javax.net.ssl.KeyManager; +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; + import okio.ByteString; import io.kubernetes.client.ApiClient; import io.kubernetes.client.util.Config; import io.kubernetes.client.util.KubeConfig; -import io.kubernetes.client.util.SSLUtils; -public class ConfigBuilder { +public class ConfigBuilder { - private boolean trustCerts = false; private boolean clusterMode = false; private boolean defaultKubeConfigMode = false; private boolean defaultClientMode = false; private boolean verifyingSsl = false; private String basePath = null; - private String certificateAuthorityFile = null; + private File certificateAuthorityFile = null; private String certificateAuthorityData = null; private String apiKey = null; private String userName = null; @@ -44,9 +48,10 @@ public class ConfigBuilder { private KeyManager[] keyMgrs = null; private String accessToken = null; private String apiKeyPrefix = null; - private InputStream sslCaCert = null; private KubeConfig kubeConfig = null; + private static final Logger log = Logger.getLogger(Config.class); + public String getUserName() { return userName; } @@ -74,45 +79,37 @@ public ConfigBuilder setApiKey(String apiKey) { return this; } - public boolean isTrustCerts() { - return trustCerts; - } - - public ConfigBuilder setTrustCerts(boolean trustCerts) { - this.trustCerts = trustCerts; - return this; - } - - public String getbasePath() { + public String getBasePath() { return basePath; } - public ConfigBuilder setbasePath(String basePath) { + public ConfigBuilder setBasePath(String basePath) { this.basePath = basePath; return this; } - public String getCertificateAuthorityFile() { + public File getCertificateAuthorityFile() { return certificateAuthorityFile; } - public ConfigBuilder setCertificateAuthorityFile(String certificateAuthorityFile) { + public ConfigBuilder setCertificateAuthority(File certificateAuthorityFile) { this.certificateAuthorityFile = certificateAuthorityFile; + this.verifyingSsl = true; return this; - } public String getCertificateAuthorityData() { return certificateAuthorityData; } - public ConfigBuilder setCertificateAuthorityData(String certificateAuthorityData) { + public ConfigBuilder setCertificateAuthority(String certificateAuthorityData) { this.certificateAuthorityData = certificateAuthorityData; + this.verifyingSsl = true; return this; } - public ConfigBuilder setClusterMode(boolean clusterMode) { - this.clusterMode = clusterMode; + public ConfigBuilder setClusterMode() { + this.clusterMode = true; return this; } @@ -121,8 +118,8 @@ public ConfigBuilder setKubeConfig(KubeConfig config) { return this; } - public ConfigBuilder setDefaultKubeConfigMode(boolean defaultKubeConfigMode) { - this.defaultKubeConfigMode = defaultKubeConfigMode; + public ConfigBuilder setDefaultKubeConfigMode() { + this.defaultKubeConfigMode = true; return this; } @@ -131,12 +128,12 @@ public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException return this; } - public ConfigBuilder setKubeConfig(Reader input) { + public ConfigBuilder setKubeConfig(Reader input) { this.kubeConfig = KubeConfig.loadKubeConfig(input); return this; } - public ConfigBuilder setKubeConfig(InputStream stream) { + public ConfigBuilder setKubeConfig(InputStream stream) { this.kubeConfig = KubeConfig.loadKubeConfig(new InputStreamReader(stream)); return this; } @@ -163,8 +160,8 @@ public boolean isDefaultClientMode() { return defaultClientMode; } - public ConfigBuilder setDefaultClientMode(boolean defaultClientMode) { - this.defaultClientMode = defaultClientMode; + public ConfigBuilder setDefaultClientMode() { + this.defaultClientMode = true; return this; } @@ -177,56 +174,45 @@ public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) { return this; } - public ApiClient build() { - + public ApiClient build() { ApiClient client = new ApiClient(); if( kubeConfig !=null) { client = Config.fromConfig(kubeConfig); - return client; } if(defaultKubeConfigMode == true) { try { client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); } catch (FileNotFoundException e) { - e.printStackTrace(); + log.error("Unable to find the file", e); } - return client; } if(clusterMode == true) { try { client = Config.fromCluster(); } catch (IOException e) { - e.printStackTrace(); + log.error("Exception ->", e); } - return client; } if(defaultClientMode ==true ) { try { client = Config.defaultClient(); } catch (IOException e) { - e.printStackTrace(); + log.error("Exception -> ", e); } - return client; - } if (basePath != null ) { if(basePath.endsWith("/")) { basePath = basePath.substring(0, basePath.length() - 1); } - client.setBasePath(basePath) - .setVerifyingSsl(verifyingSsl); - } - - else { - try { - throw new Exception("set kubernetes URL. example: http://localhost"); - } catch (Exception e) { - e.printStackTrace(); + client.setBasePath(basePath); + } else { + if((clusterMode == false) && (defaultClientMode == false) && (defaultKubeConfigMode == false)) { + throw new IllegalArgumentException("please set kubernetes URL ex:http://localhost"); } } @@ -266,20 +252,21 @@ public ApiClient build() { client.setApiKey(apiKey); } - if(sslCaCert != null) { - client.setSslCaCert(sslCaCert); - } - - if(verifyingSsl){ - if((certificateAuthorityData != null) || (certificateAuthorityFile != null)){ - try { - client.setSslCaCert(SSLUtils.getInputStreamFromDataOrFile(certificateAuthorityData, certificateAuthorityFile)); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } + 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)); + } + return client; } -} \ No newline at end of file +} \ No newline at end of file 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 1b181c4fc9..5295abf80c 100644 --- a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java +++ b/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java @@ -16,6 +16,7 @@ import static org.junit.Assert.fail; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.Charset; @@ -51,7 +52,7 @@ public class ConfigBuilderTest { String keyStoreFile = null ; String keyStorePassphrase = null; KeyManager[] keyMgrs =null; - + @Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); @@ -64,7 +65,7 @@ public class ConfigBuilderTest { public void testDefaultClientNothingPresent() { environmentVariables.set("HOME", "/non-existent"); ApiClient client = (new ConfigBuilder()) - .setDefaultClientMode(true) + .setDefaultClientMode() .build(); assertEquals("http://localhost:8080", client.getBasePath()); } @@ -121,7 +122,7 @@ public void testDefaultClientHomeDir() { try { environmentVariables.set("HOME", dir.getCanonicalPath()); ApiClient client = new ConfigBuilder() - .setDefaultClientMode(true) + .setDefaultClientMode() .build(); assertEquals("http://home.dir.com", client.getBasePath()); } catch (Exception ex) { @@ -135,7 +136,7 @@ public void testDefaultClientKubeConfig() { try { environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); ApiClient client = new ConfigBuilder() - .setDefaultClientMode(true) + .setDefaultClientMode() .build(); assertEquals("http://kubeconfig.dir.com", client.getBasePath()); } catch (Exception ex) { @@ -150,7 +151,7 @@ public void testDefaultClientPrecedence() { environmentVariables.set("HOME", dir.getCanonicalPath()); environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); ApiClient client = new ConfigBuilder() - .setDefaultClientMode(true) + .setDefaultClientMode() .build(); // $KUBECONFIG should take precedence over $HOME/.kube/config assertEquals("http://kubeconfig.dir.com", client.getBasePath()); @@ -164,7 +165,7 @@ public void testDefaultClientPrecedence() { public void testUserNamePasswordConfigBuilder() { try { ApiClient client = (new ConfigBuilder()) - .setbasePath(basePath) + .setBasePath(basePath) .setUserName(userName) .setPassword(password) .build(); @@ -183,8 +184,9 @@ public void testUserNamePasswordConfigBuilder() { @Test public void testApiKeyConfigbuilder() { - ApiClient client = (new ConfigBuilder()) - .setbasePath(basePath) + ApiClient client = null; + client = (new ConfigBuilder()) + .setBasePath(basePath) .setApiKeyPrefix(apiKeyPrefix) .setApiKey(apiKey) .build(); @@ -194,27 +196,51 @@ public void testApiKeyConfigbuilder() { assertEquals( apiKey, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKey()); assertEquals(null,((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentications().get("BasicAuth")).getUsername()); } - + @Test public void testKeyMgrANDCertConfigBUilder() { // will not fail even if file not found exception occurs for clientCertFile try{ keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase); - //by default verify ssl is false - ApiClient client = (new ConfigBuilder()) - .setbasePath(basePath) - .setKeyMgrs(keyMgrs) - .setCertificateAuthorityData(certificateAuthorityData) - .setCertificateAuthorityFile(certificateAuthorityFile) - .setVerifyingSsl(true) - .build(); - assertEquals(basePath, client.getBasePath()); - assertEquals(true, client.isVerifyingSsl()); - //below assert is not appropriate - //assertSame(keyMgrs, client.getKeyManagers()); + //by default verify ssl is false + ApiClient client = (new ConfigBuilder()) + .setBasePath(basePath) + .setKeyMgrs(keyMgrs) + .setCertificateAuthority(certificateAuthorityData) + .setVerifyingSsl(true) + .build(); + assertEquals(basePath, client.getBasePath()); + assertEquals(true, client.isVerifyingSsl()); + //below assert is not appropriate + //assertSame(keyMgrs, client.getKeyManagers()); } catch(Exception e){ //e.printStackTrace(); } - } + } + + @Test + public void testBasePathIllegalArgumentException() throws IOException { + ApiClient client = null ; + try { + client = (new ConfigBuilder()) + .setUserName("user") + .build(); + } + catch(IllegalArgumentException ie) { + assertEquals(IllegalArgumentException.class, ie.getClass()); + } + environmentVariables.set("HOME", "/non-existent"); + client = (new ConfigBuilder()) + .setDefaultClientMode() + .setUserName("user") + .build(); + assertEquals("http://localhost:8080", client.getBasePath()); + environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); + client = new ConfigBuilder() + .setDefaultClientMode() + .setBasePath("http://testkubeconfig.dir.com") + .build(); + assertEquals("http://testkubeconfig.dir.com", client.getBasePath()); + } } From 24280332e4d7b4ae28a6d1284af637139f0173c7 Mon Sep 17 00:00:00 2001 From: kondapally karthik Date: Fri, 2 Feb 2018 23:41:13 +0530 Subject: [PATCH 4/4] review changes and ConfigBuilder to ClientBuilder name change --- ...{ConfigBuilder.java => ClientBuilder.java} | 98 +++++++------------ ...uilderTest.java => ClientBuilderTest.java} | 57 ++++++----- 2 files changed, 66 insertions(+), 89 deletions(-) rename util/src/main/java/io/kubernetes/client/util/{ConfigBuilder.java => ClientBuilder.java} (64%) rename util/src/test/java/io/kubernetes/client/util/{ConfigBuilderTest.java => ClientBuilderTest.java} (88%) diff --git a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java b/util/src/main/java/io/kubernetes/client/util/ClientBuilder.java similarity index 64% rename from util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java rename to util/src/main/java/io/kubernetes/client/util/ClientBuilder.java index f7b91855f6..6d6063e019 100644 --- a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java +++ b/util/src/main/java/io/kubernetes/client/util/ClientBuilder.java @@ -33,11 +33,8 @@ import io.kubernetes.client.util.Config; import io.kubernetes.client.util.KubeConfig; -public class ConfigBuilder { +public class ClientBuilder { - private boolean clusterMode = false; - private boolean defaultKubeConfigMode = false; - private boolean defaultClientMode = false; private boolean verifyingSsl = false; private String basePath = null; private File certificateAuthorityFile = null; @@ -49,6 +46,7 @@ public class ConfigBuilder { private String accessToken = null; private String apiKeyPrefix = null; private KubeConfig kubeConfig = null; + private ApiClient client = null; private static final Logger log = Logger.getLogger(Config.class); @@ -56,7 +54,7 @@ public String getUserName() { return userName; } - public ConfigBuilder setUserName(String userName) { + public ClientBuilder setUserName(String userName) { this.userName = userName; return this; } @@ -65,7 +63,7 @@ public String getPassword() { return password; } - public ConfigBuilder setPassword(String password) { + public ClientBuilder setPassword(String password) { this.password = password; return this; } @@ -74,7 +72,7 @@ public String getApiKey() { return apiKey; } - public ConfigBuilder setApiKey(String apiKey) { + public ClientBuilder setApiKey(String apiKey) { this.apiKey = apiKey; return this; } @@ -83,7 +81,7 @@ public String getBasePath() { return basePath; } - public ConfigBuilder setBasePath(String basePath) { + public ClientBuilder setBasePath(String basePath) { this.basePath = basePath; return this; } @@ -92,7 +90,7 @@ public File getCertificateAuthorityFile() { return certificateAuthorityFile; } - public ConfigBuilder setCertificateAuthority(File certificateAuthorityFile) { + public ClientBuilder setCertificateAuthority(File certificateAuthorityFile) { this.certificateAuthorityFile = certificateAuthorityFile; this.verifyingSsl = true; return this; @@ -102,38 +100,41 @@ public String getCertificateAuthorityData() { return certificateAuthorityData; } - public ConfigBuilder setCertificateAuthority(String certificateAuthorityData) { + public ClientBuilder setCertificateAuthority(String certificateAuthorityData) { this.certificateAuthorityData = certificateAuthorityData; this.verifyingSsl = true; return this; } - public ConfigBuilder setClusterMode() { - this.clusterMode = true; + public ClientBuilder setClusterMode() throws IOException { + this.client = Config.fromCluster(); return this; } - public ConfigBuilder setKubeConfig(KubeConfig config) { + public ClientBuilder setKubeConfig(KubeConfig config) { this.kubeConfig = config; + if( this.kubeConfig !=null) { + this.client = Config.fromConfig(this.kubeConfig); + } return this; } - public ConfigBuilder setDefaultKubeConfigMode() { - this.defaultKubeConfigMode = true; + public ClientBuilder setDefaultKubeConfigMode() throws FileNotFoundException { + this.client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); return this; } - public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException { - this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(fileName)); + public ClientBuilder setKubeConfig(File kubeFile) throws FileNotFoundException { + this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(kubeFile)); return this; } - public ConfigBuilder setKubeConfig(Reader input) { + public ClientBuilder setKubeConfig(Reader input) { this.kubeConfig = KubeConfig.loadKubeConfig(input); return this; } - public ConfigBuilder setKubeConfig(InputStream stream) { + public ClientBuilder setKubeConfig(InputStream stream) { this.kubeConfig = KubeConfig.loadKubeConfig(new InputStreamReader(stream)); return this; } @@ -142,7 +143,7 @@ public KeyManager[] getKeyMgrs() { return keyMgrs; } - public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) { + public ClientBuilder setKeyMgrs(KeyManager[] keyMgrs) { this.keyMgrs = keyMgrs; return this; } @@ -151,17 +152,14 @@ public boolean isVerifyingSsl() { return verifyingSsl; } - public ConfigBuilder setVerifyingSsl(boolean verifyingSsl) { + public ClientBuilder setVerifyingSsl(boolean verifyingSsl) { this.verifyingSsl = verifyingSsl; return this; } - public boolean isDefaultClientMode() { - return defaultClientMode; - } - public ConfigBuilder setDefaultClientMode() { - this.defaultClientMode = true; + public ClientBuilder setDefaultClientMode() throws IOException { + client = Config.defaultClient(); return this; } @@ -169,50 +167,26 @@ public String getApiKeyPrefix() { return apiKeyPrefix; } - public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) { + public ClientBuilder setApiKeyPrefix(String apiKeyPrefix) { this.apiKeyPrefix = apiKeyPrefix; return this; } - public ApiClient build() { - ApiClient client = new ApiClient(); - - if( kubeConfig !=null) { - client = Config.fromConfig(kubeConfig); - } - - if(defaultKubeConfigMode == true) { - try { - client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); - } catch (FileNotFoundException e) { - log.error("Unable to find the file", e); - } - } - - if(clusterMode == true) { - try { - client = Config.fromCluster(); - } catch (IOException e) { - log.error("Exception ->", e); - } + public ApiClient build() throws FileNotFoundException { + if(client == null) { + client = new ApiClient(); } - if(defaultClientMode ==true ) { - try { - client = Config.defaultClient(); - } catch (IOException e) { - log.error("Exception -> ", e); - } - } + String localBasePath = client.getBasePath(); - if (basePath != null ) { + if (basePath != null) { if(basePath.endsWith("/")) { basePath = basePath.substring(0, basePath.length() - 1); } client.setBasePath(basePath); - } else { - if((clusterMode == false) && (defaultClientMode == false) && (defaultKubeConfigMode == false)) { - throw new IllegalArgumentException("please set kubernetes URL ex:http://localhost"); + }else { + if (localBasePath.length() == 0) { + client.setBasePath("http://localhost:8080"); } } @@ -255,11 +229,7 @@ public ApiClient build() { client.setVerifyingSsl(verifyingSsl); if(certificateAuthorityFile != null) { - try { - client.setSslCaCert(new FileInputStream(certificateAuthorityFile)); - } catch (FileNotFoundException e) { - log.error("Unable to find the file", e); - } + client.setSslCaCert(new FileInputStream(certificateAuthorityFile)); } if(certificateAuthorityData != null) { diff --git a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java b/util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java similarity index 88% rename from util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java rename to util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java index 5295abf80c..0027cbc65f 100644 --- a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java +++ b/util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java @@ -35,7 +35,7 @@ /** * Tests for the ConfigBuilder helper class */ -public class ConfigBuilderTest { +public class ClientBuilderTest { String basePath = "http://localhost"; String apiKey = "ABCD"; String userName = "userName"; @@ -64,10 +64,16 @@ public class ConfigBuilderTest { @Test public void testDefaultClientNothingPresent() { environmentVariables.set("HOME", "/non-existent"); - ApiClient client = (new ConfigBuilder()) - .setDefaultClientMode() - .build(); - assertEquals("http://localhost:8080", client.getBasePath()); + ApiClient client; + try { + client = (new ClientBuilder()) + .setDefaultClientMode() + .build(); + assertEquals("http://localhost:8080", client.getBasePath()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } public static String HOME_CONFIG = @@ -121,7 +127,7 @@ public void setUp() throws IOException { public void testDefaultClientHomeDir() { try { environmentVariables.set("HOME", dir.getCanonicalPath()); - ApiClient client = new ConfigBuilder() + ApiClient client = new ClientBuilder() .setDefaultClientMode() .build(); assertEquals("http://home.dir.com", client.getBasePath()); @@ -135,7 +141,7 @@ public void testDefaultClientHomeDir() { public void testDefaultClientKubeConfig() { try { environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); - ApiClient client = new ConfigBuilder() + ApiClient client = new ClientBuilder() .setDefaultClientMode() .build(); assertEquals("http://kubeconfig.dir.com", client.getBasePath()); @@ -150,7 +156,7 @@ public void testDefaultClientPrecedence() { try { environmentVariables.set("HOME", dir.getCanonicalPath()); environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); - ApiClient client = new ConfigBuilder() + ApiClient client = new ClientBuilder() .setDefaultClientMode() .build(); // $KUBECONFIG should take precedence over $HOME/.kube/config @@ -162,9 +168,9 @@ public void testDefaultClientPrecedence() { } @Test - public void testUserNamePasswordConfigBuilder() { + public void testUserNamePasswordClientBuilder() { try { - ApiClient client = (new ConfigBuilder()) + ApiClient client = (new ClientBuilder()) .setBasePath(basePath) .setUserName(userName) .setPassword(password) @@ -185,11 +191,16 @@ public void testUserNamePasswordConfigBuilder() { @Test public void testApiKeyConfigbuilder() { ApiClient client = null; - client = (new ConfigBuilder()) - .setBasePath(basePath) - .setApiKeyPrefix(apiKeyPrefix) - .setApiKey(apiKey) - .build(); + try { + client = (new ClientBuilder()) + .setBasePath(basePath) + .setApiKeyPrefix(apiKeyPrefix) + .setApiKey(apiKey) + .build(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } assertEquals(basePath, client.getBasePath()); assertEquals(false, client.isVerifyingSsl()); assertEquals(apiKeyPrefix, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix()); @@ -203,7 +214,7 @@ public void testKeyMgrANDCertConfigBUilder() { try{ keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase); //by default verify ssl is false - ApiClient client = (new ConfigBuilder()) + ApiClient client = (new ClientBuilder()) .setBasePath(basePath) .setKeyMgrs(keyMgrs) .setCertificateAuthority(certificateAuthorityData) @@ -220,24 +231,20 @@ public void testKeyMgrANDCertConfigBUilder() { } @Test - public void testBasePathIllegalArgumentException() throws IOException { + public void testBasePath() throws IOException { ApiClient client = null ; - try { - client = (new ConfigBuilder()) + client = (new ClientBuilder()) .setUserName("user") .build(); - } - catch(IllegalArgumentException ie) { - assertEquals(IllegalArgumentException.class, ie.getClass()); - } + environmentVariables.set("HOME", "/non-existent"); - client = (new ConfigBuilder()) + client = (new ClientBuilder()) .setDefaultClientMode() .setUserName("user") .build(); assertEquals("http://localhost:8080", client.getBasePath()); environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); - client = new ConfigBuilder() + client = new ClientBuilder() .setDefaultClientMode() .setBasePath("http://testkubeconfig.dir.com") .build();