Skip to content

Commit

Permalink
acl-save wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Nov 2, 2024
1 parent 48b5143 commit d31ec72
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions fakeredis/_msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
TIMESERIES_BAD_FILTER_EXPRESSION = "TSDB: failed parsing labels"
HEXPIRE_NUMFIELDS_DIFFERENT = "The `numfields` parameter must match the number of arguments"

MISSING_ACLFILE_CONFIG = "ERR This Redis instance is not configured to use an ACL file. You may want to specify users via the ACL SETUSER command and then issue a CONFIG REWRITE (assuming you have a Redis configuration file set) in order to store users in the Redis configuration."

# Command flags
FLAG_NO_SCRIPT = "s" # Command not allowed in scripts
FLAG_LEAVE_EMPTY_VAL = "v"
Expand Down
21 changes: 18 additions & 3 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ def _create_version(v: VersionType) -> Tuple[int, ...]:
class FakeServer:
_servers_map: Dict[str, "FakeServer"] = dict()

def __init__(self, version: VersionType = (7,), server_type: ServerType = "redis") -> None:
def __init__(
self,
version: VersionType = (7,),
server_type: ServerType = "redis",
config: Dict[bytes, bytes] = None,
) -> None:
"""Initialize a new FakeServer instance.
:param version: The version of the server (e.g. 6, 7.4, "7.4.1", can also be a tuple)
:param server_type: The type of server (redis, dragonfly, valkey)
:param config: A dictionary of configuration options.
Configuration options:
- `requirepass`: The password required to authenticate to the server.
- `aclfile`: The path to the ACL file.
"""
self.lock = threading.Lock()
self.dbs: Dict[int, Database] = defaultdict(lambda: Database(self.lock))
# Maps channel/pattern to a weak set of sockets
Expand All @@ -50,7 +64,7 @@ def __init__(self, version: VersionType = (7,), server_type: ServerType = "redis
if server_type not in ("redis", "dragonfly", "valkey"):
raise ValueError(f"Unsupported server type: {server_type}")
self.server_type: str = server_type
self.config: Dict[bytes, bytes] = dict()
self.config: Dict[bytes, bytes] = config or dict()
self.acl: AccessControlList = AccessControlList()

@staticmethod
Expand Down Expand Up @@ -78,7 +92,8 @@ def __init__(
else:
host, port = kwargs.get("host"), kwargs.get("port")
self.server_key = f"{host}:{port}"
self.server_key += f":{server_type}:v{version[0]}"
version_str = ".".join(version) if isinstance(version, tuple) else str(version)
self.server_key += f":{server_type}:v{version_str}"
self._server = FakeServer.get_server(self.server_key, server_type=server_type, version=version)
self._server.connected = connected
super().__init__(*args, **kwargs)
5 changes: 4 additions & 1 deletion fakeredis/commands_mixins/acl_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ def acl_whoami(self) -> bytes:

@command(name="ACL SAVE", fixed=(), repeat=())
def acl_save(self) -> SimpleString:
return OK # TODO
if b"aclfile" not in self._server_config:
raise SimpleError(msgs.MISSING_ACLFILE_CONFIG)
# TODO
return OK

@command(name="ACL LOAD", fixed=(), repeat=())
def acl_load(self) -> SimpleString:
Expand Down
2 changes: 2 additions & 0 deletions redis-conf/redis-stack.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
aclfile /etc/redis/users.acl

1 change: 1 addition & 0 deletions redis-conf/users.acl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user default on nopass sanitize-payload ~* &* +@all

0 comments on commit d31ec72

Please sign in to comment.