"""Isolated loopback-only Valkey control: same operations before/after ACL narrowing.""" import json import secrets import redis def main(): admin = redis.Redis(host="127.0.0.1", port=6378, decode_responses=True) wide_pass, limited_pass = secrets.token_urlsafe(24), secrets.token_urlsafe(24) admin.execute_command( "ACL", "SETUSER", "wide", "on", ">" + wide_pass, "~security-study:*", "+@all" ) admin.execute_command( "ACL", "SETUSER", "limited", "on", ">" + limited_pass, "~security-study:jobs", "+ping", "+xgroup|create", "+xreadgroup", "+xautoclaim", "+xack", "+xdel", ) rows = [] for role, password in (("wide", wide_pass), ("limited", limited_pass)): client = redis.Redis( host="127.0.0.1", port=6378, username=role, password=password, decode_responses=True ) for name, operation in ( ("queue_admin_acl_list", lambda client=client: client.acl_list()), ( "queue_publish", lambda client=client: client.xadd("security-study:jobs", {"body": "synthetic"}), ), ( "queue_other_key_write", lambda client=client: client.set("security-study:other", "synthetic"), ), ): try: operation() allowed = True except redis.ResponseError: allowed = False rows.append({"role": role, "operation": name, "allowed": allowed}) client.close() print( json.dumps( { "isolated_port": 6378, "rows": rows, "passed": all(r["allowed"] == (r["role"] == "wide") for r in rows), } ) ) admin.shutdown(nosave=True) if __name__ == "__main__": main()