lab4: S3-вложения, presigned upload и проверки
This commit is contained in:
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail when the SeaweedFS identities and the API environment disagree.
|
||||
|
||||
The stand keeps synthetic credentials in two places on purpose: the identities
|
||||
file is mounted into SeaweedFS, while the API receives the same values through
|
||||
the environment. This check keeps the copies from drifting.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
IDENTITIES = ROOT / "infra/s3/s3.json"
|
||||
|
||||
|
||||
def parse_env(path: Path) -> dict[str, str]:
|
||||
values: dict[str, str] = {}
|
||||
for raw in path.read_text().splitlines():
|
||||
line = raw.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value.strip().strip('"').strip("'")
|
||||
return values
|
||||
|
||||
|
||||
def main() -> int:
|
||||
identities = json.loads(IDENTITIES.read_text())
|
||||
by_name = {item["name"]: item for item in identities.get("identities", [])}
|
||||
|
||||
source = ROOT / ".env"
|
||||
if not source.is_file():
|
||||
source = ROOT / ".env.example"
|
||||
env = parse_env(source)
|
||||
|
||||
expected = {
|
||||
"admin": ("S3_ADMIN_ACCESS_KEY", "S3_ADMIN_SECRET_ACCESS_KEY"),
|
||||
"app": ("S3_ACCESS_KEY_ID", "S3_SECRET_ACCESS_KEY"),
|
||||
}
|
||||
|
||||
failures: list[str] = []
|
||||
for name, (key_var, secret_var) in expected.items():
|
||||
identity = by_name.get(name)
|
||||
if identity is None:
|
||||
failures.append(f"{IDENTITIES}: identity {name!r} is missing")
|
||||
continue
|
||||
credentials = identity.get("credentials") or []
|
||||
if not credentials:
|
||||
failures.append(f"{IDENTITIES}: identity {name!r} has no credentials")
|
||||
continue
|
||||
access_key = credentials[0].get("accessKey")
|
||||
secret_key = credentials[0].get("secretKey")
|
||||
if env.get(key_var) != access_key:
|
||||
failures.append(f"{source.name}: {key_var} does not match identity {name!r}")
|
||||
if env.get(secret_var) != secret_key:
|
||||
failures.append(f"{source.name}: {secret_var} does not match identity {name!r}")
|
||||
|
||||
if failures:
|
||||
print("FAIL: S3 credentials are inconsistent")
|
||||
for failure in failures:
|
||||
print(" -", failure)
|
||||
return 1
|
||||
|
||||
print(f"PASS: S3 identities match {source.name} (admin + app)")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user