78 lines
3.3 KiB
Python
78 lines
3.3 KiB
Python
"""Идемпотентный private bucket и отдельная ограниченная учётная запись API."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
from tempfile import TemporaryDirectory
|
|
import time
|
|
from urllib.request import urlopen
|
|
|
|
|
|
def secret(name):
|
|
value = (Path("/run/secrets") / name).read_text().strip()
|
|
if not value:
|
|
raise ValueError("Пустой файл секрета")
|
|
return value
|
|
|
|
|
|
def main():
|
|
endpoint = os.environ["S3_ENDPOINT"]
|
|
bucket = os.environ["S3_BUCKET"]
|
|
root_user, root_password = secret("minio_root_user"), secret("minio_root_password")
|
|
access, password = secret("s3_access_key_id"), secret("s3_secret_access_key")
|
|
if access == root_user:
|
|
raise ValueError("API не должен использовать root credentials")
|
|
deadline = time.monotonic() + 60
|
|
while True:
|
|
try:
|
|
with urlopen(endpoint + "/minio/health/ready", timeout=2) as response:
|
|
if response.status == 200:
|
|
break
|
|
except OSError:
|
|
pass
|
|
if time.monotonic() >= deadline:
|
|
raise RuntimeError("MinIO не готов за 60 секунд")
|
|
time.sleep(1)
|
|
with TemporaryDirectory(prefix="minio-bootstrap-") as directory:
|
|
config = Path(directory)
|
|
# Credentials не передаются mc alias set через аргументы или окружение.
|
|
config_file = config / "config.json"
|
|
config_file.write_text(json.dumps({"version": "10", "aliases": {"storage": {
|
|
"url": endpoint, "accessKey": root_user, "secretKey": root_password,
|
|
"api": "S3v4", "path": "auto",
|
|
}}}))
|
|
config_file.chmod(0o600)
|
|
|
|
def mc(*args):
|
|
result = subprocess.run(["mc", "--config-dir", directory, *args],
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30)
|
|
if result.returncode:
|
|
raise RuntimeError("Ошибка bootstrap MinIO; значения credentials скрыты")
|
|
|
|
mc("mb", "--ignore-existing", "storage/" + bucket)
|
|
mc("anonymous", "set", "none", "storage/" + bucket)
|
|
policy = {
|
|
"Version": "2012-10-17", "Statement": [
|
|
{"Effect": "Allow", "Action": ["s3:GetBucketLocation", "s3:ListBucket"],
|
|
"Resource": [f"arn:aws:s3:::{bucket}"]},
|
|
{"Effect": "Allow", "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
|
"Resource": [f"arn:aws:s3:::{bucket}/originals/*"]},
|
|
],
|
|
}
|
|
policy_file = config / "policy.json"
|
|
policy_file.write_text(json.dumps(policy))
|
|
mc("admin", "policy", "create", "storage", "messenger-attachments", str(policy_file))
|
|
# mc не поддерживает stdin для user add: вывод и ошибки команды скрыты.
|
|
mc("admin", "user", "add", "storage", access, password)
|
|
mc("admin", "policy", "attach", "storage", "messenger-attachments", "--user", access)
|
|
print("MinIO private bucket and API policy configured", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print("MinIO bootstrap failed: " + type(error).__name__, flush=True)
|
|
raise SystemExit(1) from None
|