Skip to content

Commit c59c8cc

Browse files
committed
Change SslOptions to use null for defaults rather than empty sets
Update `SslOptions` so that `null` is used for default values rather than empty sets. Most libraries use `null` to indicate defaults so aligning our class makes things easier. See gh-34814
1 parent 77c468c commit c59c8cc

File tree

8 files changed

+31
-39
lines changed

8 files changed

+31
-39
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.springframework.context.annotation.Lazy;
5959
import org.springframework.context.annotation.Scope;
6060
import org.springframework.core.io.Resource;
61-
import org.springframework.util.CollectionUtils;
6261
import org.springframework.util.StringUtils;
6362

6463
/**
@@ -159,8 +158,7 @@ private void configureDefaultSslContext(CqlSessionBuilder builder) {
159158

160159
private void configureSsl(CqlSessionBuilder builder, SslBundle sslBundle) {
161160
SslOptions options = sslBundle.getOptions();
162-
String[] ciphers = (!CollectionUtils.isEmpty(options.getCiphers()) ? null
163-
: options.getCiphers().toArray(String[]::new));
161+
String[] ciphers = (options.getCiphers() != null) ? options.getCiphers().toArray(String[]::new) : null;
164162
builder.withSslEngineFactory(new ProgrammaticSslEngineFactory(sslBundle.createSslContext(), ciphers));
165163
}
166164

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslOptions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@
3333
public interface SslOptions {
3434

3535
/**
36-
* {@link SslOptions} that returns no values.
36+
* {@link SslOptions} that returns {@code null} results.
3737
*/
38-
SslOptions NONE = of(Collections.emptySet(), Collections.emptySet());
38+
SslOptions NONE = of((Set<String>) null, (Set<String>) null);
3939

4040
/**
4141
* Return the ciphers that can be used or an empty set. The cipher names in this set
4242
* should be compatible with those supported by
4343
* {@link SSLEngine#getSupportedCipherSuites()}.
44-
* @return the ciphers that can be used
44+
* @return the ciphers that can be used or {@code null}
4545
*/
4646
Set<String> getCiphers();
4747

4848
/**
4949
* Return the protocols that should be enabled or an empty set. The protocols names in
5050
* this set should be compatible with those supported by
5151
* {@link SSLEngine#getSupportedProtocols()}.
52-
* @return the protocols to enable
52+
* @return the protocols to enable or {@code null}
5353
*/
5454
Set<String> getEnabledProtocols();
5555

@@ -74,12 +74,12 @@ static SslOptions of(Set<String> ciphers, Set<String> enabledProtocols) {
7474

7575
@Override
7676
public Set<String> getCiphers() {
77-
return (ciphers != null) ? ciphers : Collections.emptySet();
77+
return ciphers;
7878
}
7979

8080
@Override
8181
public Set<String> getEnabledProtocols() {
82-
return (enabledProtocols != null) ? enabledProtocols : Collections.emptySet();
82+
return enabledProtocols;
8383
}
8484

8585
};

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Method;
2323
import java.net.HttpURLConnection;
2424
import java.time.Duration;
25+
import java.util.Set;
2526
import java.util.concurrent.TimeUnit;
2627
import java.util.function.Supplier;
2728

@@ -49,7 +50,6 @@
4950
import org.springframework.http.client.SimpleClientHttpRequestFactory;
5051
import org.springframework.util.Assert;
5152
import org.springframework.util.ClassUtils;
52-
import org.springframework.util.CollectionUtils;
5353
import org.springframework.util.ReflectionUtils;
5454

