AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): serve the swap proxy from a service that exposes only its two calls (#14865)
Krzysztof Czerwinski15eeb66abe97
diff --git a/.github/scripts/platform-single-container-smoke.sh b/.github/scripts/platform-single-container-smoke.shindex 38e7e1c338..6a25424c89 100644--- a/.github/scripts/platform-single-container-smoke.sh+++ b/.github/scripts/platform-single-container-smoke.sh@@ -443,6 +443,7 @@ path.write_text(json.dumps({"platform_linking_service_port": 18009,"copilot_chat_bridge_port": 18010,"batch_executor_port": 18011,+ "swap_credential_service_port": 18012,}) + "\n", encoding="utf-8")os.chown(path, 10001, 10001)os.chmod(path, 0o600)@@ -471,7 +472,8 @@ assert_pinned_topology_environment() {DATABASE_API_PORT=8005 AGENT_API_HOST=127.0.0.1 AGENT_API_PORT=8006 \NOTIFICATION_SERVICE_PORT=8007 COPILOT_EXECUTOR_PORT=8008 \PLATFORM_LINKING_SERVICE_PORT=8009 COPILOT_CHAT_BRIDGE_PORT=8010 \- BATCH_EXECUTOR_PORT=8011 FORCE_FLAG_GRAPHITI_MEMORY=true; do+ BATCH_EXECUTOR_PORT=8011 SWAP_CREDENTIAL_SERVICE_PORT=8012 \+ FORCE_FLAG_GRAPHITI_MEMORY=true; dogrep -Fxq "${expected}" <<<"${process_env}"done}diff --git a/autogpt_platform/backend/backend/app.py b/autogpt_platform/backend/backend/app.pyindex e545a23931..e78e64fe29 100644--- a/autogpt_platform/backend/backend/app.py+++ b/autogpt_platform/backend/backend/app.py@@ -40,6 +40,7 @@ def main(**kwargs):from backend.api.ws_api import WebsocketServerfrom backend.copilot.bot.app import CoPilotChatBridgefrom backend.copilot.executor.manager import CoPilotExecutor+ from backend.copilot.swap_service import SwapCredentialServicefrom backend.data.db_manager import DatabaseManagerfrom backend.executor import ExecutionManager, Schedulerfrom backend.executor.batch_executor import BatchExecutor@@ -48,6 +49,7 @@ def main(**kwargs):run_processes(DatabaseManager().set_log_level("warning"),+ SwapCredentialService().set_log_level("warning"),Scheduler(),# BatchExecutor polls submitted LLM batches (Anthropic today) and# dispatches results to caller-registered namespace handlers.diff --git a/autogpt_platform/backend/backend/copilot/swap_service.py b/autogpt_platform/backend/backend/copilot/swap_service.pynew file mode 100644index 0000000000..6f537e23a8--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/swap_service.py@@ -0,0 +1,59 @@+"""The only backend service the credential swap proxy calls: the two swap+methods and nothing else. Hosted beside ``DatabaseManager`` (``backend.db``);+see ``swap_proxy/README.md`` for why."""++import logging+from contextlib import asynccontextmanager+from typing import TYPE_CHECKING, Optional++from backend.copilot.swap_credentials import (+ SwapCredential,+ get_swap_bindings,+ resolve_swap_credential,+)+from backend.data import db+from backend.util.service import AppService, UnhealthyServiceError, expose+from backend.util.settings import Config++if TYPE_CHECKING:+ from fastapi import FastAPI++logger = logging.getLogger(__name__)+++class SwapCredentialService(AppService):+ # Nothing on the swap path reads a flag; no second LaunchDarkly client.+ reads_feature_flags = False++ @classmethod+ def get_port(cls) -> int:+ return Config().swap_credential_service_port++ @asynccontextmanager+ async def lifespan(self, app: "FastAPI"):+ async with super().lifespan(app):+ await db.connect()+ logger.info(f"[{self.service_name}] ✅ Ready")+ yield+ await db.disconnect()++ async def health_check(self) -> str:+ if not db.is_connected():+ raise UnhealthyServiceError("Database is not connected")+ try:+ result = await db.query_raw_with_schema("SELECT 1 as health_check")+ if not result or result[0].get("health_check") != 1:+ raise UnhealthyServiceError("Database query test failed")+ except Exception as e:+ raise UnhealthyServiceError(f"Database health check failed: {e}")+ return await super().health_check()++ @expose+ async def get_swap_bindings(self) -> dict[str, list[str]]:+ return await get_swap_bindings()++ @expose+ async def resolve_swap_credential(+ self, user_id: str, name: str, host: str+ ) -> Optional[SwapCredential]:+ return await resolve_swap_credential(user_id, name, host)diff --git a/autogpt_platform/backend/backend/copilot/swap_service_test.py b/autogpt_platform/backend/backend/copilot/swap_service_test.pynew file mode 100644index 0000000000..3db9a88373--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/swap_service_test.py@@ -0,0 +1,105 @@+"""The swap proxy's service answers the two swap methods and nothing else."""++from unittest.mock import AsyncMock, patch++import pytest+from fastapi.testclient import TestClient+from starlette.routing import Route++from backend.copilot.swap_credentials import SwapCredential+from backend.copilot.swap_service import SwapCredentialService+from backend.data.db_manager import DatabaseManager+from backend.util.service import EXPOSED_FLAG, UnhealthyServiceError++_SWAP_METHODS = {"get_swap_bindings", "resolve_swap_credential"}++[email protected](scope="module")+def client():+ # Not entered as a context manager: the lifespan (database, Redis) is not+ # what is under test, the routes are.+ return TestClient(SwapCredentialService().build_fastapi_app())+++def _database_manager_methods() -> list[str]:+ return sorted(+ name+ for name, attr in vars(DatabaseManager).items()+ if getattr(attr, EXPOSED_FLAG, False)+ )+++def test_the_only_rpc_routes_are_the_two_swap_methods():+ app = SwapCredentialService().build_fastapi_app()+ posts = {+ route.path.lstrip("/")+ for route in app.routes+ if isinstance(route, Route) and "POST" in (route.methods or ())+ }+ assert posts == _SWAP_METHODS | {"health_check", "health_check_async"}+++def test_the_swap_methods_are_no_longer_on_database_manager():+ assert _SWAP_METHODS.isdisjoint(_database_manager_methods())+++def test_every_database_manager_method_is_refused(client):+ methods = _database_manager_methods()+ assert "get_user_credentials" in methods # the list is the real one+ answered = [+ method+ for method in methods+ if client.post(f"/{method}", json={"user_id": "user-1"}).status_code+ not in (404, 405)+ ]+ assert answered == []+++def test_resolve_swap_credential_answers(client):+ credential = SwapCredential(+ name="github", values={"access_token": "ghp_x"}, allowed_hosts=["github.com"]+ )+ with patch(+ "backend.copilot.swap_service.resolve_swap_credential",+ AsyncMock(return_value=credential),+ ) as resolve:+ response = client.post(+ "/resolve_swap_credential",+ json={"user_id": "user-1", "name": "github", "host": "github.com"},+ )+ assert response.status_code == 200+ assert response.json() == credential.model_dump()+ resolve.assert_awaited_once_with("user-1", "github", "github.com")+++def test_resolve_swap_credential_answers_none(client):+ with patch(+ "backend.copilot.swap_service.resolve_swap_credential",+ AsyncMock(return_value=None),+ ):+ response = client.post(+ "/resolve_swap_credential",+ json={"user_id": "user-1", "name": "github", "host": "evil.test"},+ )+ assert response.status_code == 200+ assert response.json() is None+++def test_get_swap_bindings_answers(client):+ response = client.post("/get_swap_bindings", json={})+ assert response.status_code == 200+ assert "github" in response.json()+++async def test_health_check_runs_a_query_not_just_the_connected_flag():+ """``is_connected`` only says an engine object exists, so a wedged one+ would read healthy."""+ query = AsyncMock(side_effect=RuntimeError("engine is wedged"))+ with (+ patch("backend.copilot.swap_service.db.is_connected", return_value=True),+ patch("backend.copilot.swap_service.db.query_raw_with_schema", query),+ pytest.raises(UnhealthyServiceError, match="wedged"),+ ):+ await SwapCredentialService().health_check()+ query.assert_awaited_once()diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.pyindex 6f2a3a7e65..fc386f0b67 100644--- a/autogpt_platform/backend/backend/data/db_manager.py+++ b/autogpt_platform/backend/backend/data/db_manager.py@@ -60,7 +60,6 @@ from backend.api.features.store.db import (from backend.api.features.store.embeddings import backfill_missing_embeddingsfrom backend.copilot import db as chat_dbfrom backend.copilot.sharing.db import link_new_execution_to_chat_share-from backend.copilot.swap_credentials import get_swap_bindings, resolve_swap_credentialfrom backend.data import bot_analytics as bot_analytics_dbfrom backend.data import bot_installs as bot_installs_dbfrom backend.data import db@@ -404,12 +403,6 @@ class DatabaseManager(AppService):get_user_credentials = _(get_user_credentials)set_user_credentials = _(set_user_credentials)- # ============ Credential Swap Proxy ============ #- # Called by the swap proxy (autogpt_platform/swap_proxy), which has no- # database access of its own; see backend/copilot/swap_credentials.py.- get_swap_bindings = _(get_swap_bindings)- resolve_swap_credential = _(resolve_swap_credential)-# ============ User Comms ============ #get_active_user_ids_in_timerange = _(get_active_user_ids_in_timerange)get_user_email_by_id = _(get_user_email_by_id)diff --git a/autogpt_platform/backend/backend/db.py b/autogpt_platform/backend/backend/db.pyindex 2661405f6d..39d8f47efa 100644--- a/autogpt_platform/backend/backend/db.py+++ b/autogpt_platform/backend/backend/db.py@@ -1,12 +1,15 @@from backend.app import run_processes+from backend.copilot.swap_service import SwapCredentialServicefrom backend.data.db_manager import DatabaseManagerdef main():"""- Run all the processes required for the AutoGPT-server REST API.+ Run the database service, and beside it the one the credential swap proxy+ calls: that one needs the same database and encryption key, and keeping it+ here keeps the proxy off every other method DatabaseManager exposes."""- run_processes(DatabaseManager())+ run_processes(SwapCredentialService().set_log_level("warning"), DatabaseManager())if __name__ == "__main__":diff --git a/autogpt_platform/backend/backend/util/service.py b/autogpt_platform/backend/backend/util/service.pyindex 088ceeb7af..134f905edd 100644--- a/autogpt_platform/backend/backend/util/service.py+++ b/autogpt_platform/backend/backend/util/service.py@@ -509,12 +509,28 @@ class AppService(BaseAppService, ABC):sentry_init()super().run()- self.fastapi_app = FastAPI(lifespan=self.lifespan)+ self.fastapi_app = self.build_fastapi_app()++ # Start the FastAPI server in a separate thread.+ api_thread = threading.Thread(+ target=self.__start_fastapi,+ daemon=True,+ name=f"{self.service_name}-http-server",+ )+ api_thread.start()++ # Run the main service loop (blocking).+ self.run_service()++ def build_fastapi_app(self) -> FastAPI:+ """The service's HTTP app: one POST route per exposed method, the+ health checks and metrics, and nothing else."""+ app = FastAPI(lifespan=self.lifespan)# Add Prometheus instrumentation to all servicestry:instrument_fastapi(- self.fastapi_app,+ app,service_name=self.service_name,expose_endpoint=True,endpoint="/metrics",@@ -533,29 +549,17 @@ class AppService(BaseAppService, ABC):for attr_name, attr in vars(type(self)).items():if getattr(attr, EXPOSED_FLAG, False):route_path = f"/{attr_name}"- self.fastapi_app.add_api_route(+ app.add_api_route(route_path,self._create_fastapi_endpoint(attr),methods=["POST"],)- self.fastapi_app.add_api_route(- "/health_check", self.health_check, methods=["POST", "GET"]- )- self.fastapi_app.add_api_route(+ app.add_api_route("/health_check", self.health_check, methods=["POST", "GET"])+ app.add_api_route("/health_check_async", self.health_check, methods=["POST", "GET"])- self._register_exception_handlers(self.fastapi_app)-- # Start the FastAPI server in a separate thread.- api_thread = threading.Thread(- target=self.__start_fastapi,- daemon=True,- name=f"{self.service_name}-http-server",- )- api_thread.start()-- # Run the main service loop (blocking).- self.run_service()+ self._register_exception_handlers(app)+ return app# --------------------------------------------------diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 98988abe9f..4eed375ad0 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -450,6 +450,12 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):description="The port for database server API to run on",)+ swap_credential_service_port: int = Field(+ default=8012,+ description="The port for the swap proxy's service "+ "(backend.copilot.swap_service)",+ )+agent_api_host: str = Field(default="0.0.0.0",description="The host for agent server API to run on",diff --git a/autogpt_platform/single-container/common.sh b/autogpt_platform/single-container/common.shindex 052ebd1625..6292d2b26c 100755--- a/autogpt_platform/single-container/common.sh+++ b/autogpt_platform/single-container/common.sh@@ -19,6 +19,7 @@ readonly AUTOGPT_COPILOT_EXECUTOR_PORT=8008readonly AUTOGPT_PLATFORM_LINKING_SERVICE_PORT=8009readonly AUTOGPT_COPILOT_CHAT_BRIDGE_PORT=8010readonly AUTOGPT_BATCH_EXECUTOR_PORT=8011+readonly AUTOGPT_SWAP_CREDENTIAL_SERVICE_PORT=8012log() {printf '%s [single-container] %s\n' "$(date -u +'%Y-%m-%dT%H:%M:%SZ')" "$*"diff --git a/autogpt_platform/single-container/entrypoint.sh b/autogpt_platform/single-container/entrypoint.shindex c4563aa76e..8ae78a7ab6 100755--- a/autogpt_platform/single-container/entrypoint.sh+++ b/autogpt_platform/single-container/entrypoint.sh@@ -165,6 +165,7 @@ configure_environment() {export PLATFORM_LINKING_SERVICE_PORT="${AUTOGPT_PLATFORM_LINKING_SERVICE_PORT}"export COPILOT_CHAT_BRIDGE_PORT="${AUTOGPT_COPILOT_CHAT_BRIDGE_PORT}"export BATCH_EXECUTOR_PORT="${AUTOGPT_BATCH_EXECUTOR_PORT}"+ export SWAP_CREDENTIAL_SERVICE_PORT="${AUTOGPT_SWAP_CREDENTIAL_SERVICE_PORT}"# Keep self-hosted product behavior without enabling LOCAL-only API docs and# asyncio debug mode on the public REST process.# BEHAVE_AS defaults to local (self-hosted product behavior) but staysdiff --git a/autogpt_platform/swap_proxy/README.md b/autogpt_platform/swap_proxy/README.mdindex ef16b9a460..c36250c317 100644--- a/autogpt_platform/swap_proxy/README.md+++ b/autogpt_platform/swap_proxy/README.md@@ -131,7 +131,7 @@ poetry run swap-proxy| Variable | Default | || --- | --- | --- || `SWAP_PROXY_LISTEN_HOST` / `SWAP_PROXY_LISTEN_PORT` | `0.0.0.0` / `1080` | SOCKS5 listener |-| `SWAP_PROXY_BACKEND_URL` | `http://localhost:8005` | the backend's internal `DatabaseManager` service |+| `SWAP_PROXY_BACKEND_URL` | `http://localhost:8012` | the backend's swap service (`SwapCredentialService`), which serves only the two calls the proxy makes || `SWAP_PROXY_CONFDIR` | `~/.mitmproxy` | directory the signing CA is mounted into; see below || `SWAP_PROXY_GENERATE_CA` | `false` | local runs only: generate a CA if the directory has none || `SWAP_PROXY_EGRESS_ALLOW` | empty | comma-separated private hosts or CIDRs boxes may reach anyway |@@ -170,18 +170,30 @@ as every box losing either its egress or its TLS to bound hosts.The same step sets the network policy around the proxy, and that policy is asecurity boundary, not housekeeping. The proxy calls one backend service-(`SWAP_PROXY_BACKEND_URL`, the internal `DatabaseManager`) and must be able to-reach that and nothing else in the backend: the policy is what keeps a-compromised proxy away from every other backend service.--It cannot do more than that. `DatabaseManager` serves every method it exposes-on the same port, with no caller authentication or per-caller allowlist, and-the proxy uses only two of them (`get_swap_bindings`,-`resolve_swap_credential`). A proxy that can reach it can call all of them,-reading any user's credentials among them. Restricting it to the two swap-methods needs a dedicated service or a per-caller allowlist (SECRT-2742);-until then that is an accepted gap, and one more reason the proxy's own host-is to be treated as holding every user's credentials.+(`SWAP_PROXY_BACKEND_URL`) and must be able to reach that and nothing else in+the backend: the policy is what keeps a compromised proxy away from every other+backend service.++That service is `SwapCredentialService` (`backend/copilot/swap_service.py`),+and it answers the two calls the proxy makes (`get_swap_bindings`,+`resolve_swap_credential`) and no other backend method (it also serves the+health checks and FastAPI's schema pages, as every service does): every other+backend method is on+another service's port, so a proxy that can reach only this port cannot call+it, whatever it sends. It runs beside `DatabaseManager` in the backend's `db`+entrypoint, on its own port (`SWAP_CREDENTIAL_SERVICE_PORT`, default 8012); the+policy should let the proxy reach that port and not `DatabaseManager`'s. What+the service does answer is any user's credential for a bound host, so the+proxy's own host is still to be treated as holding every user's credentials.++Two things are still to do at switch-on, and neither is in this repository:++- The policy itself, and publishing the service's port from the `db`+ deployment. Until both are in place `DatabaseManager` still answers every+ method to a proxy that can reach its port.+- A health probe on the service's own port. It is a second process in the `db`+ entrypoint and nothing restarts it: if it dies, `DatabaseManager`'s health+ check keeps passing while every swap fails.### What one message costs the event loopdiff --git a/autogpt_platform/swap_proxy/swap_proxy/settings.py b/autogpt_platform/swap_proxy/swap_proxy/settings.pyindex 6b952f4adb..940beae9dd 100644--- a/autogpt_platform/swap_proxy/swap_proxy/settings.py+++ b/autogpt_platform/swap_proxy/swap_proxy/settings.py@@ -24,10 +24,11 @@ class Settings:listen_port: int = field(default_factory=lambda: int(os.getenv("SWAP_PROXY_LISTEN_PORT", "1080")))- # The backend's internal service (DatabaseManager), e.g. http://host:8005+ # The backend's swap service (backend/copilot/swap_service.py), which+ # answers the two calls in source.py and nothing else.backend_url: str = field(default_factory=lambda: os.getenv(- "SWAP_PROXY_BACKEND_URL", "http://localhost:8005"+ "SWAP_PROXY_BACKEND_URL", "http://localhost:8012"))# Where the CA the proxy signs with is mounted (``mitmproxy-ca.pem``: keydiff --git a/autogpt_platform/swap_proxy/swap_proxy/source_test.py b/autogpt_platform/swap_proxy/swap_proxy/source_test.pyindex 7caf3875ac..daf02f9329 100644--- a/autogpt_platform/swap_proxy/swap_proxy/source_test.py+++ b/autogpt_platform/swap_proxy/swap_proxy/source_test.py@@ -18,7 +18,7 @@ def _backend(handler):return handler(request.url.path, body)client = httpx.AsyncClient(transport=httpx.MockTransport(transport))- return BackendCredentialSource("http://backend:8005/", client), calls+ return BackendCredentialSource("http://backend:8012/", client), callsdef _ok(path, body):