Camera permission and review fixes (openpanel C3, C4)

C4: a device's `viewers` (superusers by default, or members) covers the
image and video messages its ports publish. Anyone else gets none of their
values on the socket, in the catalogue or a flow's state, 403 for their
history, for the frames they hold (the last 8 included) and for TURN
credentials, no bytes when asking for them, and `bye: forbidden` for a live
view. A device credential stays bounded by its dashboards. The device page
sets it in its Camera section, and a tile the viewer may not see says
"No access to this camera" and asks the camera for nothing.

C3:
- A volatile upload is held to MAX_FRAME_BYTES (4 MiB, 413 above it, read
  without extra copies), and so is a frame on a device's socket.
- A frame the engine holds in memory is written to the ring directory
  before a local worker is handed it, so load_artifact finds it.
- A socket the bus dropped events for catches up with a snapshot, demand
  included, and a device's credential is checked again per frame, cached by
  the flow store revision and its nonce, so a lost event never keeps an
  unpaired device's frames coming.
- A report is at most 64 KB once its named config_schemas are put in place.
- A device away for 60 s has its true bool messages set to false.
- Refused frames are logged by fixed keys, a frame is named after its
  message, `fluksio serve` waits 5 s for open connections, a tile with a
  stream URL creates no demand, the 8-name cap keeps the newest names.
