Skip to content

Commit a365381

Browse files
committed
Add more test. Rename CredentialProvider to Authentication
1 parent 823aa70 commit a365381

13 files changed

+261
-94
lines changed

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

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@
1212
*/
1313
package io.kubernetes.client.util;
1414

15-
import io.kubernetes.client.util.credentials.AccessTokenCredentialProvider;
16-
import io.kubernetes.client.util.credentials.CredentialProvider;
17-
import io.kubernetes.client.util.credentials.KubeconfigCredentialProvider;
15+
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
16+
import io.kubernetes.client.util.credentials.Authentication;
17+
import io.kubernetes.client.util.credentials.KubeconfigAuthentication;
1818
import java.io.ByteArrayInputStream;
1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.FileNotFoundException;
2221
import java.io.FileReader;
2322
import java.io.IOException;
2423

25-
import java.io.InputStream;
24+
import java.io.Reader;
2625
import java.nio.charset.Charset;
2726
import java.nio.file.Files;
2827
import java.nio.file.Paths;
29-
import org.apache.commons.codec.binary.Base64;
3028
import org.apache.log4j.Logger;
3129

3230
import io.kubernetes.client.ApiClient;
@@ -38,45 +36,73 @@
3836
import static io.kubernetes.client.util.Config.SERVICEACCOUNT_TOKEN_PATH;
3937
import static io.kubernetes.client.util.KubeConfig.*;
4038

