TLS Configuration
TLS Configuration
Section titled “TLS Configuration”Secure RabbitMQ connections with TLS encryption and optional client certificates.
Overview
Section titled “Overview”Certificate Requirements
Section titled “Certificate Requirements”| Certificate | Purpose |
|---|---|
| CA Certificate | Root of trust |
| Server Certificate | RabbitMQ server identity |
| Client Certificate | Optional client authentication |
Generate Certificates
Section titled “Generate Certificates”Create CA
Section titled “Create CA”# Create CA directorymkdir -p /etc/rabbitmq/sslcd /etc/rabbitmq/ssl
# Generate CA keyopenssl genrsa -out ca.key 4096
# Generate CA certificateopenssl req -x509 -new -nodes \ -key ca.key \ -sha256 -days 3650 \ -out ca.crt \ -subj "/CN=Mantis RabbitMQ CA/O=Mantis"Create Server Certificate
Section titled “Create Server Certificate”# Generate server keyopenssl genrsa -out server.key 2048
# Create certificate signing requestopenssl req -new \ -key server.key \ -out server.csr \ -subj "/CN=rabbitmq.example.com/O=Mantis"
# Create SAN extension filecat > server_san.ext << EOFsubjectAltName = @alt_names[alt_names]DNS.1 = rabbitmq.example.comDNS.2 = localhostIP.1 = 127.0.0.1EOF
# Sign with CAopenssl x509 -req \ -in server.csr \ -CA ca.crt \ -CAkey ca.key \ -CAcreateserial \ -out server.crt \ -days 365 \ -sha256 \ -extfile server_san.extCreate Client Certificate (Optional)
Section titled “Create Client Certificate (Optional)”# Generate client keyopenssl genrsa -out client.key 2048
# Create CSRopenssl req -new \ -key client.key \ -out client.csr \ -subj "/CN=mantis-client/O=Mantis"
# Sign with CAopenssl x509 -req \ -in client.csr \ -CA ca.crt \ -CAkey ca.key \ -CAcreateserial \ -out client.crt \ -days 365 \ -sha256Set Permissions
Section titled “Set Permissions”# Secure the keyschmod 600 /etc/rabbitmq/ssl/*.keychmod 644 /etc/rabbitmq/ssl/*.crtchown -R rabbitmq:rabbitmq /etc/rabbitmq/sslRabbitMQ TLS Configuration
Section titled “RabbitMQ TLS Configuration”Basic TLS
Section titled “Basic TLS”# Disable plain TCP listener (recommended for production)# listeners.tcp = none
# Enable TLS listenerlisteners.ssl.default = 5671
# Certificate configurationssl_options.cacertfile = /etc/rabbitmq/ssl/ca.crtssl_options.certfile = /etc/rabbitmq/ssl/server.crtssl_options.keyfile = /etc/rabbitmq/ssl/server.key
# TLS version (require TLS 1.2+)ssl_options.versions.1 = tlsv1.2ssl_options.versions.2 = tlsv1.3
# Cipher suitesssl_options.ciphers.1 = TLS_AES_256_GCM_SHA384ssl_options.ciphers.2 = TLS_AES_128_GCM_SHA256ssl_options.ciphers.3 = ECDHE-RSA-AES256-GCM-SHA384ssl_options.ciphers.4 = ECDHE-RSA-AES128-GCM-SHA256
# Honor server cipher preferencessl_options.honor_cipher_order = truessl_options.honor_ecc_order = trueClient Certificate Verification
Section titled “Client Certificate Verification”# Require client certificatesssl_options.verify = verify_peerssl_options.fail_if_no_peer_cert = true
# Depth of certificate chain verificationssl_options.depth = 2Both TCP and TLS
Section titled “Both TCP and TLS”For migration or internal/external separation:
# TCP on localhost onlylisteners.tcp.local = 127.0.0.1:5672
# TLS on all interfaceslisteners.ssl.default = 5671Management UI TLS
Section titled “Management UI TLS”Enable HTTPS
Section titled “Enable HTTPS”# Disable HTTPmanagement.tcp.port = 0
# Enable HTTPSmanagement.ssl.port = 15671management.ssl.cacertfile = /etc/rabbitmq/ssl/ca.crtmanagement.ssl.certfile = /etc/rabbitmq/ssl/server.crtmanagement.ssl.keyfile = /etc/rabbitmq/ssl/server.keyThorax Configuration
Section titled “Thorax Configuration”TLS Connection
Section titled “TLS Connection”[queue]enabled = truehost = "rabbitmq.example.com"port = 5671username = "mantis"password = "password"ca_cert_path = "/etc/mantis/certs/rabbitmq-ca.crt"prefetch_count = 10With Client Certificate
Section titled “With Client Certificate”[queue]enabled = truehost = "rabbitmq.example.com"port = 5671username = "mantis"password = "password"ca_cert_path = "/etc/mantis/certs/ca.crt"client_cert_path = "/etc/mantis/certs/client.crt"client_key_path = "/etc/mantis/certs/client.key"Environment Variables
Section titled “Environment Variables”THORAX__QUEUE__HOST="rabbitmq.example.com"THORAX__QUEUE__PORT=5671THORAX__QUEUE__USERNAME="mantis"THORAX__QUEUE__CA_CERT_PATH="/etc/mantis/certs/ca.crt"Mandible Configuration
Section titled “Mandible Configuration”Mandible publishes commands to RabbitMQ and uses the same [queue] settings block with the MANDIBLE__ env var prefix.
TLS Connection
Section titled “TLS Connection”[queue]enabled = truehost = "rabbitmq.example.com"port = 5671username = "mantis"password = "password"ca_cert_path = "/etc/mantis/certs/rabbitmq-ca.crt"With Client Certificate
Section titled “With Client Certificate”[queue]enabled = truehost = "rabbitmq.example.com"port = 5671username = "mantis"password = "password"ca_cert_path = "/etc/mantis/certs/ca.crt"client_cert_path = "/etc/mantis/certs/client.crt"client_key_path = "/etc/mantis/certs/client.key"Environment Variables
Section titled “Environment Variables”MANDIBLE__QUEUE__HOST="rabbitmq.example.com"MANDIBLE__QUEUE__PORT=5671MANDIBLE__QUEUE__USERNAME="mantis"MANDIBLE__QUEUE__CA_CERT_PATH="/etc/mantis/certs/ca.crt"Docker Configuration
Section titled “Docker Configuration”Docker Compose
Section titled “Docker Compose”services: rabbitmq: image: rabbitmq:3.13-management-alpine hostname: rabbitmq volumes: - ./ssl/ca.crt:/etc/rabbitmq/ssl/ca.crt:ro - ./ssl/server.crt:/etc/rabbitmq/ssl/server.crt:ro - ./ssl/server.key:/etc/rabbitmq/ssl/server.key:ro - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro ports: - '5671:5671' # AMQPS - '15671:15671' # Management HTTPSKubernetes Secret
Section titled “Kubernetes Secret”apiVersion: v1kind: Secretmetadata: name: rabbitmq-tlstype: Opaquedata: ca.crt: <base64-encoded-ca> server.crt: <base64-encoded-cert> server.key: <base64-encoded-key>Verification
Section titled “Verification”Test TLS Connection
Section titled “Test TLS Connection”# Using OpenSSLopenssl s_client -connect rabbitmq.example.com:5671 \ -CAfile /etc/mantis/certs/ca.crt \ -showcerts
# Check certificateopenssl s_client -connect rabbitmq.example.com:5671 \ -CAfile /etc/mantis/certs/ca.crt \ </dev/null 2>/dev/null | openssl x509 -text -nooutTest AMQPS Connection
Section titled “Test AMQPS Connection”# Using amqp-toolsamqp-declare-queue \ --url="amqps://mantis:password@rabbitmq.example.com:5671" \ --cacert=/etc/mantis/certs/ca.crt \ --queue=test
# Using Pythonpython3 -c "import sslimport pika
context = ssl.create_default_context(cafile='/etc/mantis/certs/ca.crt')params = pika.ConnectionParameters( host='rabbitmq.example.com', port=5671, credentials=pika.PlainCredentials('mantis', 'password'), ssl_options=pika.SSLOptions(context))connection = pika.BlockingConnection(params)print('Connected successfully')connection.close()"Check RabbitMQ TLS Status
Section titled “Check RabbitMQ TLS Status”# List TLS listenerssudo rabbitmqctl eval 'rabbit_networking:active_listeners().'
# Check connection TLS infosudo rabbitmqctl list_connections name ssl ssl_protocol ssl_cipherCertificate Renewal
Section titled “Certificate Renewal”Automated Renewal
Section titled “Automated Renewal”#!/bin/bashSSL_DIR="/etc/rabbitmq/ssl"DAYS_BEFORE_EXPIRY=30
# Check if certificate needs renewalEXPIRY=$(openssl x509 -enddate -noout -in $SSL_DIR/server.crt | cut -d= -f2)EXPIRY_EPOCH=$(date -d "$EXPIRY" +%s)NOW_EPOCH=$(date +%s)DAYS_LEFT=$(( ($EXPIRY_EPOCH - $NOW_EPOCH) / 86400 ))
if [ $DAYS_LEFT -lt $DAYS_BEFORE_EXPIRY ]; then echo "Certificate expires in $DAYS_LEFT days, renewing..."
# Generate new certificate openssl req -new -key $SSL_DIR/server.key -out $SSL_DIR/server.csr \ -subj "/CN=rabbitmq.example.com/O=Mantis"
openssl x509 -req -in $SSL_DIR/server.csr \ -CA $SSL_DIR/ca.crt -CAkey $SSL_DIR/ca.key \ -CAcreateserial -out $SSL_DIR/server.crt \ -days 365 -sha256 -extfile $SSL_DIR/server_san.ext
# Reload RabbitMQ sudo systemctl reload rabbitmq-server
echo "Certificate renewed successfully"else echo "Certificate valid for $DAYS_LEFT more days"fiSchedule Renewal
Section titled “Schedule Renewal”# Weekly check0 0 * * 0 /opt/mantis/scripts/renew-rabbitmq-certs.sh >> /var/log/rabbitmq/cert-renewal.log 2>&1Troubleshooting
Section titled “Troubleshooting”TLS Handshake Failed
Section titled “TLS Handshake Failed”Error: TLS handshake failedCauses:
- Certificate hostname mismatch
- Expired certificate
- CA certificate not trusted
Solutions:
- Verify SAN includes hostname:
openssl x509 -in server.crt -text | grep DNS - Check expiry:
openssl x509 -in server.crt -enddate -noout - Verify CA is correct
Certificate Verification Failed
Section titled “Certificate Verification Failed”Error: certificate verify failedSolutions:
- Ensure CA certificate is correct
- Check certificate chain is complete
- Verify
verify_peersettings match on both sides
Connection Reset
Section titled “Connection Reset”Error: connection reset by peerCauses:
- TLS version mismatch
- Cipher suite incompatibility
Solutions:
- Check TLS versions match
- Verify cipher suites are compatible
Next Steps
Section titled “Next Steps”- Queue Topology - Queue configuration
- Monitoring - RabbitMQ monitoring
- RabbitMQ Setup - Initial setup
