Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikkondapally committed Jan 24, 2018
1 parent 412d8af commit bf764a4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 81 deletions.
105 changes: 46 additions & 59 deletions util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,31 +25,33 @@

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;
private String password = null;
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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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;
}
}
}
70 changes: 48 additions & 22 deletions util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class ConfigBuilderTest {
String keyStoreFile = null ;
String keyStorePassphrase = null;
KeyManager[] keyMgrs =null;

@Rule
public final EnvironmentVariables environmentVariables
= new EnvironmentVariables();
Expand All @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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());
Expand All @@ -164,7 +165,7 @@ public void testDefaultClientPrecedence() {
public void testUserNamePasswordConfigBuilder() {
try {
ApiClient client = (new ConfigBuilder())
.setbasePath(basePath)
.setBasePath(basePath)
.setUserName(userName)
.setPassword(password)
.build();
Expand All @@ -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();
Expand All @@ -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());
}
}

0 comments on commit bf764a4

Please sign in to comment.