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 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
32 changes: 21 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,24 @@ 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`` dict, in the URL, or the username
in the URL and the password in the ``OPTIONS``.

Be aware that if the username/password are passed both in the URL and ``OPTIONS``
dict, the ones passed in the URL prime.

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 All @@ -137,25 +143,26 @@ An alternative would be write both username and password into the URL:
}
}

In some circumstances the password you should use to connect Redis
is not URL-safe, in this case you can escape it or just use the
convenience option in ``OPTIONS`` dict:
The username can also be passed in the URL, and the password in the ``OPTIONS``:

.. code-block:: python

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"LOCATION": "redis://django@localhost:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": "mysecret"
}
}
}

Take care, that this option does not overwrites the password in the uri, so if
you have set the password in the uri, this settings will be ignored.

NOTE: In some circumstances the password you should use to connect Redis
is not URL-safe, in this case you can escape it or just use the
convenience option in ``OPTIONS`` dict.


Configure as session backend
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -693,8 +700,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
1 change: 1 addition & 0 deletions changelog.d/657.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the ability to pass redis username in the cache options
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
38 changes: 38 additions & 0 deletions tests/test_connection_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from django_redis import pool


class TestConnectionParams:
@pytest.mark.parametrize(
"connection_string",
[
"unix://tmp/foo.bar?db=1",
"redis://localhost/2",
"rediss://localhost:3333?db=2",
],
)
def test_make_connection_params_url(self, connection_string: str):
cf = pool.get_connection_factory(

Check warning on line 16 in tests/test_connection_params.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_params.py#L15-L16

Added lines #L15 - L16 were not covered by tests
path="django_redis.pool.ConnectionFactory", options={}
)
res = cf.make_connection_params(connection_string)
assert res["url"] == connection_string

Check warning on line 20 in tests/test_connection_params.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_params.py#L19-L20

Added lines #L19 - L20 were not covered by tests

def test_make_connection_params_options(self):
options = {
"USERNAME": "django",
"PASSWORD": "mysecret",
"SOCKET_TIMEOUT": 5,

Check warning on line 26 in tests/test_connection_params.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_params.py#L22-L26

Added lines #L22 - L26 were not covered by tests
}
cf = pool.get_connection_factory(
path="django_redis.pool.ConnectionFactory", options=options

Check warning on line 29 in tests/test_connection_params.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_params.py#L28-L29

Added lines #L28 - L29 were not covered by tests
)
res = cf.make_connection_params("")
res.pop("url")
res.pop("parser_class")
assert res == {
"username": "django",
"password": "mysecret",
"socket_timeout": 5,

Check warning on line 37 in tests/test_connection_params.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_params.py#L31-L37

Added lines #L31 - L37 were not covered by tests
}
19 changes: 0 additions & 19 deletions tests/test_connection_string.py

This file was deleted.