Skip to content

Commit 49f8f2a

Browse files
henryjujulienlancelot
authored andcommitted
BUILD-759 Update ITs to use PKCS12 certificates instead of JKS
1 parent d8d3290 commit 49f8f2a

34 files changed

Lines changed: 563 additions & 532 deletions

api/src/test/java/org/sonarsource/scanner/api/internal/OkHttpClientFactoryTest.java

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@
2626
import java.nio.file.Paths;
2727
import java.security.KeyStore;
2828
import java.security.SecureRandom;
29-
import java.util.List;
3029
import javax.net.ssl.KeyManagerFactory;
3130
import javax.net.ssl.SSLContext;
3231
import javax.net.ssl.SSLHandshakeException;
33-
import javax.net.ssl.SSLSocketFactory;
3432
import javax.net.ssl.TrustManagerFactory;
35-
import okhttp3.ConnectionSpec;
3633
import okhttp3.OkHttpClient;
3734
import okhttp3.Request;
3835
import okhttp3.Response;
@@ -41,32 +38,28 @@
4138
import okhttp3.mockwebserver.MockWebServer;
4239
import okhttp3.mockwebserver.RecordedRequest;
4340
import org.junit.After;
44-
import org.junit.Ignore;
4541
import org.junit.Rule;
4642
import org.junit.Test;
47-
import org.junit.experimental.theories.DataPoint;
48-
import org.junit.experimental.theories.Theories;
4943
import org.junit.experimental.theories.Theory;
5044
import org.junit.rules.ExpectedException;
51-
import org.junit.runner.RunWith;
5245
import org.sonarsource.scanner.api.internal.cache.Logger;
5346

5447
import static java.lang.String.format;
5548
import static org.assertj.core.api.Assertions.assertThat;
5649
import static org.assertj.core.api.Assertions.assertThatThrownBy;
57-
import static org.assertj.core.api.Assertions.fail;
50+
import static org.junit.Assert.fail;
5851
import static org.mockito.Mockito.mock;
5952

