Skip to content

Commit d4107a8

Browse files
Add smoke tests to verify Cassandra SSL connections
See gh-25602
1 parent 4f9616c commit d4107a8

File tree

12 files changed

+1556
-6
lines changed

12 files changed

+1556
-6
lines changed

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public CqlSessionBuilder cassandraSessionBuilder(DriverConfigLoader driverConfig
117117
ObjectProvider<CqlSessionBuilderCustomizer> builderCustomizers, ObjectProvider<SslBundles> sslBundles) {
118118
CqlSessionBuilder builder = CqlSession.builder().withConfigLoader(driverConfigLoader);
119119
configureAuthentication(builder, connectionDetails);
120-
configureSsl(builder, connectionDetails, sslBundles.getIfAvailable());
120+
configureSsl(builder, sslBundles.getIfAvailable());
121121
builder.withKeyspace(this.properties.getKeyspaceName());
122122
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
123123
return builder;
@@ -130,11 +130,7 @@ private void configureAuthentication(CqlSessionBuilder builder, CassandraConnect
130130
}
131131
}
132132

133-
private void configureSsl(CqlSessionBuilder builder, CassandraConnectionDetails connectionDetails,
134-
SslBundles sslBundles) {
135-
if (!(connectionDetails instanceof PropertiesCassandraConnectionDetails)) {
136-
return;
137-
}
133+
private void configureSsl(CqlSessionBuilder builder, SslBundles sslBundles) {
138134
Ssl properties = this.properties.getSsl();
139135
if (properties == null || !properties.isEnabled()) {
140136
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
}
5+
6+
description = "Spring Boot Data Cassandra smoke test"
7+
8+
dependencies {
9+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-cassandra"))
10+
11+
testImplementation(project(":spring-boot-project:spring-boot-test"))
12+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
13+
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
14+
testImplementation(project(":spring-boot-project:spring-boot-testcontainers"))
15+
testImplementation("org.junit.jupiter:junit-jupiter")
16+
testImplementation("org.junit.platform:junit-platform-engine")
17+
testImplementation("org.junit.platform:junit-platform-launcher")
18+
testImplementation("org.testcontainers:junit-jupiter")
19+
testImplementation("org.testcontainers:testcontainers")
20+
testImplementation("org.testcontainers:cassandra")
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 smoketest.data.cassandra;
18+
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
@SpringBootApplication
22+
public class SampleCassandraApplication {
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 smoketest.data.cassandra;
18+
19+
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
20+
import org.springframework.data.cassandra.core.mapping.Table;
21+
22+
@Table
23+
public class SampleEntity {
24+
25+
@PrimaryKey
26+
private String id;
27+
28+
private String description;
29+
30+
public String getId() {
31+
return this.id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public String getDescription() {
39+
return this.description;
40+
}
41+
42+
public void setDescription(String description) {
43+
this.description = description;
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 smoketest.data.cassandra;
18+
19+
import org.springframework.data.cassandra.repository.CassandraRepository;
20+
21+
interface SampleRepository extends CassandraRepository<SampleEntity, String> {
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 smoketest.data.cassandra;
18+
19+
import org.springframework.data.cassandra.core.CassandraTemplate;
20+
import org.springframework.stereotype.Service;
21+
22+
@Service
23+
public class SampleService {
24+
25+
private final CassandraTemplate cassandraTemplate;
26+
27+
public SampleService(CassandraTemplate cassandraTemplate) {
28+
this.cassandraTemplate = cassandraTemplate;
29+
}
30+
31+
public boolean hasRecord(SampleEntity entity) {
32+
return this.cassandraTemplate.exists(entity.getId(), SampleEntity.class);
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 smoketest.data.cassandra;
18+
19+
import java.util.UUID;
20+
21+
import com.datastax.oss.driver.api.core.CqlSession;
22+
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
23+
import org.junit.jupiter.api.Test;
24+
import org.testcontainers.junit.jupiter.Container;
25+
import org.testcontainers.junit.jupiter.Testcontainers;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.TestConfiguration;
30+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.data.cassandra.core.CassandraTemplate;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Smoke tests for Cassandra with SSL.
38+
*
39+
* @author Scott Frederick
40+
*/
41+
@Testcontainers(disabledWithoutDocker = true)
42+
@SpringBootTest(properties = { "spring.cassandra.schema-action=create-if-not-exists",
43+
"spring.cassandra.connection.connect-timeout=60s", "spring.cassandra.connection.init-query-timeout=60s",
44+
"spring.cassandra.request.timeout=60s", "spring.cassandra.ssl.bundle=client",
45+
"spring.ssl.bundle.jks.client.keystore.location=classpath:ssl/test-client.p12",
46+
"spring.ssl.bundle.jks.client.keystore.password=password",
47+
"spring.ssl.bundle.jks.client.truststore.location=classpath:ssl/test-ca.p12",
48+
"spring.ssl.bundle.jks.client.truststore.password=password" })
49+
class SampleCassandraApplicationSslTests {
50+
51+
@Container
52+
@ServiceConnection
53+
static final SecureCassandraContainer secureCassandra = new SecureCassandraContainer();
54+
55+
@Autowired
56+
private CassandraTemplate cassandraTemplate;
57+
58+
@Autowired
59+
private SampleRepository repository;
60+
61+
@Test
62+
void testRepository() {
63+
SampleEntity entity = new SampleEntity();
64+
entity.setDescription("Look, new @DataCassandraTest!");
65+
String id = UUID.randomUUID().toString();
66+
entity.setId(id);
67+
SampleEntity savedEntity = this.repository.save(entity);
68+
SampleEntity getEntity = this.cassandraTemplate.selectOneById(id, SampleEntity.class);
69+
assertThat(getEntity).isNotNull();
70+
assertThat(getEntity.getId()).isNotNull();
71+
assertThat(getEntity.getId()).isEqualTo(savedEntity.getId());
72+
this.repository.deleteAll();
73+
}
74+
75+
@TestConfiguration(proxyBeanMethods = false)
76+
static class KeyspaceTestConfiguration {
77+
78+
@Bean
79+
CqlSession cqlSession(CqlSessionBuilder cqlSessionBuilder) {
80+
try (CqlSession session = cqlSessionBuilder.build()) {
81+
session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test"
82+
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
83+
}
84+
return cqlSessionBuilder.withKeyspace("boot_test").build();
85+
}
86+
87+
}
88+
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 smoketest.data.cassandra;
18+
19+
import org.testcontainers.utility.MountableFile;
20+
21+
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
22+
23+
/**
24+
* A {@link CassandraContainer} for Cassandra with SSL configuration.
25+
*
26+
* @author Scott Frederick
27+
*/
28+
class SecureCassandraContainer extends CassandraContainer {
29+
30+
SecureCassandraContainer() {
31+
withCopyFileToContainer(MountableFile.forClasspathResource("/ssl/cassandra.yaml"),
32+
"/etc/cassandra/cassandra.yaml");
33+
withCopyFileToContainer(MountableFile.forClasspathResource("/ssl/test-server.p12"),
34+
"/etc/cassandra/server.p12");
35+
withCopyFileToContainer(MountableFile.forClasspathResource("/ssl/test-ca.p12"),
36+
"/etc/cassandra/truststore.p12");
37+
}
38+
39+
}

0 commit comments

Comments
 (0)