Skip to content

Commit 8ff1e63

Browse files
committed
Document SSL support for Docker Compose and Testcontainers
Closes gh-41137
1 parent dae891f commit 8ff1e63

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed

spring-boot-project/spring-boot-docs/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ dependencies {
7878
implementation(project(path: ":spring-boot-project:spring-boot-testcontainers"))
7979
implementation(project(path: ":spring-boot-project:spring-boot-devtools"))
8080
implementation("ch.qos.logback:logback-classic")
81+
implementation("com.redis:testcontainers-redis")
8182
implementation("com.zaxxer:HikariCP")
8283
implementation("io.micrometer:micrometer-jakarta9")
8384
implementation("io.micrometer:micrometer-tracing")
@@ -170,6 +171,7 @@ dependencies {
170171
implementation("org.testcontainers:junit-jupiter")
171172
implementation("org.testcontainers:neo4j")
172173
implementation("org.testcontainers:mongodb")
174+
implementation("org.testcontainers:elasticsearch")
173175
implementation("org.junit.jupiter:junit-jupiter")
174176
implementation("org.yaml:snakeyaml")
175177

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc

+86
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,92 @@ The following service connections are currently supported:
137137

138138

139139

140+
[[features.dev-services.docker-compose.ssl]]
141+
=== SSL support
142+
143+
Some images come with SSL enabled out of the box, or maybe you want to enable SSL for the container to mirror your production setup.
144+
Spring Boot supports SSL configuration for supported service connections.
145+
Please note that you still have to enable SSL on the service which is running inside the container yourself, this feature only configures SSL on the client side in your application.
146+
147+
SSL is supported for the following service connections:
148+
149+
* Cassandra
150+
* Couchbase
151+
* Elasticsearch
152+
* Kafka
153+
* MongoDB
154+
* RabbitMQ
155+
* Redis
156+
157+
To enable SSL support for a service, you can use https://docs.docker.com/reference/compose-file/services/#labels[service labels].
158+
159+
For JKS based keystores and truststores, you can use the following container labels:
160+
161+
* `org.springframework.boot.sslbundle.jks.key.alias`
162+
* `org.springframework.boot.sslbundle.jks.key.password`
163+
* `org.springframework.boot.sslbundle.jks.options.ciphers`
164+
* `org.springframework.boot.sslbundle.jks.options.enabled-protocols`
165+
* `org.springframework.boot.sslbundle.jks.protocol`
166+
167+
* `org.springframework.boot.sslbundle.jks.keystore.type`
168+
* `org.springframework.boot.sslbundle.jks.keystore.provider`
169+
* `org.springframework.boot.sslbundle.jks.keystore.location`
170+
* `org.springframework.boot.sslbundle.jks.keystore.password`
171+
172+
* `org.springframework.boot.sslbundle.jks.truststore.type`
173+
* `org.springframework.boot.sslbundle.jks.truststore.provider`
174+
* `org.springframework.boot.sslbundle.jks.truststore.location`
175+
* `org.springframework.boot.sslbundle.jks.truststore.password`
176+
177+
These labels mirror the properties available for xref:reference:features/ssl.adoc#features.ssl.jks[SSL bundles].
178+
179+
For PEM based keystores and truststores, you can use the following container labels:
180+
181+
* `org.springframework.boot.sslbundle.pem.key.alias`
182+
* `org.springframework.boot.sslbundle.pem.key.password`
183+
* `org.springframework.boot.sslbundle.pem.options.ciphers`
184+
* `org.springframework.boot.sslbundle.pem.options.enabled-protocols`
185+
* `org.springframework.boot.sslbundle.pem.protocol`
186+
187+
* `org.springframework.boot.sslbundle.pem.keystore.type`
188+
* `org.springframework.boot.sslbundle.pem.keystore.certificate`
189+
* `org.springframework.boot.sslbundle.pem.keystore.private-key`
190+
* `org.springframework.boot.sslbundle.pem.keystore.private-key-password`
191+
192+
* `org.springframework.boot.sslbundle.pem.truststore.type`
193+
* `org.springframework.boot.sslbundle.pem.truststore.certificate`
194+
* `org.springframework.boot.sslbundle.pem.truststore.private-key`
195+
* `org.springframework.boot.sslbundle.pem.truststore.private-key-password`
196+
197+
These labels mirror the properties available for xref:reference:features/ssl.adoc#features.ssl.pem[SSL bundles].
198+
199+
The following example enables SSL for a redis container:
200+
201+
[source,yaml,]
202+
----
203+
services:
204+
redis:
205+
image: 'redis:latest'
206+
ports:
207+
- '6379'
208+
secrets:
209+
- ssl-ca
210+
- ssl-key
211+
- ssl-cert
212+
command: 'redis-server --tls-port 6379 --port 0 --tls-cert-file /run/secrets/ssl-cert --tls-key-file /run/secrets/ssl-key --tls-ca-cert-file /run/secrets/ssl-ca'
213+
labels:
214+
- 'org.springframework.boot.sslbundle.pem.keystore.certificate=client.crt'
215+
- 'org.springframework.boot.sslbundle.pem.keystore.private-key=client.key'
216+
- 'org.springframework.boot.sslbundle.pem.truststore.certificate=ca.crt'
217+
secrets:
218+
ssl-ca:
219+
file: 'ca.crt'
220+
ssl-key:
221+
file: 'server.key'
222+
ssl-cert:
223+
file: 'server.crt'
224+
----
225+
140226
[[features.dev-services.docker-compose.custom-images]]
141227
=== Custom Images
142228

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc

+30
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ If you are using the Docker image `registry.mycompany.com/mirror/myredis`, you'd
122122

123123

124124

125+
[[testing.testcontainers.service-connections.ssl]]
126+
=== SSL with Service Connections
127+
128+
You can use the javadoc:org.springframework.boot.testcontainers.service.connection.Ssl[format=annotation], javadoc:org.springframework.boot.testcontainers.service.connection.JksKeyStore[format=annotation], javadoc:org.springframework.boot.testcontainers.service.connection.JksTrustStore[format=annotation], javadoc:org.springframework.boot.testcontainers.service.connection.PemKeyStore[format=annotation] and javadoc:org.springframework.boot.testcontainers.service.connection.PemTrustStore[format=annotation] annotations on a supported container to enable SSL support for that service connection.
129+
Please note that you still have to enable SSL on the service which is running inside the Testcontainer yourself, the annotations only configure SSL on the client side in your application.
130+
131+
include-code::MyRedisWithSslIntegrationTests[]
132+
133+
The above code uses the javadoc:org.springframework.boot.testcontainers.service.connection.PemKeyStore[format=annotation] annotation to load the client certificate and key into the keystore and the and javadoc:org.springframework.boot.testcontainers.service.connection.PemTrustStore[format=annotation] annotation to load the CA certificate into the truststore.
134+
This will authenticate the client against the server, and the CA certificate in the truststore makes sure that the server certificate is valid and trusted.
135+
136+
The `SecureRedisContainer` in this example is a custom subclass of `RedisContainer` which copies certificates to the correct places and invokes `redis-server` with commandline parameters enabling SSL.
137+
138+
The SSL annotations are supported for the following service connections:
139+
140+
* Cassandra
141+
* Couchbase
142+
* Elasticsearch
143+
* Kafka
144+
* MongoDB
145+
* RabbitMQ
146+
* Redis
147+
148+
The `ElasticsearchContainer` additionally supports automatic detection of server side SSL.
149+
To use this feature, annotate the container with javadoc:org.springframework.boot.testcontainers.service.connection.Ssl[format=annotation], as seen in the following example, and Spring Boot takes care of the client side SSL configuration for you:
150+
151+
include-code::MyElasticsearchWithSslIntegrationTests[]
152+
153+
154+
125155
[[testing.testcontainers.dynamic-properties]]
126156
== Dynamic Properties
127157

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2025 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 org.springframework.boot.docs.testing.testcontainers.serviceconnections.ssl;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.autoconfigure.data.elasticsearch.DataElasticsearchTest;
26+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
27+
import org.springframework.boot.testcontainers.service.connection.Ssl;
28+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
29+
30+
@Testcontainers
31+
@DataElasticsearchTest
32+
class MyElasticsearchWithSslIntegrationTests {
33+
34+
@Ssl
35+
@Container
36+
@ServiceConnection
37+
static ElasticsearchContainer elasticsearch = new ElasticsearchContainer(
38+
"docker.elastic.co/elasticsearch/elasticsearch:8.17.2");
39+
40+
@Autowired
41+
private ElasticsearchTemplate elasticsearchTemplate;
42+
43+
@Test
44+
void testElasticsearch() {
45+
// ...
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2025 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 org.springframework.boot.docs.testing.testcontainers.serviceconnections.ssl;
18+
19+
import com.redis.testcontainers.RedisContainer;
20+
import org.junit.jupiter.api.Test;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.boot.testcontainers.service.connection.PemKeyStore;
27+
import org.springframework.boot.testcontainers.service.connection.PemTrustStore;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.data.redis.core.RedisOperations;
30+
31+
@Testcontainers
32+
@SpringBootTest
33+
class MyRedisWithSslIntegrationTests {
34+
35+
@Container
36+
@ServiceConnection
37+
@PemKeyStore(certificate = "classpath:client.crt", privateKey = "classpath:client.key")
38+
@PemTrustStore("classpath:ca.crt")
39+
static RedisContainer redis = new SecureRedisContainer("redis:latest");
40+
41+
@Autowired
42+
private RedisOperations<Object, Object> operations;
43+
44+
@Test
45+
void testRedis() {
46+
// ...
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2025 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 org.springframework.boot.docs.testing.testcontainers.serviceconnections.ssl;
18+
19+
import com.redis.testcontainers.RedisContainer;
20+
21+
class SecureRedisContainer extends RedisContainer {
22+
23+
SecureRedisContainer(String dockerImageName) {
24+
super(dockerImageName);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)