Browse Source

Polish "Fix bug in webserver start when loading PKCS#11 KeyStore"

See gh-32179
pull/33648/head
Moritz Halbritter 3 years ago
parent
commit
16569099ba
  1. 8
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizer.java
  2. 4
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/SslServerCustomizer.java
  3. 8
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizer.java
  4. 3
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/SslBuilderCustomizer.java
  5. 2
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/MockPkcs11SecurityProvider.java
  6. 2
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizerTests.java

8
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizer.java

@ -19,7 +19,6 @@ package org.springframework.boot.web.embedded.jetty; @@ -19,7 +19,6 @@ package org.springframework.boot.web.embedded.jetty;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.Objects;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.http.HttpVersion;
@ -222,10 +221,10 @@ class SslServerCustomizer implements JettyServerCustomizer { @@ -222,10 +221,10 @@ class SslServerCustomizer implements JettyServerCustomizer {
}
private void configureSslKeyStore(SslContextFactory.Server factory, Ssl ssl) {
final String keystoreType = Objects.requireNonNullElse(ssl.getKeyStoreType(), "JKS");
final String keystoreLocation = ssl.getKeyStore();
String keystoreType = (ssl.getKeyStoreType() != null) ? ssl.getKeyStoreType() : "JKS";
String keystoreLocation = ssl.getKeyStore();
if (keystoreType.equalsIgnoreCase("PKCS11")) {
if (keystoreLocation != null && !keystoreLocation.isBlank()) {
if (keystoreLocation != null && !keystoreLocation.isEmpty()) {
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
+ keystoreLocation + "'. Must be undefined / null.");
}
@ -239,7 +238,6 @@ class SslServerCustomizer implements JettyServerCustomizer { @@ -239,7 +238,6 @@ class SslServerCustomizer implements JettyServerCustomizer {
throw new WebServerException("Could not load key store '" + keystoreLocation + "'", ex);
}
}
factory.setKeyStoreType(keystoreType);
if (ssl.getKeyStoreProvider() != null) {
factory.setKeyStoreProvider(ssl.getKeyStoreProvider());

4
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/SslServerCustomizer.java

@ -173,11 +173,10 @@ public class SslServerCustomizer implements NettyServerCustomizer { @@ -173,11 +173,10 @@ public class SslServerCustomizer implements NettyServerCustomizer {
type = (type != null) ? type : "JKS";
KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
if (type.equalsIgnoreCase("PKCS11")) {
if (resource != null && !resource.isBlank()) {
if (resource != null && !resource.isEmpty()) {
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
+ resource + "'. Must be undefined / null.");
}
store.load(null, (password != null) ? password.toCharArray() : null);
}
else {
@ -191,7 +190,6 @@ public class SslServerCustomizer implements NettyServerCustomizer { @@ -191,7 +190,6 @@ public class SslServerCustomizer implements NettyServerCustomizer {
throw new WebServerException("Could not load key store '" + resource + "'", ex);
}
}
return store;
}

8
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizer.java

@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.boot.web.embedded.tomcat;
import java.io.FileNotFoundException;
import java.util.Objects;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
@ -141,10 +140,10 @@ class SslConnectorCustomizer implements TomcatConnectorCustomizer { @@ -141,10 +140,10 @@ class SslConnectorCustomizer implements TomcatConnectorCustomizer {
}
private void configureSslKeyStore(SSLHostConfigCertificate certificate, Ssl ssl) {
final String keystoreType = Objects.requireNonNullElse(ssl.getKeyStoreType(), "JKS");
final String keystoreLocation = ssl.getKeyStore();
String keystoreType = (ssl.getKeyStoreType() != null) ? ssl.getKeyStoreType() : "JKS";
String keystoreLocation = ssl.getKeyStore();
if (keystoreType.equalsIgnoreCase("PKCS11")) {
if (keystoreLocation != null && !keystoreLocation.isBlank()) {
if (keystoreLocation != null && !keystoreLocation.isEmpty()) {
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
+ keystoreLocation + "'. Must be undefined / null.");
}
@ -157,7 +156,6 @@ class SslConnectorCustomizer implements TomcatConnectorCustomizer { @@ -157,7 +156,6 @@ class SslConnectorCustomizer implements TomcatConnectorCustomizer {
throw new WebServerException("Could not load key store '" + keystoreLocation + "'", ex);
}
}
certificate.setCertificateKeystoreType(keystoreType);
if (ssl.getKeyStoreProvider() != null) {
certificate.setCertificateKeystoreProvider(ssl.getKeyStoreProvider());

3
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/SslBuilderCustomizer.java

@ -182,11 +182,10 @@ class SslBuilderCustomizer implements UndertowBuilderCustomizer { @@ -182,11 +182,10 @@ class SslBuilderCustomizer implements UndertowBuilderCustomizer {
type = (type != null) ? type : "JKS";
KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
if (type.equalsIgnoreCase("PKCS11")) {
if (resource != null && !resource.isBlank()) {
if (resource != null && !resource.isEmpty()) {
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
+ resource + "'. Must be undefined / null.");
}
store.load(null, (password != null) ? password.toCharArray() : null);
}
else {

2
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/MockPkcs11SecurityProvider.java

@ -29,7 +29,7 @@ public class MockPkcs11SecurityProvider extends Provider { @@ -29,7 +29,7 @@ public class MockPkcs11SecurityProvider extends Provider {
private static final String DEFAULT_PROVIDER_NAME = "Mock-PKCS11";
private static final String VERSION = "0.1";
private static final double VERSION = 0.1;
private static final String DESCRIPTION = "Mock PKCS11 Provider";

2
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizerTests.java

@ -40,8 +40,8 @@ import org.junit.jupiter.api.extension.ExtendWith; @@ -40,8 +40,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.boot.web.embedded.netty.MockPkcs11SecurityProvider;
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
import org.springframework.boot.web.embedded.netty.MockPkcs11SecurityProvider;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.SslStoreProvider;
import org.springframework.boot.web.server.WebServerException;

Loading…
Cancel
Save