Skip to content

Adding support for GENPASS bits #1558

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 1, 2021
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
19 changes: 16 additions & 3 deletions redis/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,22 @@ def acl_deluser(self, username):
"Delete the ACL for the specified ``username``"
return self.execute_command('ACL DELUSER', username)

def acl_genpass(self):
"Generate a random password value"
return self.execute_command('ACL GENPASS')
def acl_genpass(self, bits=None):
"""Generate a random password value.
If ``bits`` is supplied then use this number of bits, rounded to
the next multiple of 4.
See: https://redis.io/commands/acl-genpass
"""
pieces = []
if bits is not None:
try:
b = int(bits)
if b < 0 or b > 4096:
raise ValueError
except ValueError:
raise DataError('genpass optionally accepts a bits argument, '
'between 0 and 4096.')
return self.execute_command('ACL GENPASS', *pieces)

def acl_getuser(self, username):
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ def test_acl_genpass(self, r):
password = r.acl_genpass()
assert isinstance(password, str)

with pytest.raises(exceptions.DataError):
r.acl_genpass('value')
r.acl_genpass(-5)
r.acl_genpass(5555)

r.acl_genpass(555)
assert isinstance(password, str)

@skip_if_server_version_lt(REDIS_6_VERSION)
def test_acl_getuser_setuser(self, r, request):
username = 'redis-py-user'
Expand Down