Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(agent): pin the constraint-free revocation case and stop misreporting a config fault as a bad token
Wojciech Wojtyniak9631a2a0b22c
diff --git a/core/switch_core/bridges/agent/protocol/service.py b/core/switch_core/bridges/agent/protocol/service.pyindex f59876df7..8e5390d33 100644--- a/core/switch_core/bridges/agent/protocol/service.py+++ b/core/switch_core/bridges/agent/protocol/service.py@@ -204,6 +204,17 @@ class AgentExistsError(Exception):caller did not opt into re-registration via ``overwrite=True``."""+class BootstrapOwnerMisconfiguredError(Exception):+ """Raised when the deployment-wide bootstrap key's owner account cannot+ be resolved or trusted (see ``registration_bootstrap.py``).++ Deliberately not a ``PermissionError``: the presented token is valid, so+ this is a deployment misconfiguration, not a bad credential. A caller+ that catches ``PermissionError`` to mean "bad token" must not also catch+ this and report the same thing.+ """++def _describe_room(room: Room) -> RoomDescriptor:return RoomDescriptor(id=room.id,@@ -440,10 +451,13 @@ class ProtocolService:"""Resolve a registration token to its owner, then register the agent.Raises PermissionError if the token does not match a stored ApiKey of- a registration type (see ``registration_bootstrap.REGISTRATION_KEY_TYPES``),- or if the bootstrap owner cannot be resolved (misconfigured deployment- — the detail is logged, not raised, to match the HTTP registration- path's handling of the same failure).+ a registration type (see ``registration_bootstrap.REGISTRATION_KEY_TYPES``).+ Raises BootstrapOwnerMisconfiguredError if the token is valid but its+ bootstrap owner cannot be resolved or trusted — a deployment fault,+ not a bad credential, so callers must not conflate it with the+ PermissionError above (the detail is logged here, not in the+ exception, to match the HTTP registration path's handling of the+ same failure).Raises AgentExistsError if the name is taken and ``overwrite`` is False."""token_hash = hashlib.sha256(registration_token.encode()).hexdigest()@@ -459,7 +473,7 @@ class ProtocolService:logger.error("Agent-registration bootstrap owner resolution failed: %s", exc)- raise PermissionError(+ raise BootstrapOwnerMisconfiguredError("Agent registration is temporarily unavailable") from excdiff --git a/core/switch_core/bridges/agent/registration_bootstrap.py b/core/switch_core/bridges/agent/registration_bootstrap.pyindex 4d3170b4f..03969a9cd 100644--- a/core/switch_core/bridges/agent/registration_bootstrap.py+++ b/core/switch_core/bridges/agent/registration_bootstrap.py@@ -108,10 +108,15 @@ def _is_genuine(owner: User) -> bool:def _raise_unless_genuine(owner: User) -> None:if not _is_genuine(owner):raise RuntimeError(- f"An account already exists at {BOOTSTRAP_OWNER_EMAIL} but was "- "not created by agent-registration bootstrap seeding, so it "- "cannot be trusted to carry no admin authority now or later. "- "There is no gateway endpoint that removes or renames a user: "+ f"An account already exists at {BOOTSTRAP_OWNER_EMAIL} but "+ "cannot be proven to have been created by agent-registration "+ "bootstrap seeding, so it cannot be trusted to carry no admin "+ "authority now or later. Two things produce this: something "+ "else claimed the address (a gateway admin, an OIDC login), or "+ "it was created by an earlier revision of this feature that "+ "recorded its own state on the admin's account instead of here "+ "and so left no evidence on this row to check. Either way, "+ "there is no gateway endpoint that removes or renames a user: ""this needs a direct edit to the users table before ""agent-registration bootstrap can run.")diff --git a/core/tests/switch_core/bridges/agent/test_registration_bootstrap.py b/core/tests/switch_core/bridges/agent/test_registration_bootstrap.pyindex 801a68fff..92b2c3648 100644--- a/core/tests/switch_core/bridges/agent/test_registration_bootstrap.py+++ b/core/tests/switch_core/bridges/agent/test_registration_bootstrap.py@@ -56,7 +56,7 @@ class TestEnsureBootstrapOwner:await session.commit()async with session_factory() as session:- with pytest.raises(RuntimeError, match="not created by"):+ with pytest.raises(RuntimeError, match="cannot be proven"):await ensure_bootstrap_owner(session, user_store)async def test_backfills_the_marker_onto_a_row_created_before_it_existed(@@ -225,5 +225,5 @@ class TestResolveRegistrationOwnerId:type=BOOTSTRAP_KEY_TYPE,)async with session_factory() as session:- with pytest.raises(RuntimeError, match="not created by"):+ with pytest.raises(RuntimeError, match="cannot be proven"):await resolve_registration_owner_id(session, user_store, key)diff --git a/core/tests/switch_core/test_main.py b/core/tests/switch_core/test_main.pyindex 771f32e5b..5a2721df8 100644--- a/core/tests/switch_core/test_main.py+++ b/core/tests/switch_core/test_main.py@@ -226,6 +226,52 @@ class TestSeedAgentRegistrationBootstrapKey:assert len(bootstrap_keys) == 1assert bootstrap_keys[0].key_hash == _hash("new-token")+ async def test_deleting_the_retired_row_then_restoring_the_old_token_stays_blocked(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ """The case with no constraint to fall back on. Above, restoring the+ old token while the retired row is still present is blocked twice+ over: the revoked-hashes check, and (if that ever failed) the unique+ constraint on key_hash, since the retired row still holds the old+ value. Once an operator deletes that retired row from the API Keys+ page — exactly what the warning invites them to do — that row is+ gone and the constraint can no longer save a bug here. Only the+ revoked-hashes list, recorded at retirement time, stands between the+ old token and reinstating admin-authority registration."""+ user_store, api_key_store = UserStore(), ApiKeyStore()+ admin_id = await _make_admin(session_factory)++ async with session_factory() as session:+ legacy = ApiKey(+ user_id=admin_id,+ key_hash=_hash("old-token"),+ encrypted_key="irrelevant",+ label=LEGACY_BOOTSTRAP_KEY_LABEL,+ type="registration",+ )+ session.add(legacy)+ await session.commit()++ await _seed(session_factory, user_store, api_key_store, _config("new-token"))++ async with session_factory() as session:+ keys = await api_key_store.get_by_user(session, admin_id)+ (retired,) = [k for k in keys if k.type == RETIRED_KEY_TYPE]+ await api_key_store.delete(session, retired.id)+ await session.commit()++ # No row anywhere holds H(old-token) now. If this were still guarded+ # only by the unique constraint, this call would succeed and rotate+ # the active key back onto the revoked value.+ await _seed(session_factory, user_store, api_key_store, _config("old-token"))++ async with session_factory() as session:+ keys = await api_key_store.get_by_user(session, admin_id)+ bootstrap_keys = [k for k in keys if k.type == BOOTSTRAP_KEY_TYPE]+ assert len(bootstrap_keys) == 1+ assert bootstrap_keys[0].key_hash == _hash("new-token")+ assert not any(k.key_hash == _hash("old-token") for k in keys)+async def test_a_hash_mismatched_legacy_labeled_key_is_retired_and_warned_about(self,session_factory: async_sessionmaker[AsyncSession],@@ -500,7 +546,7 @@ class TestSeedAgentRegistrationBootstrapKey:await _make_user(session_factory, email=BOOTSTRAP_OWNER_EMAIL, role="user")config = _config("dev-test-token")- with pytest.raises(RuntimeError, match="not created by"):+ with pytest.raises(RuntimeError, match="cannot be proven"):await _seed(session_factory, user_store, api_key_store, config)async def test_admin_owned_agents_are_logged_as_a_warning(