Skip to content

Commit 1375df8

Browse files
committed
Simplified ssl configuration
1 parent 6f809c8 commit 1375df8

File tree

3 files changed

+90
-79
lines changed

3 files changed

+90
-79
lines changed

instant-ssl-reloading-with-spring-grpc/src/main/java/nl/altindag/server/config/SSLConfig.java

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,102 +15,39 @@
1515
*/
1616
package nl.altindag.server.config;
1717

18+
import nl.altindag.server.model.EnhanceableSslBundle;
1819
import nl.altindag.ssl.SSLFactory;
1920
import org.springframework.beans.factory.annotation.Value;
20-
import org.springframework.boot.ssl.NoSuchSslBundleException;
21-
import org.springframework.boot.ssl.SslBundle;
22-
import org.springframework.boot.ssl.SslBundleKey;
23-
import org.springframework.boot.ssl.SslBundles;
24-
import org.springframework.boot.ssl.SslManagerBundle;
25-
import org.springframework.boot.ssl.SslOptions;
26-
import org.springframework.boot.ssl.SslStoreBundle;
21+
import org.springframework.boot.ssl.SslBundleRegistry;
2722
import org.springframework.context.annotation.Bean;
2823
import org.springframework.context.annotation.Configuration;
2924

30-
import javax.net.ssl.KeyManagerFactory;
31-
import javax.net.ssl.TrustManagerFactory;
32-
import java.util.Collections;
33-
import java.util.List;
34-
import java.util.function.BiConsumer;
35-
import java.util.function.Consumer;
36-
3725
@Configuration
3826
public class SSLConfig {
3927

40-
@Bean
41-
public SSLFactory sslFactory(@Value("${ssl.keystore-path}") String keyStorePath,
42-
@Value("${ssl.keystore-password}") char[] keyStorePassword,
43-
@Value("${ssl.truststore-path}") String trustStorePath,
44-
@Value("${ssl.truststore-password}") char[] trustStorePassword,
45-
@Value("${ssl.client-auth}") boolean isClientAuthenticationRequired) {
28+
private final SSLFactory sslFactory;
4629

47-
return SSLFactory.builder()
30+
public SSLConfig(@Value("${ssl.keystore-path}") String keyStorePath,
31+
@Value("${ssl.keystore-password}") char[] keyStorePassword,
32+
@Value("${ssl.truststore-path}") String trustStorePath,
33+
@Value("${ssl.truststore-password}") char[] trustStorePassword,
34+
@Value("${ssl.client-auth}") boolean isClientAuthenticationRequired,
35+
SslBundleRegistry registry) {
36+
37+
this.sslFactory = SSLFactory.builder()
4838
.withSwappableIdentityMaterial()
4939
.withSwappableTrustMaterial()
5040
.withIdentityMaterial(keyStorePath, keyStorePassword)
5141
.withTrustMaterial(trustStorePath, trustStorePassword)
5242
.withNeedClientAuthentication(isClientAuthenticationRequired)
5343
.build();
44+
45+
registry.registerBundle("reloadable-ssl-bundle", new EnhanceableSslBundle(sslFactory));
5446
}
5547

5648
@Bean
57-
public SslBundles sslBundles(SSLFactory sslFactory) {
58-
return new SslBundles() {
59-
@Override
60-
public SslBundle getBundle(String name) throws NoSuchSslBundleException {
61-
return new SslBundle() {
62-
@Override
63-
public SslStoreBundle getStores() {
64-
return null;
65-
}
66-
67-
@Override
68-
public SslBundleKey getKey() {
69-
return null;
70-
}
71-
72-
@Override
73-
public SslOptions getOptions() {
74-
return null;
75-
}
76-
77-
@Override
78-
public String getProtocol() {
79-
return "";
80-
}
81-
82-
@Override
83-
public SslManagerBundle getManagers() {
84-
return new SslManagerBundle() {
85-
@Override
86-
public KeyManagerFactory getKeyManagerFactory() {
87-
return sslFactory.getKeyManagerFactory().get();
88-
}
89-
90-
@Override
91-
public TrustManagerFactory getTrustManagerFactory() {
92-
return sslFactory.getTrustManagerFactory().get();
93-
}
94-
};
95-
}
96-
};
97-
}
98-
99-
@Override
100-
public void addBundleUpdateHandler(String name, Consumer<SslBundle> updateHandler) throws NoSuchSslBundleException {
101-
102-
}
103-
104-
@Override
105-
public void addBundleRegisterHandler(BiConsumer<String, SslBundle> registerHandler) {
106-
107-
}
108-
109-
@Override
110-
public List<String> getBundleNames() {
111-
return Collections.emptyList();
112-
}
113-
};
49+
public SSLFactory sslFactory() {
50+
return sslFactory;
11451
}
11552

11653
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package nl.altindag.server.model;
2+
3+
import nl.altindag.ssl.SSLFactory;
4+
import org.springframework.boot.ssl.SslBundle;
5+
import org.springframework.boot.ssl.SslBundleKey;
6+
import org.springframework.boot.ssl.SslManagerBundle;
7+
import org.springframework.boot.ssl.SslOptions;
8+
import org.springframework.boot.ssl.SslStoreBundle;
9+
10+
import javax.net.ssl.KeyManagerFactory;
11+
import javax.net.ssl.SSLContext;
12+
import javax.net.ssl.TrustManagerFactory;
13+
14+
@SuppressWarnings("OptionalGetWithoutIsPresent")
15+
public class EnhanceableSslBundle implements SslBundle {
16+
17+
private final SSLFactory sslFactory;
18+
19+
public EnhanceableSslBundle(SSLFactory sslFactory) {
20+
this.sslFactory = sslFactory;
21+
}
22+
23+
@Override
24+
public SslStoreBundle getStores() {
25+
return null;
26+
}
27+
28+
@Override
29+
public SslBundleKey getKey() {
30+
return null;
31+
}
32+
33+
@Override
34+
public SslOptions getOptions() {
35+
return new SslOptions() {
36+
@Override
37+
public String[] getCiphers() {
38+
return sslFactory.getSslParameters().getCipherSuites();
39+
}
40+
41+
@Override
42+
public String[] getEnabledProtocols() {
43+
return sslFactory.getSslParameters().getProtocols();
44+
}
45+
};
46+
}
47+
48+
@Override
49+
public String getProtocol() {
50+
return "";
51+
}
52+
53+
@Override
54+
public SslManagerBundle getManagers() {
55+
return new SslManagerBundle() {
56+
@Override
57+
public KeyManagerFactory getKeyManagerFactory() {
58+
return sslFactory.getKeyManagerFactory().get();
59+
}
60+
61+
@Override
62+
public TrustManagerFactory getTrustManagerFactory() {
63+
return sslFactory.getTrustManagerFactory().get();
64+
}
65+
};
66+
}
67+
68+
@Override
69+
public SSLContext createSslContext() {
70+
return sslFactory.getSslContext();
71+
}
72+
73+
}

instant-ssl-reloading-with-spring-grpc/src/main/resources/application.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ spring:
1212
ssl:
1313
enabled: true
1414
client-auth: require
15-
secure: true
15+
secure: true
16+
bundle: "reloadable-ssl-bundle"

0 commit comments

Comments
 (0)