- Every container's log rotates (json-file, 3 x 10 MB).
This commit is contained in:
Melvin Strobl
2026-10-17 09:20:04 +02:00
parent 6a98dcd23f
commit 72c1d73e96
36 changed files with 1268 additions and 152 deletions
+182 -60
View File
@@ -23,14 +23,17 @@ from pydantic import BaseModel
from sqlmodel import Session, col, select
from fluksio.api.deps import (
CAMERA_IS_SUPERUSERS,
CODE_IS_SUPERUSERS,
CurrentUser,
FlowControllerDep,
HiddenDep,
SessionDep,
TokenDep,
decode_token,
get_code_author,
get_current_user,
hidden_from,
user_from_token,
)
from fluksio.api.routes.messages import port_source
@@ -279,10 +282,15 @@ def _require_enabled(controller: FlowController, name: str) -> None:
)
def _flow_state(controller: FlowController, name: str) -> FlowStatePublic:
def _flow_state(
controller: FlowController, name: str, hidden: frozenset[str]
) -> FlowStatePublic:
"""The flow's values, but those of a camera the caller may not see."""
return FlowStatePublic(
values={
key: MessageValue(**value) for key, value in controller.values(name).items()
key: MessageValue(**value)
for key, value in controller.values(name).items()
if key not in hidden
},
nodes=controller.node_statuses(name),
)
@@ -892,6 +900,7 @@ async def run_flow(
request: Request,
controller: FlowControllerDep,
user: CurrentUser,
hidden: HiddenDep,
) -> Any:
"""Run every node of a flow once.
@@ -919,14 +928,14 @@ async def run_flow(
)
except RunRejected as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
return _flow_state(controller, name)
return _flow_state(controller, name, hidden)
inputs = {qualify(name, key): value for key, value in body.inputs.items()}
if controller.store.has_draft(name):
await run_in_threadpool(controller.run_preview, name, inputs)
else:
await run_in_threadpool(controller.run_flow, name, inputs)
return _flow_state(controller, name)
return _flow_state(controller, name, hidden)
@router.post("/{name}/nodes/{node_id}/trigger", response_model=FlowStatePublic)
@@ -935,6 +944,7 @@ async def trigger_node(
node_id: str,
body: TriggerRequest,
controller: FlowControllerDep,
hidden: HiddenDep,
) -> Any:
"""Feed values into a single node."""
_require_enabled(controller, name)
@@ -950,7 +960,7 @@ async def trigger_node(
# The canvas already has the failure from the bus; the person who
# clicked gets to read it too, instead of a stack trace in the log.
raise HTTPException(status_code=400, detail=error)
return _flow_state(controller, name)
return _flow_state(controller, name, hidden)
@router.post("/{name}/nodes/{node_id}/cancel", response_model=Message)
@@ -995,10 +1005,10 @@ def acknowledge_node_error(
@router.get("/{name}/state", response_model=FlowStatePublic)
def read_flow_state(name: str, controller: FlowControllerDep) -> Any:
def read_flow_state(name: str, controller: FlowControllerDep, hidden: HiddenDep) -> Any:
"""The last value seen on every message of this flow."""
_read_flow(controller, name)
return _flow_state(controller, name)
return _flow_state(controller, name, hidden)
@router.get("/{name}/history/{message}", response_model=MessageHistory)
@@ -1006,6 +1016,7 @@ def read_message_history(
name: str,
message: str,
controller: FlowControllerDep,
hidden: HiddenDep,
) -> Any:
"""The recent values of one message, for plotting.
@@ -1014,6 +1025,8 @@ def read_message_history(
"""
_read_flow(controller, name)
key = qualify(name, message)
if key in hidden:
raise HTTPException(status_code=403, detail=CAMERA_IS_SUPERUSERS)
return MessageHistory(
message=key,
numeric=as_number(controller.state.get(key)) is not None,
@@ -1034,6 +1047,7 @@ def snapshot_payload(
only: set[str] | None = None,
device: str = "",
uplink: str = "",
hidden: frozenset[str] = frozenset(),
) -> dict[str, Any]:
"""Everything a client needs to catch up, sent the moment it connects.
@@ -1052,6 +1066,8 @@ def snapshot_payload(
A device's socket also says which of its ports have ``demand`` now, and
which ``uplink`` its frames take: ``binary`` on its own socket, ``http``
through the portal, whose tunnel carries text only.
``hidden`` are the camera messages a person may not see, left out.
"""
if only is not None:
# The same keys, emptied rather than dropped: a screen already hanging
@@ -1071,9 +1087,12 @@ def snapshot_payload(
payload["demand"] = demand.ports(device)
payload["uplink"] = uplink
return payload
values = controller.values()
return {
"type": "snapshot",
"values": controller.values(),
"values": (
{k: v for k, v in values.items() if k not in hidden} if hidden else values
),
"nodes": [s.model_dump() for s in controller.node_statuses()],
"issues": [i.model_dump() for i in controller.issues],
"paused": controller.paused_flows(),
@@ -1096,6 +1115,41 @@ def device_of(token: str, app: Any) -> str:
return ""
class Credential:
"""Whether a device socket's credential still stands, asked per frame.
Checked in full only once what it is checked against has moved: the
device files, which a device's save, deletion or a revert commits to the
flow store, and the nonces in ``devices.json``, which an unpair bumps.
Otherwise a frame costs a stat of that file.
"""
def __init__(self, token: str, app: Any, device_id: str) -> None:
self.token = token
self.app = app
self.device = device_id
self._seen: tuple[int, int] | None = None
def _key(self) -> tuple[int, int] | None:
devices: DeviceStore | None = getattr(self.app.state, "device_store", None)
if devices is None:
return None
try:
return devices.flows.revision, devices.nonce(self.device)
except (OSError, ValueError):
return None
async def revoked(self) -> bool:
key = self._key()
if key is not None and key == self._seen:
return False
if await run_in_threadpool(revoked, self.token, self.app):
return True
# The key read before the check, so a change meanwhile is checked.
self._seen = key
return False
def revoked(token: str, app: Any) -> bool:
"""Whether a device's credential has stopped standing since its handshake.
@@ -1155,6 +1209,21 @@ def event_for_panel(event: dict[str, Any], only: set[str]) -> bool:
return event.get("type") == "message_value" and str(event.get("name") or "") in only
def carried(
event: dict[str, Any], only: set[str] | None, hidden: frozenset[str]
) -> bool:
"""Whether a socket carries this event on the bus.
A panel's what it draws (``event_for_panel``), a person's everything but
the values of the cameras ``hidden`` from them.
"""
if only is not None:
return event_for_panel(event, only)
return not (
hidden and event.get("type") == "message_value" and event.get("name") in hidden
)
# How many events one frame may carry. A ceiling rather than a target: the
# batch is whatever the bus happens to hold, and a client that has been away
# should not be handed the whole queue in one message.
@@ -1166,14 +1235,19 @@ MAX_FRAME_EVENTS = 64
MAX_MEDIA_NAMES = 8
def wanted_names(frame: str, only: set[str] | None) -> set[str] | None:
def wanted_names(
frame: str, only: set[str] | None, hidden: frozenset[str] = frozenset()
) -> set[str] | None:
"""What this client is asking to be sent bytes for, or None if it said
something else.
A client asks by name — the tiles it is drawing — and is answered only for
the names a panel credential would have been allowed anyway. Bytes are the
expensive thing on this socket, so nothing is pushed until something says
it is looking at it.
the names a panel credential would have been allowed anyway, and none of
the ``hidden`` cameras. Bytes are the expensive thing on this socket, so
nothing is pushed until something says it is looking at it.
Past the cap the names it gave last are kept: a client lists the tile that
came on screen most recently last, so one scrolled back to keeps working.
"""
try:
payload = orjson.loads(frame)
@@ -1186,15 +1260,16 @@ def wanted_names(frame: str, only: set[str] | None) -> set[str] | None:
return set()
asked = {str(name) for name in names}
if len(asked) > MAX_MEDIA_NAMES:
kept = list(dict.fromkeys(str(name) for name in names))[:MAX_MEDIA_NAMES]
kept = list(dict.fromkeys(str(name) for name in names))[-MAX_MEDIA_NAMES:]
logger.warning(
"A socket asked for live media on %d messages; only %d are served, "
"the first it named: %s",
"the last it named: %s",
len(asked),
MAX_MEDIA_NAMES,
", ".join(kept),
)
asked = set(kept)
asked -= hidden
return asked if only is None else asked & only
@@ -1271,28 +1346,47 @@ def parse_frame(data: bytes) -> tuple[dict[str, Any], bytes] | None:
return header, data[4 + length :]
#: Why a device's frame was dropped, by the key it is logged under once per
#: socket. Fixed, so nothing the device chose reaches the log.
FRAME_REFUSALS = {
"not_a_frame": "not a frame",
"not_a_device": "only a device sends frames",
"no_engine": "the engine is not running",
"not_wired": "it names no message one of the device's ports publishes",
"not_image": "the port it names is not wired as an image",
"media_type": "its media type is not an image type",
"too_large": "it is larger than MAX_FRAME_BYTES",
"undeclared": "no flow declares its message",
"refused": "its message refused the value",
}
def ingest_frame(app: Any, device: DeviceDef, data: bytes) -> str:
"""Store a frame a device sent on its socket and publish it.
Returns why it was refused, or "" once it is published.
Returns the key in ``FRAME_REFUSALS`` it was refused for, or "" once it
is published.
The same frame over HTTP is two requests, each authenticated: the upload,
then the publish. Here the socket was authenticated once, at its
handshake, and the frame is held and published in one step. Only a
message one of the device's own ``in`` ports is wired to, as an ``image``,
and the value is credited to the device node holding that port, as an
HTTP publish is.
HTTP publish is. The frame is named after that message.
"""
cap = settings.MAX_FRAME_BYTES
if cap and len(data) > cap:
return "too_large"
parsed = parse_frame(data)
if parsed is None or parsed[0].get("type") != "frame":
return "not a frame"
return "not_a_frame"
header, payload = parsed
name = str(header.get("name") or "")
devices: DeviceStore | None = getattr(app.state, "device_store", None)
store: Any = getattr(app.state, "artifact_store", None)
controller: FlowController | None = getattr(app.state, "flow_controller", None)
if devices is None or store is None or controller is None:
return "the engine is not running"
return "no_engine"
binding = next(
(
b
@@ -1302,24 +1396,19 @@ def ingest_frame(app: Any, device: DeviceDef, data: bytes) -> str:
None,
)
if binding is None:
return f"{name!r} is not a message one of its ports publishes"
return "not_wired"
if binding.dtype != "image":
return f"its port {binding.port!r} is wired as {binding.dtype}, not image"
return "not_image"
media_type = str(header.get("media_type") or "")
if not _IMAGE_TYPE.match(media_type):
return f"{media_type!r} is not an image type"
cap = settings.MAX_ARTIFACT_BYTES
if cap and len(payload) > cap:
return f"a frame may be at most {cap} bytes"
reference = store.put(
[payload], name=binding.port, media_type=media_type, volatile=True
)
return "media_type"
reference = store.put([payload], name=name, media_type=media_type, volatile=True)
try:
controller.publish_message(name, reference, port_source(devices, device, name))
except KeyError:
return f"no flow declares {name!r}"
except TypeError as exc:
return str(exc)
return "undeclared"
except TypeError:
return "refused"
return ""
@@ -1407,6 +1496,11 @@ async def flow_events(websocket: WebSocket, ticket: str = "", token: str = "") -
viewer = object()
# Each reason a frame was refused for, logged once per socket.
refused: set[str] = set()
# The cameras a person may not see. Looked at again with each batch, so
# a device switched to members, or back, takes effect on open sockets.
hidden = hidden_from(user, websocket.app, device=only is not None)
# A device's credential, checked again per frame once it may have moved.
credential = Credential(token, websocket.app, device) if device else None
await websocket.accept()
@@ -1420,11 +1514,36 @@ async def flow_events(websocket: WebSocket, ticket: str = "", token: str = "") -
if controller is None:
return []
payload = await run_in_threadpool(
snapshot_payload, controller, only, device, "binary"
snapshot_payload, controller, only, device, "binary", hidden
)
await websocket.send_text(orjson.dumps(payload).decode())
return list(payload.get("demand", []))
async def catch_up() -> bool:
"""Rescope a panel and send the snapshot again, demand included.
False once a device whose credential stopped standing is closed with
1008, which sends it to check: otherwise its frames would still be
taken here.
"""
nonlocal only, device_def, wanted
if only is not None:
if device and await run_in_threadpool(revoked, token, websocket.app):
await websocket.close(code=1008)
return False
# The scope was resolved once, at the handshake. A panel pointed
# at another dashboard would otherwise fetch the new document and
# then draw tiles nothing ever updates. ``or set()`` because a
# panel that was deleted resolves to None, the same as a person's
# token — and that would widen this socket to everything on the
# bus.
only = panel_scope(token, websocket.app) or set()
device_def = _device_def(websocket.app, device)
wanted &= only
demand.want(viewer, wanted)
await send_snapshot()
return True
told = await send_snapshot()
async with event_bus.subscribe() as queue:
@@ -1477,23 +1596,27 @@ async def flow_events(websocket: WebSocket, ticket: str = "", token: str = "") -
if isinstance(frame, bytes):
# A camera's frame. Refused ones are dropped and the
# socket stays open: one bad frame is not a reason to
# take the device's live values away.
# take the device's live values away. A device that
# was unpaired meanwhile is closed instead.
if credential is not None and await credential.revoked():
await websocket.close(code=1008)
return
reason = (
await run_in_threadpool(
ingest_frame, websocket.app, device_def, frame
)
if device_def is not None
else "only a device sends frames"
else "not_a_device"
)
if reason and reason not in refused:
refused.add(reason)
logger.warning(
"Dropped a frame from %s: %s",
device or "a person's socket",
reason,
FRAME_REFUSALS[reason],
)
else:
asked = wanted_names(frame, only)
asked = wanted_names(frame, only, hidden)
if asked is not None:
wanted = asked
demand.want(viewer, wanted)
@@ -1506,6 +1629,7 @@ async def flow_events(websocket: WebSocket, ticket: str = "", token: str = "") -
devices=getattr(
websocket.app.state, "device_store", None
),
superuser=user.is_superuser,
)
receiver = asyncio.create_task(_receive(websocket))
if sender not in done:
@@ -1523,48 +1647,46 @@ async def flow_events(websocket: WebSocket, ticket: str = "", token: str = "") -
except asyncio.QueueEmpty:
break
# A panel this far behind lost events the bus dropped for it:
# its demand, a dashboard published or its own unpairing. It
# catches up once this batch is out, as after a dashboard.
resync = only is not None and event_bus.dropped(queue)
if only is None:
fresh = hidden_from(user, websocket.app)
if fresh != hidden:
# A camera was opened or closed to members: the
# snapshot gives back what may now be seen.
hidden = fresh
wanted -= hidden
demand.want(viewer, wanted)
resync = True
out: list[dict[str, Any]] = []
for event in batch:
if event.get("type") == "device_demand":
kind = event.get("type")
if kind == "device_demand":
# For the device it names, and for nobody else.
if device and event.get("device") == device:
await websocket.send_text(
demand_frame(list(event.get("ports") or []))
)
continue
if only is not None and event.get("type") == "dashboard_changed":
# An unpaired or deleted device is closed with 1008,
# which sends it to check its credential: otherwise
# its frames would still be taken here.
if device and await run_in_threadpool(
revoked, token, websocket.app
):
await websocket.close(code=1008)
return
# The scope was resolved once, at the handshake. A
# panel pointed at another dashboard would otherwise
# fetch the new document and then draw tiles nothing
# ever updates. ``or set()`` because a panel that was
# deleted resolves to None, the same as a person's
# token — and that would widen this socket to
# everything on the bus.
only = panel_scope(token, websocket.app) or set()
device_def = _device_def(websocket.app, device)
wanted &= only
demand.want(viewer, wanted)
if only is not None and kind == "dashboard_changed":
if out:
await _send(websocket, out)
out = []
await send_snapshot()
if only is not None and not event_for_panel(event, only):
continue
out.append(event)
if not await catch_up():
return
if carried(event, only, hidden):
out.append(event)
if out:
# The bytes first: a tile that has the frame when the
# value lands draws it in one pass rather than two.
for frame in media_frames(out, wanted, store):
await websocket.send_bytes(frame)
await _send(websocket, out)
if resync and not await catch_up():
return
except (WebSocketDisconnect, RuntimeError):
# A peer that goes away mid-send takes the RuntimeError route
# ("websocket.send after websocket.close") rather than the clean