Files
mtusi2026lab7/app/broker.py
T
vika baafe2a3ae
Skeleton contracts / contracts (pull_request) Canceled after 0s
Добавить стенд лабораторной 7 с mTLS, OpenBao и least privilege worker.
Закрывает обязательный минимум и трек 1: ротация leaf и секрета, sealed OpenBao и сохранность queued-задачи.
2026-11-07 20:46:02 +03:00

141 lines
4.0 KiB
Python

import json
import ssl
import uuid
import pika
from app import telemetry
from app.db import jsonb
from app.settings import settings
QUEUE = "attachment.process.v1"
EXCHANGE = "attachment.process"
def _params():
ctx = ssl.create_default_context(cafile=settings.tls_ca)
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
ctx.load_cert_chain(settings.client_cert, settings.client_key)
ctx.check_hostname = True
ctx.verify_mode = ssl.CERT_REQUIRED
return pika.ConnectionParameters(
host=settings.broker_host,
port=settings.broker_port,
virtual_host="/",
credentials=pika.credentials.ExternalCredentials(),
ssl_options=pika.SSLOptions(ctx, settings.broker_host),
heartbeat=60,
blocked_connection_timeout=2,
socket_timeout=3,
connection_attempts=1,
retry_delay=0,
)
def init():
connection = pika.BlockingConnection(_params())
try:
channel = connection.channel()
channel.queue_declare(queue=QUEUE, durable=True)
finally:
connection.close()
def ready():
try:
connection = pika.BlockingConnection(_params())
try:
channel = connection.channel()
channel.queue_declare(queue=QUEUE, durable=True, passive=True)
finally:
connection.close()
return True
except Exception:
return False
def insert_outbox(conn, payload, created_at):
conn.execute(
"""
INSERT INTO outbox (job_id, attachment_id, payload, created_at)
VALUES (%s, %s, %s, %s)
""",
(uuid.UUID(str(payload["job_id"])), uuid.UUID(str(payload["attachment_id"])), jsonb(payload), created_at),
)
def publish_pending(conn, job_id=None):
query = """
SELECT job_id, payload
FROM outbox
WHERE published_at IS NULL
"""
args = []
if job_id is not None:
query += " AND job_id = %s"
args.append(job_id)
query += " ORDER BY created_at FOR UPDATE SKIP LOCKED LIMIT 20"
rows = conn.execute(query, args).fetchall()
for row in rows:
payload = row["payload"]
if isinstance(payload, str):
payload = json.loads(payload)
_publish(payload)
conn.execute("UPDATE outbox SET published_at = NOW() WHERE job_id = %s", (row["job_id"],))
return len(rows)
def _publish(payload):
carrier = {}
if payload.get("traceparent"):
carrier["traceparent"] = payload["traceparent"]
if payload.get("tracestate"):
carrier["tracestate"] = payload["tracestate"]
parent = telemetry.extract_context(carrier)
with telemetry.tracer().start_as_current_span(
"attachment.process publish",
context=parent,
kind=telemetry.trace.SpanKind.PRODUCER,
attributes={
"messaging.system": "rabbitmq",
"messaging.destination.name": QUEUE,
"messaging.operation.type": "publish",
},
):
outgoing = telemetry.inject_carrier()
body = dict(payload)
if outgoing.get("traceparent"):
body["traceparent"] = outgoing["traceparent"]
if outgoing.get("tracestate"):
body["tracestate"] = outgoing["tracestate"]
_send(body, outgoing)
telemetry.log_event(
"job.published",
job_id=str(payload.get("job_id")),
attempt=int(payload.get("attempt") or 1),
)
def _send(payload, headers):
connection = pika.BlockingConnection(_params())
try:
channel = connection.channel()
channel.confirm_delivery()
channel.basic_publish(
exchange=EXCHANGE,
routing_key=QUEUE,
body=json.dumps(payload, separators=(",", ":")).encode("utf-8"),
properties=pika.BasicProperties(
delivery_mode=2,
content_type="application/json",
headers=headers,
),
mandatory=True,
)
finally:
connection.close()
def consumer_count():
raise RuntimeError("management api is not used")