forked from mtusi2026/mtusi2026lab7
Добавить стенд лабораторной 7 с mTLS, OpenBao и least privilege worker.
Закрывает обязательный минимум и трек 1: ротация leaf и секрета, sealed OpenBao и сохранность queued-задачи.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import threading
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
|
||||
from app import telemetry
|
||||
from app.settings import settings
|
||||
|
||||
_client = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
def _client_for():
|
||||
return boto3.client(
|
||||
"s3",
|
||||
endpoint_url=settings.s3_endpoint,
|
||||
aws_access_key_id=settings.s3_access_key,
|
||||
aws_secret_access_key=settings.s3_secret_key,
|
||||
region_name=settings.s3_region,
|
||||
verify=settings.tls_ca,
|
||||
config=Config(
|
||||
signature_version="s3v4",
|
||||
s3={"addressing_style": "path"},
|
||||
connect_timeout=2,
|
||||
read_timeout=10,
|
||||
retries={"max_attempts": 2, "mode": "standard"},
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def init():
|
||||
global _client
|
||||
_client = _client_for()
|
||||
prefix = "staging/" if settings.role == "api" else "originals/"
|
||||
_client.list_objects_v2(Bucket=settings.s3_bucket, Prefix=prefix, MaxKeys=1)
|
||||
|
||||
|
||||
def ready():
|
||||
try:
|
||||
prefix = "staging/" if settings.role == "api" else "originals/"
|
||||
with _lock:
|
||||
_client.list_objects_v2(Bucket=settings.s3_bucket, Prefix=prefix, MaxKeys=1)
|
||||
return True
|
||||
except (BotoCoreError, ClientError, OSError):
|
||||
return False
|
||||
|
||||
|
||||
def put(key, data, content_type, variant):
|
||||
with telemetry.tracer().start_as_current_span(
|
||||
"s3.put",
|
||||
attributes={"s3.operation": "put", "variant": variant},
|
||||
):
|
||||
with _lock:
|
||||
_client.put_object(Bucket=settings.s3_bucket, Key=key, Body=data, ContentType=content_type)
|
||||
|
||||
|
||||
def get(key, variant):
|
||||
with telemetry.tracer().start_as_current_span(
|
||||
"s3.get",
|
||||
attributes={"s3.operation": "get", "variant": variant},
|
||||
):
|
||||
with _lock:
|
||||
response = _client.get_object(Bucket=settings.s3_bucket, Key=key)
|
||||
return response["Body"].read()
|
||||
|
||||
|
||||
def delete(key):
|
||||
with _lock:
|
||||
_client.delete_object(Bucket=settings.s3_bucket, Key=key)
|
||||
Reference in New Issue
Block a user