Skip to content
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

Passing username to redis arguments #657

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ There are several ways to specify a database number:
- If using the ``redis://`` scheme, the path argument of the URL, e.g.
``redis://localhost/0``

When using `Redis' ACLs <https://redis.io/topics/acl>`_, you will need to add the
username to the URL (and provide the password with the Cache ``OPTIONS``).
When using `Redis' ACLs <https://redis.io/topics/acl>`_, you can either pass the
username and password in the Cache ``OPTIONS``, or in the URL.

The login for the user ``django`` would look like this:

.. code-block:: python

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://django@localhost:6379/0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep both examples if they work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged the example in which the username is passed in the URL and the password in the OPTIONS with the example of a URL unsafe password, because I thought it was redundant. Tell me if it suits you as done in 039432c

"LOCATION": "redis://localhost:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"USERNAME": "django",
"PASSWORD": "mysecret"
}
}
Expand Down Expand Up @@ -693,8 +695,11 @@ In order to enable this functionality you should add the following:
# Sentinels which are passed directly to redis Sentinel.
"SENTINELS": SENTINELS,

# kwargs for redis Sentinel (optional).
"SENTINEL_KWARGS": {},
# kwargs for redis Sentinel (optional). Example with auth on sentinels
"SENTINEL_KWARGS": {
"username": "sentinel-user",
"password": "sentinel-pass",
},

# You can still override the connection pool (optional).
"CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool",
Expand Down
4 changes: 4 additions & 0 deletions django_redis/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def make_connection_params(self, url):
"parser_class": self.get_parser_cls(),
}

username = self.options.get("USERNAME", None)
if username:
kwargs["username"] = username

password = self.options.get("PASSWORD", None)
if password:
kwargs["password"] = password
Expand Down