Skip to content

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

fix(deploy): a failed bridge read is not an empty bridge list (#515)

Christian McDermott398d03d003af
diff --git a/core/tests/deploy/__init__.py b/core/tests/deploy/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/core/tests/deploy/test_setup_register_bridge.py b/core/tests/deploy/test_setup_register_bridge.py
new file mode 100644
index 000000000..a8d5d4ae7
--- /dev/null
+++ b/core/tests/deploy/test_setup_register_bridge.py
@@ -0,0 +1,235 @@
+"""The setup job must not read an errored bridge list as an empty one.
+
+`register_bridge` looks for an existing Mattermost bridge before registering
+one. Reading an error status as "there are no bridges" sends it on to register
+a second bridge with `set_as_default`, taking the default from whatever held
+it. The job is a post-install/post-upgrade Helm hook, so it runs against live
+deployments and is retried — and a duplicated bridge cannot be undone by
+retrying.
+
+Scoped to error *statuses*: a timeout or a connection failure raises out of the
+request itself and never reached the check being fixed here.
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import json
+import os
+import sys
+from pathlib import Path
+from typing import Any
+
+import httpx
+import pytest
+
+_REPO_ROOT = Path(__file__).resolve().parents[3]
+_SETUP_PY = _REPO_ROOT / "deploy" / "shared_resources" / "setup.py"
+
+# setup.py reads its configuration at import time, by design — it is a
+# single-shot container entrypoint, not a library.
+_ENV = {
+ "SWITCH_URL": "http://switch-core:8000",
+ "GATEWAY_ADMIN_EMAIL": "[email protected]",
+ "GATEWAY_ADMIN_PASSWORD": "setup-test-password",
+ "MATTERMOST_URL": "http://mattermost:8065",
+ "MATTERMOST_ADMIN_USER": "admin",
+ "MATTERMOST_ADMIN_PASSWORD": "setup-test-password",
+ "MATTERMOST_TEAM_NAME": "switch",
+ "MATTERMOST_USER": "user",
+ "MATTERMOST_USER_PASSWORD": "setup-test-password",
+}
+
+
[email protected](scope="module")
+def setup_script() -> Any:
+ saved = {k: os.environ.get(k) for k in _ENV}
+ os.environ.update(_ENV)
+ try:
+ spec = importlib.util.spec_from_file_location("_switch_setup", _SETUP_PY)
+ assert spec and spec.loader
+ module = importlib.util.module_from_spec(spec)
+ sys.modules["_switch_setup"] = module
+ spec.loader.exec_module(module)
+ yield module
+ finally:
+ sys.modules.pop("_switch_setup", None)
+ for k, v in saved.items():
+ if v is None:
+ os.environ.pop(k, None)
+ else:
+ os.environ[k] = v
+
+
+def _client(handler: Any) -> httpx.Client:
+ return httpx.Client(
+ base_url="http://switch-core:8000", transport=httpx.MockTransport(handler)
+ )
+
+
+def _listing(bridges: list[dict[str, Any]], writes: list[tuple[str, str]]) -> Any:
+ """A gateway that lists `bridges`, recording every write it is sent.
+
+ Records anything that is not the GET — not just POSTs — so a test asserting
+ an existing bridge was left alone also catches a stray PATCH.
+ """
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ if request.method == "GET":
+ return httpx.Response(200, json=bridges)
+ writes.append((request.method, request.url.path))
+ return httpx.Response(200, json={"bridge_id": "bridge-new"})
+
+ return handler
+
+
+def test_a_failed_bridge_read_registers_nothing(
+ setup_script: Any, capsys: pytest.CaptureFixture[str]
+) -> None:
+ """A 503 on the read must stop, not fall through to a second bridge."""
+ posted: list[str] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ if request.method == "POST":
+ posted.append(str(request.url))
+ return httpx.Response(200, json={"bridge_id": "should-not-happen"})
+ return httpx.Response(503, json={"detail": "Service Unavailable"})
+
+ with _client(handler) as client, pytest.raises(httpx.HTTPStatusError):
+ setup_script.register_bridge(client)
+
+ assert posted == []
+ # The traceback carries the status but not the body, so the gateway's own
+ # explanation only reaches the job log if it is printed.
+ out = capsys.readouterr().out
+ assert "503" in out and "Service Unavailable" in out
+
+
+def test_an_unauthorized_bridge_read_registers_nothing(setup_script: Any) -> None:
+ """The same for a 401 — the shape this first took in production."""
+ posted: list[str] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ if request.method == "POST":
+ posted.append(str(request.url))
+ return httpx.Response(200, json={"bridge_id": "should-not-happen"})
+ return httpx.Response(401, json={"detail": "Not authenticated"})
+
+ with _client(handler) as client, pytest.raises(httpx.HTTPStatusError):
+ setup_script.register_bridge(client)
+
+ assert posted == []
+
+
+def test_an_existing_default_bridge_is_adopted_untouched(setup_script: Any) -> None:
+ writes: list[tuple[str, str]] = []
+ bridges = [
+ {"bridge_id": "bridge-1", "bridge_type": "mattermost", "is_default": True}
+ ]
+
+ with _client(_listing(bridges, writes)) as client:
+ assert setup_script.register_bridge(client) == "bridge-1"
+
+ assert writes == []
+
+
+def test_an_existing_bridge_is_promoted_when_nothing_holds_the_default(
+ setup_script: Any,
+) -> None:
+ writes: list[tuple[str, str]] = []
+ bridges = [
+ {"bridge_id": "bridge-1", "bridge_type": "mattermost", "is_default": False}
+ ]
+
+ with _client(_listing(bridges, writes)) as client:
+ assert setup_script.register_bridge(client) == "bridge-1"
+
+ assert writes == [("POST", "/gateway/collaborations/bridge-1/default")]
+
+
+def test_a_default_held_by_another_bridge_is_left_alone(setup_script: Any) -> None:
+ """The promotion test above is narrower than its old name suggested.
+
+ The check is `any(b.get("is_default") for b in bridges)` — across *all*
+ bridges, not the Mattermost one. So an instance where Slack holds the
+ default keeps it, and Mattermost stays non-default. Pinned as the behaviour
+ it is: setup adopts an unclaimed default, it does not take a held one.
+ """
+ writes: list[tuple[str, str]] = []
+ bridges = [
+ {"bridge_id": "bridge-slack", "bridge_type": "slack", "is_default": True},
+ {"bridge_id": "bridge-1", "bridge_type": "mattermost", "is_default": False},
+ ]
+
+ with _client(_listing(bridges, writes)) as client:
+ assert setup_script.register_bridge(client) == "bridge-1"
+
+ assert writes == []
+
+
+def test_a_bridge_of_another_type_is_not_mistaken_for_the_mattermost_one(
+ setup_script: Any,
+) -> None:
+ """A populated list is not the same as a matching one — a deployment
+ holding only a Slack bridge still needs the Mattermost one registered."""
+ writes: list[tuple[str, str]] = []
+ bridges = [
+ {"bridge_id": "bridge-slack", "bridge_type": "slack", "is_default": True}
+ ]
+
+ with _client(_listing(bridges, writes)) as client:
+ assert setup_script.register_bridge(client) == "bridge-new"
+
+ assert writes == [("POST", "/gateway/collaborations")]
+
+
+def test_the_callback_address_is_pushed_to_an_existing_bridge(
+ setup_script: Any, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The one path in the adopt branch the other tests leave unexercised.
+
+ The bridge config is not readable back — it carries the admin password —
+ so the callback address is re-sent on every run rather than only when it
+ is missing. Read off a module global at import, so it is patched here.
+ """
+ monkeypatch.setattr(
+ setup_script, "MATTERMOST_CALLBACK_BASE_URL", "http://switch-core:8000"
+ )
+ sent: list[Any] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ if request.method == "GET":
+ return httpx.Response(
+ 200,
+ json=[
+ {
+ "bridge_id": "bridge-1",
+ "bridge_type": "mattermost",
+ "is_default": True,
+ }
+ ],
+ )
+ sent.append((request.method, request.url.path, json.loads(request.content)))
+ return httpx.Response(200, json={})
+
+ with _client(handler) as client:
+ assert setup_script.register_bridge(client) == "bridge-1"
+
+ assert sent == [
+ (
+ "PATCH",
+ "/gateway/collaborations/bridge-1",
+ {"connection_config": {"callback_base_url": "http://switch-core:8000"}},
+ )
+ ]
+
+
+def test_an_empty_list_still_registers_a_bridge(setup_script: Any) -> None:
+ """The fall-through the failure cases must not reach is still reachable
+ the one way it should be: a read that succeeded and found nothing."""
+ writes: list[tuple[str, str]] = []
+
+ with _client(_listing([], writes)) as client:
+ assert setup_script.register_bridge(client) == "bridge-new"
+
+ assert writes == [("POST", "/gateway/collaborations")]
diff --git a/deploy/shared_resources/setup.py b/deploy/shared_resources/setup.py
index 776d703a6..334c7c7cf 100644
--- a/deploy/shared_resources/setup.py
+++ b/deploy/shared_resources/setup.py
@@ -1,6 +1,8 @@
-"""Local dev setup — registers Mattermost bridge with Switch after both are ready.
+"""Deployment setup — registers Mattermost bridge with Switch after both are ready.
-Runs as a short-lived init container in docker-compose. Waits for Switch and
+Runs as a short-lived container: an init container under docker-compose, and a
+post-install/post-upgrade Helm hook on a cluster deployment, so it also runs
+against live installations on every upgrade. Waits for Switch and
Mattermost to be healthy, bootstraps the Mattermost admin/team, then registers
the Mattermost bridge via the authorized Switch gateway API — logging in as the
seeded gateway admin first, since bridge administration is an admin action.
@@ -223,46 +225,55 @@ def gateway_login() -> httpx.Client:
def register_bridge(client: httpx.Client) -> str:
"""Register Mattermost bridge with Switch via the authorized gateway API.
- Returns the bridge id — the one already registered, or the new one. A
- registration that fails raises, as it did before: without a bridge there is
- no deployment to speak of, so it is not something to carry on past.
+ Returns the bridge id — the one already registered, or the new one. Every
+ call it makes raises on failure, the read included: without a bridge there
+ is no deployment to speak of, so none of it is something to carry on past.
"""
- # Check if bridge already registered
+ # Check if bridge already registered. A read that answered with an error
+ # status is not an empty list: taken as one, a 401 or a 503 mid-rollout
+ # sends us on to registering a second Mattermost bridge and making it the
+ # default, taking that from whatever held it. Refuse instead — this job is
+ # retried, and a duplicated bridge cannot be undone by retrying. (A
+ # timeout already raised out of the request itself and never got here.)
resp = client.get("/gateway/collaborations")
- if resp.is_success:
- bridges = resp.json()
- for bridge in bridges:
- if bridge.get("bridge_type") == "mattermost":
- bridge_id = bridge["bridge_id"]
- print(f"Mattermost bridge already registered: {bridge_id}")
- # Adopt the default on an instance that predates it, so an
- # existing deployment gains the invariant on its next setup run
- # rather than only new ones.
- if not any(b.get("is_default") for b in bridges):
- client.post(
- f"/gateway/collaborations/{bridge_id}/default"
- ).raise_for_status()
- print(f"Set Mattermost bridge as default: {bridge_id}")
- # Same reason: a bridge registered before callbacks existed
- # would otherwise keep drawing cards with no buttons on them,
- # and the only cure would be editing a connection field by
- # hand. Sent every run rather than only when it is missing,
- # because the config a bridge holds is not readable back — it
- # carries the admin password, so no endpoint returns it.
- if MATTERMOST_CALLBACK_BASE_URL:
- client.patch(
- f"/gateway/collaborations/{bridge_id}",
- json={
- "connection_config": {
- "callback_base_url": MATTERMOST_CALLBACK_BASE_URL
- }
- },
- ).raise_for_status()
- print(
- f"Set Mattermost callback address: "
- f"{MATTERMOST_CALLBACK_BASE_URL}"
- )
- return bridge_id
+ if not resp.is_success:
+ print(
+ "ERROR: Could not read the registered collaboration bridges: "
+ f"{resp.status_code} {resp.text}"
+ )
+ resp.raise_for_status()
+ bridges = resp.json()
+ for bridge in bridges:
+ if bridge.get("bridge_type") == "mattermost":
+ bridge_id = bridge["bridge_id"]
+ print(f"Mattermost bridge already registered: {bridge_id}")
+ # Adopt the default on an instance that predates it, so an
+ # existing deployment gains the invariant on its next setup run
+ # rather than only new ones.
+ if not any(b.get("is_default") for b in bridges):
+ client.post(
+ f"/gateway/collaborations/{bridge_id}/default"
+ ).raise_for_status()
+ print(f"Set Mattermost bridge as default: {bridge_id}")
+ # Same reason: a bridge registered before callbacks existed
+ # would otherwise keep drawing cards with no buttons on them,
+ # and the only cure would be editing a connection field by
+ # hand. Sent every run rather than only when it is missing,
+ # because the config a bridge holds is not readable back — it
+ # carries the admin password, so no endpoint returns it.
+ if MATTERMOST_CALLBACK_BASE_URL:
+ client.patch(
+ f"/gateway/collaborations/{bridge_id}",
+ json={
+ "connection_config": {
+ "callback_base_url": MATTERMOST_CALLBACK_BASE_URL
+ }
+ },
+ ).raise_for_status()
+ print(
+ f"Set Mattermost callback address: {MATTERMOST_CALLBACK_BASE_URL}"
+ )
+ return bridge_id
# Register new bridge
connection_config: dict[str, str] = {