39+
/**
40+
* A Builder which allows the construction of {@link ApiClient}s in a fluent fashion.
41+
*/
4142
public class ClientBuilder {
4243

4344
private static final Logger log = Logger.getLogger(ClientBuilder.class);
4445

4546
private String basePath = Config.DEFAULT_FALLBACK_HOST;
4647
private byte[] caCertBytes = null;
4748
private boolean verifyingSsl = true;
48-
private CredentialProvider credentialProvider;
49+
private Authentication authentication;
50+
51+
/**
52+
* Creates an {@link ApiClient} by calling {@link #standard()} and {@link #build()}.
53+
*
54+
* @return An <tt>ApiClient</tt> configured using the precedence specified for {@link #standard()}.
55+
* @throws IOException
56+
* if the configuration file or a file specified in a configuration file cannot be read.
57+
*/
58+
public static ApiClient defaultClient() throws IOException {
59+
return ClientBuilder.standard().build();
60+
}
4961

50-
public static ClientBuilder defaults() throws IOException {
62+
/**
63+
* Creates a builder which is pre-configured in the following way
64+
*
65+
* <ul>
66+
* <li>If $KUBECONFIG is defined, use that config file.</li>
67+
* <li>If $HOME/.kube/config can be found, use that.</li>
68+
* <li>If the in-cluster service account can be found, assume in cluster config.</li>
69+
* <li>Default to localhost:8080 as a last resort.</li>
70+
* </ul>
71+
*
72+
* @return <tt>ClientBuilder</tt> pre-configured using the above precedence
73+
* @throws IOException
74+
* if the configuration file or a file specified in a configuration file cannot be read.
75+
*/
76+
public static ClientBuilder standard() throws IOException {
5177
final FileReader kubeConfigReader = findConfigFromEnv();
5278
if(kubeConfigReader != null) {
53-
return fromKubeConfig(loadKubeConfig(kubeConfigReader));
79+
return kubeconfig(loadKubeConfig(kubeConfigReader));
5480
}
5581
final FileReader configReader = findConfigInHomeDir();
5682
if(configReader != null) {
57-
return fromKubeConfig(loadKubeConfig(configReader));
83+
return kubeconfig(loadKubeConfig(configReader));
5884
}
5985
final File clusterCa = new File(SERVICEACCOUNT_CA_PATH);
6086
if (clusterCa.exists()) {
61-
return fromCluster();
87+
return cluster();
6288
}
6389
return new ClientBuilder();
6490
}
6591

66-
private static FileReader findConfigFromEnv() throws FileNotFoundException {
92+
private static FileReader findConfigFromEnv() {
6793
try {
6894
String kubeConfig = System.getenv(ENV_KUBECONFIG);
6995
if(kubeConfig == null) {
7096
return null;
7197
}
7298
return new FileReader(kubeConfig);
7399
} catch (FileNotFoundException e) {
74-
log.info("Could not find KUBECONFIG in environment");
100+
log.info("Could not find file specified in $KUBECONFIG");
75101
return null;
76102
}
77103
}
78104

79-
private static FileReader findConfigInHomeDir() throws FileNotFoundException {
105+
private static FileReader findConfigInHomeDir() {
80106
try {
81107
File config = new File(new File(System.getenv(ENV_HOME), KUBEDIR), KUBECONFIG);
82108
return new FileReader(config);
@@ -86,7 +112,14 @@ private static FileReader findConfigInHomeDir() throws FileNotFoundException {
86112
}
87113
}
88114

89-
public static ClientBuilder fromCluster() throws IOException {
115+
/**
116+
* Creates a builder which is pre-configured from the cluster configuration.
117+
*
118+
* @return <tt>ClientBuilder</tt> configured from the cluster configuration.
119+
* @throws IOException
120+
* if the Service Account Token Path or CA Path is not readable.
121+
*/
122+
public static ClientBuilder cluster() throws IOException {
90123
final ClientBuilder builder = new ClientBuilder();
91124

92125
final String host = System.getenv(ENV_SERVICE_HOST);
@@ -96,12 +129,23 @@ public static ClientBuilder fromCluster() throws IOException {
96129
final String token = new String(Files.readAllBytes(Paths.get(SERVICEACCOUNT_TOKEN_PATH)),
97130
Charset.defaultCharset());
98131
builder.setCertificateAuthority(Files.readAllBytes(Paths.get(SERVICEACCOUNT_CA_PATH)));
99-
builder.setCredentialProvider(new AccessTokenCredentialProvider(token));
132+
builder.setAuthentication(new AccessTokenAuthentication(token));
100133

101134
return builder;
102135
}
103136

104-
public static ClientBuilder fromKubeConfig(KubeConfig config) throws IOException {
137+
/**
138+
* Creates a builder which is pre-configured from a {@link KubeConfig}.
139+
*
140+
* To load a <tt>KubeConfig</tt>, see {@link KubeConfig#loadKubeConfig(Reader)}.
141+
*
142+
* @param config
143+
* The {@link KubeConfig} to configure the builder from.
144+
* @return <tt>ClientBuilder</tt> configured from the provided <tt>KubeConfig</tt>
145+
* @throws IOException
146+
* if the files specified in the provided <tt>KubeConfig</tt> are not readable
147+
*/
148+
public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
105149
final ClientBuilder builder = new ClientBuilder();
106150

107151
String server = config.getServer();
@@ -122,7 +166,7 @@ public static ClientBuilder fromKubeConfig(KubeConfig config) throws IOException
122166
}
123167

124168
builder.setBasePath(server);
125-
builder.setCredentialProvider(new KubeconfigCredentialProvider(config));
169+
builder.setAuthentication(new KubeconfigAuthentication(config));
126170
return builder;
127171
}
128172

@@ -135,12 +179,12 @@ public ClientBuilder setBasePath(String basePath) {
135179
return this;
136180
}
137181

138-
public CredentialProvider getCredentialProvider() {
139-
return credentialProvider;
182+
public Authentication getAuthentication() {
183+
return authentication;
140184
}
141185

142-
public ClientBuilder setCredentialProvider(final CredentialProvider credentialProvider) {
143-
this.credentialProvider = credentialProvider;
186+
public ClientBuilder setAuthentication(final Authentication authentication) {
187+
this.authentication = authentication;
144188
return this;
145189
}
146190

@@ -175,8 +219,8 @@ public ApiClient build() {
175219
client.setSslCaCert(new ByteArrayInputStream(caCertBytes));
176220
}
177221

178-
if (credentialProvider != null) {
179-
credentialProvider.provide(client);
222+
if (authentication != null) {
223+
authentication.provide(client);
180224
}
181225

182226
return client;

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,15 @@
1313
package io.kubernetes.client.util;
1414

1515
import io.kubernetes.client.ApiClient;
16-
import io.kubernetes.client.util.credentials.AccessTokenCredentialProvider;
17-
import io.kubernetes.client.util.credentials.UsernamePasswordCredentialProvider;
18-
import java.io.ByteArrayInputStream;
19-
import okio.ByteString;
16+
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
17+
import io.kubernetes.client.util.credentials.UsernamePasswordAuthentication;
2018
import org.apache.log4j.Logger;
2119

22-
import javax.net.ssl.KeyManager;
23-
import java.io.BufferedReader;
24-
import java.io.File;
25-
import java.io.FileInputStream;
26-
import java.io.FileNotFoundException;
2720
import java.io.FileReader;
2821
import java.io.IOException;
2922
import java.io.InputStream;
3023
import java.io.InputStreamReader;
3124
import java.io.Reader;
32-
import java.nio.charset.Charset;
3325

3426
public class Config {
3527
public static final String SERVICEACCOUNT_ROOT =
@@ -47,7 +39,7 @@ public class Config {
4739
private static final Logger log = Logger.getLogger(Config.class);
4840

4941
public static ApiClient fromCluster() throws IOException {
50-
return ClientBuilder.fromCluster().build();
42+
return ClientBuilder.cluster().build();
5143
}
5244

5345
public static ApiClient fromUrl(String url) {
@@ -67,7 +59,7 @@ public static ApiClient fromUserPassword(String url, String user, String passwor
6759
public static ApiClient fromUserPassword(String url, String user, String password, boolean validateSSL) {
6860
return new ClientBuilder()
6961
.setBasePath(url)
70-
.setCredentialProvider(new UsernamePasswordCredentialProvider(user, password))
62+
.setAuthentication(new UsernamePasswordAuthentication(user, password))
7163
.setVerifyingSsl(validateSSL)
7264
.build();
7365
}
@@ -79,7 +71,7 @@ public static ApiClient fromToken(String url, String token) {
7971
public static ApiClient fromToken(String url, String token, boolean validateSSL) {
8072
return new ClientBuilder()
8173
.setBasePath(url)
82-
.setCredentialProvider(new AccessTokenCredentialProvider(token))
74+
.setAuthentication(new AccessTokenAuthentication(token))
8375
.setVerifyingSsl(validateSSL)
8476
.build();
8577
}
@@ -98,7 +90,7 @@ public static ApiClient fromConfig(Reader input) throws IOException {
9890

9991
public static ApiClient fromConfig(KubeConfig config) throws IOException {
10092
return ClientBuilder
101-
.fromKubeConfig(config)
93+
.kubeconfig(config)
10294
.build();
10395
}
10496

@@ -114,6 +106,6 @@ public static ApiClient fromConfig(KubeConfig config) throws IOException {
114106
* @return The best APIClient given the previously described rules
115107
*/
116108
public static ApiClient defaultClient() throws IOException {
117-
return ClientBuilder.defaults().build();
109+
return ClientBuilder.standard().build();
118110
}
119111
}

util/src/main/java/io/kubernetes/client/util/credentials/AccessTokenCredentialProvider.java renamed to util/src/main/java/io/kubernetes/client/util/credentials/AccessTokenAuthentication.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import io.kubernetes.client.ApiClient;
44

5-
public class AccessTokenCredentialProvider implements CredentialProvider {
5+
/**
6+
* Uses a Bearer Token to configure {@link ApiClient} authentication to the Kubernetes API.
7+
*/
8+
public class AccessTokenAuthentication implements Authentication {
69
private String token;
710

8-
public AccessTokenCredentialProvider(final String token) {
11+
public AccessTokenAuthentication(final String token) {
912
this.token = token;
1013
}
1114

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import io.kubernetes.client.ApiClient;
4+
import io.kubernetes.client.util.ClientBuilder;
5+
6+
/**
7+
* Allows the implementation of different authentication mechanisms for the Kubernetes API.
8+
*
9+
* @see ClientBuilder#setAuthentication(Authentication)
10+
*/
11+
public interface Authentication {
12+
13+
void provide(ApiClient client);
14+
15+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.kubernetes.client.ApiClient;
44
import io.kubernetes.client.util.SSLUtils;
55
import java.io.IOException;
6-
import java.io.InputStream;
76
import java.security.KeyStoreException;
87
import java.security.NoSuchAlgorithmException;
98
import java.security.UnrecoverableKeyException;
@@ -12,12 +11,15 @@
1211
import javax.net.ssl.KeyManager;
1312
import org.apache.log4j.Logger;
1413

15-
public class ClientCertificateCredentialProvider implements CredentialProvider {
16-
private static final Logger log = Logger.getLogger(ClientCertificateCredentialProvider.class);
14+
/**
15+
* Uses Client Certificates to configure {@link ApiClient} authentication to the Kubernetes API.
16+
*/
17+
public class ClientCertificateAuthentication implements Authentication {
18+
private static final Logger log = Logger.getLogger(ClientCertificateAuthentication.class);
1719
private final byte[] certificate;
1820
private final byte[] key;
1921

20-
public ClientCertificateCredentialProvider(final byte[] certificate, final byte[] key) {
22+
public ClientCertificateAuthentication(final byte[] certificate, final byte[] key) {
2123
this.certificate = certificate;
2224
this.key = key;
2325
}

util/src/main/java/io/kubernetes/client/util/credentials/CredentialProvider.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

util/src/main/java/io/kubernetes/client/util/credentials/KubeconfigCredentialProvider.java renamed to util/src/main/java/io/kubernetes/client/util/credentials/KubeconfigAuthentication.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@
44
import io.kubernetes.client.util.KubeConfig;
55
import java.io.IOException;
66

7-
public class KubeconfigCredentialProvider implements CredentialProvider {
7+
/**
8+
* Uses a {@link KubeConfig} to configure {@link ApiClient} authentication to the Kubernetes API.
9+
*
10+
* Tries to configure the following authentication mechanisms in this order.
11+
* <ul>
12+
* <li>{@link ClientCertificateAuthentication} (using client certificate files or data)</li>
13+
* <li>{@link UsernamePasswordAuthentication}</li>
14+
* <li>{@link AccessTokenAuthentication}</li>
15+
* </ul>
16+
*/
17+
public class KubeconfigAuthentication implements Authentication {
818

919
private final String username;
1020
private final String password;
1121
private final String token;
1222
private final byte[] clientCert;
1323
private final byte[] clientKey;
1424

15-
public KubeconfigCredentialProvider(final KubeConfig config) throws IOException {
25+
public KubeconfigAuthentication(final KubeConfig config) throws IOException {
1626
this.clientCert = KubeConfig.getDataOrFile(config.getClientCertificateData(), config.getClientCertificateFile());
1727
this.clientKey = KubeConfig.getDataOrFile(config.getClientKeyData(), config.getClientKeyFile());
1828
this.username = config.getUsername();
@@ -22,15 +32,15 @@ public KubeconfigCredentialProvider(final KubeConfig config) throws IOException
2232

2333
@Override public void provide(ApiClient client) {
2434
if(clientCert != null && clientKey != null) {
25-
new ClientCertificateCredentialProvider(clientCert, clientKey);
35+
new ClientCertificateAuthentication(clientCert, clientKey);
2636
}
2737

2838
if(username != null && password != null) {
29-
new UsernamePasswordCredentialProvider(username, password).provide(client);
39+
new UsernamePasswordAuthentication(username, password).provide(client);
3040
}
3141

3242
if(token != null) {
33-
new AccessTokenCredentialProvider(token).provide(client);
43+
new AccessTokenAuthentication(token).provide(client);
3444
}
3545
}
3646
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import java.nio.charset.Charset;
55
import okio.ByteString;
66

7-
public class UsernamePasswordCredentialProvider implements CredentialProvider {
7+
/**
8+
* Uses Username and Password to configure {@link ApiClient} authentication to the Kubernetes API.
9+
*/
10+
public class UsernamePasswordAuthentication implements Authentication {
811
private final String username;
912
private final String password;
1013

11-
public UsernamePasswordCredentialProvider(final String username, final String password) {
14+
public UsernamePasswordAuthentication(final String username, final String password) {
1215
this.username = username;
1316
this.password = password;
1417
}

0 commit comments

Comments
 (0)