forked from mtusi2026/mtusi2026lab7
89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
"""Disposable bucket comparison; baseline credentials cannot access messenger."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
|
def client(access, secret, identity):
|
|
return boto3.client(
|
|
"s3",
|
|
endpoint_url="https://storage:8443",
|
|
aws_access_key_id=access,
|
|
aws_secret_access_key=secret,
|
|
region_name="us-east-1",
|
|
verify="/run/tls/ca.pem",
|
|
config=Config(
|
|
client_cert=(
|
|
f"/run/audit-identities/{identity}/cert.pem",
|
|
f"/run/audit-identities/{identity}/key.pem",
|
|
),
|
|
connect_timeout=2,
|
|
read_timeout=3,
|
|
retries={"total_max_attempts": 1},
|
|
s3={"addressing_style": "path"},
|
|
request_checksum_calculation="when_required",
|
|
response_checksum_validation="when_required",
|
|
),
|
|
)
|
|
|
|
|
|
def main():
|
|
root = Path("/run/audit-material")
|
|
|
|
def read(name):
|
|
return (root / name).read_text().strip()
|
|
|
|
admin = client(read("admin_s3_access"), read("admin_s3_secret"), "operator")
|
|
wide = client(read("study_wide_s3_access"), read("study_wide_s3_secret"), "operator")
|
|
limited = client(read("worker_s3_access"), read("worker_s3_secret"), "worker")
|
|
bucket = "security-study"
|
|
rows = []
|
|
try:
|
|
wide.get_object(Bucket="messenger", Key="private/not-a-real-user-object")
|
|
isolated = False
|
|
except ClientError as exc:
|
|
isolated = exc.response["ResponseMetadata"]["HTTPStatusCode"] == 403
|
|
admin.create_bucket(Bucket=bucket)
|
|
admin.put_object(Bucket=bucket, Key="private/synthetic", Body=b"synthetic-not-user-data")
|
|
try:
|
|
for role, c in (("wide", wide), ("limited", limited)):
|
|
for operation in ("read_private", "write_private"):
|
|
try:
|
|
if operation == "read_private":
|
|
response = c.get_object(Bucket=bucket, Key="private/synthetic")
|
|
response["Body"].read()
|
|
response["Body"].close()
|
|
else:
|
|
c.put_object(Bucket=bucket, Key="private/written", Body=b"synthetic")
|
|
allowed = True
|
|
except ClientError as exc:
|
|
if exc.response["ResponseMetadata"]["HTTPStatusCode"] != 403:
|
|
raise
|
|
allowed = False
|
|
rows.append({"role": role, "operation": operation, "allowed": allowed})
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"scope": "disposable bucket security-study; broad role has no messenger rights",
|
|
"rows": rows,
|
|
"wide_role_denied_application_bucket": isolated,
|
|
"passed": isolated
|
|
and all(row["allowed"] == (row["role"] == "wide") for row in rows),
|
|
}
|
|
)
|
|
)
|
|
finally:
|
|
for key in ("private/synthetic", "private/written"):
|
|
admin.delete_object(Bucket=bucket, Key=key)
|
|
admin.delete_bucket(Bucket=bucket)
|
|
for c in (admin, wide, limited):
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|