Skip to content

Commit

Permalink
Add CredentialProvider concept
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisheadden committed Jan 27, 2018
1 parent bf764a4 commit ac7c1b5
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 127 deletions.
128 changes: 11 additions & 117 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,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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();

Expand All @@ -197,7 +135,7 @@ public ApiClient build() {
}
}

if(defaultClientMode ==true ) {
if(defaultClientMode ==true) {
try {
client = Config.defaultClient();
} catch (IOException e) {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.kubernetes.client.util.credentials;

import io.kubernetes.client.ApiClient;

public interface CredentialProvider {

void provide(final ApiClient client);

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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) {
Expand All @@ -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());
Expand Down

0 comments on commit ac7c1b5

Please sign in to comment.