Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Fix Multiple CA in ca.crt file (port for 3.12.x) (#191)
Browse files Browse the repository at this point in the history
* Add HTTPS to WireMock tests
* Add support for multiple CA Certificates
* Rename `withCaCertificate` to `withCaCertificates`
  • Loading branch information
Rafał Leszko authored Jan 29, 2020
1 parent b29d488 commit 93dec80
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@

<log4j.version>1.2.12</log4j.version>
<junit.version>4.12</junit.version>
<wiremock.version>2.18.0</wiremock.version>
<wiremock.version>2.25.1</wiremock.version>
<hamcrest.version>1.3</hamcrest.version>
<mockito.version>1.10.19</mockito.version>
<powermock.version>1.7.3</powermock.version>

<hazelcast.version>3.12.4</hazelcast.version>
<hazelcast.version>3.12.5</hazelcast.version>

<maven.checkstyle.plugin.version>2.15</maven.checkstyle.plugin.version>
<maven.findbugs.plugin.version>3.0.4</maven.findbugs.plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ private JsonObject callGet(final String urlString) {
public JsonObject call() {
return Json
.parse(RestClient.create(urlString).withHeader("Authorization", String.format("Bearer %s", apiToken))
.withCaCertificate(caCertificate)
.withCaCertificates(caCertificate)
.get())
.asObject();
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/hazelcast/kubernetes/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;

Expand Down Expand Up @@ -69,7 +70,7 @@ RestClient withBody(String body) {
return this;
}

RestClient withCaCertificate(String caCertificate) {
RestClient withCaCertificates(String caCertificate) {
this.caCertificate = caCertificate;
return this;
}
Expand Down Expand Up @@ -175,7 +176,12 @@ private SSLSocketFactory buildSslSocketFactory() {
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", generateCertificate());

int i = 0;
for (Certificate certificate : generateCertificates()) {
String alias = String.format("ca-%d", i++);
keyStore.setCertificateEntry(alias, certificate);
}

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
Expand All @@ -192,13 +198,13 @@ private SSLSocketFactory buildSslSocketFactory() {
/**
* Generates CA Certificate from the default CA Cert file or from the externally provided "ca-certificate" property.
*/
private Certificate generateCertificate()
private Collection<? extends Certificate> generateCertificates()
throws IOException, CertificateException {
InputStream caInput = null;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
caInput = new ByteArrayInputStream(caCertificate.getBytes("UTF-8"));
return cf.generateCertificate(caInput);
return cf.generateCertificates(caInput);
} finally {
IOUtil.closeResource(caInput);
}
Expand Down
53 changes: 39 additions & 14 deletions src/test/java/com/hazelcast/kubernetes/RestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.File;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.hazelcast.kubernetes.KubernetesConfig.readFileContents;
import static org.junit.Assert.assertEquals;

public class RestClientTest {
Expand All @@ -52,13 +53,23 @@ public class RestClientTest {
private static final String BODY_RESPONSE = "some body response";

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig()
.dynamicHttpsPort()
.keystorePath(pathTo("keystore.jks"))
);

private String address;

@Before
public void setUp() {
address = String.format("http://localhost:%s", wireMockRule.port());
// disable hostname HTTPS verification for testing
HttpsURLConnection.setDefaultHostnameVerifier(
new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
});
address = String.format("https://localhost:%s", wireMockRule.httpsPort());
}

@Test
Expand All @@ -68,7 +79,9 @@ public void getSuccess() {
.willReturn(aResponse().withStatus(200).withBody(BODY_RESPONSE)));

// when
String result = RestClient.create(String.format("%s%s", address, API_ENDPOINT)).get();
String result = RestClient.create(String.format("%s%s", address, API_ENDPOINT))
.withCaCertificates(readFile("ca.crt"))
.get();

// then
assertEquals(BODY_RESPONSE, result);
Expand All @@ -85,8 +98,9 @@ public void getWithHeaderSuccess() {

// when
String result = RestClient.create(String.format("%s%s", address, API_ENDPOINT))
.withHeader(headerKey, headerValue)
.get();
.withHeader(headerKey, headerValue)
.withCaCertificates(readFile("ca.crt"))
.get();

// then
assertEquals(BODY_RESPONSE, result);
Expand All @@ -99,7 +113,9 @@ public void getFailure() {
.willReturn(aResponse().withStatus(500).withBody("Internal error")));

// when
RestClient.create(String.format("%s%s", address, API_ENDPOINT)).get();
RestClient.create(String.format("%s%s", address, API_ENDPOINT))
.withCaCertificates(readFile("ca.crt"))
.get();

// then
// throw exception
Expand All @@ -114,10 +130,19 @@ public void postSuccess() {

// when
String result = RestClient.create(String.format("%s%s", address, API_ENDPOINT))
.withBody(BODY_REQUEST)
.post();
.withBody(BODY_REQUEST)
.withCaCertificates(readFile("ca.crt"))
.post();

// then
assertEquals(BODY_RESPONSE, result);
}

private String readFile(String filename) {
return readFileContents(pathTo(filename));
}

private String pathTo(String filename) {
return new File(getClass().getClassLoader().getResource(filename).getFile()).getAbsolutePath();
}
}
58 changes: 58 additions & 0 deletions src/test/resources/ca.cr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qpHYe/GJQ=
-----END CERTIFICATE-----
Binary file added src/test/resources/keystore.jks
Binary file not shown.

0 comments on commit 93dec80

Please sign in to comment.