Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

A revoked controller on a cloud machine enrolls again by itself (#758)

Louis Amaudruzb8ac3f06618f
diff --git a/core/switch_core/gateway/cloud_controllers.py b/core/switch_core/gateway/cloud_controllers.py
index c16b867d8..ad539c51c 100644
--- a/core/switch_core/gateway/cloud_controllers.py
+++ b/core/switch_core/gateway/cloud_controllers.py
@@ -27,7 +27,12 @@ from switch_core.db.models import (
MachineWorkspace,
require_tenant_id,
)
-from switch_core.db.stores.hosted_machine_store import CloudMachineStore, idle_sleeping
+from switch_core.db.stores.hosted_machine_store import (
+ CloudMachineStore,
+ await_reconnect,
+ bump_revision,
+ idle_sleeping,
+)
from switch_core.keys import Keyring
# How recent the machine's last status report must be for its silence to count
@@ -125,6 +130,25 @@ async def machine_of_controller(
return machine, workspace
+async def enroll_revoked_again(
+ session: AsyncSession, controller_id: str, now: datetime
+) -> CloudMachine | None:
+ """After the bound workspace's controller on a cloud machine is revoked,
+ give the machine a new revision, so it is prepared again with a new code
+ for that workspace and, running, relaunched on the same disk to enroll.
+ The other workspaces keep their enrollment. A machine that is not meant to
+ run enrolls at its next start. None for a controller on no cloud machine.
+ The caller commits."""
+ found = await machine_of_controller(session, controller_id)
+ if found is None:
+ return None
+ machine, _workspace = found
+ if machine.desired_state == "running":
+ bump_revision(machine, now)
+ await_reconnect(machine)
+ return machine
+
+
def controller_reports(machine: CloudMachine) -> dict[str, dict[str, Any]]:
"""Per workspace on the machine (its `MachineWorkspace` id), what its
controller last reported: `{at, sessions_running}`."""
diff --git a/core/switch_core/management/service.py b/core/switch_core/management/service.py
index 1248bd7f6..e6239b0fa 100644
--- a/core/switch_core/management/service.py
+++ b/core/switch_core/management/service.py
@@ -62,6 +62,7 @@ from switch_core.db.stores.api_key_store import ApiKeyStore
from switch_core.db.stores.hosted_machine_store import CloudMachineStore
from switch_core.db.stores.switch_core_process_store import SwitchCoreProcessStore
from switch_core.gateway.cloud_controllers import (
+ enroll_revoked_again,
record_controller_status,
wake_controller_machine,
)
@@ -727,8 +728,20 @@ class ManagementService:
if controller.revoked_at is not None:
return
await self._revoke(session, tenant_id, controller_id)
+ machine = (
+ await enroll_revoked_again(session, controller_id, self.now())
+ if controller.kind == "ec2"
+ else None
+ )
await session.commit()
logger.info("Revoked agent controller %s", controller_id)
+ if machine is not None:
+ logger.info(
+ "Cloud machine %s enrolls a new controller for workspace %s at revision %s",
+ machine.id,
+ tenant_id,
+ machine.revision,
+ )
self._announce_revoked(controller_id)
async def _revoke(
diff --git a/core/tests/switch_core/gateway/test_cloud_controller_machines.py b/core/tests/switch_core/gateway/test_cloud_controller_machines.py
index f9c6c5a73..5030c1172 100644
--- a/core/tests/switch_core/gateway/test_cloud_controller_machines.py
+++ b/core/tests/switch_core/gateway/test_cloud_controller_machines.py
@@ -435,12 +435,15 @@ class TestControllerMachines:
client, owner, name="reviewer", controller_id=first["controller_id"]
)
assert created.status_code == 201, created.text
+ before = (await _machine(harness.session_factory, machine.id)).revision
revoked = await client.delete(
f"/gateway/management/controllers/{first['controller_id']}",
cookies=cookies_for(owner),
)
assert revoked.status_code == 200, revoked.text
- await _new_revision(harness.session_factory, machine.id)
+ assert (
+ await _machine(harness.session_factory, machine.id)
+ ).revision == before + 1
[prepared] = (await _prepare(hosted, machine.id))["controllers"]
assert prepared["id"] is None
second_code = prepared["enrollment_code"]
diff --git a/core/tests/switch_core/gateway/test_hosted_controller.py b/core/tests/switch_core/gateway/test_hosted_controller.py
index df466ac14..e3fc56426 100644
--- a/core/tests/switch_core/gateway/test_hosted_controller.py
+++ b/core/tests/switch_core/gateway/test_hosted_controller.py
@@ -34,6 +34,7 @@ from switch_core.gateway.auth import get_current_user
from switch_core.gateway.cloud_controllers import (
controller_machine_idle,
controller_reports,
+ enroll_revoked_again,
record_controller_status,
set_cloud_enrollment,
)
@@ -1338,3 +1339,49 @@ async def test_a_workspace_its_owner_left_no_longer_keeps_the_machine_awake(
assert response.status_code == 200, response.text
machine = await machine_of(app.factory, app.machine_id)
assert set(controller_reports(machine)) == {app.workspace_id}
+
+
+async def test_a_revoked_workspace_controller_enrolls_again_and_the_others_keep_theirs(
+ controller_app,
+):
+ app = controller_app
+ here, _ = await enrolled(app, agents=1)
+ there = await join_workspace(app, "tenant-b", agents=1, member=True)
+ before = (await machine_of(app.factory, app.machine_id)).revision
+ with tenant_scope(there.tenant_id):
+ async with app.factory() as session:
+ controller = await session.get(AgentController, there.controller_id)
+ assert controller is not None
+ controller.revoked_at = datetime.now(UTC)
+ machine = await enroll_revoked_again(
+ session, there.controller_id, datetime.now(UTC)
+ )
+ await session.commit()
+ assert machine is not None and machine.revision == before + 1
+ response = await prepare(app.client, app.machine_id)
+ assert response.status_code == 200, response.text
+ by_key = {entry["key"]: entry for entry in response.json()["controllers"]}
+ assert (
+ by_key[app.workspace_id]["id"],
+ by_key[app.workspace_id]["enrollment_code"],
+ ) == (
+ here,
+ None,
+ )
+ assert by_key[there.workspace_id]["id"] is None
+ assert by_key[there.workspace_id]["enrollment_code"] is not None
+
+
+async def test_a_stopped_machine_enrolls_its_revoked_controller_at_its_next_start(
+ controller_app,
+):
+ app = controller_app
+ here, _ = await enrolled(app, agents=0)
+ await update_machine(
+ app.factory, app.machine_id, desired_state="stopped", stop_reason="owner"
+ )
+ before = (await machine_of(app.factory, app.machine_id)).revision
+ async with app.factory() as session:
+ assert await enroll_revoked_again(session, here, datetime.now(UTC)) is not None
+ await session.commit()
+ assert (await machine_of(app.factory, app.machine_id)).revision == before