Skip to content

Commit 909c09c

Browse files
scottfrederickphilwebb
authored andcommitted
Provide centralized configuration of SSL properties
Closes gh-34814
2 parents e61adc6 + a03f857 commit 909c09c

File tree

93 files changed

+4556
-1329
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+4556
-1329
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,10 @@
20642064
"level": "error"
20652065
}
20662066
},
2067+
{
2068+
"name": "management.server.ssl.bundle",
2069+
"description": "The name of a configured SSL bundle."
2070+
},
20672071
{
20682072
"name": "management.server.ssl.certificate",
20692073
"description": "Path to a PEM-encoded SSL certificate file."

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.boot.rsocket.netty.NettyRSocketServerFactory;
3838
import org.springframework.boot.rsocket.server.RSocketServerCustomizer;
3939
import org.springframework.boot.rsocket.server.RSocketServerFactory;
40+
import org.springframework.boot.ssl.SslBundles;
4041
import org.springframework.context.annotation.Bean;
4142
import org.springframework.context.annotation.Conditional;
4243
import org.springframework.context.annotation.Configuration;
@@ -54,6 +55,7 @@
5455
* server port is configured, a new standalone RSocket server is created.
5556
*
5657
* @author Brian Clozel
58+
* @author Scott Frederick
5759
* @since 2.2.0
5860
*/
5961
@AutoConfiguration(after = RSocketStrategiesAutoConfiguration.class)
@@ -85,7 +87,7 @@ static class EmbeddedServerConfiguration {
8587
@Bean
8688
@ConditionalOnMissingBean
8789
RSocketServerFactory rSocketServerFactory(RSocketProperties properties, ReactorResourceFactory resourceFactory,
88-
ObjectProvider<RSocketServerCustomizer> customizers) {
90+
ObjectProvider<RSocketServerCustomizer> customizers, ObjectProvider<SslBundles> sslBundles) {
8991
NettyRSocketServerFactory factory = new NettyRSocketServerFactory();
9092
factory.setResourceFactory(resourceFactory);
9193
factory.setTransport(properties.getServer().getTransport());
@@ -94,6 +96,7 @@ RSocketServerFactory rSocketServerFactory(RSocketProperties properties, ReactorR
9496
map.from(properties.getServer().getPort()).to(factory::setPort);
9597
map.from(properties.getServer().getFragmentSize()).to(factory::setFragmentSize);
9698
map.from(properties.getServer().getSsl()).to(factory::setSsl);
99+
factory.setSslBundles(sslBundles.getIfAvailable());
97100
factory.setRSocketServerCustomizers(customizers.orderedStream().toList());
98101
return factory;
99102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2012-2023 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.autoconfigure.ssl;
18+
19+
import org.springframework.boot.ssl.jks.JksSslStoreBundle;
20+
21+
/**
22+
* {@link SslBundleProperties} for Java keystores.
23+
*
24+
* @author Scott Frederick
25+
* @author Phillip Webb
26+
* @since 3.1.0
27+
* @see JksSslStoreBundle
28+
*/
29+
public class JksSslBundleProperties extends SslBundleProperties {
30+
31+
/**
32+
* Keystore properties.
33+
*/
34+
private final Store keystore = new Store();
35+
36+
/**
37+
* Truststore properties.
38+
*/
39+
private final Store truststore = new Store();
40+
41+
public Store getKeystore() {
42+
return this.keystore;
43+
}
44+
45+
public Store getTruststore() {
46+
return this.truststore;
47+
}
48+
49+
/**
50+
* Store properties.
51+
*/
52+
public static class Store {
53+
54+
/**
55+
* Type of the store to create, e.g. JKS.
56+
*/
57+
private String type;
58+
59+
/**
60+
* Provider for the store.
61+
*/
62+
private String provider;
63+
64+
/**
65+
* Location of the resource containing the store content.
66+
*/
67+
private String location;
68+
69+
/**
70+
* Password used to access the store.
71+
*/
72+
private String password;
73+
74+
public String getType() {
75+
return this.type;
76+
}
77+
78+
public void setType(String type) {
79+
this.type = type;
80+
}
81+
82+
public String getProvider() {
83+
return this.provider;
84+
}
85+
86+
public void setProvider(String provider) {
87+
this.provider = provider;
88+
}
89+
90+
public String getLocation() {
91+
return this.location;
92+
}
93+
94+
public void setLocation(String location) {
95+
this.location = location;
96+
}
97+
98+
public String getPassword() {
99+
return this.password;
100+
}
101+
102+
public void setPassword(String password) {
103+
this.password = password;
104+
}
105+
106+
}
107+
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2012-2023 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.autoconfigure.ssl;
18+
19+
import org.springframework.boot.ssl.pem.PemSslStoreBundle;
20+
21+
/**
22+
* {@link SslBundleProperties} for PEM-encoded certificates and private keys.
23+
*
24+
* @author Scott Frederick
25+
* @author Phillip Webb
26+
* @since 3.1.0
27+
* @see PemSslStoreBundle
28+
*/
29+
public class PemSslBundleProperties extends SslBundleProperties {
30+
31+
/**
32+
* Keystore properties.
33+
*/
34+
private Store keystore = new Store();
35+
36+
/**
37+
* Truststore properties.
38+
*/
39+
private Store truststore = new Store();
40+
41+
public Store getKeystore() {
42+
return this.keystore;
43+
}
44+
45+
public Store getTruststore() {
46+
return this.truststore;
47+
}
48+
49+
/**
50+
* Store properties.
51+
*/
52+
public static class Store {
53+
54+
/**
55+
* Type of the store to create, e.g. JKS.
56+
*/
57+
String type;
58+
59+
/**
60+
* Location or content of the certificate in PEM format.
61+
*/
62+
String certificate;
63+
64+
/**
65+
* Location or content of the private key in PEM format.
66+
*/
67+
String privateKey;
68+
69+
public String getType() {
70+
return this.type;
71+
}
72+
73+
public void setType(String type) {
74+
this.type = type;
75+
}
76+
77+
public String getCertificate() {
78+
return this.certificate;
79+
}
80+
81+
public void setCertificate(String certificate) {
82+
this.certificate = certificate;
83+
}
84+
85+
public String getPrivateKey() {
86+
return this.privateKey;
87+
}
88+
89+
public void setPrivateKey(String privateKey) {
90+
this.privateKey = privateKey;
91+
}
92+
93+
}
94+
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2012-2023 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.autoconfigure.ssl;
18+
19+
import org.springframework.boot.autoconfigure.ssl.SslBundleProperties.Key;
20+
import org.springframework.boot.ssl.SslBundle;
21+
import org.springframework.boot.ssl.SslBundleKey;
22+
import org.springframework.boot.ssl.SslManagerBundle;
23+
import org.springframework.boot.ssl.SslOptions;
24+
import org.springframework.boot.ssl.SslStoreBundle;
25+
import org.springframework.boot.ssl.jks.JksSslStoreBundle;
26+
import org.springframework.boot.ssl.jks.JksSslStoreDetails;
27+
import org.springframework.boot.ssl.pem.PemSslStoreBundle;
28+
import org.springframework.boot.ssl.pem.PemSslStoreDetails;
29+
30+
/**
31+
* {@link SslBundle} backed by {@link JksSslBundleProperties} or
32+
* {@link PemSslBundleProperties}.
33+
*
34+
* @author Scott Frederick
35+
* @author Phillip Webb
36+
* @since 3.1.0
37+
*/
38+
public final class PropertiesSslBundle implements SslBundle {
39+
40+
private final SslStoreBundle stores;
41+
42+
private final SslBundleKey key;
43+
44+
private final SslOptions options;
45+
46+
private final String protocol;
47+
48+
private final SslManagerBundle managers;
49+
50+
private PropertiesSslBundle(SslStoreBundle stores, SslBundleProperties properties) {
51+
this.stores = stores;
52+
this.key = asSslKeyReference(properties.getKey());
53+
this.options = asSslOptions(properties.getOptions());
54+
this.protocol = properties.getProtocol();
55+
this.managers = SslManagerBundle.from(this.stores, this.key);
56+
}
57+
58+
private static SslBundleKey asSslKeyReference(Key key) {
59+
return (key != null) ? SslBundleKey.of(key.getPassword(), key.getAlias()) : SslBundleKey.NONE;
60+
}
61+
62+
private static SslOptions asSslOptions(SslBundleProperties.Options properties) {
63+
return (properties != null) ? SslOptions.of(properties.getCiphers(), properties.getEnabledProtocols())
64+
: SslOptions.NONE;
65+
}
66+
67+
@Override
68+
public SslStoreBundle getStores() {
69+
return this.stores;
70+
}
71+
72+
@Override
73+
public SslBundleKey getKey() {
74+
return this.key;
75+
}
76+
77+
@Override
78+
public SslOptions getOptions() {
79+
return this.options;
80+
}
81+
82+
@Override
83+
public String getProtocol() {
84+
return this.protocol;
85+
}
86+
87+
@Override
88+
public SslManagerBundle getManagers() {
89+
return this.managers;
90+
}
91+
92+
/**
93+
* Get an {@link SslBundle} for the given {@link PemSslBundleProperties}.
94+
* @param properties the source properties
95+
* @return an {@link SslBundle} instance
96+
*/
97+
public static SslBundle get(PemSslBundleProperties properties) {
98+
return new PropertiesSslBundle(asSslStoreBundle(properties), properties);
99+
}
100+
101+
/**
102+
* Get an {@link SslBundle} for the given {@link JksSslBundleProperties}.
103+
* @param properties the source properties
104+
* @return an {@link SslBundle} instance
105+
*/
106+
public static SslBundle get(JksSslBundleProperties properties) {
107+
return new PropertiesSslBundle(asSslStoreBundle(properties), properties);
108+
}
109+
110+
private static SslStoreBundle asSslStoreBundle(PemSslBundleProperties properties) {
111+
PemSslStoreDetails keyStoreDetails = asStoreDetails(properties.getKeystore());
112+
PemSslStoreDetails trustStoreDetails = asStoreDetails(properties.getTruststore());
113+
return new PemSslStoreBundle(keyStoreDetails, trustStoreDetails, properties.getKey().getAlias());
114+
}
115+
116+
private static PemSslStoreDetails asStoreDetails(PemSslBundleProperties.Store properties) {
117+
return new PemSslStoreDetails(properties.getType(), properties.getCertificate(), properties.getPrivateKey());
118+
}
119+
120+
private static SslStoreBundle asSslStoreBundle(JksSslBundleProperties properties) {
121+
JksSslStoreDetails keyStoreDetails = asStoreDetails(properties.getKeystore());
122+
JksSslStoreDetails trustStoreDetails = asStoreDetails(properties.getTruststore());
123+
return new JksSslStoreBundle(keyStoreDetails, trustStoreDetails);
124+
}
125+
126+
private static JksSslStoreDetails asStoreDetails(JksSslBundleProperties.Store properties) {
127+
return new JksSslStoreDetails(properties.getType(), properties.getProvider(), properties.getLocation(),
128+
properties.getPassword());
129+
}
130+
131+
}

0 commit comments

Comments
 (0)