Files
mtusi2026lab7/scripts/track1_run.py
T
vika baafe2a3ae Добавить стенд лабораторной 7 с mTLS, OpenBao и least privilege worker.
Закрывает обязательный минимум и трек 1: ротация leaf и секрета, sealed OpenBao и сохранность queued-задачи.
2026-11-07 20:46:02 +03:00

313 lines
11 KiB
Python

"""Track 1: least privilege before and after, then revoke, rotate, and keep a queued job.
The worker is stopped only for the durability section and started again before exit.
RabbitMQ identity and the worker S3 secret are restored to a working pair.
"""
import json
import secrets
import subprocess
import time
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
S3_CONFIG = ROOT / "secrets" / "seaweed" / "s3.json"
PASSWORDS = ROOT / "secrets" / "operator" / "passwords.json"
EVIDENCE = ROOT / "evidence"
BUCKET = "messenger-attachments"
def run(command, check=True, input_text=None):
result = subprocess.run(command, cwd=ROOT, input=input_text, capture_output=True, text=True)
if check and result.returncode != 0:
detail = ((result.stderr or result.stdout or "")[:180]).replace("\n", " ")
raise SystemExit("command failed: " + detail)
return result
def psql(sql):
return run(
["docker", "exec", "-i", "mtusi-lab7-postgres-1", "psql", "-U", "messenger", "-d", "messenger", "-t", "-A"],
input_text=sql,
)
def bao_put_worker_s3(access_key, secret_key):
token = json.loads((ROOT / "secrets" / "operator" / "init.json").read_text(encoding="utf-8"))["root_token"]
command = [
"docker",
"compose",
"--env-file",
".env",
"exec",
"-T",
"-e",
"BAO_ADDR=https://127.0.0.1:8200",
"-e",
"BAO_CACERT=/openbao/tls/ca.crt",
"-e",
"BAO_CLIENT_CERT=/openbao/tls/operator.crt",
"-e",
"BAO_CLIENT_KEY=/openbao/tls/operator.key",
"-e",
"BAO_TOKEN=" + token,
"openbao",
"bao",
"kv",
"put",
"secret/worker/s3",
"access_key=" + access_key,
"secret_key=" + secret_key,
]
run(command)
def restart_minio():
run(["docker", "compose", "--env-file", ".env", "restart", "-t", "5", "minio"])
for _ in range(30):
result = run(["docker", "inspect", "--format", "{{.State.Health.Status}}", "mtusi-lab7-minio-1"], check=False)
if (result.stdout or "").strip() == "healthy":
return
time.sleep(2)
raise SystemExit("minio did not become healthy")
def track1():
result = run(
["docker", "compose", "--env-file", ".env", "--profile", "tools", "run", "--rm", "--entrypoint", "python", "probe", "-m", "app.probes.track1"],
check=False,
)
line = ""
for item in (result.stdout or "").splitlines():
if item.startswith("{"):
line = item
if not line:
raise SystemExit("track1 probe produced no report")
return json.loads(line), result.returncode
def widen_s3(config):
for identity in config["identities"]:
if identity["name"] == "worker":
identity["actions"] = [
"Admin",
"Read",
"Write",
"List",
"Read:%s/canary/*" % BUCKET,
"Write:%s/canary/*" % BUCKET,
"List:%s/canary/*" % BUCKET,
]
return config
def worker_secret(config):
for identity in config["identities"]:
if identity["name"] == "worker":
return identity["credentials"][0]["accessKey"], identity["credentials"][0]["secretKey"]
raise SystemExit("worker s3 identity is missing")
def set_worker_secret(config, secret_key):
for identity in config["identities"]:
if identity["name"] == "worker":
identity["credentials"][0]["secretKey"] = secret_key
return config
def old_secret_denied(access_key, secret_key):
admin = ROOT / "secrets" / "seaweed" / "old-secret.json"
admin.write_text(json.dumps({"access_key": access_key, "secret_key": secret_key}), encoding="utf-8")
code = (
"import json,boto3\n"
"from botocore.config import Config\n"
"from botocore.exceptions import ClientError\n"
"cred=json.load(open('/c/cred.json'))\n"
"client=boto3.client('s3',endpoint_url='https://minio:8333',aws_access_key_id=cred['access_key'],"
"aws_secret_access_key=cred['secret_key'],region_name='us-east-1',verify='/c/ca.crt',"
"config=Config(signature_version='s3v4',s3={'addressing_style':'path'}))\n"
"try:\n"
" client.put_object(Bucket='messenger-attachments',Key='staging/old-secret',Body=b'x')\n"
" print('allow')\n"
"except ClientError as exc:\n"
" print('deny:'+str(exc.response.get('Error',{}).get('Code','error')))\n"
"except Exception as exc:\n"
" print('deny:'+type(exc).__name__)\n"
)
try:
result = run(
[
"docker",
"run",
"--rm",
"--network",
"mtusi-lab7_app",
"-v",
str(ROOT / "secrets" / "pki" / "ca.crt") + ":/c/ca.crt:ro",
"-v",
str(admin) + ":/c/cred.json:ro",
"--entrypoint",
"python",
"mtusi-lab7-messenger:1.7.0",
"-c",
code,
]
)
return (result.stdout or "").strip().splitlines()[-1]
finally:
admin.unlink(missing_ok=True)
def restore_rabbit():
run(
["docker", "exec", "mtusi-lab7-rabbitmq-1", "rabbitmqctl", "add_user", "worker.messenger.internal", "unused-password"],
check=False,
)
run(
[
"docker",
"exec",
"mtusi-lab7-rabbitmq-1",
"rabbitmqctl",
"set_permissions",
"-p",
"/",
"worker.messenger.internal",
"^attachment\\.process\\.v1$",
"^$",
"^attachment\\.process\\.v1$",
]
)
def queue_depth():
result = run(["docker", "exec", "mtusi-lab7-rabbitmq-1", "rabbitmqctl", "list_queues", "name", "messages"])
for line in (result.stdout or "").splitlines():
if line.startswith("attachment.process.v1"):
parts = line.split()
return int(parts[-1])
return 0
def wait_queue(minimum, timeout=25):
last = 0
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
last = queue_depth()
if last >= minimum:
return last
time.sleep(1)
return last
def upload_while_worker_stopped():
import sys
sys.path.insert(0, str(ROOT))
from tests.client import Client
client = Client("https://127.0.0.1:8443", str(ROOT / "secrets" / "pki" / "ca.crt"), timeout=15)
alice, _bob, _eve, chat = client.bootstrap()
attachment, _content = client.attachment(alice, chat)
return alice, attachment["id"]
def wait_status(actor, attachment_id):
import sys
sys.path.insert(0, str(ROOT))
from tests.client import Client
client = Client("https://127.0.0.1:8443", str(ROOT / "secrets" / "pki" / "ca.crt"), timeout=15)
deadline = time.monotonic() + 70
last = ""
while time.monotonic() < deadline:
current = client.call("GET", "/api/v1/attachments/" + attachment_id, actor=actor, schema="Attachment")["json"]
last = current["status"]
if last in {"ready", "failed"}:
return last
time.sleep(1)
return last or "timeout"
def main():
EVIDENCE.mkdir(exist_ok=True)
original = S3_CONFIG.read_text(encoding="utf-8")
original_config = json.loads(original)
access_key, old_secret = worker_secret(original_config)
report = {}
committed = False
try:
least, code = track1()
report["least_privilege_exit"] = code
report["least_privilege"] = least
psql("GRANT SELECT ON users TO worker_login;\n")
broad = widen_s3(json.loads(original))
S3_CONFIG.write_text(json.dumps(broad), encoding="utf-8")
restart_minio()
widened, widened_code = track1()
report["broad_exit"] = widened_code
report["broad"] = widened
S3_CONFIG.write_text(original, encoding="utf-8")
psql("REVOKE SELECT ON users FROM worker_login;\n")
restart_minio()
restored, restored_code = track1()
report["restored_exit"] = restored_code
report["restored"] = restored
run(["docker", "compose", "--env-file", ".env", "stop", "-t", "5", "worker"])
actor, attachment_id = upload_while_worker_stopped()
uuid.UUID(attachment_id)
report["queued_before_revoke"] = psql(
"SELECT status FROM attachments WHERE id = '%s';\n" % attachment_id
).stdout.strip()
report["queue_before_revoke"] = wait_queue(1)
run(["docker", "exec", "mtusi-lab7-rabbitmq-1", "rabbitmqctl", "delete_user", "worker.messenger.internal"])
report["queue_after_revoke"] = queue_depth()
new_secret = secrets.token_hex(24)
rotated = set_worker_secret(json.loads(original), new_secret)
S3_CONFIG.write_text(json.dumps(rotated), encoding="utf-8")
passwords = json.loads(PASSWORDS.read_text(encoding="utf-8"))
passwords["worker_s3_secret"] = new_secret
PASSWORDS.write_text(json.dumps(passwords), encoding="utf-8")
bao_put_worker_s3(access_key, new_secret)
committed = True
restart_minio()
report["old_secret"] = old_secret_denied(access_key, old_secret)
restore_rabbit()
run(["docker", "compose", "--env-file", ".env", "start", "worker"])
for _ in range(30):
health = run(["docker", "inspect", "--format", "{{.State.Health.Status}}", "mtusi-lab7-worker-1"], check=False)
if (health.stdout or "").strip() == "healthy":
break
time.sleep(2)
report["after_restore"] = wait_status(actor, attachment_id)
report["attachment_id_prefix"] = attachment_id[:8]
finally:
if not committed:
S3_CONFIG.write_text(original, encoding="utf-8")
psql("REVOKE SELECT ON users FROM worker_login;\n")
restore_rabbit()
run(["docker", "compose", "--env-file", ".env", "start", "worker"], check=False)
(EVIDENCE / "track1-run.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
print(json.dumps({key: report[key] for key in report if key != "least_privilege" and key != "broad" and key != "restored"}))
print("least", report.get("least_privilege"))
print("broad", report.get("broad"))
print("restored", report.get("restored"))
required = (
report.get("least_privilege_exit") == 0
and report.get("broad", {}).get("db_select_password_hash") == "allow"
and str(report.get("broad", {}).get("s3_get_canary", "")).startswith("allow")
and report.get("restored_exit") == 0
and report.get("queued_before_revoke") == "queued"
and report.get("queue_after_revoke", 0) >= 1
and str(report.get("old_secret", "")).startswith("deny")
and report.get("after_restore") == "ready"
)
if not required:
raise SystemExit(1)
if __name__ == "__main__":
main()