60-
@RunWith(Theories.class)
6153
public class OkHttpClientFactoryTest {
6254

63-
@DataPoint
64-
public static final String KEYSTORE_CLIENT_WITH_CA = "/client-with-ca.jks";
65-
@DataPoint
66-
public static final String KEYSTORE_CLIENT_WITH_CERTIFICATE = "/client-with-certificate.jks";
55+
private static final String KEYSTORE_CLIENT_WITH_CA = "/client-with-ca.p12";
56+
private static final String CLIENT_WITH_CA_KEYSTORE_PASSWORD = "pwdClientCAP12";
6757

68-
private static final String KEYSTORE_PASSWORD = "abcdef";
69-
private static final String KEYSTORE_FILE = "/server.jks";
58+
private static final String KEYSTORE_CLIENT_WITH_CERTIFICATE = "/client-with-certificate.p12";
59+
private static final String CLIENT_WITH_CERTIFICATE_KEYSTORE_PASSWORD = "pwdClientP12";
60+
61+
private static final String SERVER_KEYSTORE_PASSWORD = "pwdServerP12";
62+
private static final String SERVER_KEYSTORE_FILE = "/server.p12";
7063
private static final Logger logger = mock(Logger.class);
7164
private static final String SONAR_WS_TIMEOUT = "sonar.ws.timeout";
7265
private static final String COOKIE = "BIGipServerpool_sonarqube.example.com_8443=123456789.12345.0000";
@@ -79,14 +72,6 @@ public void cleanSystemProperty() {
7972
System.clearProperty(SONAR_WS_TIMEOUT);
8073
}
8174

82-
@Test
83-
public void support_tls_versions_of_java8() {
84-
OkHttpClient underTest = OkHttpClientFactory.create(logger);
85-
86-
assertTlsAndClearTextSpecifications(underTest);
87-
assertThat(underTest.sslSocketFactory()).isInstanceOf(SSLSocketFactory.getDefault().getClass());
88-
}
89-
9075
@Test
9176
public void support_custom_timeouts() {
9277
int readTimeoutSec = 2000;
@@ -106,19 +91,6 @@ public void support_custom_timeouts_throws_exception_on_non_number() {
10691
assertThatThrownBy(() -> OkHttpClientFactory.create(logger)).isInstanceOf(NumberFormatException.class);
10792
}
10893

109-
private void assertTlsAndClearTextSpecifications(OkHttpClient client) {
110-
List<ConnectionSpec> connectionSpecs = client.connectionSpecs();
111-
assertThat(connectionSpecs).hasSize(2);
112-
113-
// TLS. tlsVersions()==null means all TLS versions
114-
assertThat(connectionSpecs.get(0).tlsVersions()).isNull();
115-
assertThat(connectionSpecs.get(0).isTls()).isTrue();
116-
117-
// HTTP
118-
assertThat(connectionSpecs.get(1).tlsVersions()).isNull();
119-
assertThat(connectionSpecs.get(1).isTls()).isFalse();
120-
}
121-
12294
@Test
12395
public void test_with_external_http_server() throws IOException {
12496
Response response = call("http://www.google.com");
@@ -138,7 +110,7 @@ public void when_overriding_truststore_known_websites_are_failing(String clientK
138110
try {
139111
Path clientTruststore = Paths.get(getClass().getResource(clientKeyStore).toURI()).toAbsolutePath();
140112
System.setProperty("javax.net.ssl.trustStore", clientTruststore.toString());
141-
System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASSWORD);
113+
System.setProperty("javax.net.ssl.trustStorePassword", SERVER_KEYSTORE_PASSWORD);
142114

143115
expectedException.expect(SSLHandshakeException.class);
144116
call("https://www.google.com");
@@ -149,9 +121,18 @@ public void when_overriding_truststore_known_websites_are_failing(String clientK
149121
}
150122
}
151123

152-
@Ignore // ignore to test cirrus QA
153-
@Theory
154-
public void test_with_custom_https_server(String clientKeyStore) throws Exception {
124+
@Test
125+
public void test_with_custom_https_server_using_ca_in_truststore() throws Exception {
126+
test_with_custom_https_server(KEYSTORE_CLIENT_WITH_CA, CLIENT_WITH_CA_KEYSTORE_PASSWORD);
127+
}
128+
129+
@Test
130+
public void test_with_custom_https_server_using_server_certificate_in_truststore() throws Exception {
131+
test_with_custom_https_server(KEYSTORE_CLIENT_WITH_CERTIFICATE, CLIENT_WITH_CERTIFICATE_KEYSTORE_PASSWORD);
132+
}
133+
134+
private void test_with_custom_https_server(String clientKeyStore, String keyStorePassword) throws Exception {
135+
System.setProperty("javax.net.debug", "ssl,handshake,record");
155136
try (MockWebServer server = buildTLSServer()) {
156137
String url = format("https://localhost:%d/", server.getPort());
157138

@@ -166,7 +147,7 @@ public void test_with_custom_https_server(String clientKeyStore) throws Exceptio
166147
// Add the truststore
167148
Path clientTruststore = Paths.get(getClass().getResource(clientKeyStore).toURI()).toAbsolutePath();
168149
System.setProperty("javax.net.ssl.trustStore", clientTruststore.toString());
169-
System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASSWORD);
150+
System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
170151

171152
Response response = call(url);
172153
assertThat(response.code()).isEqualTo(200);
@@ -178,22 +159,30 @@ public void test_with_custom_https_server(String clientKeyStore) throws Exceptio
178159
}
179160
}
180161

181-
@Ignore // ignore to test cirrus QA
182-
@Theory
183-
public void test_with_cookie(String clientKeyStore) throws Exception {
162+
@Test
163+
public void test_with_cookie_using_ca_in_truststore() throws Exception {
164+
test_with_cookie(KEYSTORE_CLIENT_WITH_CA, CLIENT_WITH_CA_KEYSTORE_PASSWORD);
165+
}
166+
167+
@Test
168+
public void test_with_cookie_using_server_certificate_in_truststore() throws Exception {
169+
test_with_cookie(KEYSTORE_CLIENT_WITH_CERTIFICATE, CLIENT_WITH_CERTIFICATE_KEYSTORE_PASSWORD);
170+
}
171+
172+
private void test_with_cookie(String clientKeyStore, String keyStorePassword) throws Exception {
184173
try (MockWebServer server = buildTLSServer()) {
185174
String url = format("https://localhost:%d/", server.getPort());
186175

187176
// Add the truststore
188177
Path clientTruststore = Paths.get(getClass().getResource(clientKeyStore).toURI()).toAbsolutePath();
189178
System.setProperty("javax.net.ssl.trustStore", clientTruststore.toString());
190-
System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASSWORD);
179+
System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
191180

192-
OkHttpClientFactory.COOKIE_MANAGER.getCookieStore().removeAll(); // Clear any existing cookies
181+
OkHttpClientFactory.COOKIE_MANAGER.getCookieStore().removeAll(); // Clear any existing cookies
193182

194183
Response response = call(url);
195-
assertThat(response.header("Set-Cookie")).isEqualTo(COOKIE); // The server should have asked us to set a cookie
196-
assertThat(response.body().string()).doesNotContain(COOKIE);
184+
assertThat(response.header("Set-Cookie")).isEqualTo(COOKIE); // The server should have asked us to set a cookie
185+
assertThat(response.body().string()).doesNotContain(COOKIE);
197186

198187
response = call(url);
199188
assertThat(response.body().string()).contains(COOKIE);
@@ -205,7 +194,6 @@ public void test_with_cookie(String clientKeyStore) throws Exception {
205194
}
206195
}
207196

208-
209197
private static Response call(String url) throws IOException {
210198
return OkHttpClientFactory.create(logger).newCall(
211199
new Request.Builder()
@@ -240,17 +228,17 @@ public MockResponse dispatch(RecordedRequest request) {
240228
});
241229

242230
// JKS file storing the private key and TLS certificate
243-
Path serverCertificate = Paths.get(getClass().getResource(KEYSTORE_FILE).toURI()).toAbsolutePath();
231+
Path serverCertificate = Paths.get(getClass().getResource(SERVER_KEYSTORE_FILE).toURI()).toAbsolutePath();
244232

245233
// Load the KeyStore
246-
KeyStore serverKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
234+
KeyStore serverKeyStore = KeyStore.getInstance("pkcs12");
247235
FileInputStream stream = new FileInputStream(serverCertificate.toFile());
248-
serverKeyStore.load(stream, KEYSTORE_PASSWORD.toCharArray());
236+
serverKeyStore.load(stream, SERVER_KEYSTORE_PASSWORD.toCharArray());
249237

250238
// Load the KeyManager from the KeyStore
251239
String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
252240
KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
253-
kmf.init(serverKeyStore, "".toCharArray());
241+
kmf.init(serverKeyStore, SERVER_KEYSTORE_PASSWORD.toCharArray());
254242

255243
// Add the "Keys" (ie. private key and TLS certificate to the TrustManager
256244
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(kmfAlgorithm);
-1.5 KB
Binary file not shown.
1.73 KB
Binary file not shown.
-1.44 KB
Binary file not shown.
1.69 KB
Binary file not shown.

api/src/test/resources/client.jks

-3.77 KB
Binary file not shown.
-1.49 KB
Binary file not shown.

api/src/test/resources/server.jks

-3.83 KB
Binary file not shown.

api/src/test/resources/server.p12

4.16 KB
Binary file not shown.

its/it-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>Integration tests</description>
1212

1313
<properties>
14-
<jetty.version>9.3.11.v20160721</jetty.version>
14+
<jetty.version>9.4.27.v20200227</jetty.version>
1515
<logback.version>1.1.7</logback.version>
1616
<sonar.skip>true</sonar.skip>
1717
<skipTests>true</skipTests>

0 commit comments

Comments
 (0)