Skip to content

Commit 9c0eef2

Browse files
Improve the way it works
* Separate the generation of client certificates from server ones * Create a trust store for clients * Add comments about the different steps related to certificates * Update the default image name * Write a real readme file
1 parent befc24f commit 9c0eef2

9 files changed

+127
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*~
22
create_image.sh
33
delete_image.sh
4+
.project

Dockerfile

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
FROM rabbitmq:3.6.6
22

3-
RUN apt-get update
4-
RUN apt-get install openssl -y \
5-
&& mkdir -p /home/testca/certs \
6-
&& mkdir -p /home/testca/private \
7-
&& chmod 700 /home/testca/private \
8-
&& echo 01 > /home/testca/serial \
9-
&& touch /home/testca/index.txt
3+
RUN apt-get update \
4+
&& apt-get install openssl -y \
5+
&& mkdir -p /home/testca/certs \
6+
&& mkdir -p /home/testca/private \
7+
&& chmod 700 /home/testca/private \
8+
&& echo 01 > /home/testca/serial \
9+
&& touch /home/testca/index.txt
1010

1111
COPY rabbitmq.config /etc/rabbitmq/rabbitmq.config
1212
COPY openssl.cnf /home/testca
13-
13+
COPY prepare-server.sh generate-client-keys.sh /home/
1414

1515
RUN mkdir -p /home/server \
16-
&& mkdir -p /home/client
17-
18-
VOLUME /home/client
19-
20-
COPY ssl.sh /home
21-
22-
RUN chmod +x /home/ssl.sh
23-
RUN /bin/bash /home/ssl.sh
16+
&& mkdir -p /home/client \
17+
&& chmod +x /home/prepare-server.sh /home/generate-client-keys.sh
2418

25-
RUN /etc/init.d/rabbitmq-server restart
19+
RUN /bin/bash /home/prepare-server.sh \
20+
&& /etc/init.d/rabbitmq-server restart
2621

22+
CMD /bin/bash /home/generate-client-keys.sh && rabbitmq-server
23+
#sleep infinity

README.md

+63-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,67 @@
1-
# docker-rabbitmq-ssl
1+
# RabbitMQ with SSL Configuration in Docker
22

3-
This repository has as goal to build a rabbitmq container with SSL.
4-
## To build this image:
5-
1. Go to `tests` directory: ``cd tests``
6-
2. Run the script `build.sh`: ``./build.sh``
3+
> RabbitMQ and SSL made easy for tests.
74
8-
The generated image contains SSL certificates on client side in `/home/client`. This directory is mounted as a volume to allowing the sharing of certificates.
5+
This repository aims at building a RabbitMQ container with SSL enabled.
6+
Generation of the server certificates, as well as server configuration, are performed during
7+
the image's build. A client certificate is generated when a container is created from this image.
98

9+
It is recommended to mount a volume so that the client certificate can be reached from the
10+
host system. Client certificates are generated under the **/home/client** directory.
1011

12+
13+
## To build this image
14+
15+
```
16+
cd tests && ./build.sh
17+
```
18+
19+
The generated image contains SSL certificates for the server side.
20+
21+
22+
## To run this image
23+
24+
```
25+
mkdir -p /tmp/docker-test \
26+
&& rm -rf /tmp/docker-test/* \
27+
&& docker run -d --rm -p 12000:5671 -v /tmp/docker-test:/home/client rabbitmq-with-ssl:latest
28+
```
29+
30+
Here, we bind the port 5671 from the container on the 12000 port on the local host.
31+
We also share a local directory with the container, to retrieve the client certificate.
32+
You can verify client certificates were generated with `ls /tmp/docker-test`. This directory contains
33+
a key store and a trust store, both in the PKCS12 format.
34+
35+
36+
## To stop the container
37+
38+
`docker stop <container-id>` will stop the container.
39+
If you kept the `--rm` option, it will be deleted directly.
40+
41+
42+
## To run quick tests
43+
44+
```
45+
cd tests && ./test.sh
46+
```
47+
48+
49+
## To diagnose troubles
50+
51+
* Verify the client certificates were correctly generated: `ls -l /tmp/docker-test`
52+
* Inspect the container: `docker exec -ti <container-id> /bin/bash`
53+
* Check the logs: `docker logs <container-id>`
54+
* Verify the SSL connection works: `openssl s_client -connect 127.0.0.1:12000 -key /tmp/docker-test/key.pem`
55+
This last command will result in `Verify return code: 19 (self signed certificate in certificate chain)`, which is normal.
56+
We should specify the **-CApath**, which is inside the Docker container. This test is enough to verify SSL is enabled and
57+
the server is reachable from the host system.
58+
59+
60+
## Quick overview of the content
61+
62+
* **Dockerfile**: the file with instructions to create a Docker image.
63+
* **rabbitmq.config**: the configuration file for RabbitMQ.
64+
* **openssl.cnf**: a configuration file used during certificates creation.
65+
* **prepare-server.sh**: a script during the generation of the image and that deals with server certificates.
66+
* **generate-client-keys.sh**: a script that is run by default when a container is created from this image.
67+
It deals with the generation of client certificates.

