Skip to content

Commit

Permalink
Use password from redis+sentinel uri
Browse files Browse the repository at this point in the history
If a password is provided in the redis+sentinel uri
pass it on to the Sentinel object.
  • Loading branch information
alisaifee committed Dec 18, 2015
1 parent 3151af3 commit fbdb6fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/source/storage-url.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ Memcached
Redis
Requires the location of the redis server and optionally the database number.
:code:`redis://localhost:6379` or :code:`redis://localhost:6379/1` (for database `1`)
If the database is password protected the password can be provided in the url, for example
:code:`redis://:foobared@localhost:6379`.

Redis with Sentinel
Requires the location(s) of the redis sentinal instances and the `service-name`
that is monitored by the sentinels.
:code:`redis+sentinel://localhost:26379/my-redis-service`
or :code:`redis+sentinel://localhost:26379,localhost:26380/my-redis-service`
If the database is password protected the password can be provided in the url, for example
:code:`redis+sentinel://:foobared@localhost:26379/my-redis-service`.
8 changes: 6 additions & 2 deletions limits/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ def __init__(self, uri, **options):

parsed = urllib.parse.urlparse(uri)
sentinel_configuration = []
for loc in parsed.netloc.split(","):
password = None
if parsed.password:
password = parsed.password
for loc in parsed.netloc[parsed.netloc.find("@")+1:].split(","):
host, port = loc.split(":")
sentinel_configuration.append((host, int(port)))
self.service_name = (
Expand All @@ -408,7 +411,8 @@ def __init__(self, uri, **options):
raise ConfigurationError("'service_name' not provided")
self.sentinel = get_dependency("redis.sentinel").Sentinel(
sentinel_configuration,
socket_timeout=options.get("socket_timeout", 0.2)
socket_timeout=options.get("socket_timeout", 0.2),
password=password
)
self.initialize_storage()
super(RedisSentinelStorage, self).__init__()
Expand Down
4 changes: 4 additions & 0 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from uuid import uuid4

import hiro
import mock
import redis
import redis.lock
from redis.sentinel import Sentinel
Expand All @@ -31,6 +32,9 @@ def test_storage_string(self):
self.assertTrue(isinstance(storage_from_string("redis+sentinel://localhost:26379/localhost-redis-sentinel"), RedisSentinelStorage))
self.assertRaises(ConfigurationError, storage_from_string, "blah://")
self.assertRaises(ConfigurationError, storage_from_string, "redis+sentinel://localhost:26379")
with mock.patch("limits.storage.get_dependency") as get_dependency:
self.assertTrue(isinstance(storage_from_string("redis+sentinel://:foobared@localhost:26379/localhost-redis-sentinel"), RedisSentinelStorage))
self.assertEqual(get_dependency().Sentinel.call_args[1]['password'], 'foobared')

def test_storage_check(self):
self.assertTrue(storage_from_string("memory://").check())
Expand Down

0 comments on commit fbdb6fd

Please sign in to comment.