Skip to content

Commit f55b762

Browse files
committed
Start implementation focused around ClientBuilder instead of Config
1 parent 3b9000b commit f55b762

12 files changed

+395
-411
lines changed

util/src/main/java/io/kubernetes/client/util/ClientBuilder.java

+172-213
Large diffs are not rendered by default.

util/src/main/java/io/kubernetes/client/util/Config.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package io.kubernetes.client.util;
1414

1515
import io.kubernetes.client.ApiClient;
16+
import java.io.ByteArrayInputStream;
1617
import okio.ByteString;
1718
import org.apache.log4j.Logger;
1819

@@ -121,10 +122,8 @@ public static ApiClient fromConfig(KubeConfig config) {
121122

122123
try {
123124
KeyManager[] mgrs = SSLUtils.keyManagers(
124-
config.getClientCertificateData(),
125-
config.getClientCertificateFile(),
126-
config.getClientKeyData(),
127-
config.getClientKeyFile(),
125+
KubeConfig.getDataOrFile(config.getClientCertificateData(), config.getClientCertificateFile()),
126+
KubeConfig.getDataOrFile(config.getClientKeyData(), config.getClientKeyFile()),
128127
"RSA", "",
129128
null, null);
130129
client.setKeyManagers(mgrs);
@@ -136,14 +135,14 @@ public static ApiClient fromConfig(KubeConfig config) {
136135
// It's silly to have to do it in this order, but each SSL setup
137136
// consumes the CA cert, so if we do this before the client certs
138137
// are injected the cert input stream is exhausted and things get
139-
// grumpy'
138+
// grumpy
140139
String caCert = config.getCertificateAuthorityData();
141140
String caCertFile = config.getCertificateAuthorityFile();
142141
if (caCert != null || caCertFile != null) {
143142
try {
144-
client.setSslCaCert(SSLUtils.getInputStreamFromDataOrFile(caCert, caCertFile));
145-
} catch (FileNotFoundException ex) {
146-
log.error("Failed to find CA Cert file", ex);
143+
client.setSslCaCert(new ByteArrayInputStream(KubeConfig.getDataOrFile(caCert, caCertFile)));
144+
} catch (IOException ex) {
145+
log.error("Failed to read CA Cert file", ex);
147146
}
148147
}
149148
} else {

util/src/main/java/io/kubernetes/client/util/KubeConfig.java

+14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
*/
1313
package io.kubernetes.client.util;
1414

15+
import com.google.common.base.Charsets;
1516
import io.kubernetes.client.util.authenticators.Authenticator;
17+
import java.nio.file.Paths;
18+
import org.apache.commons.codec.binary.Base64;
1619
import org.apache.log4j.Logger;
1720
import org.yaml.snakeyaml.Yaml;
1821
import org.yaml.snakeyaml.constructor.SafeConstructor;
@@ -233,4 +236,15 @@ private static Map<String, Object> findObject(ArrayList<Object> list, String nam
233236
}
234237
return null;
235238
}
239+
240+
public static byte[] getDataOrFile(final String data, final String file)
241+
throws IOException {
242+
if(data != null) {
243+
return Base64.decodeBase64(data);
244+
}
245+
if(file != null) {
246+
return Files.readAllBytes(Paths.get(file));
247+
}
248+
return null;
249+
}
236250
}

util/src/main/java/io/kubernetes/client/util/SSLUtils.java

+8-22
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ public static boolean isNotNullOrEmpty(String val) {
5353
return val != null && val.length() > 0;
5454
}
5555

56-
public static KeyManager[] keyManagers(String certData, String certFile, String keyData, String keyFile,
56+
public static KeyManager[] keyManagers(byte[] certData, byte[] keyData,
5757
String algo, String passphrase, String keyStoreFile, String keyStorePassphrase)
5858
throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, CertificateException,
5959
InvalidKeySpecException, IOException {
6060
KeyManager[] keyManagers = null;
61-
if ((isNotNullOrEmpty(certData) || isNotNullOrEmpty(certFile))
62-
&& (isNotNullOrEmpty(keyData) || isNotNullOrEmpty(keyFile))) {
63-
KeyStore keyStore = createKeyStore(certData, certFile, keyData, keyFile, algo, passphrase, keyStoreFile,
61+
if (certData != null && keyData != null) {
62+
KeyStore keyStore = createKeyStore(certData, keyData, algo, passphrase, keyStoreFile,
6463
keyStorePassphrase);
6564
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
6665
kmf.init(keyStore, passphrase.toCharArray());
@@ -69,12 +68,11 @@ public static KeyManager[] keyManagers(String certData, String certFile, String
6968
return keyManagers;
7069
}
7170

72-
public static KeyStore createKeyStore(String clientCertData, String clientCertFile, String clientKeyData,
73-
String clientKeyFile, String clientKeyAlgo, String clientKeyPassphrase, String keyStoreFile,
74-
String keyStorePassphrase) throws IOException, CertificateException, NoSuchAlgorithmException,
75-
InvalidKeySpecException, KeyStoreException {
76-
try (InputStream certInputStream = getInputStreamFromDataOrFile(clientCertData, clientCertFile);
77-
InputStream keyInputStream = getInputStreamFromDataOrFile(clientKeyData, clientKeyFile)) {
71+
public static KeyStore createKeyStore(byte[] clientCertData, byte[] clientKeyData, String clientKeyAlgo,
72+
String clientKeyPassphrase, String keyStoreFile, String keyStorePassphrase) throws IOException,
73+
CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException {
74+
try (InputStream certInputStream = new ByteArrayInputStream(clientCertData);
75+
InputStream keyInputStream = new ByteArrayInputStream(clientKeyData)) {
7876
return createKeyStore(certInputStream, keyInputStream, clientKeyAlgo,
7977
clientKeyPassphrase != null ? clientKeyPassphrase.toCharArray() : null,
8078
keyStoreFile, getKeyStorePassphrase(keyStorePassphrase));
@@ -264,18 +262,6 @@ private static boolean loadDefaultStoreFile(KeyStore keyStore, File fileToLoad,
264262
return false;
265263
}
266264

267-
public static InputStream getInputStreamFromDataOrFile(String data, String file) throws FileNotFoundException {
268-
if (data != null) {
269-
byte[] bytes = Base64.decodeBase64(data);
270-
// TODO handle non-base64 here?
271-
return new ByteArrayInputStream(bytes);
272-
}
273-
if (file != null) {
274-
return new FileInputStream(file);
275-
}
276-
return null;
277-
}
278-
279265
private static char[] getKeyStorePassphrase(String keyStorePassphrase) {
280266
if (keyStorePassphrase == null || keyStorePassphrase.length() == 0) {
281267
return System.getProperty("javax.net.ssl.keyStorePassword", "changeit").toCharArray();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import io.kubernetes.client.ApiClient;
4+
5+
public class AccessTokenCredentialProvider implements CredentialProvider {
6+
private String token;
7+
8+
public AccessTokenCredentialProvider(final String token) {
9+
this.token = token;
10+
}
11+
12+
@Override public void provide(ApiClient client) {
13+
client.setApiKeyPrefix("Bearer");
14+
client.setApiKey(token);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import io.kubernetes.client.ApiClient;
4+
import io.kubernetes.client.util.SSLUtils;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.security.KeyStoreException;
8+
import java.security.NoSuchAlgorithmException;
9+
import java.security.UnrecoverableKeyException;
10+
import java.security.cert.CertificateException;
11+
import java.security.spec.InvalidKeySpecException;
12+
import javax.net.ssl.KeyManager;
13+
import org.apache.log4j.Logger;
14+
15+
public class ClientCertificateCredentialProvider implements CredentialProvider {
16+
private static final Logger log = Logger.getLogger(ClientCertificateCredentialProvider.class);
17+
private final byte[] certificate;
18+
private final byte[] key;
19+
20+
public ClientCertificateCredentialProvider(final byte[] certificate, final byte[] key) {
21+
this.certificate = certificate;
22+
this.key = key;
23+
}
24+
25+
@Override public void provide(ApiClient client) {
26+
try {
27+
final KeyManager[] keyManagers =
28+
SSLUtils.keyManagers(certificate, key, "RSA", "", null, null);
29+
client.setKeyManagers(keyManagers);
30+
} catch (NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException | KeyStoreException | InvalidKeySpecException | IOException e) {
31+
log.warn("Could not create key manager for Client Certificate authentication.", e);
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import io.kubernetes.client.ApiClient;
4+
5+
public interface CredentialProvider {
6+
7+
void provide(ApiClient client);
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import io.kubernetes.client.ApiClient;
4+
import io.kubernetes.client.util.KubeConfig;
5+
import java.io.IOException;
6+
7+
public class KubeconfigCredentialProvider implements CredentialProvider {
8+
9+
private final String username;
10+
private final String password;
11+
private final String token;
12+
private final byte[] clientCert;
13+
private final byte[] clientKey;
14+
15+
public KubeconfigCredentialProvider(final KubeConfig config) throws IOException {
16+
this.clientCert = KubeConfig.getDataOrFile(config.getClientCertificateData(), config.getClientCertificateFile());
17+
this.clientKey = KubeConfig.getDataOrFile(config.getClientKeyData(), config.getClientKeyFile());
18+
this.username = config.getUsername();
19+
this.password = config.getPassword();
20+
this.token = config.getAccessToken();
21+
}
22+
23+
@Override public void provide(ApiClient client) {
24+
if(clientCert != null && clientKey != null) {
25+
new ClientCertificateCredentialProvider(clientCert, clientKey);
26+
}
27+
28+
if(username != null && password != null) {
29+
new UsernamePasswordCredentialProvider(username, password).provide(client);
30+
}
31+
32+
if(token != null) {
33+
new AccessTokenCredentialProvider(token).provide(client);
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import io.kubernetes.client.ApiClient;
4+
import java.nio.charset.Charset;
5+
import okio.ByteString;
6+
7+
public class UsernamePasswordCredentialProvider implements CredentialProvider {
8+
private final String username;
9+
private final String password;
10+
11+
public UsernamePasswordCredentialProvider(final String username, final String password) {
12+
this.username = username;
13+
this.password = password;
14+
}
15+
16+
@Override public void provide(ApiClient client) {
17+
final String usernameAndPassword = username + ":" + password;
18+
client.setApiKeyPrefix("Basic");
19+
client.setApiKey(ByteString.of(usernameAndPassword.getBytes(Charset.forName("ISO-8859-1"))).base64());
20+
}
21+
}

0 commit comments

Comments
 (0)