Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public TlsConfigHelper() {}
* @param trustedCertsPem Certificate in PEM format.
*/
public void setTrustManagerFromCerts(byte[] trustedCertsPem) {
if (sslContext != null) {
throw new IllegalStateException("sslContext has been previously configured");
}
if (trustManager != null) {
throw new IllegalStateException("trustManager has been previously configured");
}
Expand Down Expand Up @@ -72,6 +75,9 @@ public void setKeyManagerFromCerts(byte[] privateKeyPem, byte[] certificatePem)
if (keyManager != null) {
throw new IllegalStateException("keyManager has been previously configured");
}
if (sslContext != null) {
throw new IllegalStateException("sslContext has been previously configured");
}

try {
keyManager = TlsUtil.keyManager(privateKeyPem, certificatePem);
Expand All @@ -95,6 +101,9 @@ public void setSslContext(SSLContext sslContext, X509TrustManager trustManager)
if (this.sslContext != null || this.trustManager != null) {
throw new IllegalStateException("sslContext or trustManager has been previously configured");
}
if (this.keyManager != null) {
throw new IllegalStateException("keyManager has been previously configured");
Comment thread
thswlsqls marked this conversation as resolved.
}
this.trustManager = trustManager;
this.sslContext = sslContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void createTrustManager_AlreadyExists_Throws() throws Exception {
SSLContext.getInstance("TLS"), TlsUtil.trustManager(serverTls.certificate().getEncoded()));
assertThatThrownBy(() -> helper.setTrustManagerFromCerts(serverTls.certificate().getEncoded()))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("trustManager has been previously configured");
.hasMessageContaining("sslContext has been previously configured");
}

@Test
Expand All @@ -66,6 +66,16 @@ void createKeyManager_AlreadyExists_Throws() throws Exception {
serverTls.privateKey().getEncoded(), serverTls.certificate().getEncoded()))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("keyManager has been previously configured");

helper = new TlsConfigHelper();
helper.setSslContext(
SSLContext.getInstance("TLS"), TlsUtil.trustManager(serverTls.certificate().getEncoded()));
assertThatThrownBy(
() ->
helper.setKeyManagerFromCerts(
serverTls.privateKey().getEncoded(), serverTls.certificate().getEncoded()))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("sslContext has been previously configured");
}

@Test
Expand Down Expand Up @@ -93,5 +103,12 @@ void setSslContext_AlreadyExists_Throws() throws Exception {
assertThatThrownBy(() -> helper.setSslContext(sslContext, trustManager))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("sslContext or trustManager has been previously configured");

helper = new TlsConfigHelper();
helper.setKeyManagerFromCerts(
serverTls.privateKey().getEncoded(), serverTls.certificate().getEncoded());
assertThatThrownBy(() -> helper.setSslContext(sslContext, trustManager))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("keyManager has been previously configured");
}
}
Loading