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:
@@ -177,10 +177,12 @@ def _device_digests(device: DeviceDef, request: Request) -> set[str]:
|
||||
controller: FlowController | None = getattr(
|
||||
request.app.state, "flow_controller", None
|
||||
)
|
||||
if controller is None:
|
||||
return set()
|
||||
names = _device_messages(device, request)
|
||||
if not names:
|
||||
return digests_of(controller, _device_messages(device, request))
|
||||
|
||||
|
||||
def digests_of(controller: FlowController | None, names: set[str]) -> set[str]:
|
||||
"""The artifacts these messages point at now, and the last few before."""
|
||||
if controller is None or not names:
|
||||
return set()
|
||||
found: set[str] = set()
|
||||
for value in controller.state.get_present(sorted(names)).values():
|
||||
@@ -191,6 +193,19 @@ def _device_digests(device: DeviceDef, request: Request) -> set[str]:
|
||||
return found
|
||||
|
||||
|
||||
def hidden_from(user: User, app: Any, *, device: bool = False) -> frozenset[str]:
|
||||
"""The camera messages ``user`` may not see (``DeviceDef.viewers``).
|
||||
|
||||
None for a superuser, and none for a device credential, which its
|
||||
dashboards bound instead: a superuser assigned them, camera tiles
|
||||
included. ``device`` says the caller is one.
|
||||
"""
|
||||
if device or user.is_superuser:
|
||||
return frozenset()
|
||||
store: DeviceStore | None = getattr(app.state, "device_store", None)
|
||||
return store.restricted() if store is not None else frozenset()
|
||||
|
||||
|
||||
def _device_may(payload: dict[str, Any], request: Request) -> None:
|
||||
"""Refuse anything a wall panel has no business asking for.
|
||||
|
||||
@@ -520,6 +535,18 @@ def get_current_user(request: Request, session: SessionDep, token: TokenDep) ->
|
||||
CurrentUser = Annotated[User, Depends(get_current_user)]
|
||||
|
||||
|
||||
def get_hidden(request: Request, user: CurrentUser) -> frozenset[str]:
|
||||
"""The camera messages the caller of this request may not see."""
|
||||
device = getattr(request.state, "device", None) is not None
|
||||
return hidden_from(user, request.app, device=device)
|
||||
|
||||
|
||||
HiddenDep = Annotated[frozenset[str], Depends(get_hidden)]
|
||||
|
||||
#: Why a camera's values, bytes or live view are refused.
|
||||
CAMERA_IS_SUPERUSERS = "Only a superuser may see this camera"
|
||||
|
||||
|
||||
def get_flow_controller(request: Request) -> FlowController:
|
||||
controller: FlowController | None = getattr(
|
||||
request.app.state, "flow_controller", None
|
||||
|
||||
@@ -18,8 +18,11 @@ from sqlmodel import Session
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
|
||||
from fluksio.api.deps import (
|
||||
CAMERA_IS_SUPERUSERS,
|
||||
FlowControllerDep,
|
||||
digests_of,
|
||||
get_current_active_superuser,
|
||||
hidden_from,
|
||||
user_from_token,
|
||||
worker_claims,
|
||||
)
|
||||
@@ -63,6 +66,8 @@ def artifact_caller(request: Request) -> str:
|
||||
user = user_from_token(session, token, request)
|
||||
if user is None:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
# For the camera check a fetch is held to, which a worker is not.
|
||||
request.state.person = user
|
||||
return user.email
|
||||
|
||||
|
||||
@@ -103,26 +108,30 @@ async def put_artifact(
|
||||
watching now, which the oldest of falls out of memory rather than being
|
||||
kept. A worker on another host or a device publishing a camera sends this.
|
||||
It is read into memory, where the ring holds it, and never touches the
|
||||
disk.
|
||||
disk, so it is held to ``MAX_FRAME_BYTES`` rather than the store's cap.
|
||||
"""
|
||||
store = _store(request)
|
||||
cap = settings.MAX_ARTIFACT_BYTES
|
||||
ring = store.volatile if volatile else None
|
||||
cap = settings.MAX_FRAME_BYTES if ring is not None else settings.MAX_ARTIFACT_BYTES
|
||||
declared = request.headers.get("content-length")
|
||||
if cap and declared and declared.isdigit() and int(declared) > cap:
|
||||
raise HTTPException(
|
||||
status_code=413, detail=f"An artifact may be at most {cap} bytes"
|
||||
)
|
||||
ring = store.volatile if volatile else None
|
||||
if ring is not None:
|
||||
body = bytearray()
|
||||
# The chunks as they came, joined once: the frame is copied into
|
||||
# memory one time, never past the cap.
|
||||
chunks: list[bytes] = []
|
||||
size = 0
|
||||
async for chunk in request.stream():
|
||||
body += chunk
|
||||
if cap and len(body) > cap:
|
||||
size += len(chunk)
|
||||
if cap and size > cap:
|
||||
raise HTTPException(
|
||||
status_code=413,
|
||||
detail=f"An artifact may be at most {cap} bytes",
|
||||
)
|
||||
return await run_in_threadpool(ring.put, [bytes(body)], name, media_type)
|
||||
chunks.append(chunk)
|
||||
return await run_in_threadpool(ring.put, chunks, name, media_type)
|
||||
handle = tempfile.NamedTemporaryFile(dir=store.root, delete=False)
|
||||
written = 0
|
||||
try:
|
||||
@@ -158,6 +167,15 @@ def get_artifact(
|
||||
scrubbing through a file asks for them.
|
||||
"""
|
||||
store = _store(request)
|
||||
user = getattr(request.state, "person", None)
|
||||
if user is not None:
|
||||
# What a camera only superusers may see points at, now or in its last
|
||||
# few frames. A worker's node reads anything, as node code can.
|
||||
device = getattr(request.state, "device", None) is not None
|
||||
hidden = hidden_from(user, request.app, device=device)
|
||||
controller = getattr(request.app.state, "flow_controller", None)
|
||||
if hidden and digest in digests_of(controller, set(hidden)):
|
||||
raise HTTPException(status_code=403, detail=CAMERA_IS_SUPERUSERS)
|
||||
path = store.path(digest)
|
||||
# A live frame is held in memory, not on disk.
|
||||
frame = store.volatile.get(digest) if path is None and store.volatile else None
|
||||
|
||||
@@ -43,12 +43,14 @@ from datetime import datetime, timedelta
|
||||
from typing import Any, cast
|
||||
|
||||
import httpx
|
||||
import orjson
|
||||
import redis
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from pydantic import BaseModel, Field, ValidationError, field_validator
|
||||
|
||||
from fluksio.api.deps import (
|
||||
CAMERA_IS_SUPERUSERS,
|
||||
CurrentUser,
|
||||
DeviceStoreDep,
|
||||
get_current_active_superuser,
|
||||
@@ -120,6 +122,10 @@ HELLO_LENGTH = 64
|
||||
#: The most a capability report may weigh. A panel with every header pin
|
||||
#: reported is a few kB.
|
||||
REPORT_MAX = 16 * 1024
|
||||
#: The most it may weigh as kept, with each named ``config_schemas`` entry put
|
||||
#: in place: thirty pins naming one schema send it once and keep it thirty
|
||||
#: times. The P4 panel's is about 7 kB.
|
||||
REPORT_STORED_MAX = 64 * 1024
|
||||
|
||||
|
||||
class _Pending(BaseModel):
|
||||
@@ -690,9 +696,13 @@ async def read_ice(device_id: str, store: DeviceStoreDep, user: CurrentUser) ->
|
||||
|
||||
STUN and TURN servers, the TURN ones with credentials of the caller's own
|
||||
that expire. Empty when none are set, which is enough on one network.
|
||||
Only for whoever may see the device's camera (``DeviceDef.viewers``).
|
||||
"""
|
||||
if await run_in_threadpool(store.find, device_id) is None:
|
||||
device = await run_in_threadpool(store.find, device_id)
|
||||
if device is None:
|
||||
raise _not_found(device_id)
|
||||
if not user.is_superuser and device.viewers != "members":
|
||||
raise HTTPException(status_code=403, detail=CAMERA_IS_SUPERUSERS)
|
||||
servers = [IceServer(**server) for server in ice_servers(str(user.id))]
|
||||
return IceServers(ice_servers=servers, ttl=ICE_TTL_S)
|
||||
|
||||
@@ -745,6 +755,12 @@ async def put_report(device_id: str, request: Request, store: DeviceStoreDep) ->
|
||||
include_url=False, include_input=False, include_context=False
|
||||
),
|
||||
)
|
||||
if len(orjson.dumps(report.stored())) > REPORT_STORED_MAX:
|
||||
raise HTTPException(
|
||||
status_code=413,
|
||||
detail=f"A report is at most {REPORT_STORED_MAX // 1024} KB once "
|
||||
"its config_schemas are put in place",
|
||||
)
|
||||
for part in left_out:
|
||||
logger.warning("Device '%s' reported %s, left out", device_id, part)
|
||||
first = await run_in_threadpool(store.report, device_id, report)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -12,7 +12,12 @@ from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from pydantic import BaseModel
|
||||
|
||||
from fluksio.api.deps import FlowControllerDep, get_current_user
|
||||
from fluksio.api.deps import (
|
||||
CAMERA_IS_SUPERUSERS,
|
||||
FlowControllerDep,
|
||||
HiddenDep,
|
||||
get_current_user,
|
||||
)
|
||||
from fluksio.flow.devices import DeviceDef, DeviceStore
|
||||
from fluksio.flow.messages import flow_of
|
||||
from fluksio.flow.pipeline import ValueSource
|
||||
@@ -66,9 +71,20 @@ class MessagePoints(BaseModel):
|
||||
|
||||
|
||||
@router.get("/", response_model=MessagesPublic)
|
||||
async def read_messages(controller: FlowControllerDep) -> Any:
|
||||
"""Every message any published flow declares, with its last value."""
|
||||
async def read_messages(controller: FlowControllerDep, hidden: HiddenDep) -> Any:
|
||||
"""Every message any published flow declares, with its last value.
|
||||
|
||||
A camera the caller may not see is listed without its value, so a tile
|
||||
can still be bound to it.
|
||||
"""
|
||||
infos = await run_in_threadpool(controller.message_catalog)
|
||||
if hidden:
|
||||
infos = [
|
||||
info.model_copy(update={"value": None, "ts": None})
|
||||
if info.name in hidden
|
||||
else info
|
||||
for info in infos
|
||||
]
|
||||
return MessagesPublic(data=infos, count=len(infos))
|
||||
|
||||
|
||||
@@ -131,8 +147,12 @@ async def publish_message(
|
||||
|
||||
|
||||
@router.get("/{name}/history", response_model=MessagePoints)
|
||||
def read_message_history(name: str, controller: FlowControllerDep) -> Any:
|
||||
def read_message_history(
|
||||
name: str, controller: FlowControllerDep, hidden: HiddenDep
|
||||
) -> Any:
|
||||
"""The series behind a chart. Numbers and flags — nothing else plots."""
|
||||
if name in hidden:
|
||||
raise HTTPException(status_code=403, detail=CAMERA_IS_SUPERUSERS)
|
||||
if controller.pipeline is None:
|
||||
return MessagePoints(message=name, numeric=False, points=[])
|
||||
series = controller.state.history(name)
|
||||
|
||||
Reference in New Issue
Block a user