From 14152623569353bcf982ee28d02556e38684cbed Mon Sep 17 00:00:00 2001 From: Luferov Victor Date: Fri, 14 Jul 2023 14:11:03 +0300 Subject: [PATCH 1/3] feat: Add username to StrictRedis client --- .gitignore | 1 + README.rst | 2 ++ aredis/client.py | 3 ++- aredis/connection.py | 8 +++++--- aredis/pool.py | 10 +++++++--- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 344dde3f..03967d08 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ aredis.egg-info build/ dist/ .idea/ +venv/ env/ env3.6/ dump.rdb diff --git a/README.rst b/README.rst index 28832617..2e4be784 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,8 @@ single node client async def example(): client = StrictRedis(host='127.0.0.1', port=6379, db=0) + # If not anonymous + # client = StrictRedis(host='127.0.0.1', port=6379, db=0, username='user', password='password') await client.flushdb() await client.set('foo', 1) assert await client.exists('foo') is True diff --git a/aredis/client.py b/aredis/client.py index 91973db5..dddfdc83 100644 --- a/aredis/client.py +++ b/aredis/client.py @@ -92,7 +92,7 @@ def from_url(cls, url, db=None, **kwargs): return cls(connection_pool=connection_pool) def __init__(self, host='localhost', port=6379, - db=0, password=None, stream_timeout=None, + db=0, username=None, password=None, stream_timeout=None, connect_timeout=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', decode_responses=False, ssl=False, ssl_context=None, @@ -104,6 +104,7 @@ def __init__(self, host='localhost', port=6379, if not connection_pool: kwargs = { 'db': db, + 'username': username, 'password': password, 'encoding': encoding, 'stream_timeout': stream_timeout, diff --git a/aredis/connection.py b/aredis/connection.py index 6dc79050..1c9d842e 100755 --- a/aredis/connection.py +++ b/aredis/connection.py @@ -372,6 +372,7 @@ def __init__(self, retry_on_timeout=False, stream_timeout=None, self._stream_timeout = stream_timeout self._reader = None self._writer = None + self.username = None self.password = '' self.db = '' self.pid = os.getpid() @@ -435,7 +436,7 @@ async def on_connect(self): # if a password is specified, authenticate if self.password: - await self.send_command('AUTH', self.password) + await self.send_command('AUTH', self.username, self.password) if nativestr(await self.read_response()) != 'OK': raise ConnectionError('Invalid Password') @@ -515,7 +516,7 @@ def disconnect(self): self._writer = None def pack_command(self, *args): - "Pack a series of arguments into the Redis protocol" + """Pack a series of arguments into the Redis protocol.""" output = [] # the client might have included 1 or more literal arguments in # the command name, e.g., 'CONFIG GET'. The Redis server expects these @@ -569,7 +570,7 @@ def pack_commands(self, commands): class Connection(BaseConnection): description = 'Connection' - def __init__(self, host='127.0.0.1', port=6379, password=None, + def __init__(self, host='127.0.0.1', port=6379, username=None, password=None, db=0, retry_on_timeout=False, stream_timeout=None, connect_timeout=None, ssl_context=None, parser_class=DefaultParser, reader_read_size=65535, encoding='utf-8', decode_responses=False, socket_keepalive=None, @@ -580,6 +581,7 @@ def __init__(self, host='127.0.0.1', port=6379, password=None, loop=loop) self.host = host self.port = port + self.username = username self.password = password self.db = db self.ssl_context = ssl_context diff --git a/aredis/pool.py b/aredis/pool.py index 1fbb2cc7..95246118 100644 --- a/aredis/pool.py +++ b/aredis/pool.py @@ -47,9 +47,9 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): For example:: - redis://[:password]@localhost:6379/0 - rediss://[:password]@localhost:6379/0 - unix://[:password]@/path/to/socket.sock?db=0 + redis://[user:password]@localhost:6379/0 + rediss://[user:password]@localhost:6379/0 + unix://[user:password]@/path/to/socket.sock?db=0 Three URL schemes are supported: @@ -103,10 +103,12 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): url_options[name] = value[0] if decode_components: + username = unquote(url.username) if url.username else None password = unquote(url.password) if url.password else None path = unquote(url.path) if url.path else None hostname = unquote(url.hostname) if url.hostname else None else: + username = url.username password = url.password path = url.path hostname = url.hostname @@ -114,6 +116,7 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): # We only support redis:// and unix:// schemes. if url.scheme == 'unix': url_options.update({ + 'username': username, 'password': password, 'path': path, 'connection_class': UnixDomainSocketConnection, @@ -123,6 +126,7 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): url_options.update({ 'host': hostname, 'port': int(url.port or 6379), + 'username': username, 'password': password, }) From 3beb625c1fbab86e867787cf69f15479f5c2dfbb Mon Sep 17 00:00:00 2001 From: Luferov Victor Date: Fri, 14 Jul 2023 14:56:08 +0300 Subject: [PATCH 2/3] fix: Typo user -> username --- aredis/pool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aredis/pool.py b/aredis/pool.py index 95246118..3b0a80c3 100644 --- a/aredis/pool.py +++ b/aredis/pool.py @@ -47,9 +47,9 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): For example:: - redis://[user:password]@localhost:6379/0 - rediss://[user:password]@localhost:6379/0 - unix://[user:password]@/path/to/socket.sock?db=0 + redis://[username:password]@localhost:6379/0 + rediss://[username:password]@localhost:6379/0 + unix://[username:password]@/path/to/socket.sock?db=0 Three URL schemes are supported: From ddc913d19829b7908f3e5e5ce8c148c4d0ddb321 Mon Sep 17 00:00:00 2001 From: Luferov Victor Date: Fri, 14 Jul 2023 17:13:05 +0300 Subject: [PATCH 3/3] fix: Username default --- aredis/connection.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aredis/connection.py b/aredis/connection.py index 1c9d842e..24409d52 100755 --- a/aredis/connection.py +++ b/aredis/connection.py @@ -436,7 +436,10 @@ async def on_connect(self): # if a password is specified, authenticate if self.password: - await self.send_command('AUTH', self.username, self.password) + if self.username: + await self.send_command('AUTH', self.username, self.password) + else: + await self.send_command('AUTH', self.password) if nativestr(await self.read_response()) != 'OK': raise ConnectionError('Invalid Password')