Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Operations and Monitoring

Security: Users, Virtual Hosts and TLS

Secure RabbitMQ: replace the guest user, create virtual hosts, grant configure/write/read permissions with regular expressions, topic permissions, user tags and TLS listeners.

A fresh RabbitMQ install is built for convenience, not for the internet. The default user guest has full rights and works only from localhost, ports are plain text, and anything that can reach port 5672 with valid credentials can read and write queues. This lesson turns that into a safe setup: users with least-privilege permissions inside virtual hosts, encrypted connections with TLS, and a short hardening checklist.

RabbitMQ cannot run on this page, so the rabbitmqctl commands and configuration are read-only. The runnable blocks are plain Python stand-ins that evaluate permission regexes the same way the broker does conceptually, using sample users, so you can test your patterns before applying them.

Virtual hosts: separate worlds

A virtual host (vhost) is a logical namespace inside one broker: its own exchanges, queues, bindings, policies and permissions. Names in one vhost never collide with another, and a user can access only the vhosts you grant. Use a vhost per application or per environment (shop-prod, shop-staging) on a shared broker; the default vhost is /.

Text
rabbitmqctl add_vhost shop
rabbitmqctl list_vhosts
rabbitmqctl delete_vhost old-app          # destroys everything in it

A client chooses its vhost at connect time, in the connection parameters (virtual_host="shop") or the URL: amqp://orders_svc:[email protected]:5672/shop. Because / is also the default vhost, URLs for it need %2F.

Users and tags

Text
rabbitmqctl add_user orders_svc 'a-long-random-secret'
rabbitmqctl set_user_tags alice administrator
rabbitmqctl list_users
rabbitmqctl change_password orders_svc 'new-secret'
rabbitmqctl delete_user guest

Tags control management UI access, not AMQP rights: management (own vhosts), policymaker, monitoring (read-only view of everything), administrator (full). Application accounts need no tags at all. Create one user per service so you can rotate and revoke credentials separately and see who connects from where. Never share the administrator password with applications.

Delete the guest user (or restrict it) on any broker that is not a throwaway local one, and never put real credentials in source control: read them from environment variables or a secret store.

Permissions: configure, write, read

Every user has, per vhost, three regular expressions:

Permission Allows
configure Declaring, deleting and changing exchanges and queues whose name matches
write Publishing to exchanges, and binding a queue (to the exchange you write to)
read Consuming and getting from queues, and binding a queue (from the exchange you read)
Text
rabbitmqctl set_permissions -p shop orders_svc "^orders\." "^(events|orders\.)" "^orders\."
#                                           user      configure   write                 read
rabbitmqctl list_permissions -p shop

An empty pattern "" means no access; ".*" means everything. The match is unanchored unless you use ^ and $, so orders also matches old-orders-archive, a frequent mistake: always anchor patterns.

The amq.* built-in exchanges (including amq.default, the default exchange, which every exchange="" publish uses) match patterns as well; to publish with exchange="" the user needs write on the amq.default exchange, and a pattern like ^orders\. would not cover it. Many setups simply give .* write access on a service-specific vhost for simplicity and rely on the vhost boundary.

Python Stand-in: test permission patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line is the trap: an unanchored orders lets sloppy_svc configure old-orders-archive.

Topic permissions

For topic exchanges you can also restrict routing keys, per exchange and per direction:

Text
rabbitmqctl set_topic_permissions -p shop orders_svc events "^order\." "^$"
#                                                            exchange  write       read

Now orders_svc may publish only routing keys starting with order. to events, and read none through it. This is how you prevent a service from forging another's events (see Publish/Subscribe and Event Fanout).

Encrypting connections with TLS

Credentials and messages travel in clear text over port 5672. Use TLS (port 5671) so they are encrypted and the broker is authenticated. Minimum configuration in rabbitmq.conf:

Text
listeners.ssl.default = 5671
ssl_options.cacertfile = /etc/rabbitmq/tls/ca.pem
ssl_options.certfile   = /etc/rabbitmq/tls/server.pem
ssl_options.keyfile    = /etc/rabbitmq/tls/server.key
ssl_options.verify     = verify_peer
ssl_options.fail_if_no_peer_cert = false
ssl_options.versions.1 = tlsv1.3
ssl_options.versions.2 = tlsv1.2

# once all clients use TLS, close the plain port:
# listeners.tcp = none

Set fail_if_no_peer_cert = true to require client certificates (mutual TLS), and enable the rabbitmq_auth_mechanism_ssl plugin to log in with the certificate's identity instead of a password. Clients connect with an SSL context:

Python
import ssl
import pika

context = ssl.create_default_context(cafile="/etc/rabbitmq/tls/ca.pem")
params = pika.ConnectionParameters(
    host="rabbit.example.internal",
    port=5671,
    virtual_host="shop",
    credentials=pika.PlainCredentials("orders_svc", os.environ["RABBIT_PASSWORD"]),
    ssl_options=pika.SSLOptions(context, server_hostname="rabbit.example.internal"),
)

Always verify the hostname. Use short-lived certificates from an internal CA and plan certificate rotation before the expiry date surprises you (alert on days left). Inter-node traffic in a cluster has its own TLS settings (inter_node_tls), and the management UI/API and Prometheus endpoints need TLS too (management.ssl.*).

Python Stand-in: audit a configuration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hardening checklist

  • Delete guest; one least-privilege user per service; vhost per application or environment.
  • Anchor every permission regex; use topic permissions on shared exchanges.
  • TLS on AMQP, management, Prometheus and inter-node traffic; close plain ports when possible.
  • Firewall: only application subnets reach 5671/5672; only operators reach 15672; only cluster nodes reach 4369 and 25672 (see Clustering Fundamentals).
  • Keep the Erlang cookie secret and file permissions at 400.
  • Limit connections per vhost/user, set max_message_size, and audit users regularly.
  • Keep RabbitMQ and Erlang patched; subscribe to release announcements.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do the three permission regexes of a RabbitMQ user cover, in order?

    set_permissions takes configure, write and read patterns, evaluated per vhost.
  2. 2. A user has the read pattern orders. Which queue can it read?

    Patterns match anywhere in the name unless anchored with ^ and $.
  3. 3. Which tag controls management UI access rather than AMQP permissions?

    User tags such as management, monitoring, policymaker and administrator control the UI and HTTP API.
  4. 4. Which port is used for AMQP over TLS?

    5671 is AMQPS. 5672 is plain AMQP, 15671 is the HTTPS management UI and 5552 the stream protocol.

Summary

  • Delete guest, create one user per service and use vhosts as boundaries.
  • Permissions are three anchored regexes (configure, write, read) per vhost; topic permissions restrict routing keys.
  • Enable TLS (5671), verify hostnames, consider client certificates and secure the management and inter-node channels.
  • Combine with firewalls, connection limits and patching.

Next: Project: Order Processing Pipeline