Skip to content

Commit f281ff4

Browse files
committed
Merge branch '4.0.x'
Closes gh-50610
2 parents 7cf7d23 + e51e821 commit f281ff4

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

module/spring-boot-reactor-netty/src/main/java/org/springframework/boot/reactor/netty/SslServerCustomizer.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ public HttpServer apply(HttpServer server) {
7676
}
7777

7878
private void applySecurity(SslContextSpec spec) {
79-
spec.sslContext(this.sslProvider.getSslContext()).setSniAsyncMappings((serverName, promise) -> {
80-
SslProvider provider = (serverName != null) ? this.serverNameSslProviders.get(serverName)
81-
: this.sslProvider;
82-
return promise.setSuccess(provider);
83-
});
79+
spec.sslContext(this.sslProvider.getSslContext())
80+
.setSniAsyncMappings((serverName, promise) -> promise.setSuccess(getSslProvider(serverName)));
81+
}
82+
83+
SslProvider getSslProvider(@Nullable String serverName) {
84+
return (serverName != null) ? this.serverNameSslProviders.getOrDefault(serverName, this.sslProvider)
85+
: this.sslProvider;
8486
}
8587

8688
void updateSslBundle(@Nullable String serverName, SslBundle sslBundle) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.reactor.netty;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Test;
23+
import reactor.netty.tcp.SslProvider;
24+
25+
import org.springframework.boot.ssl.SslBundle;
26+
import org.springframework.boot.ssl.pem.PemSslStoreBundle;
27+
import org.springframework.boot.ssl.pem.PemSslStoreDetails;
28+
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
29+
import org.springframework.boot.web.server.Ssl;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link SslServerCustomizer}.
35+
*
36+
* @author Daeho Kwon
37+
*/
38+
class SslServerCustomizerTests {
39+
40+
@Test
41+
@WithPackageResources({ "1.key", "1.crt", "2.key", "2.crt" })
42+
void getSslProviderReturnsMappedProviderForKnownServerName() {
43+
SslBundle defaultBundle = createBundle("1.key", "1.crt");
44+
SslBundle mappedBundle = createBundle("2.key", "2.crt");
45+
SslServerCustomizer customizer = new SslServerCustomizer(null, Ssl.ClientAuth.NONE, defaultBundle,
46+
Map.of("mapped.example", mappedBundle));
47+
SslProvider mapped = customizer.getSslProvider("mapped.example");
48+
assertThat(mapped).isNotNull().isNotSameAs(customizer.getSslProvider(null));
49+
}
50+
51+
@Test
52+
@WithPackageResources({ "1.key", "1.crt", "2.key", "2.crt" })
53+
void getSslProviderFallsBackToDefaultWhenServerNameIsUnmapped() {
54+
SslBundle defaultBundle = createBundle("1.key", "1.crt");
55+
SslBundle mappedBundle = createBundle("2.key", "2.crt");
56+
SslServerCustomizer customizer = new SslServerCustomizer(null, Ssl.ClientAuth.NONE, defaultBundle,
57+
Map.of("mapped.example", mappedBundle));
58+
assertThat(customizer.getSslProvider("unmapped.example")).isSameAs(customizer.getSslProvider(null));
59+
}
60+
61+
@Test
62+
@WithPackageResources({ "1.key", "1.crt" })
63+
@SuppressWarnings("NullAway") // Test null check
64+
void getSslProviderReturnsDefaultWhenServerNameIsNull() {
65+
SslBundle defaultBundle = createBundle("1.key", "1.crt");
66+
SslServerCustomizer customizer = new SslServerCustomizer(null, Ssl.ClientAuth.NONE, defaultBundle,
67+
Collections.emptyMap());
68+
assertThat(customizer.getSslProvider(null)).isNotNull();
69+
}
70+
71+
private static SslBundle createBundle(String key, String certificate) {
72+
return SslBundle.of(new PemSslStoreBundle(
73+
new PemSslStoreDetails(null, "classpath:" + certificate, "classpath:" + key), null));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)