Files
rust-discovery/tests/local_access_database.py
T

65 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Exercise local MFA and MCP credentials against disposable PostgreSQL with tenant RLS."""
from pathlib import Path
import subprocess
import time
import uuid
ROOT = Path(__file__).resolve().parents[1]
def command(args, data=None, timeout=120):
result = subprocess.run(args, input=data, capture_output=True, timeout=timeout)
if result.returncode:
raise RuntimeError(result.stderr.decode(errors="replace")[-4000:])
return result.stdout
def main():
name = "rmm-mcp-token-" + uuid.uuid4().hex[:10]
command(["docker", "run", "-d", "--network", "none", "--name", name,
"-e", "POSTGRES_HOST_AUTH_METHOD=trust", "postgres:17.6-bookworm"])
try:
for _ in range(100):
ready = subprocess.run(["docker", "exec", name, "pg_isready", "-U", "postgres"],
capture_output=True)
if ready.returncode == 0 and b"init process complete" in command(["docker", "logs", name]):
break
time.sleep(0.2)
else:
raise RuntimeError("ISOLATED_POSTGRES_NOT_READY")
def sql(role, statement):
return command(["docker", "exec", "-i", name, "psql", "-XqAt", "-v",
"ON_ERROR_STOP=1", "-U", role, "-d", "postgres"], statement.encode())
sql("postgres", """CREATE ROLE rmm_migrator LOGIN NOSUPERUSER NOBYPASSRLS;
CREATE ROLE rmm_app LOGIN NOINHERIT NOSUPERUSER NOBYPASSRLS;
ALTER DATABASE postgres OWNER TO rmm_migrator;
ALTER SCHEMA public OWNER TO rmm_migrator;""")
for path in sorted((ROOT / "migrations").glob("*.sql")):
sql("rmm_migrator", path.read_text())
sql("postgres", (ROOT / "tests/customer_access.sql").read_text())
print("PASS selected, global and future customer grants, role boundaries, revocation and tenant context")
test_command = [
"docker", "run", "--rm", "--network", "container:" + name,
"--read-only", "--tmpfs", "/tmp:rw,size=512m",
"-v", str(ROOT) + ":/src:ro", "-w", "/src",
"-v", str(ROOT / "devstate/windows-server-contract/target") + ":/target",
"-e", "CARGO_TARGET_DIR=/target",
"-e", "RMM_TEST_REPAIR_ADMIN_URL=postgres://postgres@127.0.0.1/postgres",
"-e", "RMM_TEST_REPAIR_APP_URL=postgres://rmm_app@127.0.0.1/postgres",
"--entrypoint", "cargo", "rust-discovery-windows-tls-full:local",
"test", "--locked", "--offline", "-p", "rmm-server",
]
for test_filter in ("local_auth::tests", "mcp_tokens::tests", "devices::tests"):
output = command([*test_command, test_filter, "--", "--include-ignored", "--nocapture"], timeout=300)
print(output.decode())
print("PASS all migrations under NOSUPERUSER/NOBYPASSRLS owner; local MFA and MCP tests in isolated PostgreSQL")
finally:
subprocess.run(["docker", "rm", "-f", name], capture_output=True)
if __name__ == "__main__":
main()