Skip to content

Commit 3a9d836

Browse files
committed
Merge branch '3.5.x' into 4.0.x
Closes gh-50180
2 parents 8e013b6 + e22083a commit 3a9d836

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

module/spring-boot-cassandra/src/main/java/org/springframework/boot/cassandra/autoconfigure/CassandraAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CqlSessionBuilder cassandraSessionBuilder(DriverConfigLoader driverConfigLoader,
116116
ObjectProvider<CqlSessionBuilderCustomizer> builderCustomizers) {
117117
CqlSessionBuilder builder = CqlSession.builder().withConfigLoader(driverConfigLoader);
118118
configureAuthentication(builder, connectionDetails);
119-
configureSsl(builder, connectionDetails);
119+
configureSsl(builder, connectionDetails, this.properties.getSsl().isVerifyHostname());
120120
builder.withKeyspace(this.properties.getKeyspaceName());
121121
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
122122
return builder;
@@ -130,15 +130,16 @@ private void configureAuthentication(CqlSessionBuilder builder, CassandraConnect
130130
}
131131
}
132132

133-
private void configureSsl(CqlSessionBuilder builder, CassandraConnectionDetails connectionDetails) {
133+
private void configureSsl(CqlSessionBuilder builder, CassandraConnectionDetails connectionDetails,
134+
boolean verifyHostname) {
134135
SslBundle sslBundle = connectionDetails.getSslBundle();
135136
if (sslBundle == null) {
136137
return;
137138
}
138139
SslOptions options = sslBundle.getOptions();
139140
Assert.state(options.getEnabledProtocols() == null, "SSL protocol options cannot be specified with Cassandra");
140-
builder
141-
.withSslEngineFactory(new ProgrammaticSslEngineFactory(sslBundle.createSslContext(), options.getCiphers()));
141+
builder.withSslEngineFactory(
142+
new ProgrammaticSslEngineFactory(sslBundle.createSslContext(), options.getCiphers(), verifyHostname));
142143
}
143144

144145
@Bean(destroyMethod = "")

module/spring-boot-cassandra/src/main/java/org/springframework/boot/cassandra/autoconfigure/CassandraProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ public static class Ssl {
226226
*/
227227
private @Nullable Boolean enabled;
228228

229+
/**
230+
* Whether to perform hostname verification.
231+
*/
232+
private boolean verifyHostname = true;
233+
229234
/**
230235
* SSL bundle name.
231236
*/
@@ -247,6 +252,14 @@ public void setBundle(@Nullable String bundle) {
247252
this.bundle = bundle;
248253
}
249254

255+
public boolean isVerifyHostname() {
256+
return this.verifyHostname;
257+
}
258+
259+
public void setVerifyHostname(boolean verifyHostname) {
260+
this.verifyHostname = verifyHostname;
261+
}
262+
250263
}
251264

252265
public static class Connection {

module/spring-boot-cassandra/src/test/java/org/springframework/boot/cassandra/autoconfigure/CassandraAutoConfigurationTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,23 @@ void cqlSessionBuilderWithSslEnabled() {
8686
this.contextRunner.withPropertyValues("spring.cassandra.ssl.enabled=true").run((context) -> {
8787
CqlSessionBuilder builder = context.getBean(CqlSessionBuilder.class);
8888
assertThat(builder).hasFieldOrPropertyWithValue("programmaticSslFactory", true);
89+
assertThat(builder).extracting("programmaticArgumentsBuilder.sslEngineFactory")
90+
.hasFieldOrPropertyWithValue("requireHostnameValidation", true);
8991
});
9092
}
9193

94+
@Test
95+
void cqlSessionBuilderWithSslEnabledAndVerifyHostnameDisabled() {
96+
this.contextRunner
97+
.withPropertyValues("spring.cassandra.ssl.enabled=true", "spring.cassandra.ssl.verify-hostname=false")
98+
.run((context) -> {
99+
CqlSessionBuilder builder = context.getBean(CqlSessionBuilder.class);
100+
assertThat(builder).hasFieldOrPropertyWithValue("programmaticSslFactory", true);
101+
assertThat(builder).extracting("programmaticArgumentsBuilder.sslEngineFactory")
102+
.hasFieldOrPropertyWithValue("requireHostnameValidation", false);
103+
});
104+
}
105+
92106
@Test
93107
@WithPackageResources("test.jks")
94108
void cqlSessionBuilderWithSslBundle() {
@@ -100,6 +114,24 @@ void cqlSessionBuilderWithSslBundle() {
100114
.run((context) -> {
101115
CqlSessionBuilder builder = context.getBean(CqlSessionBuilder.class);
102116
assertThat(builder).hasFieldOrPropertyWithValue("programmaticSslFactory", true);
117+
assertThat(builder).extracting("programmaticArgumentsBuilder.sslEngineFactory")
118+
.hasFieldOrPropertyWithValue("requireHostnameValidation", true);
119+
});
120+
}
121+
122+
@Test
123+
@WithPackageResources("test.jks")
124+
void cqlSessionBuilderWithSslBundleAndVerifyHostnameDisabled() {
125+
this.contextRunner
126+
.withPropertyValues("spring.cassandra.ssl.bundle=test-bundle", "spring.cassandra.ssl.verify-hostname=false",
127+
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:test.jks",
128+
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret",
129+
"spring.ssl.bundle.jks.test-bundle.key.password=password")
130+
.run((context) -> {
131+
CqlSessionBuilder builder = context.getBean(CqlSessionBuilder.class);
132+
assertThat(builder).hasFieldOrPropertyWithValue("programmaticSslFactory", true);
133+
assertThat(builder).extracting("programmaticArgumentsBuilder.sslEngineFactory")
134+
.hasFieldOrPropertyWithValue("requireHostnameValidation", false);
103135
});
104136
}
105137

0 commit comments

Comments
 (0)