forked from mtusi2026/mtusi2026lab7
247 lines
5.7 KiB
Python
247 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Positive and negative RustFS IAM tests over verified HTTPS."""
|
|
|
|
import os
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
|
BUCKET = os.environ["S3_BUCKET"]
|
|
ENDPOINT = os.environ["S3_ENDPOINT"]
|
|
CA_FILE = os.environ["AWS_CA_BUNDLE"]
|
|
|
|
assert ENDPOINT == "https://minio:9000"
|
|
assert Path(CA_FILE).is_file()
|
|
|
|
values = {}
|
|
|
|
for line in Path("/run/lab7/local.env").read_text(
|
|
encoding="utf-8"
|
|
).splitlines():
|
|
line = line.strip()
|
|
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
|
|
name, value = line.split("=", 1)
|
|
values[name.strip()] = value.strip().strip('"').strip("'")
|
|
|
|
|
|
def client(access_key, secret_key):
|
|
return boto3.client(
|
|
"s3",
|
|
endpoint_url=ENDPOINT,
|
|
aws_access_key_id=access_key,
|
|
aws_secret_access_key=secret_key,
|
|
region_name="us-east-1",
|
|
verify=CA_FILE,
|
|
config=Config(
|
|
signature_version="s3v4",
|
|
s3={"addressing_style": "path"},
|
|
connect_timeout=5,
|
|
read_timeout=10,
|
|
retries={"max_attempts": 1},
|
|
),
|
|
)
|
|
|
|
|
|
admin = client(
|
|
os.environ["S3_ACCESS_KEY"],
|
|
os.environ["S3_SECRET_KEY"],
|
|
)
|
|
|
|
api = client(
|
|
values["S3_API_ACCESS_KEY"],
|
|
values["S3_API_SECRET_KEY"],
|
|
)
|
|
|
|
worker = client(
|
|
values["S3_WORKER_ACCESS_KEY"],
|
|
values["S3_WORKER_SECRET_KEY"],
|
|
)
|
|
|
|
suffix = uuid.uuid4().hex
|
|
|
|
keys = {
|
|
"staging": f"staging/acl-test-{suffix}",
|
|
"original": f"originals/acl-test-{suffix}",
|
|
"thumbnail": f"thumbnails/acl-test-{suffix}.jpg",
|
|
}
|
|
|
|
payload = b"lab7-rustfs-iam-test"
|
|
|
|
|
|
def put(s3, key):
|
|
s3.put_object(
|
|
Bucket=BUCKET,
|
|
Key=key,
|
|
Body=payload,
|
|
)
|
|
|
|
|
|
def get(s3, key):
|
|
response = s3.get_object(
|
|
Bucket=BUCKET,
|
|
Key=key,
|
|
)
|
|
try:
|
|
return response["Body"].read()
|
|
finally:
|
|
response["Body"].close()
|
|
|
|
|
|
def delete(s3, key):
|
|
s3.delete_object(
|
|
Bucket=BUCKET,
|
|
Key=key,
|
|
)
|
|
|
|
|
|
def allowed(label, operation):
|
|
operation()
|
|
print(f"PASS: {label}")
|
|
|
|
|
|
def denied(label, operation):
|
|
try:
|
|
operation()
|
|
except ClientError as exc:
|
|
error = exc.response.get("Error", {})
|
|
status = exc.response.get(
|
|
"ResponseMetadata", {}
|
|
).get("HTTPStatusCode")
|
|
|
|
code = str(error.get("Code", ""))
|
|
|
|
if status == 403 or code in (
|
|
"AccessDenied",
|
|
"AllAccessDisabled",
|
|
):
|
|
print(f"PASS: {label} denied")
|
|
return
|
|
|
|
raise AssertionError(
|
|
f"{label}: unexpected S3 error "
|
|
f"{code}, HTTP {status}"
|
|
) from exc
|
|
|
|
raise AssertionError(
|
|
f"{label}: forbidden operation was allowed"
|
|
)
|
|
|
|
|
|
def main():
|
|
print("RustFS endpoint:", ENDPOINT)
|
|
print("Bucket:", BUCKET)
|
|
print("TLS verification: enabled")
|
|
|
|
try:
|
|
# Prepare existing objects so read-denial tests
|
|
# cannot be confused with missing-object errors.
|
|
for key in keys.values():
|
|
put(admin, key)
|
|
|
|
print("PASS: temporary test objects prepared")
|
|
|
|
# API: staging upload/delete and processed-object reads.
|
|
allowed(
|
|
"API can upload staging",
|
|
lambda: put(api, keys["staging"]),
|
|
)
|
|
|
|
allowed(
|
|
"API can read original",
|
|
lambda: get(api, keys["original"]),
|
|
)
|
|
|
|
allowed(
|
|
"API can read thumbnail",
|
|
lambda: get(api, keys["thumbnail"]),
|
|
)
|
|
|
|
allowed(
|
|
"API can delete staging",
|
|
lambda: delete(api, keys["staging"]),
|
|
)
|
|
|
|
# Restore staging for the worker tests.
|
|
put(admin, keys["staging"])
|
|
|
|
# Worker: read staging, write processed objects.
|
|
allowed(
|
|
"worker can read staging",
|
|
lambda: get(worker, keys["staging"]),
|
|
)
|
|
|
|
allowed(
|
|
"worker can write original",
|
|
lambda: put(worker, keys["original"]),
|
|
)
|
|
|
|
allowed(
|
|
"worker can write thumbnail",
|
|
lambda: put(worker, keys["thumbnail"]),
|
|
)
|
|
|
|
# Negative API tests.
|
|
denied(
|
|
"API staging read",
|
|
lambda: get(api, keys["staging"]),
|
|
)
|
|
|
|
denied(
|
|
"API original write",
|
|
lambda: put(api, keys["original"]),
|
|
)
|
|
|
|
denied(
|
|
"API thumbnail write",
|
|
lambda: put(api, keys["thumbnail"]),
|
|
)
|
|
|
|
denied(
|
|
"API original delete",
|
|
lambda: delete(api, keys["original"]),
|
|
)
|
|
|
|
# Negative worker tests.
|
|
denied(
|
|
"worker original read",
|
|
lambda: get(worker, keys["original"]),
|
|
)
|
|
|
|
denied(
|
|
"worker thumbnail read",
|
|
lambda: get(worker, keys["thumbnail"]),
|
|
)
|
|
|
|
denied(
|
|
"worker staging write",
|
|
lambda: put(worker, keys["staging"]),
|
|
)
|
|
|
|
denied(
|
|
"worker staging delete",
|
|
lambda: delete(worker, keys["staging"]),
|
|
)
|
|
|
|
print("PASS: all RustFS IAM ACL tests passed")
|
|
|
|
finally:
|
|
# Root credentials are used only for test cleanup.
|
|
for key in keys.values():
|
|
try:
|
|
delete(admin, key)
|
|
except Exception:
|
|
print("WARNING: cleanup failed for", key)
|
|
|
|
print("RustFS ACL test cleanup attempted")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|