aio-pika icon indicating copy to clipboard operation
aio-pika copied to clipboard

No SSL context support

Open ztqsteve opened this issue 2 years ago • 3 comments

Why aio-pika doesn't support ssl_context like aiormq? That's necessary to be used to connect to cloud AMQP services such as AWS RabbitMQ.

ztqsteve avatar May 11 '22 03:05 ztqsteve

Let's monkey-patch the class until we receive any response :)

Since the connect method accepts connection_class, we can feed a monkey-patched Connection class that passes ssl_context when calling the deeper connect. Downside is that we may have to follow any future updates to the method though. This code successfully made connection to my AWS Rabbitmq broker in a private VPC via an SSH tunnel.

class CustomConnection(aio_pika.Connection):
    async def connect(self, timeout=None) -> None:
        transport = await aio_pika.abc.UnderlayConnection.connect(
            self.url, self._on_connection_close,
            timeout=timeout,
            # Modified this line
            **dict(**self.kwargs, context=ssl_context)
        )
        await self._on_connected(transport)
        self.transport = transport


async def connect_async():
    connect = await aio_pika.connect(host='localhost', port=5671, login='guest', password='guest', ssl=True,
                                     connection_class=CustomConnection)
    channel = await connect.channel()
    await channel.declare_queue('hello')

sein-park-development avatar Jul 06 '22 12:07 sein-park-development

Actually inheritance is not a monkey-patch.

mosquito avatar Jul 06 '22 12:07 mosquito

Oh, yes, you're right :)

sein-park-development avatar Jul 06 '22 12:07 sein-park-development