forked from mtusi2026/mtusi2026lab7
Implement lab7 security and compromised worker recovery evidence
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
||||
from bootstrap import PRIVATE, ROOT, write
|
||||
from provision import compose
|
||||
|
||||
|
||||
def database_baseline():
|
||||
script = """BEGIN;
|
||||
CREATE ROLE baseline_worker SUPERUSER NOLOGIN;
|
||||
SET LOCAL ROLE baseline_worker;
|
||||
SELECT count(*) FROM users;
|
||||
SELECT count(*) FROM sessions;
|
||||
DELETE FROM messages WHERE false;
|
||||
CREATE TABLE baseline_rollback_probe(id int);
|
||||
ROLLBACK;
|
||||
"""
|
||||
compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"db",
|
||||
"psql",
|
||||
"-v",
|
||||
"ON_ERROR_STOP=1",
|
||||
"-U",
|
||||
"operator",
|
||||
"-d",
|
||||
"messenger",
|
||||
stdin=script,
|
||||
)
|
||||
return dict(
|
||||
isolation="transaction rolled back; NOLOGIN role",
|
||||
read_password_table=True,
|
||||
read_session_table=True,
|
||||
delete_messages_permission=True,
|
||||
ddl=True,
|
||||
)
|
||||
|
||||
|
||||
def broker_baseline():
|
||||
name = "lab7-isolated-broker-" + uuid.uuid4().hex[:8]
|
||||
image = (
|
||||
"redis:7.4-alpine@sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f"
|
||||
)
|
||||
try:
|
||||
subprocess.run(
|
||||
[
|
||||
"docker",
|
||||
"run",
|
||||
"-d",
|
||||
"--name",
|
||||
name,
|
||||
"--network",
|
||||
"none",
|
||||
"--memory",
|
||||
"64m",
|
||||
"--cpus",
|
||||
"0.2",
|
||||
image,
|
||||
"redis-server",
|
||||
"--bind",
|
||||
"127.0.0.1",
|
||||
"--save",
|
||||
"",
|
||||
"--appendonly",
|
||||
"no",
|
||||
],
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
for command in [
|
||||
("SET", "foreign-secret", "synthetic"),
|
||||
("GET", "foreign-secret"),
|
||||
("FLUSHALL",),
|
||||
]:
|
||||
result = (
|
||||
subprocess.check_output(["docker", "exec", name, "redis-cli", *command])
|
||||
.decode()
|
||||
.strip()
|
||||
)
|
||||
assert result in ("OK", "synthetic")
|
||||
finally:
|
||||
subprocess.run(["docker", "rm", "-f", name], capture_output=True, check=True)
|
||||
return dict(
|
||||
isolation="separate disposable network-none container, no volumes",
|
||||
foreign_keys=True,
|
||||
flush_all=True,
|
||||
)
|
||||
|
||||
|
||||
def s3_baseline():
|
||||
data = json.loads((PRIVATE / "operator/credentials.json").read_text())
|
||||
name = "baseline-" + uuid.uuid4().hex[:8]
|
||||
bucket = "lab7-" + name
|
||||
password = __import__("secrets").token_hex(24)
|
||||
policy = dict(
|
||||
Version="2012-10-17",
|
||||
Statement=[
|
||||
dict(
|
||||
Effect="Allow",
|
||||
Action=["s3:*"],
|
||||
Resource=["arn:aws:s3:::" + bucket, "arn:aws:s3:::" + bucket + "/*"],
|
||||
)
|
||||
],
|
||||
)
|
||||
write(ROOT / "infra/security/policies/baseline-s3.json", json.dumps(policy))
|
||||
prefix = f"set -eu\nmc alias set local https://minio:9000 {data['minio_root_user']} {data['minio_root_password']} >/dev/null\n"
|
||||
setup = (
|
||||
prefix
|
||||
+ f"mc mb local/{bucket} >/dev/null\nmc admin user add local {name} {password} >/dev/null\nmc admin policy create local {name} /policies/baseline-s3.json >/dev/null\nmc admin policy attach local {name} --user {name} >/dev/null\n"
|
||||
)
|
||||
write(
|
||||
PRIVATE / "operator/baseline.json",
|
||||
json.dumps(dict(access=name, secret=password, bucket=bucket)),
|
||||
)
|
||||
try:
|
||||
compose("run", "--rm", "--no-deps", "-T", "s3-admin", stdin=setup)
|
||||
code = "import boto3,json;d=json.load(open('/audit-secrets/operator/baseline.json'));s=boto3.client('s3',endpoint_url='https://minio:9000',aws_access_key_id=d['access'],aws_secret_access_key=d['secret'],verify='/audit-secrets/public/ca.crt');s.put_object(Bucket=d['bucket'],Key='admin/synthetic',Body=b'synthetic');assert s.get_object(Bucket=d['bucket'],Key='admin/synthetic')['Body'].read()==b'synthetic';s.delete_object(Bucket=d['bucket'],Key='admin/synthetic');print('PASS broad prefix operations in disposable bucket')"
|
||||
compose("run", "--rm", "--no-deps", "-T", "audit", "-c", code)
|
||||
finally:
|
||||
cleanup = (
|
||||
prefix
|
||||
+ f"mc admin user rm local {name} >/dev/null\nmc admin policy rm local {name} >/dev/null\nmc rb --force local/{bucket} >/dev/null\n"
|
||||
)
|
||||
compose("run", "--rm", "--no-deps", "-T", "s3-admin", stdin=cleanup)
|
||||
(ROOT / "infra/security/policies/baseline-s3.json").unlink()
|
||||
(PRIVATE / "operator/baseline.json").unlink()
|
||||
return dict(
|
||||
isolation="disposable private bucket and temporary principal",
|
||||
arbitrary_prefix_put_get_delete=True,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
baseline = dict(database=database_baseline(), broker=broker_baseline(), s3=s3_baseline())
|
||||
script = (ROOT / "student-tests/worker_rights.py").read_bytes()
|
||||
result = subprocess.run(
|
||||
["docker", "compose", "exec", "-T", "--index", "1", "worker", "python", "-"],
|
||||
input=script,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
restricted = json.loads(result.stdout)
|
||||
output = dict(
|
||||
status="PASS",
|
||||
baseline=baseline,
|
||||
least_privilege=restricted,
|
||||
interpretation="Reproduces broad lab6 permissions in isolated resources; production lab7 privileges are never broadened. Worker can still corrupt pipeline metadata/results, exhaust its processing budget and read image originals; revocation is needed in addition to least privilege.",
|
||||
)
|
||||
write(ROOT / "evidence/worker-compromise.json", json.dumps(output, indent=2) + "\n")
|
||||
print(json.dumps(output, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user