ssl.SSLCertVerificationError当使用django-channels连接heroku redis时

qxsslcnc  于 2023-05-29  发布在  Go
关注(0)|答案(2)|浏览(138)

我在聊天。我需要使用WebSocket并将我的应用程序部署到Heroku。我使用免费的heroku-redis和django-channels在我的设置py:

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

我尝试使用REDIS_URL,但我得到了同样的错误
然后切换到REDIS_TLS_URL。这两个错误都是从consumers.py在“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()
.........

这是原木

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')
gcxthw6b

gcxthw6b1#

我在channels_redis上发现了这个开放的问题,它解决了这个问题:https://github.com/django/channels_redis/issues/235
这似乎是一个临时修复,直到channels_redis更新为正确设置SSL上下文

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

heroku_redis_ssl_host = {
    'address': 'rediss://:password@127.0.0.1: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,)
        }
    },
}
pb3skfrl

pb3skfrl2#

对于我们的例子,我们有以下版本:channels==3.0.3 redis版本7.0.11

ssl_context = ssl.SSLContext()
ssl_context.verify_mode = ssl.VerifyMode(ssl.CERT_NONE)

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            'hosts': [
                    {
                        'address': 'YOUR_REDIS_SERVER_ADDRESS',
                        'ssl': ssl_context
                    }
                ],
        },
    },
}

相关问题