feat(lifecycle): add stage 4 authoritative unbind and durable task routing
Unify broker-mode binding changes in database transactions: advance device generation, invalidate pending authorizations/tasks and prior pairing-code epochs, and persist audit plus invalidation outbox. Add authenticated device unbind alongside robot/admin paths. Pairing codes are hashed, generation- fenced and consumed atomically with binding. Replay Redis invalidation effects without undoing committed unbind. Guard online cleanup and delayed kill events by generation. Introduce a separate router admission credential with no platform-event injection authority. Router verifies signed nonce-bound internal admission on handshake, task traffic and periodic checks; reject stale sessions and control-plane outages. Persist task owner/device/generation/session/worker and original message mapping. Sign routing metadata, verify late results, claim replies once and mark ambiguous/crashed tasks unknown instead of replaying writes. Fence notifications across same-user rebind. Release in-memory slots/waiters even when database or Redis cleanup fails. Validation: 213 passed, 3 skipped (real PostgreSQL not configured), 30 existing uvicorn/websockets deprecation warnings. New lifecycle coverage: 28 tests, including real loopback WebSocket isolation, reverse completion, missed kill, periodic unbind checks, outbox recovery, transaction rollback and cleanup failures. Syntax, docs and staged diff checks pass. Default legacy mode remains unchanged. Broker mode now requires coordinated router admission configuration and a v2 client handshake. No production ECS configuration, real credentials or client installer was changed. PostgreSQL, real Redis multi-node, ARM64 and actual Work/client rollout remain stage 5.
This commit is contained in:
@@ -35,6 +35,7 @@ class Platform:
|
||||
self.resume = None
|
||||
self.scopes = "read"
|
||||
self.sent = []
|
||||
self.replies = []
|
||||
self.user_info_failure = False
|
||||
|
||||
async def credential_exchange(self, code):
|
||||
@@ -61,6 +62,9 @@ class Platform:
|
||||
async def tenant_token(self):
|
||||
return "fake-tenant"
|
||||
|
||||
async def reply(self, message_id, text):
|
||||
self.replies.append((message_id,text))
|
||||
|
||||
async def send(self, owner, kind, content, delivery_id, *, token):
|
||||
self.sent.append((owner, content))
|
||||
return {"message_id": "om_fake"}
|
||||
@@ -80,6 +84,7 @@ async def env(tmp_path):
|
||||
keyring.write_text(json.dumps({"active_key_id": "test-key", "keys": {
|
||||
"test-key": base64.b64encode(os.urandom(32)).decode()}}))
|
||||
settings = Settings(environment="test", admin_secret="a"*40, internal_secret="b"*40,
|
||||
router_admission_secret="c"*40,
|
||||
ca_cert=ca_path, connector_key=connector_key, data_dir=tmp_path, feishu_enabled=True,
|
||||
feishu_app_id="cli_test", feishu_app_secret="fake-app-secret", credential_mode="broker",
|
||||
credential_keyring_file=keyring, credential_permission_profiles={"product-v1": ["read"], "product-v2": ["read", "write"]},
|
||||
@@ -317,7 +322,8 @@ async def test_authorization_notification_uses_server_owned_transaction(env):
|
||||
item = await start(env)
|
||||
host = "a"*32
|
||||
fields = dict(request_id="notify-test-001", kind="authorization", text="", title="",
|
||||
oauth_state=item["transaction_id"], binding_owner="ou_a")
|
||||
oauth_state=item["transaction_id"], binding_owner="ou_a",
|
||||
binding_generation=env["store"].device(host)["binding_generation"])
|
||||
proof = sign(dict(host_id=host, purpose="work-notify-v1", ts=int(time.time()), nonce=secrets.token_hex(16),
|
||||
payload_hash=hashlib.sha256(canonical(fields)).hexdigest()), env["keys"][host])
|
||||
response = await env["client"].post("/device/notifications", json={**fields, "host_id": host, "proof": proof})
|
||||
@@ -466,6 +472,14 @@ async def test_postgres_authorization_and_binding_generation(env, tmp_path):
|
||||
assert result.status_code == 200 and "fake-refresh" not in result.text
|
||||
app.state.store.unbind("ou_a")
|
||||
assert app.state.store.device("a"*32)["binding_generation"] == 2
|
||||
lifecycle = app.state.store.lifecycle
|
||||
lifecycle.issue_code("ou_a","fake-pg-old-code")
|
||||
app.state.store.unbind("ou_a")
|
||||
with pytest.raises(LookupError):
|
||||
lifecycle.consume_code("a"*32,"fake-pg-old-code")
|
||||
lifecycle.issue_code("ou_a","fake-pg-new-code")
|
||||
assert lifecycle.consume_code("a"*32,"fake-pg-new-code") == "ou_a"
|
||||
assert app.state.store.db.execute("SELECT COUNT(*) AS n FROM work_binding_events_v2").fetchone()["n"] == 1
|
||||
finally:
|
||||
if app:
|
||||
app.state.store.close()
|
||||
@@ -475,6 +489,7 @@ async def test_postgres_authorization_and_binding_generation(env, tmp_path):
|
||||
|
||||
def test_settings_require_explicit_key_and_product_profiles(tmp_path):
|
||||
common = dict(environment="test", admin_secret="a"*40, internal_secret="b"*40, credential_mode="broker",
|
||||
router_admission_secret="c"*40,
|
||||
feishu_enabled=True, feishu_app_id="test-app", feishu_app_secret="fake-secret")
|
||||
with pytest.raises(ValueError):
|
||||
Settings(**common)
|
||||
|
||||
Reference in New Issue
Block a user