Skip to content

Commit 89b677b

Browse files
committed
Address PR comments
1 parent a365381 commit 89b677b

File tree

10 files changed

+127
-26
lines changed

10 files changed

+127
-26
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
149149
final ClientBuilder builder = new ClientBuilder();
150150

151151
String server = config.getServer();
152-
if (!server.startsWith("http://") && !server.startsWith("https://")) {
152+
if (!server.contains("://")) {
153153
if (server.contains(":443")) {
154154
server = "https://" + server;
155155
} else {
@@ -160,7 +160,10 @@ public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
160160
if(config.verifySSL()) {
161161
final byte[] caBytes = KubeConfig.getDataOrFile(config.getCertificateAuthorityData(),
162162
config.getCertificateAuthorityFile());
163-
builder.setCertificateAuthority(caBytes);
163+
if(caBytes != null) {
164+
builder.setCertificateAuthority(caBytes);
165+
}
166+
builder.setVerifyingSsl(true);
164167
} else {
165168
builder.setVerifyingSsl(false);
166169
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.kubernetes.client.util.credentials;
22

3+
import com.google.common.base.Preconditions;
34
import io.kubernetes.client.ApiClient;
45

56
/**
@@ -9,6 +10,7 @@ public class AccessTokenAuthentication implements Authentication {
910
private String token;
1011

1112
public AccessTokenAuthentication(final String token) {
13+
Preconditions.checkNotNull(token, "Access Token cannot be null");
1214
this.token = token;
1315
}
1416

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public ClientCertificateAuthentication(final byte[] certificate, final byte[] ke
3131
client.setKeyManagers(keyManagers);
3232
} catch (NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException | KeyStoreException | InvalidKeySpecException | IOException e) {
3333
log.warn("Could not create key manager for Client Certificate authentication.", e);
34+
throw new RuntimeException(e);
3435
}
3536
}
3637
}

util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,30 +157,6 @@ public void testCredentialProviderInvoked() throws IOException {
157157
verify(provider).provide(client);
158158
}
159159

160-
@Test
161-
public void testUserNamePasswordClientBuilder() throws Exception{
162-
final ApiClient client = (new ClientBuilder())
163-
.setBasePath(basePath)
164-
.setAuthentication(new UsernamePasswordAuthentication(userName, password))
165-
.build();
166-
assertEquals(basePath, client.getBasePath());
167-
assertEquals(true, client.isVerifyingSsl());
168-
assertEquals("Basic", ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix());
169-
assertEquals(ByteString.of((userName+":"+password).getBytes(Charset.forName("ISO-8859-1"))).base64(), ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKey());
170-
}
171-
172-
@Test
173-
public void testApiKeyConfigbuilder() throws Exception {
174-
final ApiClient client = new ClientBuilder()
175-
.setBasePath(basePath)
176-
.setAuthentication(new AccessTokenAuthentication(apiKey))
177-
.build();
178-
assertEquals(basePath, client.getBasePath());
179-
assertEquals(apiKeyPrefix, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix());
180-
assertEquals( apiKey, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKey());
181-
assertEquals(null,((io.kubernetes.client.auth.HttpBasicAuth)client.getAuthentications().get("BasicAuth")).getUsername());
182-
}
183-
184160
/**
185161
* We can't verify anything here because of how things are configured in swagger-codegen and
186162
* okhttp but combined with {@link #testSslCertCaBad()} we have some certainty that it is being
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.kubernetes.client.util;
2+
3+
import io.kubernetes.client.ApiClient;
4+
import io.kubernetes.client.auth.ApiKeyAuth;
5+
6+
public class TestUtils {
7+
8+
public static ApiKeyAuth getApiKeyAuthFromClient(ApiClient client) {
9+
return (ApiKeyAuth)client.getAuthentications().get("BearerToken");
10+
}
11+
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
package io.kubernetes.client.util.credentials;
22

3+
import io.kubernetes.client.ApiClient;
4+
import org.junit.Test;
5+
6+
import static io.kubernetes.client.util.TestUtils.getApiKeyAuthFromClient;
7+
import static org.hamcrest.core.Is.is;
38
import static org.junit.Assert.*;
49

510
public class AccessTokenAuthenticationTest {
611

12+
@Test
13+
public void testTokenProvided() {
14+
final ApiClient client = new ApiClient();
15+
new AccessTokenAuthentication("token").provide(client);
16+
assertThat(getApiKeyAuthFromClient(client).getApiKeyPrefix(), is("Bearer"));
17+
assertThat(getApiKeyAuthFromClient(client).getApiKey(), is("token"));
18+
}
19+
20+
@Test(expected = NullPointerException.class)
21+
public void testTokenNonnull() {
22+
new AccessTokenAuthentication(null);
23+
}
24+
725
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import com.google.common.io.Resources;
4+
import io.kubernetes.client.ApiClient;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
import org.junit.Test;
8+
9+
public class ClientCertificateAuthenticationTest {
10+
private static final String CLIENT_CERT_PATH = Resources.getResource("clientauth.cert").getPath();
11+
private static final String CLIENT_KEY_PATH = Resources.getResource("clientauth.key").getPath();
12+
13+
@Test
14+
public void testValidCertificates() throws Exception {
15+
final ApiClient client = new ApiClient();
16+
final byte[] certificate = Files.readAllBytes(Paths.get(CLIENT_CERT_PATH));
17+
final byte[] key = Files.readAllBytes(Paths.get(CLIENT_KEY_PATH));
18+
new ClientCertificateAuthentication(certificate, key).provide(client);
19+
}
20+
21+
@Test(expected = RuntimeException.class)
22+
public void testInvalidCertificates() {
23+
final ApiClient client = new ApiClient();
24+
new ClientCertificateAuthentication(new byte[]{}, new byte[]{}).provide(client);
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.kubernetes.client.util.credentials;
2+
3+
import com.google.common.base.Charsets;
4+
import io.kubernetes.client.ApiClient;
5+
import java.nio.charset.Charset;
6+
import okio.ByteString;
7+
import org.junit.Test;
8+
9+
import static io.kubernetes.client.util.TestUtils.getApiKeyAuthFromClient;
10+
import static org.hamcrest.core.Is.is;
11+
import static org.junit.Assert.*;
12+
13+
public class UsernamePasswordAuthenticationTest {
14+
15+
private static final String USERNAME = "username";
16+
private static final String PASSWORD = "password";
17+
public static final byte[] USERNAME_PASSWORD_BYTES = (USERNAME + ":" + PASSWORD).getBytes(
18+
Charsets.ISO_8859_1);
19+
20+
@Test
21+
public void testUsernamePasswordProvided() {
22+
final ApiClient client = new ApiClient();
23+
new UsernamePasswordAuthentication(USERNAME, PASSWORD).provide(client);
24+
assertThat(getApiKeyAuthFromClient(client).getApiKeyPrefix(), is("Basic"));
25+
assertThat(getApiKeyAuthFromClient(client).getApiKey(), is(ByteString.of(USERNAME_PASSWORD_BYTES).base64()));
26+
}
27+
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDNDCCAp2gAwIBAgIJANpXxe5h/f0+MA0GCSqGSIb3DQEBBQUAMHAxCzAJBgNV
3+
BAYTAlVTMQswCQYDVQQIEwJOWTERMA8GA1UEBxMITmV3IFlvcmsxGjAYBgNVBAoT
4+
EUt1YmVybmV0ZXMgQ2xpZW50MSUwIwYDVQQDExxDbGllbnQgQ2VydGlmaWNhdGUg
5+
QXV0aCBUZXN0MB4XDTE4MDIwNjE2Mzg1NloXDTI4MDIwNDE2Mzg1NlowcDELMAkG
6+
A1UEBhMCVVMxCzAJBgNVBAgTAk5ZMREwDwYDVQQHEwhOZXcgWW9yazEaMBgGA1UE
7+
ChMRS3ViZXJuZXRlcyBDbGllbnQxJTAjBgNVBAMTHENsaWVudCBDZXJ0aWZpY2F0
8+
ZSBBdXRoIFRlc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAPPZeFjGKAzV
9+
NWXr476eXiUz65M2zhPyHafZtRzsZWCNI3dePovpy4h37Dqj2MoQL3RmBt7ZSRYC
10+
TFIbdsbOGVMczpCHjOLyj3JuD5i399jNHoqGwi3bLUP/KLIwLgUJzegBFeHt2OGT
11+
epm5Lixt5f8OyoTrVKv03NZHpG+qCKYPAgMBAAGjgdUwgdIwHQYDVR0OBBYEFHKm
12+
d7b+opkmBW5It4A6/nq3ZEqbMIGiBgNVHSMEgZowgZeAFHKmd7b+opkmBW5It4A6
13+
/nq3ZEqboXSkcjBwMQswCQYDVQQGEwJVUzELMAkGA1UECBMCTlkxETAPBgNVBAcT
14+
CE5ldyBZb3JrMRowGAYDVQQKExFLdWJlcm5ldGVzIENsaWVudDElMCMGA1UEAxMc
15+
Q2xpZW50IENlcnRpZmljYXRlIEF1dGggVGVzdIIJANpXxe5h/f0+MAwGA1UdEwQF
16+
MAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAKs/UhwO9r+uW7wUnoGwDvGSwkgQO2rwp
17+
xOPgwkbptoxQIaKyIRwqnnxIVKwNZop8Thrr94fOsn0oVe6PSSVZLOi4BqIZ3LtH
18+
5z3O2m3f318fYudQfZIzTCrreViK2f2zmPgZDRqYmWSIFB5EcjAutckE8ZTQUB6C
19+
OWkzeV5IvUI=
20+
-----END CERTIFICATE-----
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICXQIBAAKBgQDz2XhYxigM1TVl6+O+nl4lM+uTNs4T8h2n2bUc7GVgjSN3Xj6L
3+
6cuId+w6o9jKEC90Zgbe2UkWAkxSG3bGzhlTHM6Qh4zi8o9ybg+Yt/fYzR6KhsIt
4+
2y1D/yiyMC4FCc3oARXh7djhk3qZuS4sbeX/DsqE61Sr9NzWR6RvqgimDwIDAQAB
5+
AoGBAKnZs6MaO3Fc3TnmChePVgJR3OgIx5hLD+8HjMjdvGt5Q9f0dFqeed/PsGLU
6+
F7//cB6Cpox5Cxhid2jFqoEls6q9jnnVGYGDJHDQ7u//xigVfHf9lJzmdxQWId6G
7+
F6VoU0N6Oh2AtG4L/SQqnbacG1JVXZun/8J8bBon6C4PeacZAkEA/YaNeLcLtv22
8+
992UJaj3Rd9i5Sxs6dH6PDoABvkbipqWtj4osiMt6a5h32Xy/811CYDI1wvcVDDr
9+
9HAQP1pfPQJBAPY6vcA+JuKdg9KSZzCh77aE5LGXqz62rcMlUHPtRchNfcFo06MC
10+
ehKA+YIBmT5we0GrdyAXsYGIVypK0l8irzsCQEkr43r6wavP8FX7or131d5Zye5A
11+
8zJNAz8MsmNQ1G0djvAMYqx/UMoIJYFXqFnCD8xtWgoPB0lZUVCcY2QVjjUCQGYk
12+
A+alYZgL400MckXYRwodooiQ8/Z17SrQZclRGet3Sb1bcL9kHaNjYR0u8JTYMCkT
13+
qbzkVzv2hMIEe7P/PVUCQQCxLiT5qDxQawP1/ZFnp4KCHTKTQ1Mvw8cGajEG+DKP
14+
2GzD8K03HaJU5eN4Z1Uo1TYPQY1tRD3nM+sDH9EcLvqP
15+
-----END RSA PRIVATE KEY-----

0 commit comments

Comments
 (0)