Skip to content

Sentinel authentication (sentinel_kwargs) #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
REDIS_MASTER_HOST: redis
REDIS_MASTER_SET: sentinel
REDIS_SENTINEL_QUORUM: "1"
REDIS_SENTINEL_PASSWORD: channels_redis

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 4 additions & 1 deletion channels_redis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ def create_pool(self, index):
elif "master_name" in host:
sentinels = host.pop("sentinels")
master_name = host.pop("master_name")
sentinel_kwargs = host.pop("sentinel_kwargs", None)
return aioredis.sentinel.SentinelConnectionPool(
master_name, aioredis.sentinel.Sentinel(sentinels), **host
master_name,
aioredis.sentinel.Sentinel(sentinels, sentinel_kwargs=sentinel_kwargs),
**host
)
else:
return aioredis.ConnectionPool(**host)
Expand Down
5 changes: 4 additions & 1 deletion channels_redis/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class RedisSingleShardConnection:
def __init__(self, host, channel_layer):
self.host = host.copy() if type(host) is dict else {"address": host}
self.master_name = self.host.pop("master_name", None)
self.sentinel_kwargs = self.host.pop("sentinel_kwargs", None)
self.channel_layer = channel_layer
self._subscribed_to = set()
self._lock = asyncio.Lock()
Expand Down Expand Up @@ -348,7 +349,9 @@ def _ensure_redis(self):
pool = aioredis.sentinel.SentinelConnectionPool(
self.master_name,
aioredis.sentinel.Sentinel(
self.host["sentinels"], socket_timeout=2
self.host["sentinels"],
socket_timeout=2,
sentinel_kwargs=self.sentinel_kwargs,
),
)
self._redis = aioredis.Redis(connection_pool=pool)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_pubsub_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
from channels_redis.pubsub import RedisPubSubChannelLayer

SENTINEL_MASTER = "sentinel"
TEST_HOSTS = [{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER}]
SENTINEL_KWARGS = {"password": "channels_redis"}
TEST_HOSTS = [
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
}
]


@pytest.fixture()
Expand Down
81 changes: 69 additions & 12 deletions tests/test_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,76 @@
from channels_redis.core import ChannelFull, RedisChannelLayer

SENTINEL_MASTER = "sentinel"
TEST_HOSTS = [{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER}]

SENTINEL_KWARGS = {"password": "channels_redis"}

TEST_HOSTS = [
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
}
]
MULTIPLE_TEST_HOSTS = [
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 0},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 1},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 2},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 3},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 4},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 5},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 6},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 7},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 8},
{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER, "db": 9},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 0,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 1,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 2,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 3,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 4,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 5,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 6,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 7,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 8,
},
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
"db": 9,
},
]


Expand Down