Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions monitoring_lab/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
extra_hosts:
- "host.docker.internal:host-gateway"

grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
depends_on:
- prometheus
8 changes: 8 additions & 0 deletions monitoring_lab/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
lobal:
scrape_interval: 5s # How often to fetch metrics

scrape_configs:
- job_name: 'MLPA_local'
metrics_path: '/metrics'
static_configs:
- targets: ['host.docker.internal:8080']
14 changes: 14 additions & 0 deletions src/mlpa/core/auth/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from mlpa.core.classes import AuthorizedChatRequest, ChatRequest, ServiceType
from mlpa.core.config import env
from mlpa.core.logger import logger
from mlpa.core.prometheus_metrics import metrics
from mlpa.core.routers.appattest import app_attest_auth
from mlpa.core.routers.fxa import fxa_auth
from mlpa.core.utils import parse_app_attest_jwt
Expand All @@ -16,13 +18,19 @@ async def authorize_request(
use_app_attest: Annotated[bool | None, Header()] = None,
use_qa_certificates: Annotated[bool | None, Header()] = None,
) -> AuthorizedChatRequest:
metrics.auth_request_count_total.inc()
if not authorization:
metrics.auth_error_count_total.labels(error=f"MalformedRequest")
raise HTTPException(status_code=401, detail="Missing authorization header")
if use_app_attest:
assertionAuth = parse_app_attest_jwt(authorization, "assert")
data = await app_attest_auth(assertionAuth, chat_request, use_qa_certificates)
if data:
if data.get("error"):
metrics.auth_error_count_total.labels(error=f"AppattestUnauthorized")
logger.error(
"Unauthorized", extra={"error": data["error"], "type": "appattest"}
)
raise HTTPException(status_code=401, detail=data["error"])
return AuthorizedChatRequest(
user=f"{assertionAuth.key_id_b64}:{service_type.value}", # "user" is key_id_b64 from app attest
Expand All @@ -33,11 +41,17 @@ async def authorize_request(
fxa_user_id = fxa_auth(authorization)
if fxa_user_id:
if fxa_user_id.get("error"):
metrics.auth_error_count_total.labels(error=f"FxAUnauthorized")
logger.error(
"Unauthorized", extra={"error": fxa_user_id["error"], "type": "FxA"}
)
raise HTTPException(status_code=401, detail=fxa_user_id["error"])
return AuthorizedChatRequest(
user=f"{fxa_user_id['user']}:{service_type.value}",
**chat_request.model_dump(exclude_unset=True),
)
metrics.auth_error_count_total.labels(error=f"Cancelled")
logger.error("Auth Cancelled", extra={"error": "Cancelled", "type": "None"})
raise HTTPException(
status_code=401, detail="Please authenticate with App Attest or FxA."
)
Loading