generate-client-keys.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
#
6+
# Prepare the client's stuff.
7+
#
8+
cd /home/client
9+
10+
# Generate a private RSA key.
11+
openssl genrsa -out key.pem 2048
12+
13+
# Generate a certificate from our private key.
14+
openssl req -new -key key.pem -out req.pem -outform PEM -subj /CN=$(hostname)/O=client/ -nodes
15+
16+
# Sign the certificate with our CA.
17+
cd /home/testca
18+
openssl ca -config openssl.cnf -in /home/client/req.pem -out /home/client/cert.pem -notext -batch -extensions client_ca_extensions
19+
20+
# Create a key store that will contain our certificate.
21+
cd /home/client
22+
openssl pkcs12 -export -out key-store.p12 -in cert.pem -inkey key.pem -passout pass:roboconf
23+
24+
# Create a trust store that will contain the certificate of our CA.
25+
openssl pkcs12 -export -out trust-store.p12 -in /home/testca/cacert.pem -inkey /home/testca/private/cakey.pem -passout pass:roboconf

openssl.cnf

-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,3 @@ extendedKeyUsage = 1.3.6.1.5.5.7.3.2
5252
basicConstraints = CA:false
5353
keyUsage = keyEncipherment
5454
extendedKeyUsage = 1.3.6.1.5.5.7.3.1
55-

ssl.sh prepare-server.sh

+21-13
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@
22

33
set -eu
44

5+
#
6+
# Prepare the certificate authority (self-signed).
7+
#
58
cd /home/testca
9+
10+
# Create a self-signed certificate that will serve a certificate authority (CA).
11+
# The private key is located under "private".
612
openssl req -x509 -config openssl.cnf -newkey rsa:2048 -days 365 -out cacert.pem -outform PEM -subj /CN=MyTestCA/ -nodes
13+
14+
# Encode our certificate with DER.
715
openssl x509 -in cacert.pem -out cacert.cer -outform DER
816

9-
# On server side
17+
18+
19+
#
20+
# Prepare the server's stuff.
21+
#
1022
cd /home/server
23+
24+
# Generate a private RSA key.
1125
openssl genrsa -out key.pem 2048
26+
27+
# Generate a certificate from our private key.
1228
openssl req -new -key key.pem -out req.pem -outform PEM -subj /CN=$(hostname)/O=server/ -nodes
29+
30+
# Sign the certificate with our CA.
1331
cd /home/testca
1432
openssl ca -config openssl.cnf -in /home/server/req.pem -out /home/server/cert.pem -notext -batch -extensions server_ca_extensions
15-
cd /home/server
16-
openssl pkcs12 -export -out keycert.p12 -in cert.pem -inkey key.pem -passout pass:roboconf
1733

18-
# On client side
19-
cd /home/client
20-
openssl genrsa -out key.pem 2048
21-
openssl req -new -key key.pem -out req.pem -outform PEM -subj /CN=$(hostname)/O=client/ -nodes
22-
cd /home/testca
23-
openssl ca -config openssl.cnf -in /home/client/req.pem -out /home/client/cert.pem -notext -batch -extensions client_ca_extensions
24-
cd /home/client
34+
# Create a key store that will contain our certificate.
35+
cd /home/server
2536
openssl pkcs12 -export -out keycert.p12 -in cert.pem -inkey key.pem -passout pass:roboconf
26-
27-
# Restart rabbitmq server
28-
#service rabbitmq-server restart

rabbitmq.config

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
{certfile,"/home/server/cert.pem"},
99
{keyfile,"/home/server/key.pem"},
1010
{verify,verify_peer},
11-
{fail_if_no_peer_cert,false},
11+
{fail_if_no_peer_cert,true},
1212
{versions, ['tlsv1.2', 'tlsv1.1']}
1313
]}
14-
1514
] }
16-
1715
].

tests/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

3-
IMAGE_NAME="test-rabbitmq-ssl"
3+
IMAGE_NAME="rabbitmq-with-ssl"
44

55
docker build --no-cache=true -t ${IMAGE_NAME} ..

tests/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Launch a Docker container from rabbitmq-ssl image
4-
IMAGE_NAME=test-rabbitmq-ssl
4+
IMAGE_NAME=rabbitmq-with-ssl
55
SUCCESS=true
66
TMP_DIR=/tmp/rabbitmq-ssl/
77
mkdir -p $TMP_DIR

0 commit comments

Comments
 (0)