Restart safety: transient credential checks are 1013/503, not a refusal
- A device credential that cannot be checked now (device store not running, nonces unreadable) raises DeviceUnavailable: 503 over HTTP, and the /flows/ws handshake and the tunnel stream accept and close with 1013. 1008 (a 403 before accepting) stays for a revoked or invalid one - Device files are written through a temporary file, so a read mid-save no longer looks like a deleted device - The first report turns Touch friendly on only for a device paired since pairings are noted (DeviceState.paired_at), so devices paired before keep the switch they were given - NodeDef reads `device`/`device_policy` from older clients as the worker label, for a release - Tests: transient vs revoked socket, the touch default, the old worker keys, and a restart with a device node and a node_health rule sending no push
This commit is contained in:
@@ -159,6 +159,9 @@ class DeviceState(BaseModel):
|
||||
#: What the device says it has, written by the device itself.
|
||||
report: dict[str, Any] | None = None
|
||||
reported_at: datetime | None = None
|
||||
#: When a pairing here last minted the device a credential. None for one
|
||||
#: paired before this was kept, whose settings were all chosen by hand.
|
||||
paired_at: datetime | None = None
|
||||
|
||||
|
||||
_STATES = TypeAdapter(dict[str, DeviceState])
|
||||
@@ -231,6 +234,15 @@ class DeviceExists(ValueError):
|
||||
self.id = device_id
|
||||
|
||||
|
||||
class DeviceUnavailable(RuntimeError):
|
||||
"""Whether a device credential stands cannot be told right now.
|
||||
|
||||
Not a refusal: a device told its credential is refused pairs again, so
|
||||
a store that is not running or a nonce file that does not read answers
|
||||
"try again later" instead.
|
||||
"""
|
||||
|
||||
|
||||
class DeviceStore:
|
||||
"""Device definitions in the flow repository, their nonces beside it.
|
||||
|
||||
@@ -260,7 +272,11 @@ class DeviceStore:
|
||||
def _write(self, device: DeviceDef) -> None:
|
||||
path = self._file(device.id)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(device.model_dump_json(indent=2) + "\n")
|
||||
# Through a temporary file: a credential checked against a half
|
||||
# written one would read as a deleted device and be refused.
|
||||
tmp = path.with_name(path.name + ".tmp")
|
||||
tmp.write_text(device.model_dump_json(indent=2) + "\n")
|
||||
os.replace(tmp, path)
|
||||
|
||||
# -- what the published flows wire ------------------------------------
|
||||
|
||||
@@ -433,6 +449,13 @@ class DeviceStore:
|
||||
self._write_states(states)
|
||||
return state.nonce
|
||||
|
||||
def paired(self, device_id: str) -> None:
|
||||
"""Note that a pairing here just minted this device a credential."""
|
||||
with self._state_lock:
|
||||
states = self._read_states()
|
||||
states.setdefault(device_id, DeviceState()).paired_at = datetime.now(UTC)
|
||||
self._write_states(states)
|
||||
|
||||
def report(self, device_id: str, report: DeviceReport) -> bool:
|
||||
"""Keep what the device says it has, in place of what it said before.
|
||||
|
||||
|
||||
@@ -226,6 +226,27 @@ class NodeDef(BaseModel):
|
||||
),
|
||||
)
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def _old_worker_keys(cls, data: Any) -> Any:
|
||||
"""Read ``device`` and ``device_policy`` as the worker label they were.
|
||||
|
||||
What a ``fluksio sync`` or SDK from before the rename to ``worker``
|
||||
sends, which would otherwise be dropped without a word. A new key
|
||||
sent beside an old one wins.
|
||||
|
||||
ponytail: drop after a release, once clients have caught up.
|
||||
"""
|
||||
if not isinstance(data, dict) or not (
|
||||
"device" in data or "device_policy" in data
|
||||
):
|
||||
return data
|
||||
data = dict(data)
|
||||
for old, new in (("device", "worker"), ("device_policy", "worker_policy")):
|
||||
if old in data:
|
||||
data.setdefault(new, data.pop(old))
|
||||
return data
|
||||
|
||||
@field_validator("id")
|
||||
@classmethod
|
||||
def _check_id(cls, value: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user