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
+109 -4
View File
@@ -244,10 +244,14 @@ def test_a_frame_the_device_may_not_send_is_dropped(
current = controller.values()["cams.camera"]["value"]
data = client.app.state.artifact_store.volatile.get(current["digest"])
assert data == b"\xff\xd8 after"
from fluksio.api.routes.flows import FRAME_REFUSALS
# By fixed keys, never in the words the device chose.
said = [r.getMessage() for r in caplog.records if "Dropped a frame" in r.message]
assert any("'person' is wired as bool" in line for line in said)
assert any("not a message one of its ports publishes" in line for line in said)
assert any("only a device sends frames" in line for line in said)
for key in ("not_image", "not_wired", "media_type", "not_a_frame"):
assert f"Dropped a frame from cam1: {FRAME_REFUSALS[key]}" in said
assert any(FRAME_REFUSALS["not_a_device"] in line for line in said)
assert not any("elsewhere" in line or "text/html" in line for line in said)
def test_an_unpaired_device_is_closed_and_its_frames_stop(
@@ -264,6 +268,106 @@ def test_an_unpaired_device_is_closed_and_its_frames_stop(
assert closed.value.code == 1008
def test_a_frame_past_the_cap_is_dropped(
client: TestClient, camera, monkeypatch: pytest.MonkeyPatch
) -> None:
controller = client.app.state.flow_controller
monkeypatch.setattr(settings, "MAX_FRAME_BYTES", 1000)
with client.websocket_connect(SOCKET, headers=camera) as device:
device.receive_json()
device.send_bytes(frame("cams.camera", b"\xff" * 2000))
device.send_bytes(frame("cams.camera", b"\xff\xd8 small"))
wait_for(lambda: (controller.values().get("cams.camera") or {}).get("value"))
digest = controller.values()["cams.camera"]["value"]["digest"]
store = client.app.state.artifact_store
assert store.volatile.get(digest) == b"\xff\xd8 small"
def test_a_frame_is_named_after_its_message(client: TestClient, camera) -> None:
controller = client.app.state.flow_controller
with client.websocket_connect(SOCKET, headers=camera) as device:
device.receive_json()
device.send_bytes(frame("cams.camera", b"\xff\xd8 named"))
value = wait_for(
lambda: (controller.values().get("cams.camera") or {}).get("value")
)
assert value["name"] == "cams.camera"
def test_an_unpaired_device_is_closed_at_its_next_frame(
client: TestClient, camera
) -> None:
"""Without the event an unpair sends, which a full queue may have lost."""
from starlette.websockets import WebSocketDisconnect
with client.websocket_connect(SOCKET, headers=camera) as device:
device.receive_json()
device.send_bytes(frame("cams.camera", b"\xff\xd8 one"))
client.app.state.device_store.bump("cam1")
device.send_bytes(frame("cams.camera", b"\xff\xd8 two"))
with pytest.raises(WebSocketDisconnect) as closed:
while True:
device.receive_json()
assert closed.value.code == 1008
store = client.app.state.artifact_store
current = client.app.state.flow_controller.values()["cams.camera"]["value"]
assert store.volatile.get(current["digest"]) == b"\xff\xd8 one"
def test_a_device_that_lost_events_catches_up(
client: TestClient, camera, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Its demand and its unpairing come again with a snapshot, after the
events it did get."""
from starlette.websockets import WebSocketDisconnect
controller = client.app.state.flow_controller
with client.websocket_connect(SOCKET, headers=camera) as device:
assert device.receive_json()["demand"] == []
# Demand changes while the bus drops what the socket has queued.
monkeypatch.setattr(event_bus, "dropped", lambda queue: True)
demand.want("someone", {"cams.camera"})
try:
seen = []
while not seen or seen[-1].get("type") != "snapshot":
seen.append(device.receive_json())
assert seen[-1]["demand"] == ["camera"]
finally:
demand.drop("someone")
# And an unpair it never heard of closes it at the next batch.
client.app.state.device_store.bump("cam1")
controller.publish_message("cams.person", True)
with pytest.raises(WebSocketDisconnect) as closed:
while True:
device.receive_json()
assert closed.value.code == 1008
def test_a_report_is_bounded_once_its_schemas_are_in_place(
client: TestClient, camera
) -> None:
"""Thirty pins naming one big schema are thirty copies of it, kept."""
from fluksio.api.routes.devices import REPORT_MAX, REPORT_STORED_MAX
schema = {
"type": "object",
"properties": {f"p{i}": {"type": "integer"} for i in range(250)},
}
pins = [
{"port": f"gpio.{i}", "direction": "in", "dtype": "bool", "config": "pin"}
for i in range(60)
]
report = {**REPORT, "config_schemas": {"pin": schema}, "ports": pins}
raw = orjson.dumps(report)
assert len(raw) < REPORT_MAX
answer = client.put(f"{PREFIX}/cam1/report", headers=camera, content=raw)
assert answer.status_code == 413, answer.text
kept = client.app.state.device_store.state("cam1").report
assert len(orjson.dumps(kept)) < REPORT_STORED_MAX
assert kept["board"] == "nano"
def test_a_screen_may_fetch_the_last_few_frames(client: TestClient, camera) -> None:
"""A remote screen fetching frame by frame is often one behind."""
controller = client.app.state.flow_controller
@@ -290,7 +394,8 @@ def test_a_viewer_asks_for_a_few_cameras_at_most() -> None:
names = [f"cams.c{i}" for i in range(MAX_MEDIA_NAMES + 3)]
asked = wanted_names(json.dumps({"type": "media", "names": names}), None)
assert asked == set(names[:MAX_MEDIA_NAMES])
# The newest, which a page lists last: a tile scrolled back to keeps working.
assert asked == set(names[-MAX_MEDIA_NAMES:])
def test_viewers_through_the_portal_count_too(