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 @@ -72,6 +72,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 +98,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 on lines 100 to +102

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, but consider also adding an explicit sslContext guard to setTrustManagerFromCerts for full symmetry with the two new guards here and in setKeyManagerFromCerts.

Today the setSslContextsetTrustManagerFromCerts case is caught transitively, because setSslContext also assigns this.trustManager, so the existing trustManager != null check fires. Two small downsides:

  1. The resulting message is "trustManager has been previously configured", which is a little misleading when the user actually called setSslContext.
  2. It silently relies on setSslContext requiring a non-null X509TrustManager. If that ever loosens, the guard vanishes.

Proposed addition in setTrustManagerFromCerts, mirroring the pattern used elsewhere in this PR:

  public void setTrustManagerFromCerts(byte[] trustedCertsPem) {
    if (trustManager != null) {
      throw new IllegalStateException("trustManager has been previously configured");
    }
    if (sslContext != null) {
      throw new IllegalStateException("sslContext has been previously configured");
    }
    ...
  }

With this, all three setters follow the same "reject each other mode explicitly" shape.

}
this.trustManager = trustManager;
this.sslContext = sslContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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