Files
mtusi2026lab4/scripts/bootstrap-s3.py
2026-11-17 02:09:04 +03:00

154 lines
4.8 KiB
Python

#!/usr/bin/env python3
"""Idempotently create the private bucket and restricted RustFS API user."""
import json
import hashlib
import os
from pathlib import Path
import time
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
import boto3
import botocore
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
from botocore.credentials import Credentials
from botocore.exceptions import ClientError
from botocore.config import Config
def secret(name):
return Path(os.environ[name + "_FILE"]).read_text(encoding="utf-8").strip()
def signed_admin_request(root_access, root_secret, method, path, body=None):
endpoint = os.environ.get("S3_ENDPOINT", "http://s3:9000")
region = os.environ.get("S3_REGION", "us-east-1")
content = json.dumps(body, separators=(",", ":")).encode("utf-8") if body else b""
url = endpoint.rstrip("/") + path
headers = {
"Host": url.split("/", 3)[2],
"Content-Type": "application/json",
"X-Amz-Content-SHA256": hashlib.sha256(content).hexdigest(),
}
request = AWSRequest(method=method, url=url, data=content, headers=headers)
SigV4Auth(Credentials(root_access, root_secret), "s3", region).add_auth(request)
http_request = Request(
url,
data=content if content else None,
headers=dict(request.headers),
method=method,
)
try:
with urlopen(http_request, timeout=5) as response:
response.read()
except HTTPError as error:
detail = error.read(2048).decode("utf-8", "replace")
if error.code == 409 and "add-user" in path:
return
raise RuntimeError(
"RustFS IAM bootstrap request failed: "
+ method
+ " "
+ path.split("?", 1)[0]
+ " returned "
+ str(error.code)
+ ": "
+ detail[:300]
) from None
except URLError as error:
raise RuntimeError("RustFS IAM bootstrap service is unavailable") from error
def main():
endpoint = os.environ.get("S3_ENDPOINT", "http://s3:9000")
region = os.environ.get("S3_REGION", "us-east-1")
bucket = os.environ.get("S3_BUCKET", "messenger-attachments")
root_access = secret("S3_ROOT_ACCESS_KEY")
root_secret = secret("S3_ROOT_SECRET_KEY")
app_access = secret("S3_ACCESS_KEY_ID")
app_secret = secret("S3_SECRET_ACCESS_KEY")
config = Config(
signature_version="s3v4",
s3={"addressing_style": "path"},
connect_timeout=2,
read_timeout=5,
retries={"max_attempts": 2, "mode": "standard"},
)
root = boto3.client(
"s3",
endpoint_url=endpoint,
region_name=region,
aws_access_key_id=root_access,
aws_secret_access_key=root_secret,
config=config,
)
for attempt in range(30):
try:
try:
root.create_bucket(Bucket=bucket)
except ClientError as error:
if error.response.get("Error", {}).get("Code") != "BucketAlreadyOwnedByYou":
raise
root.head_bucket(Bucket=bucket)
break
except (botocore.exceptions.BotoCoreError, ClientError):
if attempt == 29:
raise
time.sleep(2)
policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::" + bucket],
},
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": ["arn:aws:s3:::" + bucket + "/attachments/*"],
},
],
}
signed_admin_request(
root_access,
root_secret,
"PUT",
"/rustfs/admin/v3/add-user?" + urlencode({"accessKey": app_access}),
{"secretKey": app_secret, "status": "enabled"},
)
signed_admin_request(
root_access,
root_secret,
"PUT",
"/rustfs/admin/v3/add-canned-policy?" + urlencode({"name": "messenger-api"}),
policy,
)
signed_admin_request(
root_access,
root_secret,
"POST",
"/rustfs/admin/v3/idp/builtin/policy/attach",
{"policies": ["messenger-api"], "user": app_access},
)
api_client = boto3.client(
"s3",
endpoint_url=endpoint,
region_name=region,
aws_access_key_id=app_access,
aws_secret_access_key=app_secret,
config=config,
)
api_client.head_bucket(Bucket=bucket)
root.close()
api_client.close()
print("S3 bootstrap complete: private bucket and scoped API credentials are ready.")
if __name__ == "__main__":
main()