Home > Mobile >  ssl.SSLCertVerificationError When connecting to heroku redis with django-channels
ssl.SSLCertVerificationError When connecting to heroku redis with django-channels

Time:12-31

I'm making chat. I have a need to use WebSocket and deploy my app to Heroku. I use free heroku-redis and django-channels In my settings py:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [os.environ.get('REDIS_TLS_URL')],
        },
    },
}

I tried to use REDIS_URL but I was getting the same error

Then I switched to REDIS_TLS_URL. Both errors were raised from consumers.py at "await self.channel_layer.group_add()"

class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        print('==================================')
        print('==================================')
        print(self.room_name)
        print(self.room_group_name)
        print('==================================')
        print('==================================')

        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()
.........

Here is logs

2021-12-25T08:59:56.469939 00:00 app[web.1]: 2021-12-25 08:59:56,469 DEBUG    Upgraded connection ['10.1.3.218', 11273] to WebSocket
2021-12-25T08:59:56.859805 00:00 app[web.1]: 2021-12-25 08:59:56,859 INFO     ==================================
2021-12-25T08:59:56.859921 00:00 app[web.1]: 2021-12-25 08:59:56,859 INFO     ==================================
2021-12-25T08:59:56.860015 00:00 app[web.1]: 2021-12-25 08:59:56,859 INFO     FIRST
2021-12-25T08:59:56.860107 00:00 app[web.1]: 2021-12-25 08:59:56,860 INFO     chat_FIRST
2021-12-25T08:59:56.860196 00:00 app[web.1]: 2021-12-25 08:59:56,860 INFO     ==================================
2021-12-25T08:59:56.860287 00:00 app[web.1]: 2021-12-25 08:59:56,860 INFO     ==================================
2021-12-25T08:59:56.860674 00:00 app[web.1]: 2021-12-25 08:59:56,860 DEBUG    Creating tcp connection to ('ec2-34-241-115-34.eu-west-1.compute.amazonaws.com', 29080)
2021-12-25T08:59:56.861684 00:00 app[web.1]: 2021-12-25 08:59:56,861 DEBUG    Creating tcp connection to ('ec2-34-241-115-34.eu-west-1.compute.amazonaws.com', 29080)
2021-12-25T08:59:56.872570 00:00 app[web.1]: 2021-12-25 08:59:56,872 DEBUG    Closed 0 connection(s)
2021-12-25T08:59:57.708867 00:00 app[web.1]: 2021-12-25 08:59:57,706 ERROR    Exception inside application: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)
2021-12-25T08:59:57.708874 00:00 app[web.1]: Traceback (most recent call last):
2021-12-25T08:59:57.708875 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/routing.py", line 71, in __call__
2021-12-25T08:59:57.708876 00:00 app[web.1]: return await application(scope, receive, send)
2021-12-25T08:59:57.708877 00:00 app[web.1]: File "/usr/src/app/./config/middlewares.py", line 58, in __call__
2021-12-25T08:59:57.708878 00:00 app[web.1]: return await super().__call__(scope, receive, send)
2021-12-25T08:59:57.708878 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/middleware.py", line 26, in __call__
2021-12-25T08:59:57.708878 00:00 app[web.1]: return await self.inner(scope, receive, send)
2021-12-25T08:59:57.708878 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/routing.py", line 150, in __call__
2021-12-25T08:59:57.708879 00:00 app[web.1]: return await application(
2021-12-25T08:59:57.708879 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/consumer.py", line 94, in app
2021-12-25T08:59:57.708880 00:00 app[web.1]: return await consumer(scope, receive, send)
2021-12-25T08:59:57.708880 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/consumer.py", line 58, in __call__
2021-12-25T08:59:57.708881 00:00 app[web.1]: await await_many_dispatch(
2021-12-25T08:59:57.708882 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/utils.py", line 51, in await_many_dispatch
2021-12-25T08:59:57.708882 00:00 app[web.1]: await dispatch(result)
2021-12-25T08:59:57.708882 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/consumer.py", line 73, in dispatch
2021-12-25T08:59:57.708883 00:00 app[web.1]: await handler(message)
2021-12-25T08:59:57.708883 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels/generic/websocket.py", line 173, in websocket_connect
2021-12-25T08:59:57.708883 00:00 app[web.1]: await self.connect()
2021-12-25T08:59:57.708883 00:00 app[web.1]: File "/usr/src/app/./chat/consumers.py", line 49, in connect
2021-12-25T08:59:57.708884 00:00 app[web.1]: await self.channel_layer.group_add(
2021-12-25T08:59:57.708884 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels_redis/core.py", line 646, in group_add
2021-12-25T08:59:57.708885 00:00 app[web.1]: async with self.connection(self.consistent_hash(group)) as connection:
2021-12-25T08:59:57.708885 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels_redis/core.py", line 902, in __aenter__
2021-12-25T08:59:57.708885 00:00 app[web.1]: self.conn = await self.pool.pop()
2021-12-25T08:59:57.708885 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels_redis/core.py", line 93, in pop
2021-12-25T08:59:57.708886 00:00 app[web.1]: conn = await self.create_conn(loop)
2021-12-25T08:59:57.708886 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/channels_redis/core.py", line 79, in create_conn
2021-12-25T08:59:57.708886 00:00 app[web.1]: return await aioredis.create_redis_pool(**kwargs)
2021-12-25T08:59:57.708887 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/aioredis/commands/__init__.py", line 188, in create_redis_pool
2021-12-25T08:59:57.708887 00:00 app[web.1]: pool = await create_pool(address, db=db,
2021-12-25T08:59:57.708887 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/aioredis/pool.py", line 58, in create_pool
2021-12-25T08:59:57.708888 00:00 app[web.1]: await pool._fill_free(override_min=False)
2021-12-25T08:59:57.708888 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/aioredis/pool.py", line 383, in _fill_free
2021-12-25T08:59:57.708888 00:00 app[web.1]: conn = await self._create_new_connection(self._address)
2021-12-25T08:59:57.708889 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/aioredis/connection.py", line 111, in create_connection
2021-12-25T08:59:57.708889 00:00 app[web.1]: reader, writer = await asyncio.wait_for(open_connection(
2021-12-25T08:59:57.708889 00:00 app[web.1]: File "/usr/local/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
2021-12-25T08:59:57.708890 00:00 app[web.1]: return await fut
2021-12-25T08:59:57.708890 00:00 app[web.1]: File "/usr/local/lib/python3.9/site-packages/aioredis/stream.py", line 23, in open_connection
2021-12-25T08:59:57.708890 00:00 app[web.1]: transport, _ = await get_event_loop().create_connection(
2021-12-25T08:59:57.708891 00:00 app[web.1]: File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1081, in create_connection
2021-12-25T08:59:57.708891 00:00 app[web.1]: transport, protocol = await self._create_connection_transport(
2021-12-25T08:59:57.708891 00:00 app[web.1]: File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1111, in _create_connection_transport
2021-12-25T08:59:57.708892 00:00 app[web.1]: await waiter
2021-12-25T08:59:57.708892 00:00 app[web.1]: File "/usr/local/lib/python3.9/asyncio/sslproto.py", line 528, in data_received
2021-12-25T08:59:57.708892 00:00 app[web.1]: ssldata, appdata = self._sslpipe.feed_ssldata(data)
2021-12-25T08:59:57.708892 00:00 app[web.1]: File "/usr/local/lib/python3.9/asyncio/sslproto.py", line 188, in feed_ssldata
2021-12-25T08:59:57.708893 00:00 app[web.1]: self._sslobj.do_handshake()
2021-12-25T08:59:57.708893 00:00 app[web.1]: File "/usr/local/lib/python3.9/ssl.py", line 944, in do_handshake
2021-12-25T08:59:57.708893 00:00 app[web.1]: self._sslobj.do_handshake()
2021-12-25T08:59:57.708894 00:00 app[web.1]: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)
2021-12-25T08:59:57.709065 00:00 app[web.1]: 2021-12-25 08:59:57,708 INFO     failing WebSocket opening handshake ('Internal server error')

CodePudding user response:

I found this open issue on channels_redis that addresses this: https://github.com/django/channels_redis/issues/235

This appears to be a temporary fix until channels_redis is updated to properly set the SSL context

ssl_context = ssl.SSLContext()
ssl_context.check_hostname = False

heroku_redis_ssl_host = {
    'address': 'rediss://:[email protected]:6379/0'  # The 'rediss' schema denotes a SSL connection.
    'ssl': ssl_context
}

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': (heroku_redis_ssl_host,)
        }
    },
}
  • Related