Skip to content

Commit 53a638d

Browse files
author
Dmitry Spikhalskiy
authored
Fix SimpleSslContextBuilderTest not testing anything by not loading keys correctly (#970)
1 parent 7373d36 commit 53a638d

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed

temporal-serviceclient/src/main/java/io/temporal/serviceclient/SimpleSslContextBuilder.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.security.KeyStore;
2929
import java.security.KeyStoreException;
3030
import java.security.NoSuchAlgorithmException;
31+
import javax.annotation.Nullable;
3132
import javax.net.ssl.*;
3233

3334
public class SimpleSslContextBuilder {
@@ -44,9 +45,9 @@ public class SimpleSslContextBuilder {
4445
// gRPC requires http2 protocol.
4546
ApplicationProtocolNames.HTTP_2);
4647

47-
private final PKCS pkcs;
48-
private final InputStream keyCertChain;
49-
private final InputStream key;
48+
private final @Nullable PKCS pkcs;
49+
private final @Nullable InputStream keyCertChain;
50+
private final @Nullable InputStream key;
5051
private TrustManager trustManager;
5152
private boolean useInsecureTrustManager;
5253
private String keyPassword;
@@ -66,20 +67,32 @@ public static SimpleSslContextBuilder newBuilder(InputStream keyCertChain, Input
6667
return forPKCS8(keyCertChain, key);
6768
}
6869

70+
/**
71+
* Explicitly creates a builder without a client private key or certificate chain.
72+
*
73+
* <p>{@link #forPKCS8} and {@link #forPKCS12} support null inputs too for easier configuration
74+
* API
75+
*/
76+
public static SimpleSslContextBuilder noKeyOrCertChain() {
77+
return new SimpleSslContextBuilder(null, null, null);
78+
}
79+
6980
/**
7081
* @param keyCertChain - an input stream for an X.509 client certificate chain in PEM format.
7182
* @param key - an input stream for a PKCS#8 client private key in PEM format.
7283
*/
73-
public static SimpleSslContextBuilder forPKCS8(InputStream keyCertChain, InputStream key) {
84+
public static SimpleSslContextBuilder forPKCS8(
85+
@Nullable InputStream keyCertChain, @Nullable InputStream key) {
7486
return new SimpleSslContextBuilder(PKCS.PKCS_8, keyCertChain, key);
7587
}
7688

7789
/** @param pfxKeyArchive - an input stream for .pfx or .p12 PKCS12 archive file */
78-
public static SimpleSslContextBuilder forPKCS12(InputStream pfxKeyArchive) {
90+
public static SimpleSslContextBuilder forPKCS12(@Nullable InputStream pfxKeyArchive) {
7991
return new SimpleSslContextBuilder(PKCS.PKCS_12, null, pfxKeyArchive);
8092
}
8193

82-
private SimpleSslContextBuilder(PKCS pkcs, InputStream keyCertChain, InputStream key) {
94+
private SimpleSslContextBuilder(
95+
@Nullable PKCS pkcs, @Nullable InputStream keyCertChain, @Nullable InputStream key) {
8396
this.pkcs = pkcs;
8497
this.keyCertChain = keyCertChain;
8598
this.key = key;
@@ -109,16 +122,18 @@ public SslContext build() throws SSLException {
109122
: getDefaultTrustManager())
110123
.applicationProtocolConfig(DEFAULT_APPLICATION_PROTOCOL_CONFIG);
111124

112-
switch (pkcs) {
113-
case PKCS_8:
114-
// netty by default supports PKCS8
115-
sslContextBuilder.keyManager(keyCertChain, key, keyPassword);
116-
break;
117-
case PKCS_12:
118-
sslContextBuilder.keyManager(createPKCS12KeyManager());
119-
break;
120-
default:
121-
throw new IllegalArgumentException("PKCS " + pkcs + " is not implemented");
125+
if (pkcs != null && (key != null || keyCertChain != null)) {
126+
switch (pkcs) {
127+
case PKCS_8:
128+
// netty by default supports PKCS8
129+
sslContextBuilder.keyManager(keyCertChain, key, keyPassword);
130+
break;
131+
case PKCS_12:
132+
sslContextBuilder.keyManager(createPKCS12KeyManager());
133+
break;
134+
default:
135+
throw new IllegalArgumentException("PKCS " + pkcs + " is not implemented");
136+
}
122137
}
123138

124139
return sslContextBuilder.build();

temporal-serviceclient/src/test/java/io/temporal/serviceclient/SimpleSslContextBuilderTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,55 @@
1919

2020
package io.temporal.serviceclient;
2121

22+
import com.google.common.base.Preconditions;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import org.junit.Test;
2526

2627
public class SimpleSslContextBuilderTest {
2728
@Test
2829
public void ableToLoadPKCS12Key() throws IOException {
29-
try (InputStream in = SimpleSslContextBuilderTest.class.getResourceAsStream("pkcs12-key.pfx")) {
30+
try (InputStream in =
31+
SimpleSslContextBuilderTest.class.getClassLoader().getResourceAsStream("pkcs12-key.pfx")) {
32+
Preconditions.checkState(in != null);
3033
SimpleSslContextBuilder builder = SimpleSslContextBuilder.forPKCS12(in);
3134
builder.build();
3235
}
3336
}
3437

38+
// to give easier API for configuration to users we allow null inputs
39+
@Test
40+
public void nullInputIsAcceptedForPKCS12Key() throws IOException {
41+
SimpleSslContextBuilder builder = SimpleSslContextBuilder.forPKCS12(null);
42+
builder.build();
43+
}
44+
3545
@Test
3646
public void ableToLoadPKCS8Key() throws IOException {
37-
try (InputStream pkIn = SimpleSslContextBuilderTest.class.getResourceAsStream("pkcs8-pk.pem");
47+
try (InputStream pkIn =
48+
SimpleSslContextBuilderTest.class.getClassLoader().getResourceAsStream("pkcs8-pk.pem");
3849
InputStream crtIn =
39-
SimpleSslContextBuilderTest.class.getResourceAsStream("pkcs8-crt-chain.pem")) {
50+
SimpleSslContextBuilderTest.class
51+
.getClassLoader()
52+
.getResourceAsStream("pkcs8-crt-chain.pem")) {
53+
Preconditions.checkState(pkIn != null);
54+
Preconditions.checkState(crtIn != null);
55+
4056
SimpleSslContextBuilder builder = SimpleSslContextBuilder.forPKCS8(crtIn, pkIn);
4157
builder.build();
4258
}
4359
}
60+
61+
// to give easier API for configuration to users we allow null inputs
62+
@Test
63+
public void nullInputIsAcceptedForPKCS8Key() throws IOException {
64+
SimpleSslContextBuilder builder = SimpleSslContextBuilder.forPKCS8(null, null);
65+
builder.build();
66+
}
67+
68+
@Test
69+
public void ableToCreateWithoutKeyOrCerts() throws IOException {
70+
SimpleSslContextBuilder builder = SimpleSslContextBuilder.noKeyOrCertChain();
71+
builder.build();
72+
}
4473
}

0 commit comments

Comments
 (0)