Skip to content

Commit 063e4fc

Browse files
committed
Improved keystore type handling
1 parent 73f3203 commit 063e4fc

File tree

8 files changed

+49
-37
lines changed

8 files changed

+49
-37
lines changed

cautils/src/main/java/com/dajudge/proxybase/ca/Helpers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static byte[] serialize(final KeyStore keyStore, final char[] password) {
133133
});
134134
}
135135

136-
public static KeyStore createJks(
136+
public static KeyStore createKeyStore(
137137
final ThrowingConsumer<KeyStore> withKeyStore,
138138
final String type
139139
) {

cautils/src/main/java/com/dajudge/proxybase/ca/selfsign/CertificateAuthority.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private PrivateKey caPrivateKey() {
4949
}
5050

5151
public KeyStore getTrustStore(final String type) {
52-
return Helpers.createJks(keyStore -> {
52+
return Helpers.createKeyStore(keyStore -> {
5353
keyStore.setCertificateEntry("ca", caCert());
5454
}, type);
5555
}

proxybase/src/main/java/com/dajudge/proxybase/certs/ReloadingKeyStoreManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class ReloadingKeyStoreManager implements KeyStoreManager {
3434
private final long updateIntervalMsecs;
3535
private final Object keyStoreLock = new Object();
3636
private final Object clockLock = new Object();
37-
private long lastUpdate;
3837
private final AtomicBoolean loading = new AtomicBoolean();
38+
private long lastUpdate;
3939
private KeyStoreWrapper keyStore;
4040

4141
public ReloadingKeyStoreManager(
@@ -60,6 +60,7 @@ public static ReloadingKeyStoreManager createReloader(
6060
);
6161
}
6262

63+
@Override
6364
public KeyStoreWrapper getKeyStore() {
6465
updateIfNecessary();
6566
synchronized (keyStoreLock) {

proxybase/src/test/java/com/dajudge/proxybase/BaseProxyTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444

4545
public abstract class BaseProxyTest {
4646
private static final Logger LOG = LoggerFactory.getLogger(BaseProxyTest.class);
47-
47+
protected static final String KEYSTORE_TYPE = "jks";
4848
static final TestCertificationAuthority DOWNSTREAM_SERVER_CA =
49-
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamServerCA");
49+
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamServerCA", KEYSTORE_TYPE);
5050
static final TestCertificationAuthority DOWNSTREAM_CLIENT_CA =
51-
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamClientCA");
51+
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamClientCA", KEYSTORE_TYPE);
5252
static final TestCertificationAuthority UPSTREAM_SERVER_CA =
53-
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamServerCA");
53+
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamServerCA", KEYSTORE_TYPE);
5454
static final TestCertificationAuthority UPSTREAM_CLIENT_CA =
55-
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamClientCA");
55+
new TestCertificationAuthority(System::currentTimeMillis, "cn=downstreamClientCA", KEYSTORE_TYPE);
5656

5757
private void withDownstreamServer(
5858
final SslConfiguration sslConfig,

proxybase/src/test/java/com/dajudge/proxybase/HotCertReplaceTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@
3535
public class HotCertReplaceTest extends BaseProxyTest {
3636
private final TestKeyStoreManager downstreamClientKeyStore = new TestKeyStoreManager(
3737
DOWNSTREAM_CLIENT_CA,
38-
"cn=downstreamClient"
38+
"cn=downstreamClient",
39+
KEYSTORE_TYPE
3940
);
4041
private final SslConfiguration downstreamMtls = new TwoWaySslConfiguration(
4142
downstreamClientKeyStore,
42-
() -> new KeyStoreWrapper(DOWNSTREAM_SERVER_CA.getTrustStore("jks"), null),
43-
new TestKeyStoreManager(DOWNSTREAM_SERVER_CA, "cn=downstreamServer"),
44-
() -> new KeyStoreWrapper(DOWNSTREAM_CLIENT_CA.getTrustStore("jks"), null)
43+
() -> new KeyStoreWrapper(DOWNSTREAM_SERVER_CA.getTrustStore(KEYSTORE_TYPE), null),
44+
new TestKeyStoreManager(DOWNSTREAM_SERVER_CA, "cn=downstreamServer", KEYSTORE_TYPE),
45+
() -> new KeyStoreWrapper(DOWNSTREAM_CLIENT_CA.getTrustStore(KEYSTORE_TYPE), null)
4546
);
4647
private final SslConfiguration upstreamMtls = new TwoWaySslConfiguration(
47-
new TestKeyStoreManager(UPSTREAM_CLIENT_CA, "cn=upstreamClient"),
48-
() -> new KeyStoreWrapper(UPSTREAM_SERVER_CA.getTrustStore("jks"), null),
49-
new TestKeyStoreManager(UPSTREAM_SERVER_CA, "cn=upstreamServer"),
50-
() -> new KeyStoreWrapper(UPSTREAM_CLIENT_CA.getTrustStore("jks"), null)
48+
new TestKeyStoreManager(UPSTREAM_CLIENT_CA, "cn=upstreamClient", KEYSTORE_TYPE),
49+
() -> new KeyStoreWrapper(UPSTREAM_SERVER_CA.getTrustStore(KEYSTORE_TYPE), null),
50+
new TestKeyStoreManager(UPSTREAM_SERVER_CA, "cn=upstreamServer", KEYSTORE_TYPE),
51+
() -> new KeyStoreWrapper(UPSTREAM_CLIENT_CA.getTrustStore(KEYSTORE_TYPE), null)
5152
);
5253

5354
@Test

proxybase/src/test/java/com/dajudge/proxybase/RoundtripTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@
3838
public class RoundtripTest extends BaseProxyTest {
3939

4040
private static final SslConfiguration PLAINTEXT = new PlaintextSslConfiguration();
41+
protected static final String KEYSTORE_TYPE = "jks";
4142
private static final SslConfiguration DOWNSTREAM_TLS = new OneWaySslConfiguration(
42-
() -> new KeyStoreWrapper(DOWNSTREAM_SERVER_CA.getTrustStore("jks"), null),
43-
new TestKeyStoreManager(DOWNSTREAM_SERVER_CA, "cn=downstreamServer")
43+
() -> new KeyStoreWrapper(DOWNSTREAM_SERVER_CA.getTrustStore(KEYSTORE_TYPE), null),
44+
new TestKeyStoreManager(DOWNSTREAM_SERVER_CA, "cn=downstreamServer", KEYSTORE_TYPE)
4445
);
4546
private static final SslConfiguration DOWNSTREAM_MTLS = new TestSslConfiguration.TwoWaySslConfiguration(
46-
new TestKeyStoreManager(DOWNSTREAM_CLIENT_CA, "cn=downstreamClient"),
47-
() -> new KeyStoreWrapper(DOWNSTREAM_SERVER_CA.getTrustStore("jks"), null),
48-
new TestKeyStoreManager(DOWNSTREAM_SERVER_CA, "cn=downstreamServer"),
49-
() -> new KeyStoreWrapper(DOWNSTREAM_CLIENT_CA.getTrustStore("jks"), null)
47+
new TestKeyStoreManager(DOWNSTREAM_CLIENT_CA, "cn=downstreamClient", KEYSTORE_TYPE),
48+
() -> new KeyStoreWrapper(DOWNSTREAM_SERVER_CA.getTrustStore(KEYSTORE_TYPE), null),
49+
new TestKeyStoreManager(DOWNSTREAM_SERVER_CA, "cn=downstreamServer", KEYSTORE_TYPE),
50+
() -> new KeyStoreWrapper(DOWNSTREAM_CLIENT_CA.getTrustStore(KEYSTORE_TYPE), null)
5051
);
5152
private static final SslConfiguration UPSTREAM_TLS = new OneWaySslConfiguration(
52-
() -> new KeyStoreWrapper(UPSTREAM_SERVER_CA.getTrustStore("jks"), null),
53-
new TestKeyStoreManager(UPSTREAM_SERVER_CA, "cn=upstreamServer")
53+
() -> new KeyStoreWrapper(UPSTREAM_SERVER_CA.getTrustStore(KEYSTORE_TYPE), null),
54+
new TestKeyStoreManager(UPSTREAM_SERVER_CA, "cn=upstreamServer", KEYSTORE_TYPE)
5455
);
5556
private static final SslConfiguration UPSTREAM_MTLS = new TestSslConfiguration.TwoWaySslConfiguration(
56-
new TestKeyStoreManager(UPSTREAM_CLIENT_CA, "cn=upstreamClient"),
57-
() -> new KeyStoreWrapper(UPSTREAM_SERVER_CA.getTrustStore("jks"), null),
58-
new TestKeyStoreManager(UPSTREAM_SERVER_CA, "cn=upstreamServer"),
59-
() -> new KeyStoreWrapper(UPSTREAM_CLIENT_CA.getTrustStore("jks"), null)
57+
new TestKeyStoreManager(UPSTREAM_CLIENT_CA, "cn=upstreamClient", KEYSTORE_TYPE),
58+
() -> new KeyStoreWrapper(UPSTREAM_SERVER_CA.getTrustStore(KEYSTORE_TYPE), null),
59+
new TestKeyStoreManager(UPSTREAM_SERVER_CA, "cn=upstreamServer", KEYSTORE_TYPE),
60+
() -> new KeyStoreWrapper(UPSTREAM_CLIENT_CA.getTrustStore(KEYSTORE_TYPE), null)
6061
);
6162

6263
@Parameterized.Parameters

proxybase/src/test/java/com/dajudge/proxybase/util/TestKeyStoreManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@
2323

2424
public class TestKeyStoreManager implements KeyStoreManager {
2525
private final TestCertificationAuthority ca;
26+
private final String keyStoreType;
2627
private String dn;
2728

2829
public TestKeyStoreManager(
2930
final TestCertificationAuthority ca,
30-
final String dn
31+
final String dn,
32+
final String keyStoreType
3133
) {
3234
this.ca = ca;
3335
this.dn = dn;
36+
this.keyStoreType = keyStoreType;
3437
}
3538

3639
@Override
3740
public KeyStoreWrapper getKeyStore() {
38-
return ca.createNewKeyStore(dn);
41+
return ca.createNewKeyStore(dn, keyStoreType);
3942
}
4043

4144
public void replaceDn(final String dn) {

testca/src/main/java/com/dajudge/proxybase/ca/test/TestCertificationAuthority.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,22 @@
3232
public class TestCertificationAuthority extends CertificateAuthority {
3333
private static final String KEY_ALIAS = UUID.randomUUID().toString();
3434
private final Supplier<Long> clock;
35+
private String keystoreType;
3536

36-
public TestCertificationAuthority(final Supplier<Long> clock, final String dn) {
37-
super(createCaKeyStore(dn, clock), KEY_ALIAS);
37+
public TestCertificationAuthority(final Supplier<Long> clock, final String dn, final String keystoreType) {
38+
super(createCaKeyStore(dn, clock, keystoreType), KEY_ALIAS);
3839
this.clock = clock;
40+
this.keystoreType = keystoreType;
3941
}
4042

41-
private static KeyStoreManager createCaKeyStore(final String dn, final Supplier<Long> clock) {
43+
private static KeyStoreManager createCaKeyStore(
44+
final String dn,
45+
final Supplier<Long> clock,
46+
final String keystoreType
47+
) {
4248
final String keyPassword = UUID.randomUUID().toString();
4349
final KeyStoreWrapper wrapper = new KeyStoreWrapper(
44-
Helpers.createJks(keyStore -> {
50+
Helpers.createKeyStore(keyStore -> {
4551
final KeyPair keyPair = Helpers.keyPair();
4652
final X509Certificate cert = Helpers.selfSignedCert(
4753
dn,
@@ -57,13 +63,13 @@ private static KeyStoreManager createCaKeyStore(final String dn, final Supplier<
5763
keyPassword.toCharArray(),
5864
new Certificate[]{cert}
5965
);
60-
}, "jks"),
66+
}, keystoreType),
6167
keyPassword.toCharArray()
6268
);
6369
return () -> wrapper;
6470
}
6571

66-
public KeyStoreWrapper createNewKeyStore(final String dn) {
72+
public KeyStoreWrapper createNewKeyStore(final String dn, final String keyStoreType) {
6773
final String keyPassword = UUID.randomUUID().toString();
6874
return new KeyStoreWrapper(
6975
createKeyStore(
@@ -72,7 +78,7 @@ public KeyStoreWrapper createNewKeyStore(final String dn) {
7278
keyPassword.toCharArray(),
7379
Helpers.now(clock),
7480
Helpers.plus(Helpers.now(clock), Duration.ofDays(1)),
75-
"jks"
81+
keyStoreType
7682
),
7783
keyPassword.toCharArray()
7884
);

0 commit comments

Comments
 (0)