feat(serve): log CDM calls for the dashboard

Add GET /api/dashboard/cdm-logs: one record per pywidevine/pyplayready call plus open sessions per device, with no key or licence material.
This commit is contained in:
imSp4rky
2026-10-06 14:58:08 -06:00
parent 94e5264396
commit eea4b41b70
6 changed files with 689 additions and 28 deletions
+17 -2
View File
@@ -25,7 +25,14 @@ from unshackle.core.api.handlers import (
server_cdm_max_height_error,
validate_server_accounts,
)
from unshackle.core.api.stats import key_rate_limit, rate_limit_error, ring, stats, stats_middleware
from unshackle.core.api.stats import (
cdm_log_middleware,
key_rate_limit,
rate_limit_error,
ring,
stats,
stats_middleware,
)
from unshackle.core.config import config
from unshackle.core.console import console
from unshackle.core.constants import context_settings
@@ -424,7 +431,13 @@ def serve(
if no_key:
app = web.Application(
middlewares=[cors_middleware, stats_middleware, dashboard_authentication, compression_middleware]
middlewares=[
cors_middleware,
stats_middleware,
cdm_log_middleware,
dashboard_authentication,
compression_middleware,
]
)
else:
serve_auth = create_serve_authentication(serve_playready and bool(prd_devices))
@@ -432,6 +445,7 @@ def serve(
middlewares=[
cors_middleware,
stats_middleware,
cdm_log_middleware,
dashboard_authentication,
serve_auth,
compression_middleware,
@@ -495,6 +509,7 @@ def serve(
app.router.add_route("*", "/playready", playready_ping)
app.add_subapp("/playready", playready_app)
app["playready_app"] = playready_app
log.info(f"PlayReady CDM endpoints available at http://{host}:{port}/playready/")
elif serve_playready:
log.info("No PlayReady devices found, skipping PlayReady CDM endpoints")
+22 -8
View File
@@ -2342,19 +2342,36 @@ def _poll_cursor(before: int, items: List[Dict[str, Any]]) -> int:
return max(before, items[-1]["seq"]) if items else before
def _since(request: web.Request) -> int:
"""The ``since`` cursor of a poll; 0 (everything) when it is missing or not a number."""
try:
return int(request.query.get("since", 0))
except ValueError:
return 0
async def dashboard_logs_handler(request: web.Request) -> web.Response:
"""Recent log records; `since` (seq) and `level` filter the ring buffer."""
from unshackle.core.api.stats import ring
try:
since = int(request.query.get("since", 0))
except ValueError:
since = 0
since = _since(request)
before = ring.seq
records = ring.since(since, request.query.get("level"), request.query.get("logger"))
return web.json_response({"seq": _poll_cursor(before, records), "records": records})
async def dashboard_cdm_logs_handler(request: web.Request) -> web.Response:
"""CDM calls after `since` (seq), plus a snapshot of the open sessions of each live Cdm."""
from unshackle.core.api.stats import cdm_calls, cdm_sessions
since = _since(request)
before = cdm_calls.seq
records = cdm_calls.since(since)
return web.json_response(
{"seq": _poll_cursor(before, records), "records": records, "sessions": cdm_sessions(request.app)}
)
async def dashboard_session_logs_handler(request: web.Request) -> web.Response:
"""One remote session's mirrored service log, for an operator watching a failing auth.
@@ -2368,10 +2385,7 @@ async def dashboard_session_logs_handler(request: web.Request) -> web.Response:
session = get_session_store().peek(session_id)
if session is None:
raise APIError(APIErrorCode.SESSION_NOT_FOUND, f"Remote session not found: {session_id}")
try:
since = int(request.query.get("since", 0))
except ValueError:
since = 0
since = _since(request)
buffer = session.log_buffer
before = buffer.last_seq if buffer else 0
records = buffer.since(since) if buffer else []
+87
View File
@@ -21,6 +21,7 @@ from unshackle.core.api.handlers import (
clear_cache_handler,
clear_finished_download_jobs_handler,
clear_temp_handler,
dashboard_cdm_logs_handler,
dashboard_events_handler,
dashboard_health_handler,
dashboard_jobs_handler,
@@ -2644,6 +2645,91 @@ async def dashboard_logs(request: web.Request) -> web.Response:
return await dashboard_logs_handler(request)
@api_handler
async def dashboard_cdm_logs(request: web.Request) -> web.Response:
"""
Dashboard: recent CDM calls and open CDM sessions.
---
summary: Dashboard CDM call log
description: >
The last 5000 calls to the Widevine and PlayReady CDM device routes, one record for each
call, with a snapshot of the open sessions of each live Cdm. A record never holds an API
key, `init_data`, a challenge, a certificate, a licence body or a content key.
Poll with `since` set to the `seq` of the last reply. The server does not publish these
records on the event stream.
tags: [Dashboard]
parameters:
- name: since
in: query
required: false
schema:
type: integer
description: Return only records with seq greater than this value
responses:
'200':
description: Records plus the current seq and the open sessions
content:
application/json:
schema:
type: object
properties:
seq:
type: integer
records:
type: array
items:
type: object
properties:
seq:
type: integer
ts:
type: number
drm:
type: string
enum: [widevine, playready]
op:
type: string
device:
type: string
key_id:
type: string
user:
type: string
session_id:
type: string
nullable: true
status:
type: integer
ok:
type: boolean
error:
type: string
nullable: true
ms:
type: number
sessions:
type: array
items:
type: object
properties:
drm:
type: string
device:
type: string
key_id:
type: string
user:
type: string
open:
type: integer
max:
type: integer
'401':
description: Dashboard key missing or invalid
"""
return await dashboard_cdm_logs_handler(request)
@api_handler
async def dashboard_session_logs(request: web.Request) -> web.Response:
"""
@@ -2986,6 +3072,7 @@ DASHBOARD_ROUTES: list[tuple[str, str, Handler, bool]] = [
("GET", DASHBOARD_PREFIX + "sessions", dashboard_sessions, True),
("GET", DASHBOARD_PREFIX + "jobs", dashboard_jobs, True),
("GET", DASHBOARD_PREFIX + "logs", dashboard_logs, True),
("GET", DASHBOARD_PREFIX + "cdm-logs", dashboard_cdm_logs, True),
("GET", DASHBOARD_PREFIX + "keys", dashboard_keys, True),
("GET", DASHBOARD_PREFIX + "services", dashboard_services, True),
("GET", DASHBOARD_PREFIX + "health", dashboard_health, True),
+215 -18
View File
@@ -2,12 +2,15 @@
from __future__ import annotations
import gzip
import hashlib
import json
import logging
import time
from collections import Counter, deque
from dataclasses import dataclass, field
from typing import Any, Deque, Dict, List, Optional
from functools import lru_cache
from typing import Any, Deque, Dict, List, Optional, Tuple
from aiohttp import web
@@ -17,6 +20,20 @@ from unshackle.core.config import config
from unshackle.core.utils.redact import redact_secrets
RATE_LIMIT_WINDOW = 3600.0
CDM_OPS = frozenset(
{
"open",
"close",
"set_service_certificate",
"get_service_certificate",
"get_license_challenge",
"parse_license",
"get_keys",
}
)
CDM_ERROR_LIMIT = 300
CDM_SESSION_ID_LIMIT = 64
CDM_DEVICE_LIMIT = 255
def _users() -> Dict[str, Any]:
@@ -215,40 +232,220 @@ async def stats_middleware(request: web.Request, handler: Any) -> web.StreamResp
return response
class RingLogHandler(logging.Handler):
"""Keep the last N log records in memory and publish each one on the event bus."""
class SeqRing:
"""Keep the last N records in memory, each with a ``seq`` one above the record before it."""
def __init__(self, maxlen: int = 1000) -> None:
super().__init__()
def __init__(self, maxlen: int) -> None:
self.records: Deque[Dict[str, Any]] = deque(maxlen=maxlen)
self.seq = 0
def emit(self, record: logging.LogRecord) -> None:
"""Buffer and publish one record; ``seq`` moves only after the append, as in ``EventBus``."""
def append(self, record: Dict[str, Any]) -> Dict[str, Any]:
"""Buffer one record; ``seq`` moves only after the append, as in ``EventBus``."""
seq = self.seq + 1
item = {
"seq": seq,
"ts": record.created,
"level": record.levelname,
"logger": record.name,
"msg": redact_secrets(self.format(record)),
}
item = {"seq": seq, **record}
self.records.append(item)
self.seq = seq
return item
def since(self, seq: int = 0) -> List[Dict[str, Any]]:
"""The records after ``seq``, oldest first, from a copy: ``append()`` can run on a worker thread."""
return [r for r in self.records.copy() if r["seq"] > seq]
class RingLogHandler(SeqRing, logging.Handler):
"""Keep the last N log records in memory and publish each one on the event bus."""
def __init__(self, maxlen: int = 1000) -> None:
logging.Handler.__init__(self)
SeqRing.__init__(self, maxlen)
def emit(self, record: logging.LogRecord) -> None:
"""Buffer and publish one record."""
item = self.append(
{
"ts": record.created,
"level": record.levelname,
"logger": record.name,
"msg": redact_secrets(self.format(record)),
}
)
bus.publish("log", item)
def since(self, seq: int = 0, level: Optional[str] = None, logger: Optional[str] = None) -> List[Dict[str, Any]]:
"""Filter a copy of the records: ``emit()`` appends from worker threads."""
"""The records after ``seq`` at or above ``level``, from ``logger`` or one of its children."""
min_level = logging.getLevelName(level.upper()) if level else 0
if not isinstance(min_level, int):
min_level = 0
return [
r
for r in self.records.copy()
if r["seq"] > seq
and logging.getLevelName(r["level"]) >= min_level
for r in super().since(seq)
if logging.getLevelName(r["level"]) >= min_level
and (not logger or r["logger"] == logger or r["logger"].startswith(logger + "."))
]
ring = RingLogHandler()
cdm_calls = SeqRing(maxlen=5000)
@lru_cache(maxsize=None)
def _cdm_op(pattern: str) -> Optional[Tuple[str, str]]:
"""The ``(drm, op)`` of a route pattern, else None. Cached: the route table does not change."""
parts = pattern.strip("/").split("/")
drm = "widevine"
if parts[:1] == ["playready"]:
drm, parts = "playready", parts[1:]
if len(parts) < 2 or parts[0] != "{device}" or parts[1] not in CDM_OPS:
return None
return drm, parts[1]
def _cdm_route(request: web.Request) -> Optional[Tuple[str, str]]:
"""The ``(drm, op)`` of a CDM device route, else None.
Reads the matched route pattern, not the path, so a device name cannot pass for an operation
or for the ``/playready`` prefix.
"""
resource = request.match_info.route.resource
return _cdm_op(resource.canonical) if resource is not None else None
def _hex_id(value: Any) -> Optional[str]:
"""``value`` in lowercase when it is a hexadecimal string of a plausible length, else None."""
if not isinstance(value, str) or not value or len(value) > CDM_SESSION_ID_LIMIT:
return None
try:
bytes.fromhex(value)
except ValueError:
return None
return value.lower()
def _key_fields(secret_key: Optional[str]) -> Dict[str, str]:
"""The ``key_id`` and ``user`` of an API key: the ``id`` and ``label`` of its ``stats.keys`` entry."""
key = configured_key(secret_key)
return {"key_id": key_id(key), "user": mask_key(key)}
def _reply_json(response: Any) -> Dict[str, Any]:
"""The JSON object a handler answered, or ``{}``. ``compression_middleware`` runs inside the
CDM log middleware, so the body can arrive as gzip."""
try:
body = response.body
if response.headers.get("Content-Encoding") == "gzip":
body = gzip.decompress(body)
data = json.loads(body)
except Exception:
return {}
return data if isinstance(data, dict) else {}
async def _request_session_id(request: web.Request) -> Any:
"""The ``session_id`` of the JSON request body, when the handler consumed it: aiohttp caches that read.
A body nobody read belongs to a call refused before the handler, a 401 or a 429. It stays
unread: reading it would spend time and memory on an unauthenticated caller, and wait on a
body that never finishes arriving.
"""
if not request.content.at_eof():
return None
try:
body = await request.json()
except Exception:
return None
return body.get("session_id") if isinstance(body, dict) else None
async def _cdm_record(request: web.Request, drm: str, op: str, outcome: Any) -> Dict[str, Any]:
"""The record of one call. ``outcome`` is the response, or the exception the handler raised.
An exception that is not an HTTP one gives its class name only: its text can hold the
presented API key. The reply body is read only for a refusal and for the session id of an
``open``, so a challenge, a licence or a content key never passes through here.
"""
from unshackle.core.api.handlers import request_secret_key
reply: Dict[str, Any] = {}
if isinstance(outcome, web.StreamResponse):
status = outcome.status
if status >= 400 or op == "open":
reply = _reply_json(outcome)
message = reply.get("message")
error = message if isinstance(message, str) else outcome.reason
else:
status, error = 500, type(outcome).__name__
ok = status < 400
if op == "open":
data = reply.get("data") if ok else None
session_id = data.get("session_id") if isinstance(data, dict) else None
elif op == "close":
session_id = request.match_info.get("session_id")
else:
session_id = await _request_session_id(request)
return {
"drm": drm,
"op": op,
"device": request.match_info["device"][:CDM_DEVICE_LIMIT],
**_key_fields(request_secret_key(request)),
"session_id": _hex_id(session_id),
"status": status,
"ok": ok,
"error": None if ok else str(error)[:CDM_ERROR_LIMIT],
}
async def _record_cdm_call(
request: web.Request, route: Tuple[str, str], ts: float, started: float, outcome: Any
) -> None:
"""Append one call to ``cdm_calls``. Logs a failure and does not raise it: it must never fail the CDM call."""
ms = round((time.perf_counter() - started) * 1000, 1)
try:
cdm_calls.append({"ts": ts, **await _cdm_record(request, *route, outcome), "ms": ms})
except Exception:
logging.getLogger("serve").debug("CDM call not recorded", exc_info=True)
@web.middleware
async def cdm_log_middleware(request: web.Request, handler: Any) -> web.StreamResponse:
"""Record each call to a CDM device route in ``cdm_calls``.
Must run outside the serve authentication middleware, so the log holds a 401 too.
"""
route = _cdm_route(request)
if route is None:
return await handler(request)
ts, started = time.time(), time.perf_counter()
try:
response = await handler(request)
except Exception as e:
await _record_cdm_call(request, route, ts, started, e)
raise
await _record_cdm_call(request, route, ts, started, response)
return response
def cdm_sessions(app: web.Application) -> List[Dict[str, Any]]:
"""One row per live Cdm object: its open session count and the most it allows.
Both CDM libraries refuse an ``open`` only above ``MAX_NUM_OF_SESSIONS``, so the true limit
is one more. A Cdm with a ``SESSION_TIMEOUT`` drops older sessions on its next ``open``,
so those do not count as open.
"""
now = time.time()
rows: List[Dict[str, Any]] = []
for drm, owner in (("widevine", app), ("playready", app.get("playready_app"))):
cdms = owner.get("cdms") if owner is not None else None
for (secret_key, device), cdm in (cdms or {}).items():
sessions = getattr(cdm, "_Cdm__sessions", {}).values()
timeout = getattr(cdm, "SESSION_TIMEOUT", None)
rows.append(
{
"drm": drm,
"device": device,
**_key_fields(secret_key),
"open": len(sessions) if timeout is None else sum(now - s.opened_at <= timeout for s in sessions),
"max": cdm.MAX_NUM_OF_SESSIONS + 1,
}
)
return rows