Выполнить лабораторную 7 по безопасности мессенджера

This commit is contained in:
2026-12-04 23:51:24 +03:00
parent 20afc2c790
commit 65bbf3224a
86 changed files with 6376 additions and 144 deletions
+69
View File
@@ -0,0 +1,69 @@
"""Rotate a worker's scoped key while synthetic jobs are queued."""
import json
from pathlib import Path
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(ROOT), str(ROOT / "tests")]
from client import Client
from scripts.operator import administrative_bao, operator_credentials, settings
PROBE = """
import json, sys, secrets
sys.path.insert(0, "/audit")
from botocore.exceptions import ClientError
from tls_matrix import storage
client = storage(json.load(sys.stdin))
try:
client.put_object(Bucket="messenger-attachments",
Key="attachments/audit-" + secrets.token_hex(8), Body=b"synthetic")
except ClientError as error:
print(error.response["Error"]["Code"])
else:
print("ok")
"""
def command(*args, **kwargs):
return subprocess.run(args, cwd=ROOT, check=True, text=True, **kwargs)
def probe(key):
result = command("docker", "compose", "run", "--rm", "--no-deps", "-T",
"audit", "python", "-c", PROBE, input=json.dumps(key), stdout=subprocess.PIPE)
return result.stdout.strip()
def main():
port = settings().get("HTTPS_PORT", "8443")
client = Client(f"https://localhost:{port}", str(ROOT / "secrets/trust/edge.crt"))
alice, _, _, chat = client.bootstrap()
bao = administrative_bao()
admin = operator_credentials()["root_token"]
path = "runtime/data/worker"
old = bao.request(path, token=admin)["data"]["data"]
assert probe(old) == "ok"
before, first_bytes = client.attachment(alice, chat)
start = time.monotonic()
prior = command(sys.executable, "scripts/operator.py", "rotate-s3-add", stdout=subprocess.PIPE).stdout.strip()
assert prior == old["S3_ACCESS_KEY_ID"]
current = bao.request(path, token=admin)["data"]["data"]
assert current != old and probe(current) == "ok"
during, second_bytes = client.attachment(alice, chat)
for attachment, content in ((before, first_bytes), (during, second_bytes)):
client.wait_ready(alice, attachment)
assert client.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=alice)["bytes"] == content
command(sys.executable, "scripts/operator.py", "rotate-s3-remove", prior, stdout=subprocess.DEVNULL)
decision = probe(old)
assert decision in ("AccessDenied", "InvalidAccessKeyId", "SignatureDoesNotMatch"), decision
after, body = client.attachment(alice, chat)
client.wait_ready(alice, after)
assert client.call("GET", f"/api/v1/attachments/{after['id']}/content", actor=alice)["bytes"] == body
print(json.dumps({"jobs_with_verified_original": 3, "old_key_before": "ok",
"old_key_after": decision, "new_key": "ok", "seconds": round(time.monotonic() - start, 2)}))
if __name__ == "__main__":
main()