3232import com .datastax .oss .driver .api .core .config .DriverConfigLoader ;
3333import com .datastax .oss .driver .api .core .config .DriverOption ;
3434import com .datastax .oss .driver .api .core .config .ProgrammaticDriverConfigLoaderBuilder ;
35+ import com .datastax .oss .driver .api .core .ssl .ProgrammaticSslEngineFactory ;
3536import com .datastax .oss .driver .internal .core .config .typesafe .DefaultDriverConfigLoader ;
3637import com .datastax .oss .driver .internal .core .config .typesafe .DefaultProgrammaticDriverConfigLoaderBuilder ;
3738import com .typesafe .config .Config ;
4344import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Connection ;
4445import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Controlconnection ;
4546import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Request ;
47+ import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Ssl ;
4648import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .Throttler ;
4749import org .springframework .boot .autoconfigure .cassandra .CassandraProperties .ThrottlerType ;
4850import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
4951import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
5052import org .springframework .boot .context .properties .EnableConfigurationProperties ;
5153import 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 ;
5257import org .springframework .context .annotation .Bean ;
5358import org .springframework .context .annotation .Lazy ;
5459import org .springframework .context .annotation .Scope ;
5560import org .springframework .core .io .Resource ;
61+ import org .springframework .util .CollectionUtils ;
62+ import org .springframework .util .StringUtils ;
5663
5764/**
5865 * {@link EnableAutoConfiguration Auto-configuration} for Cassandra.
6673 * @author Moritz Halbritter
6774 * @author Andy Wilkinson
6875 * @author Phillip Webb
76+ * @author Scott Frederick
6977 * @since 1.3.0
7078 */
7179@ AutoConfiguration
@@ -106,10 +114,10 @@ public CqlSession cassandraSession(CqlSessionBuilder cqlSessionBuilder) {
106114 @ Scope ("prototype" )
107115 public CqlSessionBuilder cassandraSessionBuilder (DriverConfigLoader driverConfigLoader ,
108116 CassandraConnectionDetails connectionDetails ,
109- ObjectProvider <CqlSessionBuilderCustomizer > builderCustomizers ) {
117+ ObjectProvider <CqlSessionBuilderCustomizer > builderCustomizers , ObjectProvider < SslBundles > sslBundles ) {
110118 CqlSessionBuilder builder = CqlSession .builder ().withConfigLoader (driverConfigLoader );
111119 configureAuthentication (builder , connectionDetails );
112- configureSsl (builder , connectionDetails );
120+ configureSsl (builder , connectionDetails , sslBundles . getIfAvailable () );
113121 builder .withKeyspace (this .properties .getKeyspaceName ());
114122 builderCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (builder ));
115123 return builder ;
@@ -122,15 +130,38 @@ private void configureAuthentication(CqlSessionBuilder builder, CassandraConnect
122130 }
123131 }
124132
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 ;
133141 }
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 ));
134165 }
135166
136167 @ Bean (destroyMethod = "" )
0 commit comments