Files
mtusi2026lab5/student-tests/test_storage_security.py

158 lines
6.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Защита хранилища вне API: приватный bucket, ограниченные ключи API,
закрытые административные интерфейсы, идемпотентный bootstrap.
Проверки заходят внутрь контейнеров стенда (docker compose exec/run),
поэтому запускаются явно: make test-storage (RUN_STORAGE_TESTS=1).
"""
import json
import os
import subprocess
import httpx
import pytest
from helpers import ROOT, auth, compose, container_json
pytestmark = pytest.mark.skipif(
os.environ.get("RUN_STORAGE_TESTS") != "1",
reason="заходит в контейнеры стенда: запускайте через make test-storage",
)
# Порты SeaweedFS кроме S3: master, volume, filer (HTTP с веб-интерфейсами
# и gRPC). Они слушают только loopback контейнера s3.
ADMIN_PORTS = (9333, 19333, 8080, 18080, 8888, 18888, 8181, 9101)
def test_anonymous_s3_access_is_denied(api: httpx.Client, make_user, make_chat, upload) -> None:
"""Прямой неподписанный GET объекта (ключ известен) и списка bucket — 403"""
alice = make_user("alice")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
object_key = compose(
"exec",
"-T",
"postgres",
"sh",
"-c",
'psql -X -q -tA -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c '
f"\"SELECT object_key FROM attachments WHERE id = '{attachment['id']}'\"",
).strip()
assert object_key.startswith("originals/")
result = container_json(
"api",
f"""
import json, urllib.error, urllib.request
from config import S3_BUCKET, S3_ENDPOINT
codes = {{}}
for name, path in {{
"object": "/" + S3_BUCKET + "/{object_key}",
"bucket_list": "/" + S3_BUCKET,
"service": "/",
}}.items():
try:
with urllib.request.urlopen(S3_ENDPOINT + path, timeout=3) as response:
codes[name] = response.status
except urllib.error.HTTPError as error:
codes[name] = error.code
print(json.dumps(codes))
""",
)
assert result == {"object": 403, "bucket_list": 403, "service": 403}
# Через API участник по-прежнему скачивает файл
content = api.get(f"/api/v1/attachments/{attachment['id']}/content", headers=auth(alice))
assert content.status_code == 200
def test_api_credentials_are_limited_to_own_bucket_objects() -> None:
"""Ключи API: объекты своего bucket — да; bucket, политика, ACL — нет"""
result = container_json(
"api",
"""
import asyncio, json
from infrastructure.dependencies import storage_manager
from infrastructure.utils import storage_error_code
async def attempt(name, call, results):
try:
await call
results[name] = "allowed"
except Exception as error:
results[name] = storage_error_code(error)
async def main():
s3 = await storage_manager.create_client()
bucket = storage_manager.bucket
key = "originals/security-probe"
policy = json.dumps({"Version": "2012-10-17", "Statement": [{"Effect": "Allow",
"Principal": "*", "Action": "s3:GetObject",
"Resource": "arn:aws:s3:::" + bucket + "/*"}]})
results = {}
await attempt("put_object", s3.put_object(Bucket=bucket, Key=key, Body=b"x"), results)
await attempt("get_object", s3.get_object(Bucket=bucket, Key=key), results)
await attempt("delete_object", s3.delete_object(Bucket=bucket, Key=key), results)
await attempt("create_bucket", s3.create_bucket(Bucket="intruder-bucket"), results)
await attempt("delete_bucket", s3.delete_bucket(Bucket=bucket), results)
await attempt("put_bucket_policy", s3.put_bucket_policy(Bucket=bucket, Policy=policy), results)
await attempt("put_bucket_acl", s3.put_bucket_acl(Bucket=bucket, ACL="public-read"), results)
await storage_manager.close()
print(json.dumps(results))
asyncio.run(main())
""",
)
assert result["put_object"] == result["get_object"] == "allowed"
assert result["delete_object"] == "allowed"
for forbidden in ("create_bucket", "delete_bucket", "put_bucket_policy", "put_bucket_acl"):
assert result[forbidden] == "AccessDenied", result
def test_api_container_has_no_admin_credentials() -> None:
files = compose("exec", "-T", "api", "ls", "/run/secrets").split()
assert sorted(files) == ["s3_access_key_id", "s3_secret_access_key"]
def test_admin_interfaces_are_not_reachable() -> None:
"""Из сети storage открыт только S3 API; наружу стенда S3 не опубликован"""
result = container_json(
"api",
f"""
import json, socket
state = {{}}
for port in (9000,) + {ADMIN_PORTS!r}:
try:
socket.create_connection(("s3", port), timeout=2).close()
state[port] = "open"
except OSError as error:
state[port] = type(error).__name__
print(json.dumps(state))
""",
)
assert result.pop("9000") == "open"
assert all(value != "open" for value in result.values()), result
for line in compose("ps", "--format", "json", "s3").splitlines():
publishers = json.loads(line)["Publishers"] or []
assert all(item["PublishedPort"] == 0 for item in publishers), publishers
def test_balancer_network_cannot_reach_storage() -> None:
"""nginx (сеть edge) не видит S3: клиентский трафик идёт только через API"""
result = subprocess.run(
["docker", "compose", "--env-file", ".env", "exec", "-T", "nginx",
"wget", "-q", "-T", "2", "-O", "/dev/null", "http://s3:9000/"],
cwd=ROOT,
capture_output=True,
text=True,
timeout=30,
)
assert result.returncode != 0
def test_bucket_bootstrap_is_idempotent() -> None:
for _ in range(2):
output = compose("run", "--rm", "--no-deps", "s3-bootstrap")
assert "уже существует" in output
assert "приватен" in output