32
32
import com .datastax .oss .driver .api .core .config .DriverConfigLoader ;
33
33
import com .datastax .oss .driver .api .core .config .DriverOption ;
34
34
import com .datastax .oss .driver .api .core .config .ProgrammaticDriverConfigLoaderBuilder ;
35
+ import com .datastax .oss .driver .api .core .ssl .ProgrammaticSslEngineFactory ;
35
36
import com .datastax .oss .driver .internal .core .config .typesafe .DefaultDriverConfigLoader ;
36
37
import com .datastax .oss .driver .internal .core .config .typesafe .DefaultProgrammaticDriverConfigLoaderBuilder ;
37
38
import com .typesafe .config .Config ;
43
44
import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Connection ;
44
45
import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Controlconnection ;
45
46
import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Request ;
47
+ import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Ssl ;
46
48
import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Throttler ;
47
49
import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .ThrottlerType ;
48
50
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
49
51
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
50
52
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
51
53
import org .springframework .boot .context .properties .PropertyMapper ;
54
+ import org .springframework .boot .ssl .SslBundle ;
55
+ import org .springframework .boot .ssl .SslBundles ;
56
+ import org .springframework .boot .ssl .SslOptions ;
52
57
import org .springframework .context .annotation .Bean ;
53
58
import org .springframework .context .annotation .Lazy ;
54
59
import org .springframework .context .annotation .Scope ;
55
60
import org .springframework .core .io .Resource ;
61
+ import org .springframework .util .CollectionUtils ;
62
+ import org .springframework .util .StringUtils ;
56
63
57
64
/**
58
65
* {@link EnableAutoConfiguration Auto-configuration} for Cassandra.
66
73
* @author Moritz Halbritter
67
74
* @author Andy Wilkinson
68
75
* @author Phillip Webb
76
+ * @author Scott Frederick
69
77
* @since 1.3.0
70
78
*/
71
79
@ AutoConfiguration
@@ -106,10 +114,10 @@ public CqlSession cassandraSession(CqlSessionBuilder cqlSessionBuilder) {
106
114
@ Scope ("prototype" )
107
115
public CqlSessionBuilder cassandraSessionBuilder (DriverConfigLoader driverConfigLoader ,
108
116
CassandraConnectionDetails connectionDetails ,
109
- ObjectProvider <CqlSessionBuilderCustomizer > builderCustomizers ) {
117
+ ObjectProvider <CqlSessionBuilderCustomizer > builderCustomizers , ObjectProvider < SslBundles > sslBundles ) {
110
118
CqlSessionBuilder builder = CqlSession .builder ().withConfigLoader (driverConfigLoader );
111
119
configureAuthentication (builder , connectionDetails );
112
- configureSsl (builder , connectionDetails );
120
+ configureSsl (builder , connectionDetails , sslBundles . getIfAvailable () );
113
121
builder .withKeyspace (this .properties .getKeyspaceName ());
114
122
builderCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (builder ));
115
123
return builder ;
@@ -122,15 +130,38 @@ private void configureAuthentication(CqlSessionBuilder builder, CassandraConnect
122
130
}
123
131
}
124
132
125
- private void configureSsl (CqlSessionBuilder builder , CassandraConnectionDetails connectionDetails ) {
126
- if ( connectionDetails instanceof PropertiesCassandraConnectionDetails && this . properties . isSsl () ) {
127
- try {
128
- builder . withSslContext ( SSLContext . getDefault ()) ;
129
- }
130
- catch ( NoSuchAlgorithmException ex ) {
131
- throw new IllegalStateException ( "Could not setup SSL default context for Cassandra" , ex );
132
- }
133
+ private void configureSsl (CqlSessionBuilder builder , CassandraConnectionDetails connectionDetails ,
134
+ SslBundles sslBundles ) {
135
+ if (!( connectionDetails instanceof PropertiesCassandraConnectionDetails )) {
136
+ return ;
137
+ }
138
+ Ssl properties = this . properties . getSsl ();
139
+ if ( properties == null || ! properties . isEnabled ()) {
140
+ return ;
133
141
}
142
+ String bundleName = properties .getBundle ();
143
+ if (!StringUtils .hasLength (bundleName )) {
144
+ configureDefaultSslContext (builder );
145
+ }
146
+ else {
147
+ configureSsl (builder , sslBundles .getBundle (bundleName ));
148
+ }
149
+ }
150
+
151
+ private void configureDefaultSslContext (CqlSessionBuilder builder ) {
152
+ try {
153
+ builder .withSslContext (SSLContext .getDefault ());
154
+ }
155
+ catch (NoSuchAlgorithmException ex ) {
156
+ throw new IllegalStateException ("Could not setup SSL default context for Cassandra" , ex );
157
+ }
158
+ }
159
+
160
+ private void configureSsl (CqlSessionBuilder builder , SslBundle sslBundle ) {
161
+ SslOptions options = sslBundle .getOptions ();
162
+ String [] ciphers = (!CollectionUtils .isEmpty (options .getCiphers ()) ? null
163
+ : options .getCiphers ().toArray (String []::new ));
164
+ builder .withSslEngineFactory (new ProgrammaticSslEngineFactory (sslBundle .createSslContext (), ciphers ));
134
165
}
135
166
136
167
@ Bean (destroyMethod = "" )
0 commit comments