Files
mtusi2026lab5/app/main.py
T
Artu-RitoandClaude Sonnet 5 bdba93f022 Лаба 5: Redis Streams очередь + асинхронная обработка изображений (оценка 4)
Переиспользован стек lab4 (LB/2 реплики/Postgres/SeaweedFS). upload теперь
202 queued; отдельный worker-процесс генерирует thumbnail/crop/optimized.
Transactional outbox закрывает разрыв COMMIT/publish; durable retry
(job_retry_schedule) переживает падение worker во время backoff; lease
recovery через XAUTOCLAIM; DLQ-реестр (job_failures) с явным operator
replay (scripts/replay_failed.py). make test/test-persistence/test-failover/
validate зелёные; в процессе экспериментов найдены и исправлены два
реальных бага (неполный try/except вокруг S3-записи вариантов, ошибка типов
asyncpg в построении interval) — см. REPORT.md и evidence/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 01:21:42 +03:00

725 lines
27 KiB
Python

"""Messenger API — lab 5: durable queue + async attachment processing."""
import asyncio
import base64
import binascii
import json
import os
import re
import socket
import uuid
from datetime import datetime, timezone
import asyncpg
from fastapi import FastAPI, Header, Request
from fastapi.responses import JSONResponse, Response
from pydantic import BaseModel, ConfigDict, Field, ValidationError
import hashlib
import os
from . import db, sessions, storage, images, broker, outbox
from . import idempotency
from .errors import (
ApiError, bad_request, conflict, not_found, payload_too_large,
unauthorized, unavailable, unsupported_media_type,
)
from .security import hash_password, verify_password
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
IDEMPOTENCY_KEY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
FILENAME_FORBIDDEN_RE = re.compile(r"[/\\\x00-\x1f]")
MAX_UPLOAD_BYTES = int(os.environ.get("MAX_UPLOAD_BYTES", "10485760"))
MAX_IMAGE_PIXELS = int(os.environ.get("MAX_IMAGE_PIXELS", "40000000"))
INSTANCE_ID = os.environ.get("INSTANCE_ID") or f"{socket.gethostname()}-{os.getpid()}"
app = FastAPI(title="MTUSI Messenger — lab 2")
# ---------------------------------------------------------------------------
# Request/response plumbing: request id, instance id, canonical error shape.
# ---------------------------------------------------------------------------
@app.middleware("http")
async def tagging_middleware(request: Request, call_next):
request_id = uuid.uuid4().hex
request.state.request_id = request_id
try:
response = await call_next(request)
except ApiError as exc:
response = _error_response(exc, request_id)
except Exception: # noqa: BLE001 - never leak internals
response = _error_response(ApiError(500, "internal_error", "internal error"), request_id)
response.headers["X-Request-Id"] = request_id
response.headers["X-Instance-Id"] = INSTANCE_ID
return response
def _error_response(exc: ApiError, request_id: str) -> JSONResponse:
headers = {}
if exc.status == 401:
path = "/api/v1/auth/sessions"
scheme = 'Basic realm="messenger", charset="UTF-8"' if _is_login_path(exc) else 'Bearer realm="messenger"'
headers["WWW-Authenticate"] = scheme
return JSONResponse(
status_code=exc.status,
content={"error": {"code": exc.code, "message": exc.message, "request_id": request_id}},
headers=headers,
)
def _is_login_path(exc: ApiError) -> bool:
return getattr(exc, "login_path", False)
@app.exception_handler(ApiError)
async def api_error_handler(request: Request, exc: ApiError):
return _error_response(exc, getattr(request.state, "request_id", uuid.uuid4().hex))
def iso(value: datetime) -> str:
return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
def user_json(record) -> dict:
return {
"id": str(record["id"]),
"username": record["username"],
"display_name": record["display_name"],
"created_at": iso(record["created_at"]),
}
def chat_json(record, member_ids: list[uuid.UUID]) -> dict:
return {
"id": str(record["id"]),
"title": record["title"],
"member_ids": [str(m) for m in member_ids],
"created_at": iso(record["created_at"]),
}
def message_json(record, attachment_ids: list[uuid.UUID]) -> dict:
return {
"id": str(record["id"]),
"chat_id": str(record["chat_id"]),
"sender_id": str(record["sender_id"]),
"text": record["text"],
"attachment_ids": [str(a) for a in attachment_ids],
"created_at": iso(record["created_at"]),
}
def attachment_json(record, variant_rows=()) -> dict:
return {
"id": str(record["id"]),
"chat_id": str(record["chat_id"]),
"owner_id": str(record["owner_id"]),
"filename": record["filename"],
"content_type": record["content_type"],
"size_bytes": record["size_bytes"],
"sha256": record["sha256"],
"status": record["status"],
"variants": [variant_json(v) for v in variant_rows],
"error_code": record["error_code"],
"created_at": iso(record["created_at"]),
"updated_at": iso(record["updated_at"]),
}
def variant_json(record) -> dict:
return {
"name": record["name"],
"content_type": record["content_type"],
"size_bytes": record["size_bytes"],
"width": record["width"],
"height": record["height"],
"sha256": record["sha256"],
}
# ---------------------------------------------------------------------------
# Cursors: opaque base64url(json) of the keyset tuple, scoped per resource.
# ---------------------------------------------------------------------------
def encode_cursor(payload: dict) -> str:
raw = json.dumps(payload, separators=(",", ":")).encode()
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
def decode_cursor(value: str) -> dict:
try:
padded = value + "=" * (-len(value) % 4)
raw = base64.urlsafe_b64decode(padded.encode())
return json.loads(raw)
except (binascii.Error, ValueError, UnicodeDecodeError):
raise bad_request("malformed cursor")
# ---------------------------------------------------------------------------
# Auth: Basic only on POST /auth/sessions, Bearer everywhere else protected.
# ---------------------------------------------------------------------------
async def require_bearer(authorization: str | None) -> asyncpg.Record:
if not authorization or not authorization.startswith("Bearer "):
raise unauthorized()
token = authorization[len("Bearer "):].strip()
if not token:
raise unauthorized()
try:
user_id = await sessions.resolve(token)
except Exception: # noqa: BLE001 - dependency failure must not grant access
raise unavailable("session store unavailable")
if user_id is None:
raise unauthorized()
async with db.pool().acquire() as conn:
record = await conn.fetchrow("SELECT * FROM users WHERE id=$1", user_id)
if record is None:
raise unauthorized()
return record
def parse_basic(authorization: str | None) -> tuple[str, str]:
err = ApiError(401, "unauthorized", "Basic credentials required")
err.login_path = True
if not authorization or not authorization.startswith("Basic "):
raise err
try:
decoded = base64.b64decode(authorization[len("Basic "):].strip()).decode("utf-8")
except (binascii.Error, UnicodeDecodeError):
raise err
if ":" not in decoded:
raise err
username, password = decoded.split(":", 1)
return username, password
# ---------------------------------------------------------------------------
# Request bodies (additionalProperties: false in the contract -> extra="forbid").
# ---------------------------------------------------------------------------
class CreateUser(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str
display_name: str = Field(min_length=1, max_length=100)
password: str = Field(min_length=12, max_length=128)
class CreateChat(BaseModel):
model_config = ConfigDict(extra="forbid")
title: str = Field(min_length=1, max_length=100)
member_ids: list[str] = Field(min_length=1, max_length=100)
class CreateMessage(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(max_length=4000) # required key, "" allowed if attachments are present
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
async def parse_body(request: Request, model):
try:
raw = await request.json()
except json.JSONDecodeError:
raise bad_request("malformed JSON")
try:
return model.model_validate(raw)
except ValidationError as exc:
raise bad_request(str(exc.errors()[0]["msg"]) if exc.errors() else "invalid body")
# ---------------------------------------------------------------------------
# Health
# ---------------------------------------------------------------------------
@app.get("/health/live")
async def health_live():
return {"status": "ok"}
@app.get("/health/ready")
async def health_ready():
if await db.ping() and await sessions.ping() and await storage.ping() and await broker.ping():
return {"status": "ok"}
raise unavailable("database, session store, object storage or broker not ready")
# ---------------------------------------------------------------------------
# Users
# ---------------------------------------------------------------------------
@app.post("/api/v1/users", status_code=201)
async def register_user(request: Request):
body = await parse_body(request, CreateUser)
if not USERNAME_RE.match(body.username):
raise bad_request("username must match ^[a-z0-9_]{3,32}$")
if not body.display_name.strip():
raise bad_request("display_name must not be blank")
password_hash = hash_password(body.password)
try:
async with db.pool().acquire() as conn:
record = await conn.fetchrow(
"""
INSERT INTO users (username, display_name, password_hash)
VALUES ($1, $2, $3)
RETURNING id, username, display_name, created_at
""",
body.username,
body.display_name,
password_hash,
)
except asyncpg.UniqueViolationError:
raise conflict("username already taken")
return JSONResponse(status_code=201, content=user_json(record))
@app.get("/api/v1/users/me")
async def current_user(authorization: str | None = Header(default=None)):
record = await require_bearer(authorization)
return user_json(record)
# ---------------------------------------------------------------------------
# Sessions
# ---------------------------------------------------------------------------
@app.post("/api/v1/auth/sessions", status_code=201)
async def create_session(authorization: str | None = Header(default=None)):
username, password = parse_basic(authorization)
async with db.pool().acquire() as conn:
record = await conn.fetchrow("SELECT * FROM users WHERE username=$1", username)
err = ApiError(401, "unauthorized", "invalid credentials")
err.login_path = True
if record is None or not verify_password(password, record["password_hash"]):
raise err
try:
token, expires_at = await sessions.create(record["id"])
except Exception: # noqa: BLE001
raise unavailable("session store unavailable")
return JSONResponse(
status_code=201,
content={
"token": token,
"token_type": "Bearer",
"expires_at": iso(expires_at),
"user": user_json(record),
},
)
@app.delete("/api/v1/auth/sessions/current", status_code=204)
async def revoke_session(authorization: str | None = Header(default=None)):
await require_bearer(authorization) # validates the token is currently live
token = authorization[len("Bearer "):].strip()
try:
await sessions.revoke(token)
except Exception: # noqa: BLE001
raise unavailable("session store unavailable")
return Response(status_code=204)
# ---------------------------------------------------------------------------
# Chats
# ---------------------------------------------------------------------------
@app.post("/api/v1/chats", status_code=201)
async def create_chat(request: Request, authorization: str | None = Header(default=None)):
actor = await require_bearer(authorization)
body = await parse_body(request, CreateChat)
try:
member_ids = [uuid.UUID(m) for m in body.member_ids]
except ValueError:
raise bad_request("member_ids must be UUIDs")
if len(set(member_ids)) != len(member_ids):
raise bad_request("member_ids must not contain duplicates")
if actor["id"] not in member_ids:
raise bad_request("caller must be a member of the chat")
async with db.pool().acquire() as conn:
async with conn.transaction():
existing = await conn.fetch(
"SELECT id FROM users WHERE id = ANY($1::uuid[])", member_ids
)
if len(existing) != len(member_ids):
raise bad_request("unknown member in member_ids")
chat = await conn.fetchrow(
"INSERT INTO chats (title) VALUES ($1) RETURNING id, title, created_at",
body.title,
)
await conn.executemany(
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
[(chat["id"], m) for m in member_ids],
)
return JSONResponse(status_code=201, content=chat_json(chat, member_ids))
@app.get("/api/v1/chats")
async def list_chats(
authorization: str | None = Header(default=None),
limit: int = 50,
cursor: str | None = None,
):
actor = await require_bearer(authorization)
if not (1 <= limit <= 100):
raise bad_request("limit must be between 1 and 100")
after_created_at = after_id = None
if cursor:
payload = decode_cursor(cursor)
try:
after_created_at = datetime.fromisoformat(payload["created_at"])
after_id = uuid.UUID(payload["id"])
except (KeyError, ValueError):
raise bad_request("malformed cursor")
async with db.pool().acquire() as conn:
rows = await conn.fetch(
"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE cm.user_id = $1
AND ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2, $3))
ORDER BY c.created_at ASC, c.id ASC
LIMIT $4
""",
actor["id"],
after_created_at,
after_id,
limit + 1,
)
has_more = len(rows) > limit
rows = rows[:limit]
items = []
for row in rows:
members = await conn.fetch("SELECT user_id FROM chat_members WHERE chat_id=$1", row["id"])
items.append(chat_json(row, [m["user_id"] for m in members]))
next_cursor = None
if has_more:
last = rows[-1]
next_cursor = encode_cursor({"created_at": iso(last["created_at"]), "id": str(last["id"])})
return {"items": items, "next_cursor": next_cursor}
async def _load_chat_if_member(conn, chat_id: uuid.UUID, user_id: uuid.UUID):
chat = await conn.fetchrow("SELECT id, title, created_at FROM chats WHERE id=$1", chat_id)
if chat is None:
raise not_found("chat not found")
membership = await conn.fetchrow(
"SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2", chat_id, user_id
)
if membership is None:
raise not_found("chat not found")
return chat
@app.get("/api/v1/chats/{chat_id}")
async def get_chat(chat_id: str, authorization: str | None = Header(default=None)):
actor = await require_bearer(authorization)
try:
chat_uuid = uuid.UUID(chat_id)
except ValueError:
raise bad_request("chat_id must be a UUID")
async with db.pool().acquire() as conn:
chat = await _load_chat_if_member(conn, chat_uuid, actor["id"])
members = await conn.fetch("SELECT user_id FROM chat_members WHERE chat_id=$1", chat_uuid)
return chat_json(chat, [m["user_id"] for m in members])
# ---------------------------------------------------------------------------
# Messages
# ---------------------------------------------------------------------------
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
async def create_message(
chat_id: str,
request: Request,
authorization: str | None = Header(default=None),
idempotency_key: str | None = Header(default=None, alias="Idempotency-Key"),
):
# Session/access are re-checked on every call, including idempotent replays.
actor = await require_bearer(authorization)
try:
chat_uuid = uuid.UUID(chat_id)
except ValueError:
raise bad_request("chat_id must be a UUID")
if not idempotency_key or not IDEMPOTENCY_KEY_RE.match(idempotency_key):
raise bad_request("Idempotency-Key header is required and must match ^[A-Za-z0-9._:-]{1,128}$")
body = await parse_body(request, CreateMessage)
try:
attachment_uuids = [uuid.UUID(a) for a in body.attachment_ids]
except ValueError:
raise bad_request("attachment_ids must be UUIDs")
if len(set(attachment_uuids)) != len(attachment_uuids):
raise bad_request("attachment_ids must not contain duplicates")
if not body.text.strip() and not attachment_uuids:
raise bad_request("text must not be blank unless attachments are present")
async def perform():
record = await conn.fetchrow(
"""
INSERT INTO messages (chat_id, sender_id, text)
VALUES ($1, $2, $3)
RETURNING id, chat_id, sender_id, text, created_at
""",
chat_uuid,
actor["id"],
body.text,
)
if attachment_uuids:
owned = await conn.fetch(
"SELECT id, status FROM attachments WHERE chat_id=$1 AND id = ANY($2::uuid[])",
chat_uuid, attachment_uuids,
)
if len(owned) != len(attachment_uuids):
raise not_found("one or more attachment_ids are not available in this chat")
if any(r["status"] == "failed" for r in owned):
raise conflict("one or more attachment_ids reference a failed attachment")
await conn.executemany(
"INSERT INTO message_attachments (message_id, attachment_id) VALUES ($1, $2)",
[(record["id"], a) for a in attachment_uuids],
)
return 201, message_json(record, attachment_uuids)
async with db.pool().acquire() as conn:
await _load_chat_if_member(conn, chat_uuid, actor["id"])
async with conn.transaction():
try:
status_code, payload = await idempotency.run_once(
conn,
actor["id"],
"POST",
request.url.path,
idempotency_key,
{"text": body.text, "attachment_ids": sorted(body.attachment_ids)},
perform,
)
except idempotency.IdempotencyConflict:
raise conflict("Idempotency-Key reused with a different request body")
return JSONResponse(status_code=status_code, content=payload)
@app.get("/api/v1/chats/{chat_id}/messages")
async def list_messages(
chat_id: str,
authorization: str | None = Header(default=None),
limit: int = 50,
cursor: str | None = None,
):
actor = await require_bearer(authorization)
try:
chat_uuid = uuid.UUID(chat_id)
except ValueError:
raise bad_request("chat_id must be a UUID")
if not (1 <= limit <= 100):
raise bad_request("limit must be between 1 and 100")
after_created_at = after_id = None
if cursor:
payload = decode_cursor(cursor)
if payload.get("chat_id") != chat_id:
raise bad_request("cursor does not belong to this chat")
try:
after_created_at = datetime.fromisoformat(payload["created_at"])
after_id = uuid.UUID(payload["id"])
except (KeyError, ValueError):
raise bad_request("malformed cursor")
async with db.pool().acquire() as conn:
await _load_chat_if_member(conn, chat_uuid, actor["id"])
rows = await conn.fetch(
"""
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3))
ORDER BY created_at ASC, id ASC
LIMIT $4
""",
chat_uuid,
after_created_at,
after_id,
limit + 1,
)
has_more = len(rows) > limit
rows = rows[:limit]
items = []
for r in rows:
attached = await conn.fetch(
"SELECT attachment_id FROM message_attachments WHERE message_id=$1", r["id"]
)
items.append(message_json(r, [a["attachment_id"] for a in attached]))
next_cursor = None
if has_more:
last = rows[-1]
next_cursor = encode_cursor(
{"chat_id": chat_id, "created_at": iso(last["created_at"]), "id": str(last["id"])}
)
return {"items": items, "next_cursor": next_cursor}
# ---------------------------------------------------------------------------
# Attachments
# ---------------------------------------------------------------------------
@app.post("/api/v1/chats/{chat_id}/attachments", status_code=201)
async def upload_attachment(
chat_id: str,
request: Request,
filename: str,
authorization: str | None = Header(default=None),
):
actor = await require_bearer(authorization)
try:
chat_uuid = uuid.UUID(chat_id)
except ValueError:
raise bad_request("chat_id must be a UUID")
if not (1 <= len(filename) <= 255) or FILENAME_FORBIDDEN_RE.search(filename):
raise bad_request("filename must be 1-255 characters with no path separators or control characters")
content_type = request.headers.get("content-type", "")
if content_type not in ("image/png", "image/jpeg"):
raise unsupported_media_type("only image/png and image/jpeg are accepted")
# Enforce the byte budget BEFORE reading an unbounded body into memory.
data = bytearray()
async for chunk in request.stream():
data.extend(chunk)
if len(data) > MAX_UPLOAD_BYTES:
raise payload_too_large(f"upload exceeds {MAX_UPLOAD_BYTES} bytes")
data = bytes(data)
if not data:
raise bad_request("empty upload body")
try:
images.validate(data, content_type, MAX_IMAGE_PIXELS)
except images.InvalidImage as exc:
raise unsupported_media_type(str(exc))
digest = hashlib.sha256(data).hexdigest()
# Staging IS the final S3 prefix here (same bucket/prefix the worker later
# reads from) -- there is no separate move step, which is an honest
# simplification the contract explicitly allows when staging is already S3.
object_key = storage.new_object_key()
pool = db.pool()
async with pool.acquire() as conn:
await _load_chat_if_member(conn, chat_uuid, actor["id"])
try:
await storage.put_object(object_key, data, content_type)
except Exception: # noqa: BLE001
raise unavailable("object storage unavailable")
async with conn.transaction():
record = await conn.fetchrow(
"""
INSERT INTO attachments (chat_id, owner_id, filename, content_type, size_bytes, sha256, status, s3_key)
VALUES ($1, $2, $3, $4, $5, $6, 'queued', $7)
RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at
""",
chat_uuid, actor["id"], filename, content_type, len(data), digest, object_key,
)
job = await outbox.enqueue(conn, record["id"])
# Best-effort immediate publish; if this process dies right here, the
# outbox relay (any replica) still publishes it from the committed row.
await outbox.publish_best_effort(pool, job)
return JSONResponse(status_code=202, content=attachment_json(record))
async def _load_attachment_if_member(conn, attachment_id: uuid.UUID, user_id: uuid.UUID):
record = await conn.fetchrow(
"""
SELECT a.* FROM attachments a
JOIN chat_members cm ON cm.chat_id = a.chat_id
WHERE a.id = $1 AND cm.user_id = $2
""",
attachment_id, user_id,
)
if record is None:
raise not_found("attachment not found")
return record
@app.get("/api/v1/attachments/{attachment_id}")
async def get_attachment(attachment_id: str, authorization: str | None = Header(default=None)):
actor = await require_bearer(authorization)
try:
att_uuid = uuid.UUID(attachment_id)
except ValueError:
raise bad_request("attachment_id must be a UUID")
async with db.pool().acquire() as conn:
record = await _load_attachment_if_member(conn, att_uuid, actor["id"])
variant_rows = await conn.fetch(
"SELECT name, content_type, size_bytes, width, height, sha256 FROM attachment_variants WHERE attachment_id=$1",
att_uuid,
)
return attachment_json(record, variant_rows)
@app.get("/api/v1/attachments/{attachment_id}/content")
async def get_attachment_content(
attachment_id: str,
authorization: str | None = Header(default=None),
variant: str = "original",
):
actor = await require_bearer(authorization)
try:
att_uuid = uuid.UUID(attachment_id)
except ValueError:
raise bad_request("attachment_id must be a UUID")
if variant not in ("original", "thumbnail", "crop", "optimized"):
raise bad_request("unknown variant")
async with db.pool().acquire() as conn:
record = await _load_attachment_if_member(conn, att_uuid, actor["id"])
if record["status"] in ("queued", "processing"):
raise conflict("attachment is not ready yet")
if record["status"] == "failed":
raise conflict("attachment processing failed")
if variant == "original":
s3_key, content_type = record["s3_key"], record["content_type"]
else:
variant_row = await conn.fetchrow(
"SELECT s3_key, content_type FROM attachment_variants WHERE attachment_id=$1 AND name=$2",
att_uuid, variant,
)
if variant_row is None:
raise not_found(f"variant {variant} is not available for this attachment")
s3_key, content_type = variant_row["s3_key"], variant_row["content_type"]
try:
data = await storage.get_object(s3_key)
except Exception: # noqa: BLE001
raise unavailable("object storage unavailable")
return Response(content=data, media_type=content_type)
# ---------------------------------------------------------------------------
# Lifecycle
# ---------------------------------------------------------------------------
@app.on_event("startup")
async def on_startup():
database_url = _read_secret("DATABASE_URL_FILE", "DATABASE_URL")
cache_url = _read_secret("CACHE_URL_FILE", "CACHE_URL")
await db.init_pool(database_url)
sessions.init(cache_url)
storage.init(
endpoint=os.environ["S3_ENDPOINT"],
region=os.environ.get("S3_REGION", "us-east-1"),
access_key=_read_secret("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID"),
secret_key=_read_secret("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY"),
bucket=os.environ.get("S3_BUCKET", "messenger-attachments"),
)
await storage.ensure_bucket()
broker.init(cache_url)
await broker.ensure_group()
app.state.outbox_relay_task = asyncio.create_task(outbox.relay_loop(db.pool()))
@app.on_event("shutdown")
async def on_shutdown():
app.state.outbox_relay_task.cancel()
await db.close_pool()
await sessions.close()
def _read_secret(file_env: str, inline_env: str) -> str:
path = os.environ.get(file_env)
if path and os.path.isfile(path):
return open(path, encoding="utf-8").read().strip()
value = os.environ.get(inline_env)
if not value:
raise RuntimeError(f"{file_env} or {inline_env} must be set")
return value