Skip to content

Commit 1656909

Browse files
committed
Polish "Fix bug in webserver start when loading PKCS#11 KeyStore"
See gh-32179
1 parent 716a839 commit 1656909

File tree

6 files changed

+10
-17
lines changed

6 files changed

+10
-17
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.net.InetSocketAddress;
2121
import java.net.URL;
22-
import java.util.Objects;
2322

2423
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
2524
import org.eclipse.jetty.http.HttpVersion;
@@ -222,10 +221,10 @@ private void configureSslPasswords(SslContextFactory.Server factory, Ssl ssl) {
222221
}
223222

224223
private void configureSslKeyStore(SslContextFactory.Server factory, Ssl ssl) {
225-
final String keystoreType = Objects.requireNonNullElse(ssl.getKeyStoreType(), "JKS");
226-
final String keystoreLocation = ssl.getKeyStore();
224+
String keystoreType = (ssl.getKeyStoreType() != null) ? ssl.getKeyStoreType() : "JKS";
225+
String keystoreLocation = ssl.getKeyStore();
227226
if (keystoreType.equalsIgnoreCase("PKCS11")) {
228-
if (keystoreLocation != null && !keystoreLocation.isBlank()) {
227+
if (keystoreLocation != null && !keystoreLocation.isEmpty()) {
229228
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
230229
+ keystoreLocation + "'. Must be undefined / null.");
231230
}
@@ -239,7 +238,6 @@ private void configureSslKeyStore(SslContextFactory.Server factory, Ssl ssl) {
239238
throw new WebServerException("Could not load key store '" + keystoreLocation + "'", ex);
240239
}
241240
}
242-
243241
factory.setKeyStoreType(keystoreType);
244242
if (ssl.getKeyStoreProvider() != null) {
245243
factory.setKeyStoreProvider(ssl.getKeyStoreProvider());

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ private KeyStore loadStore(String type, String provider, String resource, String
173173
type = (type != null) ? type : "JKS";
174174
KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
175175
if (type.equalsIgnoreCase("PKCS11")) {
176-
if (resource != null && !resource.isBlank()) {
176+
if (resource != null && !resource.isEmpty()) {
177177
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
178178
+ resource + "'. Must be undefined / null.");
179179
}
180-
181180
store.load(null, (password != null) ? password.toCharArray() : null);
182181
}
183182
else {
@@ -191,7 +190,6 @@ private KeyStore loadStore(String type, String provider, String resource, String
191190
throw new WebServerException("Could not load key store '" + resource + "'", ex);
192191
}
193192
}
194-
195193
return store;
196194
}
197195

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.web.embedded.tomcat;
1818

1919
import java.io.FileNotFoundException;
20-
import java.util.Objects;
2120

2221
import org.apache.catalina.connector.Connector;
2322
import org.apache.coyote.ProtocolHandler;
@@ -141,10 +140,10 @@ protected void configureSslStoreProvider(AbstractHttp11JsseProtocol<?> protocol,
141140
}
142141

143142
private void configureSslKeyStore(SSLHostConfigCertificate certificate, Ssl ssl) {
144-
final String keystoreType = Objects.requireNonNullElse(ssl.getKeyStoreType(), "JKS");
145-
final String keystoreLocation = ssl.getKeyStore();
143+
String keystoreType = (ssl.getKeyStoreType() != null) ? ssl.getKeyStoreType() : "JKS";
144+
String keystoreLocation = ssl.getKeyStore();
146145
if (keystoreType.equalsIgnoreCase("PKCS11")) {
147-
if (keystoreLocation != null && !keystoreLocation.isBlank()) {
146+
if (keystoreLocation != null && !keystoreLocation.isEmpty()) {
148147
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
149148
+ keystoreLocation + "'. Must be undefined / null.");
150149
}
@@ -157,7 +156,6 @@ private void configureSslKeyStore(SSLHostConfigCertificate certificate, Ssl ssl)
157156
throw new WebServerException("Could not load key store '" + keystoreLocation + "'", ex);
158157
}
159158
}
160-
161159
certificate.setCertificateKeystoreType(keystoreType);
162160
if (ssl.getKeyStoreProvider() != null) {
163161
certificate.setCertificateKeystoreProvider(ssl.getKeyStoreProvider());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,10 @@ private KeyStore loadStore(String type, String provider, String resource, String
182182
type = (type != null) ? type : "JKS";
183183
KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
184184
if (type.equalsIgnoreCase("PKCS11")) {
185-
if (resource != null && !resource.isBlank()) {
185+
if (resource != null && !resource.isEmpty()) {
186186
throw new IllegalArgumentException("Input keystore location is not valid for keystore type 'PKCS11': '"
187187
+ resource + "'. Must be undefined / null.");
188188
}
189-
190189
store.load(null, (password != null) ? password.toCharArray() : null);
191190
}
192191
else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class MockPkcs11SecurityProvider extends Provider {
2929

3030
private static final String DEFAULT_PROVIDER_NAME = "Mock-PKCS11";
3131

32-
private static final String VERSION = "0.1";
32+
private static final double VERSION = 0.1;
3333

3434
private static final String DESCRIPTION = "Mock PKCS11 Provider";
3535

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
import org.springframework.boot.testsupport.system.CapturedOutput;
4242
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
43-
import org.springframework.boot.web.embedded.netty.MockPkcs11SecurityProvider;
4443
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
44+
import org.springframework.boot.web.embedded.netty.MockPkcs11SecurityProvider;
4545
import org.springframework.boot.web.server.Ssl;
4646
import org.springframework.boot.web.server.SslStoreProvider;
4747
import org.springframework.boot.web.server.WebServerException;

0 commit comments

Comments
 (0)