5555
/**
@@ -172,10 +172,8 @@ private static HttpClient createHttpClient(Duration readTimeout, SslBundle sslBu
172172
}
173173
if (sslBundle != null) {
174174
SslOptions options = sslBundle.getOptions();
175-
String[] enabledProtocols = (!CollectionUtils.isEmpty(options.getEnabledProtocols()))
176-
? options.getEnabledProtocols().toArray(String[]::new) : null;
177-
String[] ciphers = (!CollectionUtils.isEmpty(options.getCiphers()))
178-
? options.getCiphers().toArray(String[]::new) : null;
175+
String[] enabledProtocols = toArray(options.getEnabledProtocols());
176+
String[] ciphers = toArray(options.getCiphers());
179177
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslBundle.createSslContext(),
180178
enabledProtocols, ciphers, new DefaultHostnameVerifier());
181179
connectionManagerBuilder.setSSLSocketFactory(socketFactory);
@@ -184,6 +182,10 @@ private static HttpClient createHttpClient(Duration readTimeout, SslBundle sslBu
184182
return HttpClientBuilder.create().setConnectionManager(connectionManager).build();
185183
}
186184

185+
private static String[] toArray(Set<String> set) {
186+
return (set != null) ? set.toArray(String[]::new) : null;
187+
}
188+
187189
}
188190

189191
/**

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import org.springframework.boot.web.server.Ssl.ClientAuth;
4141
import org.springframework.util.Assert;
4242
import org.springframework.util.ClassUtils;
43-
import org.springframework.util.CollectionUtils;
44-
import org.springframework.util.ObjectUtils;
4543

4644
/**
4745
* {@link JettyServerCustomizer} that configures SSL on the given Jetty server instance.
@@ -180,11 +178,11 @@ protected void configureSsl(SslContextFactory.Server factory, ClientAuth clientA
180178
factory.setKeyStorePassword(stores.getKeyStorePassword());
181179
}
182180
factory.setCertAlias(key.getAlias());
183-
if (!ObjectUtils.isEmpty(options.getCiphers())) {
181+
if (options.getCiphers() != null) {
184182
factory.setIncludeCipherSuites(options.getCiphers().toArray(String[]::new));
185183
factory.setExcludeCipherSuites();
186184
}
187-
if (!CollectionUtils.isEmpty(options.getEnabledProtocols())) {
185+
if (options.getEnabledProtocols() != null) {
188186
factory.setIncludeProtocols(options.getEnabledProtocols().toArray(String[]::new));
189187
}
190188
try {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.boot.ssl.SslOptions;
2727
import org.springframework.boot.web.server.Http2;
2828
import org.springframework.boot.web.server.Ssl;
29-
import org.springframework.util.CollectionUtils;
3029

3130
/**
3231
* {@link NettyServerCustomizer} that configures SSL for the given Reactor Netty server
@@ -66,12 +65,8 @@ protected AbstractProtocolSslContextSpec<?> createSslContextSpec() {
6665
sslContextSpec.configure((builder) -> {
6766
builder.trustManager(this.sslBundle.getManagers().getTrustManagerFactory());
6867
SslOptions options = this.sslBundle.getOptions();
69-
if (!CollectionUtils.isEmpty(options.getEnabledProtocols())) {
70-
builder.protocols(options.getEnabledProtocols());
71-
}
72-
if (!CollectionUtils.isEmpty(options.getCiphers())) {
73-
builder.ciphers(options.getCiphers());
74-
}
68+
builder.protocols(options.getEnabledProtocols());
69+
builder.ciphers(options.getCiphers());
7570
builder.clientAuth(org.springframework.boot.web.server.Ssl.ClientAuth.map(this.clientAuth, ClientAuth.NONE,
7671
ClientAuth.OPTIONAL, ClientAuth.REQUIRE));
7772
});

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.boot.ssl.SslStoreBundle;
3131
import org.springframework.boot.web.server.Ssl.ClientAuth;
3232
import org.springframework.util.Assert;
33-
import org.springframework.util.CollectionUtils;
3433
import org.springframework.util.StringUtils;
3534

3635
/**
@@ -86,7 +85,7 @@ void configureSsl(AbstractHttp11JsseProtocol<?> protocol) {
8685
certificate.setCertificateKeyAlias(key.getAlias());
8786
}
8887
sslHostConfig.addCertificate(certificate);
89-
if (!CollectionUtils.isEmpty(options.getCiphers())) {
88+
if (options.getCiphers() != null) {
9089
String ciphers = StringUtils.collectionToCommaDelimitedString(options.getCiphers());
9190
sslHostConfig.setCiphers(ciphers);
9291
}
@@ -96,9 +95,10 @@ void configureSsl(AbstractHttp11JsseProtocol<?> protocol) {
9695

9796
private void configureEnabledProtocols(AbstractHttp11JsseProtocol<?> protocol) {
9897
SslOptions options = this.sslBundle.getOptions();
99-
if (!CollectionUtils.isEmpty(options.getEnabledProtocols())) {
98+
if (options.getEnabledProtocols() != null) {
99+
String enabledProtocols = StringUtils.collectionToCommaDelimitedString(options.getEnabledProtocols());
100100
for (SSLHostConfig sslHostConfig : protocol.findSslHostConfigs()) {
101-
sslHostConfig.setProtocols(StringUtils.collectionToCommaDelimitedString(options.getEnabledProtocols()));
101+
sslHostConfig.setProtocols(enabledProtocols);
102102
}
103103
}
104104
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.boot.ssl.SslBundle;
2929
import org.springframework.boot.ssl.SslOptions;
3030
import org.springframework.boot.web.server.Ssl.ClientAuth;
31-
import org.springframework.util.CollectionUtils;
3231

3332
/**
3433
* {@link UndertowBuilderCustomizer} that configures SSL on the given builder instance.
@@ -62,10 +61,10 @@ public void customize(Undertow.Builder builder) {
6261
builder.addHttpsListener(this.port, getListenAddress(), sslContext);
6362
builder.setSocketOption(Options.SSL_CLIENT_AUTH_MODE, ClientAuth.map(this.clientAuth,
6463
SslClientAuthMode.NOT_REQUESTED, SslClientAuthMode.REQUESTED, SslClientAuthMode.REQUIRED));
65-
if (!CollectionUtils.isEmpty(options.getEnabledProtocols())) {
64+
if (options.getEnabledProtocols() != null) {
6665
builder.setSocketOption(Options.SSL_ENABLED_PROTOCOLS, Sequence.of(options.getEnabledProtocols()));
6766
}
68-
if (!CollectionUtils.isEmpty(options.getCiphers())) {
67+
if (options.getCiphers() != null) {
6968
builder.setSocketOption(Options.SSL_ENABLED_CIPHER_SUITES, Sequence.of(options.getCiphers()));
7069
}
7170
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ssl/SslOptionsTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
class SslOptionsTests {
3131

3232
@Test
33-
void noneReturnsEmptyCollections() {
33+
void noneReturnsNull() {
3434
SslOptions options = SslOptions.NONE;
35-
assertThat(options.getCiphers()).isEmpty();
36-
assertThat(options.getEnabledProtocols()).isEmpty();
35+
assertThat(options.getCiphers()).isNull();
36+
assertThat(options.getEnabledProtocols()).isNull();
3737
}
3838

3939
@Test
@@ -50,8 +50,8 @@ void ofWithNullArraysCreatesSslOptions() {
5050
String[] ciphers = null;
5151
String[] enabledProtocols = null;
5252
SslOptions options = SslOptions.of(ciphers, enabledProtocols);
53-
assertThat(options.getCiphers()).isEmpty();
54-
assertThat(options.getEnabledProtocols()).isEmpty();
53+
assertThat(options.getCiphers()).isNull();
54+
assertThat(options.getEnabledProtocols()).isNull();
5555
}
5656

5757
@Test
@@ -68,8 +68,8 @@ void ofWithNullSetCreatesSslOptions() {
6868
Set<String> ciphers = null;
6969
Set<String> enabledProtocols = null;
7070
SslOptions options = SslOptions.of(ciphers, enabledProtocols);
71-
assertThat(options.getCiphers()).isEmpty();
72-
assertThat(options.getEnabledProtocols()).isEmpty();
71+
assertThat(options.getCiphers()).isNull();
72+
assertThat(options.getEnabledProtocols()).isNull();
7373
}
7474

7575
}

0 commit comments

Comments
 (0)