File: tls_mutual_authentication.rst

package info (click to toggle)
python-pika 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,064 kB
  • sloc: python: 20,886; makefile: 136
file content (93 lines) | stat: -rw-r--r-- 3,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
TLS parameters example
======================

This example demonstrates a TLS session with RabbitMQ using mutual authentication (server and client authentication).

See `the RabbitMQ TLS/SSL documentation <https://www.rabbitmq.com/ssl.html>`_ for certificate generation and RabbitMQ TLS configuration. Please note that the `RabbitMQ TLS (x509 certificate) authentication mechanism <https://github.com/rabbitmq/rabbitmq-auth-mechanism-ssl>`_ must be enabled for these examples to work.

tls_example.py::

    import logging
    import pika
    import ssl

    logging.basicConfig(level=logging.INFO)

    context = ssl.create_default_context(
        cafile="PIKA_DIR/testdata/certs/ca_certificate.pem")
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_cert_chain("PIKA_DIR/testdata/certs/client_certificate.pem",
                            "PIKA_DIR/testdata/certs/client_key.pem")
    ssl_options = pika.SSLOptions(context, "localhost")
    conn_params = pika.ConnectionParameters(port=5671,
                                            ssl_options=ssl_options)
    
    with pika.BlockingConnection(conn_params) as conn:
        ch = conn.channel()
        ch.queue_declare("foobar")
        ch.basic_publish("", "foobar", "Hello, world!")
        print(ch.basic_get("foobar"))

rabbitmq.config::

    # Enable AMQPS
    listeners.ssl.default = 5671
    ssl_options.cacertfile = PIKA_DIR/testdata/certs/ca_certificate.pem
    ssl_options.certfile = PIKA_DIR/testdata/certs/server_certificate.pem
    ssl_options.keyfile = PIKA_DIR/testdata/certs/server_key.pem
    ssl_options.verify = verify_peer
    ssl_options.fail_if_no_peer_cert = true

    # Enable HTTPS
    management.listener.port = 15671
    management.listener.ssl = true
    management.listener.ssl_opts.cacertfile = PIKA_DIR/testdata/certs/ca_certificate.pem
    management.listener.ssl_opts.certfile = PIKA_DIR/testdata/certs/server_certificate.pem
    management.listener.ssl_opts.keyfile = PIKA_DIR/testdata/certs/server_key.pem


To perform mutual authentication with a Twisted connection::

    from pika import ConnectionParameters
    from pika.adapters import twisted_connection
    from pika.credentials import ExternalCredentials

    from twisted.internet import defer, protocol, ssl, reactor

    @defer.inlineCallbacks
    def publish(connection):
        channel = yield connection.channel()
        yield channel.basic_publish(
            exchange='amq.topic',
            routing_key='hello.world',
            body='Hello World!',
        )
        print("published")

    def connection_ready(conn):
        conn.ready.addCallback(lambda _ :conn)
        return conn.ready

    # Load the CA certificate to validate the server's identity
    with open("PIKA_DIR/testdata/certs/ca_certificate.pem") as fd:
        ca_cert = ssl.Certificate.loadPEM(fd.read())

    # Load the client certificate and key to authenticate with the server
    with open("PIKA_DIR/testdata/certs/client_key.pem") as fd:
        client_key = fd.read()
    with open("PIKA_DIR/testdata/certs/client_certificate.pem") as fd:
        client_cert = fd.read()
    client_keypair = ssl.PrivateCertificate.loadPEM(client_key + client_cert)

    context_factory = ssl.optionsForClientTLS(
        "localhost",
        trustRoot=ca_cert,
        clientCertificate=client_keypair,
    )
    params = ConnectionParameters(credentials=ExternalCredentials())
    cc = protocol.ClientCreator(
        reactor, twisted_connection.TwistedProtocolConnection, params)
    deferred = cc.connectSSL("localhost", 5671, context_factory)
    deferred.addCallback(connection_ready)
    deferred.addCallback(publish)
    reactor.run()