"""Private S3 access using bounded credentials and sanitized spans.""" import os from threading import Lock import boto3 from botocore.config import Config from .credentials import credentials from .telemetry import span BUCKET = os.getenv("S3_BUCKET", "messenger-attachments") class Storage: def __init__(self): self.lock = Lock() self.keys = None self.client = None def __getattr__(self, name): def call(*args, **kwargs): with self.lock: snapshot = credentials.get() keys = snapshot["s3_user"], snapshot["s3_password"] if keys != self.keys: self.client = boto3.client( "s3", endpoint_url="https://s3:9000", region_name="us-east-1", aws_access_key_id=keys[0], aws_secret_access_key=keys[1], verify=os.environ["TLS_CA_FILE"], config=Config(connect_timeout=2, read_timeout=5, retries={"total_max_attempts": 1}, s3={"addressing_style": "path"}), ) self.keys = keys client = self.client with span(f"s3.{name}"): return getattr(client, name)(*args, **kwargs) return call s3 = Storage()