Skip to content

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

Go through an organization's own connector app, behind a flag

Petr Bauchdd8b00cd8b67
diff --git a/core/switch_core/bridges/agent/operations/agent_services.py b/core/switch_core/bridges/agent/operations/agent_services.py
index 05c78b539..3bfeddd95 100644
--- a/core/switch_core/bridges/agent/operations/agent_services.py
+++ b/core/switch_core/bridges/agent/operations/agent_services.py
@@ -104,7 +104,7 @@ async def set_agent_service(
"agent": agent.name,
"service": grant.service,
"access": grant.access,
- "summary": broker.summary(agent.display_name or agent.name, grant),
+ "summary": await broker.summary(agent.display_name or agent.name, grant),
"warning": warning,
}
diff --git a/core/switch_core/bridges/agent/operations/my_context.py b/core/switch_core/bridges/agent/operations/my_context.py
index 0357aeca1..4e9f750a6 100644
--- a/core/switch_core/bridges/agent/operations/my_context.py
+++ b/core/switch_core/bridges/agent/operations/my_context.py
@@ -98,7 +98,8 @@ async def get_my_context() -> dict[str, Any]:
set up; `unavailable_code` says why not, one of "switched_off" (its
operator turned it off), "needs_https" (it can't work on this
server), "app_not_set_up" (its operator can set it up under Admin →
- Connector apps in Switch Console), "not_set_up",
+ Connector apps in Switch Console), "organization_app_unusable" (the
+ app your organization saved for it can't be used), "not_set_up",
"not_fully_set_up" or "key_never_expires" (it connects with a key
that never expires, which Switch does not hand to agents);
`connection` is your owner's: "not_connected", "active",
@@ -231,7 +232,7 @@ async def get_my_context() -> dict[str, Any]:
],
"how_to_add": _how_to_add(protocol),
},
- "services": _services(protocol, connections, agents, grants),
+ "services": await _services(protocol, connections, agents, grants),
"agents": [
{
"id": agent.id,
@@ -287,7 +288,7 @@ def _how_to_add(protocol: Any) -> list[dict[str, Any]]:
return entries
-def _services(
+async def _services(
protocol: Any,
connections: dict[str, str],
agents: list[Agent],
@@ -302,13 +303,13 @@ def _services(
definition = entry.definition
if not definition.enabled:
continue
- unavailable = broker.unavailable(definition.slug)
+ unavailable = await broker.unavailable(definition.slug)
entries.append(
{
"slug": definition.slug,
"name": definition.name,
"available": unavailable is None
- and broker.connectable(definition.slug),
+ and await broker.connectable(definition.slug),
"unavailable_reason": None
if unavailable is None
else unavailable.reason,
diff --git a/core/switch_core/config.py b/core/switch_core/config.py
index b692b5ae5..22869354d 100644
--- a/core/switch_core/config.py
+++ b/core/switch_core/config.py
@@ -21,6 +21,7 @@ from switch_core.feature_flags import (
GROUP_CHATS,
HOSTED_AGENTS,
KNOWN_FEATURE_FLAGS,
+ ORGANIZATION_CONNECTOR_APPS,
SERVICE_CONNECTIONS,
)
from switch_core.keys import Keyring
@@ -1324,6 +1325,10 @@ class SwitchConfig(BaseSettings):
def service_connections_enabled(self) -> bool:
return self.feature_flags[SERVICE_CONNECTIONS]
+ @property
+ def organization_connector_apps_enabled(self) -> bool:
+ return self.feature_flags[ORGANIZATION_CONNECTOR_APPS]
+
@property
def follow_the_conversation_enabled(self) -> bool:
return self.feature_flags[FOLLOW_THE_CONVERSATION]
diff --git a/core/switch_core/connections/app_credentials.py b/core/switch_core/connections/app_credentials.py
index 93cfcab50..87f05f612 100644
--- a/core/switch_core/connections/app_credentials.py
+++ b/core/switch_core/connections/app_credentials.py
@@ -44,6 +44,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from starlette.datastructures import State
from switch_core.config import SwitchConfig
+from switch_core.connections.adapters import ServiceAdapter
from switch_core.connections.adapters.github import GitHubApp, load_github_app
from switch_core.connections.adapters.oauth import OAuthClientCredentials
from switch_core.connections.loader import Connection, ConnectionDefinition
@@ -246,6 +247,21 @@ def github_app_from_values(values: AppValues, public_url: str | None) -> GitHubA
return GitHubApp(connections, signer)
+def app_from_values(
+ spec: AppSpec, values: AppValues, public_url: str | None
+) -> tuple[GitHubApp | None, OAuthClientCredentials | None]:
+ """The app saved values describe, as its adapter takes it: a GitHub App
+ or an OAuth client, the other None. Raises `AppLoadError` when they
+ cannot be used."""
+ if spec.kind == "github_app":
+ return github_app_from_values(values, public_url), None
+ if "client_secret" not in values.secrets:
+ raise AppLoadError("The saved client has no client secret.")
+ return None, OAuthClientCredentials(
+ values.client_id, values.secrets["client_secret"], authentication=None
+ )
+
+
def install_github_app(state: State, app: GitHubApp | None) -> None:
"""Put `app` on the gateway's state. A sign-in under way is a row in
`sign_in_flows`, so it outlives the swap on its own; one that started
@@ -363,19 +379,33 @@ class ConnectorApps:
effective = await self.reload(service)
broker.replace_adapter(
service,
- build_adapter(
+ self.build(
service,
- self._catalog[service].definition,
github_app=effective.github_app,
oauth_client=effective.oauth_client,
- http=self._http,
- registration=self._registration,
),
)
if effective.spec.kind == "github_app":
install_github_app(state, effective.github_app)
return effective
+ def build(
+ self,
+ service: str,
+ *,
+ github_app: GitHubApp | None,
+ oauth_client: OAuthClientCredentials | None,
+ ) -> ServiceAdapter | None:
+ """`service`'s adapter over the given app, or None without one."""
+ return build_adapter(
+ service,
+ self._catalog[service].definition,
+ github_app=github_app,
+ oauth_client=oauth_client,
+ http=self._http,
+ registration=self._registration,
+ )
+
def effective(self, service: str) -> EffectiveCredentials:
effective = self._effective.get(service)
if effective is None:
@@ -463,16 +493,7 @@ class ConnectorApps:
return self._none(spec, load_error=UNREADABLE, unreadable=True)
values = AppValues(row.client_id, dict(row.settings), secrets)
try:
- if spec.kind == "github_app":
- github_app = github_app_from_values(values, self.public_url)
- oauth_client = None
- else:
- if "client_secret" not in secrets:
- raise AppLoadError("The saved client has no client secret.")
- github_app = None
- oauth_client = OAuthClientCredentials(
- row.client_id, secrets["client_secret"], authentication=None
- )
+ github_app, oauth_client = app_from_values(spec, values, self.public_url)
except AppLoadError as error:
logger.error(
"The app saved for %s cannot be used, so it is not set up: %s",
diff --git a/core/switch_core/connections/app_signals.py b/core/switch_core/connections/app_signals.py
index d527fcc53..e43eeab0b 100644
--- a/core/switch_core/connections/app_signals.py
+++ b/core/switch_core/connections/app_signals.py
@@ -15,6 +15,14 @@ before announcing it. An apply that fails is kept and tried again, and every
process re-checks on reconnecting and every `RECHECK_SECONDS` which rows
changed since it resolved them, so a missed signal costs a delay rather than
a process using an old app until it restarts.
+
+An organization's own app (`organization_apps.py`) is announced the same way,
+with the organization's id beside the service. A process hearing it forgets
+the adapter it kept for that organization and service, and builds it again
+from the row the next time one of the organization's workspaces needs it.
+Forgetting reads nothing, so there is nothing to retry; and the kept adapters
+are trusted only for a while, which covers a missed announcement. On
+reconnecting, a process forgets them all.
"""
from __future__ import annotations
@@ -26,6 +34,7 @@ if TYPE_CHECKING:
from switch_core.connections.app_credentials import ConnectorApps
from switch_core.connections.broker import ServiceBroker
+ from switch_core.connections.organization_apps import OrganizationApps
from switch_core.signals import SignalBus
CONNECTOR_APP_CHANGED_SIGNAL = "connector_app.changed"
@@ -36,23 +45,39 @@ class ConnectorAppSignals:
self,
*,
apps: ConnectorApps,
+ organization_apps: OrganizationApps | None,
broker: ServiceBroker,
state: State,
bus: SignalBus,
) -> None:
self._apps = apps
+ self._organization_apps = organization_apps
self._broker = broker
self._state = state
self._bus = bus
self._owed = bus.pending_work("connector apps", self._apply)
bus.handle(CONNECTOR_APP_CHANGED_SIGNAL, self._on_changed)
bus.on_resync(self.recheck)
+ bus.on_resync(self._forget_organizations)
bus.on_recheck(self.recheck)
apps.use_signals(self)
+ if organization_apps is not None:
+ organization_apps.use_signals(self)
def changed(self, service: str) -> None:
self._bus.publish(CONNECTOR_APP_CHANGED_SIGNAL, service=service)
+ def organization_changed(self, organization_id: str, service: str) -> None:
+ self._bus.publish(
+ CONNECTOR_APP_CHANGED_SIGNAL,
+ service=service,
+ organization_id=organization_id,
+ )
+
+ async def _forget_organizations(self) -> None:
+ if self._organization_apps is not None:
+ self._organization_apps.forget_all()
+
async def recheck(self) -> None:
"""Apply again every app whose saved row changed since this process
resolved it."""
@@ -62,8 +87,13 @@ class ConnectorAppSignals:
def _on_changed(self, signal: dict[str, Any]) -> None:
service = signal["service"]
# A service this build has no app for, from a newer process mid-roll.
- if service in self._apps.specs:
+ if service not in self._apps.specs:
+ return
+ organization_id = signal.get("organization_id")
+ if organization_id is None:
self._owed.add(service)
+ elif self._organization_apps is not None:
+ self._organization_apps.forget(organization_id, service)
async def _apply(self, service: str) -> None:
await self._apps.apply(service, broker=self._broker, state=self._state)
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.py
index a8e9f96ea..00c60447e 100644
--- a/core/switch_core/connections/broker.py
+++ b/core/switch_core/connections/broker.py
@@ -65,6 +65,7 @@ from switch_core.connections.loader import (
ConnectionDefinition,
TokenPolicy,
)
+from switch_core.connections.organization_apps import OrganizationApps
from switch_core.connections.shielded import finish_shielded
from switch_core.db.audit import AuditAction, record_audit_event
from switch_core.db.models import (
@@ -157,6 +158,9 @@ UnavailableCode = Literal[
"app_not_set_up",
# Nothing on this server connects it, and nothing the operator saves would.
"not_set_up",
+ # The workspace's organization saved its own app for it, and that app
+ # can't be used; the server's is not used in its place.
+ "organization_app_unusable",
# It can be connected but not granted: its app is missing a part.
"not_fully_set_up",
# Its credential (a pasted key) never expires, so it is never handed to
@@ -165,6 +169,14 @@ UnavailableCode = Literal[
]
+@dataclass(frozen=True)
+class _Choice:
+ adapter: ServiceAdapter | None
+ # Why the workspace's organization's app can't be used, where that app
+ # is the one that applies.
+ problem: str | None
+
+
@dataclass(frozen=True)
class Unavailable:
code: UnavailableCode
@@ -378,6 +390,7 @@ class ServiceBroker:
keyring: Keyring,
catalog: dict[str, Connection],
adapters: dict[str, ServiceAdapter],
+ organization_apps: OrganizationApps | None,
disabled: dict[str, str],
store: ServiceConnectionStore,
token_retention: timedelta,
@@ -396,7 +409,10 @@ class ServiceBroker:
self._session_factory = session_factory
self._keyring = keyring
self._catalog = catalog
+ # The server's adapters, by service.
self._adapters = adapters
+ # Organizations' own apps, with `organization_connector_apps` on.
+ self._organization_apps = organization_apps
# Switched off here, by service, with the reason people are shown.
self._disabled = {
service: reason or SWITCHED_OFF for service, reason in disabled.items()
@@ -421,7 +437,7 @@ class ServiceBroker:
)
return entry
- def sign_in(self, service: str) -> tuple[ConnectionDefinition, OAuthAdapter]:
+ async def sign_in(self, service: str) -> tuple[ConnectionDefinition, OAuthAdapter]:
"""The catalog entry and adapter a person signs in to `service`
through, with Switch's generic OAuth flow (`connections/flows.py`)."""
definition = self._entry(service).definition
@@ -433,12 +449,12 @@ class ServiceBroker:
f"{definition.name} is connected through its own sign-in.",
retryable=False,
)
- adapter = self._adapters.get(service)
+ adapter = await self._here(service)
if not isinstance(adapter, OAuthAdapter):
raise ServiceError(
503,
INTERNAL,
- self.availability(service) or "Not available on this server.",
+ await self.availability(service) or "Not available on this server.",
retryable=False,
)
return definition, adapter
@@ -474,30 +490,46 @@ class ServiceBroker:
return set()
return await self._workspace_services.disabled_services(session, self._catalog)
- def _adapter(self, service: str) -> ServiceAdapter:
+ async def _choose(self, service: str) -> _Choice:
+ """The adapter `service` goes through in the bound workspace: its
+ organization's app where it saved one, else the server's."""
+ if self._organization_apps is not None:
+ app = await self._organization_apps.for_workspace(
+ require_tenant_id(), service
+ )
+ if app is not None:
+ return _Choice(app.adapter, app.problem)
+ return _Choice(self._adapters.get(service), None)
+
+ async def _here(self, service: str) -> ServiceAdapter | None:
+ """`service`'s adapter in the bound workspace, or None."""
+ return (await self._choose(service)).adapter
+
+ async def _adapter(self, service: str) -> ServiceAdapter:
"""The adapter that issues for `service`, or the refusal saying why not."""
- adapter = self._refresher(service)
+ adapter = await self._refresher(service)
if not adapter.can_issue:
raise ServiceError(
503,
INTERNAL,
- self.availability(service) or "Not available on this server.",
+ await self.availability(service) or "Not available on this server.",
retryable=False,
)
return adapter
- def _refresher(self, service: str) -> ServiceAdapter:
+ async def _refresher(self, service: str) -> ServiceAdapter:
"""The adapter that keeps `service`'s connections signed in."""
- adapter = self._adapters.get(service)
- if adapter is None:
+ choice = await self._choose(service)
+ if choice.adapter is None:
raise ServiceError(
503,
INTERNAL,
- f"{self._entry(service).definition.name} is not available on "
+ choice.problem
+ or f"{self._entry(service).definition.name} is not available on "
"this server.",
retryable=False,
)
- return adapter
+ return choice.adapter
# ── Grant decision ───────────────────────────────────────────────────────
@@ -695,7 +727,7 @@ class ServiceBroker:
service: str,
) -> ServiceToken:
decision = await self.decide(session, agent_id, principal, service)
- adapter = self._adapter(service)
+ adapter = await self._adapter(service)
await session.commit()
access = await self._access_token(decision.owner_id, service, adapter)
request = IssueRequest(
@@ -1002,7 +1034,7 @@ class ServiceBroker:
"Connections.",
retryable=False,
)
- if self.client_replaced(connection):
+ if self._client_replaced(connection, adapter):
await self._store.mark_needs_reauthorization(
session, owner_id, service, CLIENT_REPLACED
)
@@ -1157,7 +1189,7 @@ class ServiceBroker:
)
return grants
- def connectable(self, service: str) -> bool:
+ async def connectable(self, service: str) -> bool:
"""Whether a person can connect `service` here: it has an adapter, is
not switched off, and what it connects could be handed to an agent."""
entry = self._catalog.get(service)
@@ -1165,26 +1197,36 @@ class ServiceBroker:
entry is not None
and entry.definition.enabled
and _issued_to_agents(entry)
- and service in self._adapters
and service not in self._disabled
+ and await self._here(service) is not None
)
- def server_setup_needed(self, service: str) -> bool:
+ async def server_setup_needed(self, service: str) -> bool:
"""Whether `service` waits on the operator setting up its app here."""
- unavailable = self.unavailable(service)
+ unavailable = await self.unavailable(service)
return unavailable is not None and unavailable.code == "app_not_set_up"
- def client_replaced(self, connection: ServiceConnection) -> bool:
+ async def client_replaced(self, connection: ServiceConnection) -> bool:
"""Whether `connection` signed in through a client its service no
- longer uses, so it must reconnect. Unknown either side is no."""
+ longer uses here, so it must reconnect. Unknown either side is no."""
if connection.oauth_client_id is None:
return False
- adapter = self._adapters.get(connection.service)
+ return self._client_replaced(connection, await self._here(connection.service))
+
+ @staticmethod
+ def _client_replaced(
+ connection: ServiceConnection, adapter: ServiceAdapter | None
+ ) -> bool:
current = None if adapter is None else adapter.current_client_id()
- return current is not None and current != connection.oauth_client_id
+ return (
+ connection.oauth_client_id is not None
+ and current is not None
+ and current != connection.oauth_client_id
+ )
def replace_adapter(self, service: str, adapter: ServiceAdapter | None) -> None:
- """Use `adapter` for `service` from the next call on, or none.
+ """Use `adapter` for `service` from the next call on, or none, where
+ the server's app applies.
One assignment on the event loop, so a reader sees the old adapter or
the new one, never a mix. A call already under way finishes on the
@@ -1197,14 +1239,15 @@ class ServiceBroker:
else:
self._adapters[service] = adapter
- def availability(self, service: str) -> str | None:
- """Why `service` cannot be granted on this server, or None if it can."""
- unavailable = self.unavailable(service)
+ async def availability(self, service: str) -> str | None:
+ """Why `service` cannot be granted in the bound workspace, or None if
+ it can."""
+ unavailable = await self.unavailable(service)
return None if unavailable is None else unavailable.reason
- def unavailable(self, service: str) -> Unavailable | None:
- """Why `service` cannot be granted on this server, as a code and the
- sentence people read, or None if it can."""
+ async def unavailable(self, service: str) -> Unavailable | None:
+ """Why `service` cannot be granted in the bound workspace, as a code
+ and the sentence people read, or None if it can."""
entry = self._catalog.get(service)
if entry is None or not entry.definition.enabled:
return Unavailable("coming_soon", "Not available yet.")
@@ -1221,11 +1264,14 @@ class ServiceBroker:
and "loopback" not in oauth.redirect
):
return Unavailable("needs_public_url", needs_public_url(name))
- adapter = self._adapters.get(service)
+ choice = await self._choose(service)
+ adapter = choice.adapter
if adapter is None:
kind = app_kind(entry.definition)
if github_needs_https(kind, self._public_url):
return Unavailable("needs_https", cannot_work_here(name))
+ if choice.problem is not None:
+ return Unavailable("organization_app_unusable", choice.problem)
if kind is not None:
return Unavailable("app_not_set_up", not_set_up(name))
return Unavailable("not_set_up", f"{name} is not set up on this server.")
@@ -1237,10 +1283,10 @@ class ServiceBroker:
)
return None
- def summary(self, agent_name: str, grant: ServiceGrant) -> str:
+ async def summary(self, agent_name: str, grant: ServiceGrant) -> str:
"""A grant's reach in a sentence, as the vendor's adapter words it."""
access: AccessLevel = "write" if grant.access == "write" else "read"
- adapter = self._adapters.get(grant.service)
+ adapter = await self._here(grant.service)
if adapter is not None:
return adapter.summary(agent_name, access, dict(grant.resources))
verb = "read and write" if access == "write" else "read"
@@ -1263,13 +1309,13 @@ class ServiceBroker:
raise ServiceError(
404, NOT_FOUND, f"No service named {service!r}.", retryable=False
)
- unavailable = self.availability(service)
+ unavailable = await self.availability(service)
if unavailable is not None:
raise ServiceError(422, VALIDATION_ERROR, unavailable, retryable=False)
await self.require_workspace_enabled(session, service)
entry = self._entry(service)
name = entry.definition.name
- adapter = self._adapter(service)
+ adapter = await self._adapter(service)
connection = await self._store.get_connection(session, owner_id, service)
if connection is None:
raise ServiceError(
@@ -1594,7 +1640,7 @@ class ServiceBroker:
warnings = []
if replaced is not None and replaced.access_token != secret.access_token:
- adapter = self._adapters.get(service)
+ adapter = await self._here(service)
try:
if adapter is None:
raise ServiceUnavailableError(
@@ -1632,7 +1678,7 @@ class ServiceBroker:
f"Switch could not revoke the sign-in {name} just issued. Remove "
f"Switch from your {name} account's connected apps."
)
- adapter = self._adapters.get(service)
+ adapter = await self._here(service)
try:
if adapter is None:
raise ServiceUnavailableError(
@@ -1672,12 +1718,12 @@ class ServiceBroker:
raise ServiceError(
403, FORBIDDEN, _never_expires(definition.name), retryable=False
)
- adapter = self._adapters.get(service)
+ adapter = await self._here(service)
if not isinstance(adapter, ApiKeyAdapter):
raise ServiceError(
503,
INTERNAL,
- self.availability(service) or "Not available on this server.",
+ await self.availability(service) or "Not available on this server.",
retryable=False,
)
try:
@@ -1716,7 +1762,7 @@ class ServiceBroker:
return None
await session.commit()
return (
- await self._access_token(user_id, service, self._refresher(service))
+ await self._access_token(user_id, service, await self._refresher(service))
).token
async def queue_agent_revocation(
@@ -1856,7 +1902,7 @@ class ServiceBroker:
for service, secret in removed:
entry = self._catalog.get(service)
name = service if entry is None else entry.definition.name
- adapter = self._adapters.get(service)
+ adapter = await self._here(service)
if adapter is None:
logger.warning(
"%s sign-in not revoked: the service is not set up on this server",
@@ -2006,7 +2052,7 @@ class ServiceBroker:
async def revoke(
row_id: str, service: str, encrypted: str | None
) -> str | None:
- adapter = self._adapters.get(service)
+ adapter = await self._here(service)
try:
if adapter is None or encrypted is None:
raise ServiceUnavailableError(
diff --git a/core/switch_core/connections/organization_apps.py b/core/switch_core/connections/organization_apps.py
new file mode 100644
index 000000000..a2369eae4
--- /dev/null
+++ b/core/switch_core/connections/organization_apps.py
@@ -0,0 +1,209 @@
+"""The connector apps organizations registered, as the broker uses them.
+
+A workspace whose organization saved an app for a service
+(`organization_app_credentials`) is issued and refreshed through that app;
+one whose organization saved none goes through the server's, as
+`connections/app_credentials.py` resolves it. Only GitHub's App and the
+static OAuth clients have one. All of this is behind the
+`organization_connector_apps` flag: with it off the broker holds no
+`OrganizationApps`, and no workspace looks past the server's apps.
+
+An organization's adapter is built the first time one of its workspaces
+needs it, and kept. The process that changes an organization's app forgets
+the adapter at once and announces the change (`app_signals.py`), and every
+other process forgets it on hearing so. A signal can be missed, so a kept
+adapter is trusted for `TRUSTED_SECONDS` after its row was last read; the
+next lookup after that reads the row again and rebuilds only if the row
+changed. Which organization a workspace belongs to is read once and kept:
+workspaces do not move between organizations.
+
+An organization's app that cannot be used leaves the service unavailable in
+its workspaces, with the reason. It does not fall back to the server's app:
+that is not the app the organization chose, and its members would be signed
+in to the vendor through someone else's.
+"""
+
+from __future__ import annotations
+
+import logging
+import time
+from dataclasses import dataclass
+from datetime import datetime
+from typing import TYPE_CHECKING
+
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.connections.adapters import ServiceAdapter
+from switch_core.connections.app_credentials import (
+ UNREADABLE,
+ AppLoadError,
+ AppValues,
+ ConnectorApps,
+ app_from_values,
+)
+from switch_core.db.models import OrganizationAppCredential, Tenant
+from switch_core.db.session_scope import tenant_session
+from switch_core.db.stores.organization_app_credential_store import (
+ OrganizationAppCredentialStore,
+)
+from switch_core.keys import UndecryptableError
+from switch_core.signals import RECHECK_SECONDS
+
+if TYPE_CHECKING:
+ from switch_core.connections.app_signals import ConnectorAppSignals
+
+logger = logging.getLogger(__name__)
+
+# How long a kept adapter stands for its row without reading it again: what a
+# missed announcement costs, as the server's apps' re-check does.
+TRUSTED_SECONDS = RECHECK_SECONDS
+
+
+def unusable(name: str, error: str) -> str:
+ """What members are told about a service whose organization's app
+ cannot be used."""
+ return (
+ f"Your organization's {name} app can't be used: {error} An organization "
+ "admin can fix it under Organization, Connectors."
+ )
+
+
+@dataclass(frozen=True)
+class OrganizationApp:
+ """The app an organization saved for a service, as built here.
+
+ `adapter` is None when the app cannot be used, and `problem` then says
+ why, for members to read."""
+
+ organization_id: str
+ adapter: ServiceAdapter | None
+ problem: str | None
+ changed_at: datetime
+
+
+@dataclass
+class _Kept:
+ # None when the organization saved no app for the service.
+ app: OrganizationApp | None
+ read_at: float
+
+
+class OrganizationApps:
+ def __init__(
+ self,
+ *,
+ apps: ConnectorApps,
+ store: OrganizationAppCredentialStore,
+ session_factory: async_sessionmaker[AsyncSession],
+ ) -> None:
+ self._apps = apps
+ self._store = store
+ self._session_factory = session_factory
+ self._organization_of: dict[str, str] = {}
+ self._kept: dict[tuple[str, str], _Kept] = {}
+ # Bumped by each `forget`, so a build that read the row before it
+ # does not keep what it read.
+ self._generation: dict[tuple[str, str], int] = {}
+ self._signals: ConnectorAppSignals | None = None
+
+ def use_signals(self, signals: ConnectorAppSignals) -> None:
+ """Announce each change made here, so every other process forgets
+ the adapter too (`app_signals.py`)."""
+ self._signals = signals
+
+ async def for_workspace(
+ self, tenant_id: str, service: str
+ ) -> OrganizationApp | None:
+ """The app `tenant_id`'s organization saved for `service`, or None
+ when it saved none and the server's applies."""
+ if service not in self._apps.specs:
+ return None
+ organization_id = self._organization_of.get(tenant_id)
+ if organization_id is not None:
+ kept = self._kept.get((organization_id, service))
+ if kept is not None and time.monotonic() - kept.read_at < TRUSTED_SECONDS:
+ return kept.app
+ async with tenant_session(self._session_factory, tenant_id) as session:
+ if organization_id is None:
+ organization_id = await session.scalar(
+ select(Tenant.organization_id).where(Tenant.id == tenant_id)
+ )
+ # A workspace a process from before organizations created.
+ if organization_id is None:
+ return None
+ self._organization_of[tenant_id] = organization_id
+ key = (organization_id, service)
+ generation = self._generation.get(key, 0)
+ read_at = time.monotonic()
+ row = await self._store.get(session, organization_id, service)
+ kept = self._kept.get(key)
+ if row is None:
+ app = None
+ elif (
+ kept is not None
+ and kept.app is not None
+ and kept.app.changed_at == row.updated_at
+ ):
+ app = kept.app
+ else:
+ app = self._build(row)
+ if self._generation.get(key, 0) == generation:
+ self._kept[key] = _Kept(app, read_at)
+ return app
+
+ def changed(self, organization_id: str, service: str) -> None:
+ """The organization's app for `service` was saved, replaced or
+ removed, and the change has committed: forget the adapter here and
+ tell every other process to."""
+ self.forget(organization_id, service)
+ if self._signals is not None:
+ self._signals.organization_changed(organization_id, service)
+
+ def forget(self, organization_id: str, service: str) -> None:
+ """Drop the adapter kept for the organization's app, after it changed."""
+ key = (organization_id, service)
+ self._kept.pop(key, None)
+ self._generation[key] = self._generation.get(key, 0) + 1
+
+ def forget_all(self) -> None:
+ """Drop every kept adapter: after this process may have missed
+ announcements, as on reconnecting to the database."""
+ for organization_id, service in list(self._kept):
+ self.forget(organization_id, service)
+
+ def _build(self, row: OrganizationAppCredential) -> OrganizationApp:
+ spec = self._apps.specs[row.service]
+ try:
+ try:
+ secrets = self._store.secrets(row)
+ except UndecryptableError:
+ raise AppLoadError(UNREADABLE) from None
+ github_app, oauth_client = app_from_values(
+ spec,
+ AppValues(row.client_id, dict(row.settings), secrets),
+ self._apps.public_url,
+ )
+ except AppLoadError as error:
+ logger.error(
+ "The %s app organization %s saved cannot be used, so %s is "
+ "unavailable in its workspaces: %s",
+ spec.name,
+ row.organization_id,
+ spec.name,
+ error,
+ )
+ return OrganizationApp(
+ organization_id=row.organization_id,
+ adapter=None,
+ problem=unusable(spec.name, str(error)),
+ changed_at=row.updated_at,
+ )
+ return OrganizationApp(
+ organization_id=row.organization_id,
+ adapter=self._apps.build(
+ row.service, github_app=github_app, oauth_client=oauth_client
+ ),
+ problem=None,
+ changed_at=row.updated_at,
+ )
diff --git a/core/switch_core/feature_flags.py b/core/switch_core/feature_flags.py
index 81ea0cfb0..ab4abf389 100644
--- a/core/switch_core/feature_flags.py
+++ b/core/switch_core/feature_flags.py
@@ -72,6 +72,13 @@ SWITCH_BUDDY = "switch_buddy"
# turning it on before the rollout finishes is not protected.
GROUP_CHATS = "group_chats"
+# Connector apps per organization: a workspace signs in to GitHub and the
+# static OAuth services through the app its organization registered, where
+# it registered one, and through the server's otherwise
+# (`connections/organization_apps.py`). With it off every workspace uses the
+# server's apps, as before organizations.
+ORGANIZATION_CONNECTOR_APPS = "organization_connector_apps"
+
# Every flag the server recognises.
KNOWN_FEATURE_FLAGS: frozenset[str] = frozenset(
{
@@ -84,5 +91,6 @@ KNOWN_FEATURE_FLAGS: frozenset[str] = frozenset(
AGENT_CHATS,
SWITCH_BUDDY,
GROUP_CHATS,
+ ORGANIZATION_CONNECTOR_APPS,
}
)
diff --git a/core/switch_core/gateway/service_connections.py b/core/switch_core/gateway/service_connections.py
index 40fb52355..2b5aa868b 100644
--- a/core/switch_core/gateway/service_connections.py
+++ b/core/switch_core/gateway/service_connections.py
@@ -68,7 +68,7 @@ async def _owned_agent(session: AsyncSession, agent_id: str, user: User) -> Agen
return agent
-def _grant_view(broker: ServiceBroker, agent: Agent, grant: ServiceGrant) -> dict:
+async def _grant_view(broker: ServiceBroker, agent: Agent, grant: ServiceGrant) -> dict:
entry = CATALOG.get(grant.service)
access: AccessLevel = "write" if grant.access == "write" else "read"
return {
@@ -83,17 +83,17 @@ def _grant_view(broker: ServiceBroker, agent: Agent, grant: ServiceGrant) -> dic
else effective_tools(entry, access, grant.tool_mode, list(grant.tools))
),
"resources": dict(grant.resources),
- "summary": broker.summary(agent.display_name or agent.name, grant),
+ "summary": await broker.summary(agent.display_name or agent.name, grant),
}
-def _status(broker: ServiceBroker, connection: ServiceConnection | None) -> str:
+async def _status(broker: ServiceBroker, connection: ServiceConnection | None) -> str:
"""The connection's status, as a person acts on it: one made through an
app this server has since replaced needs reconnecting before anyone uses
it and finds out."""
if connection is None:
return "not_connected"
- if connection.status == "active" and broker.client_replaced(connection):
+ if connection.status == "active" and await broker.client_replaced(connection):
return "needs_reauthorization"
return connection.status
@@ -114,7 +114,7 @@ async def list_service_connections(
for entry in CATALOG.values():
definition = entry.definition
connection = connections.get(definition.slug)
- unavailable = broker.unavailable(definition.slug)
+ unavailable = await broker.unavailable(definition.slug)
entries.append(
{
"slug": definition.slug,
@@ -123,7 +123,7 @@ async def list_service_connections(
"description": definition.description,
"enabled": definition.enabled,
"auth_type": definition.auth.type,
- "connectable": broker.connectable(definition.slug),
+ "connectable": await broker.connectable(definition.slug),
"configured": unavailable is None,
"unavailable_reason": None
if unavailable is None
@@ -133,7 +133,9 @@ async def list_service_connections(
"unavailable_code": None if unavailable is None else unavailable.code,
# Whether it waits on the server's operator setting up its app
# under Connectors, so a client can offer them that.
- "server_setup_needed": broker.server_setup_needed(definition.slug),
+ "server_setup_needed": await broker.server_setup_needed(
+ definition.slug
+ ),
# Whether a workspace admin left it on for this workspace.
"workspace_enabled": definition.slug not in switched_off,
# A grant hands the agent the owner's own token: it is on or
@@ -155,7 +157,7 @@ async def list_service_connections(
# What a person pastes to connect a service connected with a
# key, and where they make one; null for any other.
"key": _key_form(definition),
- "status": _status(broker, connection),
+ "status": await _status(broker, connection),
"consent": None if connection is None else connection.consent,
"external_identity": (
None if connection is None else connection.external_identity
@@ -314,7 +316,7 @@ async def list_service_grants(
agent = await _owned_agent(session, agent_id, user)
grants = await STORE.list_grants(session, agent.id)
return {
- "grants": [_grant_view(broker, agent, grant) for grant in grants],
+ "grants": [await _grant_view(broker, agent, grant) for grant in grants],
"addressing_open": await _others_can_address(session, agent, user),
}
@@ -372,7 +374,7 @@ async def set_service_grant(
)
except ServiceError as error:
return service_refusal(error)
- return {"grant": _grant_view(broker, agent, grant), "warning": warning}
+ return {"grant": await _grant_view(broker, agent, grant), "warning": warning}
@router.delete("/agents/{agent_id}/service-grants/{service}", response_model=None)
diff --git a/core/switch_core/gateway/service_flows.py b/core/switch_core/gateway/service_flows.py
index d84aafccb..d61a62777 100644
--- a/core/switch_core/gateway/service_flows.py
+++ b/core/switch_core/gateway/service_flows.py
@@ -120,7 +120,7 @@ async def start(
"""Begin signing in: where to send the browser, and which way it returns."""
try:
await broker.require_workspace_enabled(session, service)
- definition, adapter = broker.sign_in(service)
+ definition, adapter = await broker.sign_in(service)
except ServiceError as error:
return service_refusal(error)
tenant = await session.get(Tenant, require_tenant_id())
@@ -174,7 +174,7 @@ async def authorize(
) -> Response:
"""The browser leaves for the vendor through Core, which marks it."""
try:
- definition, adapter = broker.sign_in(service)
+ definition, adapter = await broker.sign_in(service)
except ServiceError:
return _page("Connections", INTERRUPTED, 400)
try:
@@ -247,7 +247,7 @@ async def complete(
Core exchanges it, reads the account and writes the connection."""
flow = await _owned(flows, service, flow_id, user)
try:
- _, adapter = broker.sign_in(service)
+ _, adapter = await broker.sign_in(service)
except ServiceError as error:
return service_refusal(error)
try:
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 23548d9d5..01f0c6308 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -108,6 +108,7 @@ from switch_core.connections.loader import CATALOG
from switch_core.connections.maintenance import (
maintenance_loop as service_token_maintenance_loop,
)
+from switch_core.connections.organization_apps import OrganizationApps
from switch_core.connections.registry import ClientRegistration, build_adapters
from switch_core.db import encrypted_json
from switch_core.db.boot_lock import boot_lock
@@ -154,6 +155,9 @@ from switch_core.db.stores.media_store import MediaStore
from switch_core.db.stores.message_store import MessageStore
from switch_core.db.stores.messaging_event_store import MessagingEventReceiptStore
from switch_core.db.stores.messaging_install_store import MessagingInstallStore
+from switch_core.db.stores.organization_app_credential_store import (
+ OrganizationAppCredentialStore,
+)
from switch_core.db.stores.organization_store import OrganizationStore
from switch_core.db.stores.ownership_lease_store import OwnershipLeaseStore
from switch_core.db.stores.package_store import PackageStore
@@ -1363,6 +1367,17 @@ async def run(config: SwitchConfig, process_id: str) -> None:
# A service is issued only where this server is set up for it, as its
# catalog entry's adapter says (`connections/registry.py`). Without any,
# the broker's upkeep still revokes and prunes whatever records exist.
+ # With `organization_connector_apps` on, a workspace whose organization
+ # saved its own app goes through that one (`connections/organization_apps.py`).
+ organization_apps = (
+ OrganizationApps(
+ apps=connector_apps,
+ store=OrganizationAppCredentialStore(config.keyring),
+ session_factory=session_factory,
+ )
+ if config.organization_connector_apps_enabled
+ else None
+ )
service_broker = ServiceBroker(
session_factory=session_factory,
keyring=config.keyring,
@@ -1374,6 +1389,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
http=service_http,
registration=service_registration,
),
+ organization_apps=organization_apps,
disabled=config.disabled_services,
store=ServiceConnectionStore(),
token_retention=timedelta(days=config.service_token_retention_days),
@@ -1386,6 +1402,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
# A connector app the operator saves or removes, put to work in every process.
ConnectorAppSignals(
apps=connector_apps,
+ organization_apps=organization_apps,
broker=service_broker,
state=gateway_app.state,
bus=signal_bus,
diff --git a/core/switch_core/template_runs.py b/core/switch_core/template_runs.py
index 436be5e85..dd22dd4dd 100644
--- a/core/switch_core/template_runs.py
+++ b/core/switch_core/template_runs.py
@@ -402,7 +402,7 @@ class TemplateAgents:
)
)
for connection in plan.connections:
- unavailable = self._broker.availability(connection.service)
+ unavailable = await self._broker.availability(connection.service)
if unavailable is not None:
problems.append(
Problem(
diff --git a/core/tests/switch_core/connections/adapters/test_oauth.py b/core/tests/switch_core/connections/adapters/test_oauth.py
index 044d3d779..7edb73480 100644
--- a/core/tests/switch_core/connections/adapters/test_oauth.py
+++ b/core/tests/switch_core/connections/adapters/test_oauth.py
@@ -527,6 +527,7 @@ class TestThroughTheBroker:
keyring=TEST_KEYRING,
catalog=pass_through_catalog(tmp_path / "catalog"),
adapters={"example": adapter},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
@@ -608,6 +609,7 @@ class TestThroughTheBroker:
keyring=TEST_KEYRING,
catalog=pass_through_catalog(tmp_path / "catalog"),
adapters={"example": adapter},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/connections/github_seed.py b/core/tests/switch_core/connections/github_seed.py
index 8bfe95bfa..fd006a358 100644
--- a/core/tests/switch_core/connections/github_seed.py
+++ b/core/tests/switch_core/connections/github_seed.py
@@ -84,6 +84,7 @@ def github_broker(
keyring=keyring,
catalog=CATALOG,
adapters={"github": vendor},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/connections/service_harness.py b/core/tests/switch_core/connections/service_harness.py
index 9b3b80dba..dc72af963 100644
--- a/core/tests/switch_core/connections/service_harness.py
+++ b/core/tests/switch_core/connections/service_harness.py
@@ -71,6 +71,7 @@ def build_service_harness(
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={"github": vendor},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/connections/test_app_credentials.py b/core/tests/switch_core/connections/test_app_credentials.py
index cf80180d2..fd19db5d7 100644
--- a/core/tests/switch_core/connections/test_app_credentials.py
+++ b/core/tests/switch_core/connections/test_app_credentials.py
@@ -124,6 +124,7 @@ def _broker(session_factory: async_sessionmaker[AsyncSession]) -> ServiceBroker:
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={},
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
@@ -331,7 +332,7 @@ class TestApply:
apps = _apps(session_factory)
await apps.load()
broker = _broker(session_factory)
- assert broker.connectable("google-workspace") is False
+ assert await broker.connectable("google-workspace") is False
await _save(
session_factory,
@@ -341,15 +342,15 @@ class TestApply:
)
await apps.apply("google-workspace", broker=broker, state=State())
- assert broker.connectable("google-workspace") is True
- _, adapter = broker.sign_in("google-workspace")
+ assert await broker.connectable("google-workspace") is True
+ _, adapter = await broker.sign_in("google-workspace")
assert isinstance(adapter, OAuthAdapter)
async with session_factory() as session:
await apps.store.remove(session, "google-workspace")
await session.commit()
await apps.apply("google-workspace", broker=broker, state=State())
- assert broker.connectable("google-workspace") is False
+ assert await broker.connectable("google-workspace") is False
async def test_the_saved_github_app_is_swapped_in_place(
self, session_factory
diff --git a/core/tests/switch_core/connections/test_app_signals.py b/core/tests/switch_core/connections/test_app_signals.py
index cd50874ef..b95016cb1 100644
--- a/core/tests/switch_core/connections/test_app_signals.py
+++ b/core/tests/switch_core/connections/test_app_signals.py
@@ -10,7 +10,7 @@ from __future__ import annotations
import asyncio
import uuid
-from collections.abc import AsyncIterator, Callable
+from collections.abc import AsyncIterator, Awaitable, Callable
from dataclasses import dataclass
import pytest
@@ -58,13 +58,19 @@ async def _process(
broker = _broker(session_factory)
state = State()
bus = _bus(postgres_url, session_factory)
- signals = ConnectorAppSignals(apps=apps, broker=broker, state=state, bus=bus)
+ signals = ConnectorAppSignals(
+ apps=apps, organization_apps=None, broker=broker, state=state, bus=bus
+ )
return _Process(apps, broker, state, bus, signals)
-async def _eventually(check: Callable[[], bool]) -> None:
+async def _not(answer: Awaitable[bool]) -> bool:
+ return not await answer
+
+
+async def _eventually(check: Callable[[], Awaitable[bool]]) -> None:
async with asyncio.timeout(DELIVERY_TIMEOUT):
- while not check():
+ while not await check():
await asyncio.sleep(0.02)
@@ -89,7 +95,7 @@ class TestAcrossProcesses:
two_processes: tuple[_Process, _Process],
) -> None:
here, there = two_processes
- assert there.broker.connectable(GOOGLE) is False
+ assert await there.broker.connectable(GOOGLE) is False
# As the save route does: commit, apply here, announce.
await _save(
@@ -118,7 +124,7 @@ class TestAcrossProcesses:
)
for process in (here, there):
await process.apps.apply(GOOGLE, broker=process.broker, state=process.state)
- assert there.broker.connectable(GOOGLE) is True
+ assert await there.broker.connectable(GOOGLE) is True
async with session_factory() as session:
await here.apps.store.remove(session, GOOGLE)
@@ -126,7 +132,7 @@ class TestAcrossProcesses:
await here.apps.apply(GOOGLE, broker=here.broker, state=here.state)
here.apps.announce(GOOGLE)
- await _eventually(lambda: not there.broker.connectable(GOOGLE))
+ await _eventually(lambda: _not(there.broker.connectable(GOOGLE)))
class TestRecheck:
diff --git a/core/tests/switch_core/connections/test_broker.py b/core/tests/switch_core/connections/test_broker.py
index b8afd10ff..fbe2ad85f 100644
--- a/core/tests/switch_core/connections/test_broker.py
+++ b/core/tests/switch_core/connections/test_broker.py
@@ -80,6 +80,7 @@ def broker(
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={"github": vendor},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
@@ -454,6 +455,7 @@ class TestChecks:
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
@@ -469,7 +471,7 @@ class TestChecks:
False,
)
assert "not available on this server" in refused.message
- assert broker.connectable("github") is False
+ assert await broker.connectable("github") is False
async def test_an_owner_no_longer_in_the_workspace_is_forbidden(
self, broker, session_factory
@@ -744,6 +746,7 @@ class TestConnectOnly:
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={"github": vendor},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
@@ -753,13 +756,13 @@ class TestConnectOnly:
)
world = await _world(session_factory, expires_in=60)
- reason = broker.availability("github")
+ reason = await broker.availability("github")
assert reason is not None and "not granted to agents" in reason
- unavailable = broker.unavailable("github")
+ unavailable = await broker.unavailable("github")
assert unavailable is not None and unavailable.code == "not_fully_set_up"
- assert broker.server_setup_needed("github") is False
- assert broker.connectable("github") is True
- assert broker.connectable("asana") is False
+ assert await broker.server_setup_needed("github") is False
+ assert await broker.connectable("github") is True
+ assert await broker.connectable("asana") is False
refused = await _refused(broker, session_factory, world.agent.id)
assert (refused.status_code, refused.code, refused.retryable) == (
503,
@@ -821,6 +824,7 @@ def _switched_off(
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={"github": vendor},
+ organization_apps=None,
disabled=disabled,
store=STORE,
token_retention=timedelta(days=30),
@@ -838,10 +842,10 @@ class TestSwitchedOff:
broker = _switched_off(session_factory, vendor, {"github": reason})
world = await _world(session_factory)
- assert broker.availability("github") == reason
- unavailable = broker.unavailable("github")
+ assert await broker.availability("github") == reason
+ unavailable = await broker.unavailable("github")
assert unavailable is not None and unavailable.code == "switched_off"
- assert broker.connectable("github") is False
+ assert await broker.connectable("github") is False
refused = await _refused(broker, session_factory, world.agent.id)
assert (refused.status_code, refused.code, refused.retryable) == (
403,
@@ -893,7 +897,7 @@ class TestSwitchedOff:
self, session_factory, vendor
) -> None:
broker = _switched_off(session_factory, vendor, {"github": ""})
- assert broker.availability("github") == "Switched off on this server."
+ assert await broker.availability("github") == "Switched off on this server."
async def test_a_service_outside_the_catalog_stops_the_server(
self, session_factory, vendor
@@ -938,6 +942,7 @@ async def _pass_through(
keyring=TEST_KEYRING,
catalog=pass_through_catalog(root, max_lifetime=max_lifetime, entry=entry),
adapters={"example": vendor},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
@@ -1351,6 +1356,7 @@ async def _gated(
keyring=TEST_KEYRING,
catalog=pass_through_catalog(root),
adapters={"github": github, "example": example},
+ organization_apps=None,
disabled=disabled or {},
store=STORE,
token_retention=timedelta(days=30),
@@ -1765,4 +1771,4 @@ class TestReplacedClient:
assert token.resources == RESOURCES
connection = await _connection(session_factory, world.owner.id)
assert connection is not None
- assert broker.client_replaced(connection) is False
+ assert await broker.client_replaced(connection) is False
diff --git a/core/tests/switch_core/connections/test_organization_apps.py b/core/tests/switch_core/connections/test_organization_apps.py
new file mode 100644
index 000000000..6b2fd5494
--- /dev/null
+++ b/core/tests/switch_core/connections/test_organization_apps.py
@@ -0,0 +1,387 @@
+"""A workspace goes through its organization's connector app, where it saved one.
+
+Against Postgres, with the broker and `OrganizationApps` on the restricted
+role row-level security applies to; `owner` only arranges fixtures. Google
+Workspace stands for every static OAuth client: its adapter names the client
+it signs in through, which is what tells one app from another here.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import uuid
+from collections.abc import Awaitable, Callable
+from datetime import timedelta
+from pathlib import Path
+
+import httpx
+import pytest
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
+from sqlalchemy.pool import NullPool
+from starlette.datastructures import State
+
+from switch_core.connections import organization_apps as organization_apps_module
+from switch_core.connections.app_credentials import SECRET_NAMES, ConnectorApps
+from switch_core.connections.app_signals import (
+ CONNECTOR_APP_CHANGED_SIGNAL,
+ ConnectorAppSignals,
+)
+from switch_core.connections.broker import ServiceBroker, ServiceError
+from switch_core.connections.loader import CATALOG
+from switch_core.connections.organization_apps import OrganizationApps
+from switch_core.connections.registry import ClientRegistration, build_adapters
+from switch_core.db.models import Organization, OrganizationAppCredential, Tenant, User
+from switch_core.db.stores.organization_app_credential_store import (
+ OrganizationAppCredentialStore,
+)
+from switch_core.db.stores.service_app_credential_store import (
+ ServiceAppCredentialStore,
+)
+from switch_core.db.stores.service_connection_store import ServiceConnectionStore
+from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore
+from switch_core.keys import Keyring
+from switch_core.signals import SignalBus
+from switch_core.tenant_context import tenant_scope
+from tests.conftest import TEST_KEYRING, RLSHarness
+from tests.switch_core.connections.adapters.test_github import _config
+
+pytestmark = pytest.mark.no_ambient_tenant
+
+PUBLIC_URL = "https://switch.example.test"
+GOOGLE = "google-workspace"
+SERVER_CLIENT = "server-client-id"
+DELIVERY_TIMEOUT = 10.0
+
+
+def _id(prefix: str) -> str:
+ return f"{prefix}-{uuid.uuid4().hex[:8]}"
+
+
+async def _workspace(
+ owner: async_sessionmaker[AsyncSession], *, in_organization: bool
+) -> tuple[str | None, str]:
+ """A workspace, in an organization of its own or, as one a process from
+ before organizations created, in none."""
+ organization_id = _id("org") if in_organization else None
+ tenant_id = _id("tenant")
+ async with owner() as session:
+ if organization_id is not None:
+ session.add(
+ Organization(id=organization_id, slug=organization_id, name="O")
+ )
+ await session.flush()
+ session.add(
+ Tenant(
+ id=tenant_id, slug=tenant_id, name="W", organization_id=organization_id
+ )
+ )
+ await session.commit()
+ return organization_id, tenant_id
+
+
+async def _save(
+ owner: async_sessionmaker[AsyncSession],
+ organization_id: str,
+ client_id: str,
+ *,
+ keyring: Keyring = TEST_KEYRING,
+) -> None:
+ async with owner() as session:
+ user = User(name="admin", email=f"{_id('admin')}@example.invalid", role="user")
+ session.add(user)
+ await session.flush()
+ await OrganizationAppCredentialStore(keyring).save(
+ session,
+ organization_id=organization_id,
+ service=GOOGLE,
+ client_id=client_id,
+ settings={},
+ secrets={"client_secret": "example-secret-2222"},
+ secret_names=SECRET_NAMES["oauth_client"],
+ updated_by=user.id,
+ )
+ await session.commit()
+
+
+class _Server:
+ """One process: the server's apps, organizations' apps and the broker."""
+
+ def __init__(
+ self, session_factory: async_sessionmaker[AsyncSession], tmp_path: Path
+ ) -> None:
+ client_file = tmp_path / f"google-{uuid.uuid4().hex[:6]}.json"
+ client_file.write_text(
+ json.dumps(
+ {"client_id": SERVER_CLIENT, "client_secret": "example-secret-0000"}
+ )
+ )
+ settings = _config(gateway_public_url=PUBLIC_URL)
+ self.http = httpx.AsyncClient()
+ self.registration = ClientRegistration(
+ session_factory=session_factory,
+ keyring=TEST_KEYRING,
+ public_url=PUBLIC_URL,
+ server_name="switch.local",
+ )
+ self.apps = ConnectorApps(
+ catalog=CATALOG,
+ config=settings,
+ environ={"GOOGLE_WORKSPACE_CLIENT_CONFIG_PATH": str(client_file)},
+ session_factory=session_factory,
+ store=ServiceAppCredentialStore(TEST_KEYRING),
+ http=self.http,
+ registration=self.registration,
+ )
+ self.organization_apps = OrganizationApps(
+ apps=self.apps,
+ store=OrganizationAppCredentialStore(TEST_KEYRING),
+ session_factory=session_factory,
+ )
+ self._session_factory = session_factory
+
+ async def start(self) -> ServiceBroker:
+ await self.apps.load()
+ return ServiceBroker(
+ session_factory=self._session_factory,
+ keyring=TEST_KEYRING,
+ catalog=CATALOG,
+ adapters=build_adapters(
+ CATALOG,
+ github_app=self.apps.github_app(),
+ oauth_clients=self.apps.oauth_clients(),
+ http=self.http,
+ registration=self.registration,
+ ),
+ organization_apps=self.organization_apps,
+ disabled={},
+ store=ServiceConnectionStore(),
+ token_retention=timedelta(days=30),
+ workspace_services=WorkspaceServiceStore(),
+ workspace_gate=False,
+ public_url=PUBLIC_URL,
+ )
+
+
+async def _client_in(broker: ServiceBroker, tenant_id: str) -> str | None:
+ """The OAuth client `tenant_id` signs in to Google Workspace through."""
+ with tenant_scope(tenant_id):
+ _, adapter = await broker.sign_in(GOOGLE)
+ return adapter.current_client_id()
+
+
+async def _eventually(check: Callable[[], Awaitable[bool]]) -> None:
+ async with asyncio.timeout(DELIVERY_TIMEOUT):
+ while not await check():
+ await asyncio.sleep(0.02)
+
+
+class TestWhichApp:
+ async def test_a_workspace_goes_through_its_organizations_app(
+ self, rls_harness: RLSHarness, tmp_path: Path
+ ) -> None:
+ own, with_app = await _workspace(rls_harness.owner, in_organization=True)
+ _, without_app = await _workspace(rls_harness.owner, in_organization=True)
+ _, no_organization = await _workspace(rls_harness.owner, in_organization=False)
+ assert own is not None
+ await _save(rls_harness.owner, own, "organization-client-id")
+ broker = await _Server(rls_harness.restricted, tmp_path).start()
+
+ assert await _client_in(broker, with_app) == "organization-client-id"
+ assert await _client_in(broker, without_app) == SERVER_CLIENT
+ assert await _client_in(broker, no_organization) == SERVER_CLIENT
+
+ async def test_an_unusable_organization_app_does_not_fall_back_to_the_servers(
+ self, rls_harness: RLSHarness, tmp_path: Path
+ ) -> None:
+ organization_id, tenant_id = await _workspace(
+ rls_harness.owner, in_organization=True
+ )
+ assert organization_id is not None
+ retired = Keyring.parse("retired:" + "r" * 40, legacy_secret=None)
+ await _save(rls_harness.owner, organization_id, "lost-client", keyring=retired)
+ broker = await _Server(rls_harness.restricted, tmp_path).start()
+
+ with tenant_scope(tenant_id):
+ unavailable = await broker.unavailable(GOOGLE)
+ assert unavailable is not None
+ assert unavailable.code == "organization_app_unusable"
+ assert "Your organization's Google Workspace app" in unavailable.reason
+ assert await broker.connectable(GOOGLE) is False
+ with pytest.raises(ServiceError) as refused:
+ await broker.sign_in(GOOGLE)
+ assert refused.value.message == unavailable.reason
+
+ async def test_a_service_without_an_app_never_reads_the_organization(
+ self, rls_harness: RLSHarness, tmp_path: Path
+ ) -> None:
+ server = _Server(rls_harness.restricted, tmp_path)
+ await server.start()
+
+ # No such workspace: a read would have nothing to bind.
+ assert (
+ await server.organization_apps.for_workspace("no-such", "atlassian") is None
+ )
+
+
+class TestKept:
+ async def test_the_adapter_is_kept_until_the_app_changes(
+ self, rls_harness: RLSHarness, tmp_path: Path
+ ) -> None:
+ organization_id, tenant_id = await _workspace(
+ rls_harness.owner, in_organization=True
+ )
+ assert organization_id is not None
+ await _save(rls_harness.owner, organization_id, "first-client")
+ server = _Server(rls_harness.restricted, tmp_path)
+ broker = await server.start()
+ assert await _client_in(broker, tenant_id) == "first-client"
+
+ await _save(rls_harness.owner, organization_id, "second-client")
+ assert await _client_in(broker, tenant_id) == "first-client"
+
+ server.organization_apps.changed(organization_id, GOOGLE)
+ assert await _client_in(broker, tenant_id) == "second-client"
+
+ async def test_a_kept_adapter_is_read_again_once_no_longer_trusted(
+ self,
+ rls_harness: RLSHarness,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ ) -> None:
+ organization_id, tenant_id = await _workspace(
+ rls_harness.owner, in_organization=True
+ )
+ assert organization_id is not None
+ await _save(rls_harness.owner, organization_id, "first-client")
+ server = _Server(rls_harness.restricted, tmp_path)
+ await server.start()
+ monkeypatch.setattr(organization_apps_module, "TRUSTED_SECONDS", 0.0)
+
+ first = await server.organization_apps.for_workspace(tenant_id, GOOGLE)
+ unchanged = await server.organization_apps.for_workspace(tenant_id, GOOGLE)
+ assert first is not None and unchanged is first
+
+ # Saved by a process whose announcement never arrived.
+ await _save(rls_harness.owner, organization_id, "second-client")
+ changed = await server.organization_apps.for_workspace(tenant_id, GOOGLE)
+ assert changed is not None and changed.adapter is not None
+ assert changed.adapter.current_client_id() == "second-client"
+
+ async def test_a_change_during_a_read_is_not_overwritten_by_it(
+ self,
+ rls_harness: RLSHarness,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ ) -> None:
+ organization_id, tenant_id = await _workspace(
+ rls_harness.owner, in_organization=True
+ )
+ assert organization_id is not None
+ await _save(rls_harness.owner, organization_id, "first-client")
+ server = _Server(rls_harness.restricted, tmp_path)
+ await server.start()
+ store = OrganizationAppCredentialStore(TEST_KEYRING)
+ reads = 0
+
+ async def read_then_change(
+ session: AsyncSession, organization: str, service: str
+ ) -> OrganizationAppCredential | None:
+ nonlocal reads
+ reads += 1
+ row = await store.get(session, organization, service)
+ if reads == 1:
+ server.organization_apps.forget(organization, service)
+ return row
+
+ monkeypatch.setattr(server.organization_apps._store, "get", read_then_change)
+ await server.organization_apps.for_workspace(tenant_id, GOOGLE)
+ await server.organization_apps.for_workspace(tenant_id, GOOGLE)
+ assert reads == 2
+
+
+class TestSignals:
+ def _bus(
+ self, postgres_url: str, session_factory: async_sessionmaker[AsyncSession]
+ ) -> SignalBus:
+ return SignalBus(
+ session_factory,
+ lambda: create_async_engine(postgres_url, poolclass=NullPool),
+ process_id=str(uuid.uuid4()),
+ )
+
+ async def test_a_change_in_one_process_is_forgotten_in_the_other(
+ self,
+ postgres_url: str,
+ rls_harness: RLSHarness,
+ session_factory: async_sessionmaker[AsyncSession],
+ tmp_path: Path,
+ ) -> None:
+ organization_id, tenant_id = await _workspace(
+ rls_harness.owner, in_organization=True
+ )
+ assert organization_id is not None
+ await _save(rls_harness.owner, organization_id, "first-client")
+ servers = [_Server(rls_harness.restricted, tmp_path) for _ in range(2)]
+ brokers = []
+ buses = []
+ for server in servers:
+ broker = await server.start()
+ bus = self._bus(postgres_url, session_factory)
+ ConnectorAppSignals(
+ apps=server.apps,
+ organization_apps=server.organization_apps,
+ broker=broker,
+ state=State(),
+ bus=bus,
+ )
+ await bus.start()
+ brokers.append(broker)
+ buses.append(bus)
+ try:
+ here, there = servers
+ assert await _client_in(brokers[1], tenant_id) == "first-client"
+
+ # As the save route will: commit, then forget here and announce.
+ await _save(rls_harness.owner, organization_id, "second-client")
+ here.organization_apps.changed(organization_id, GOOGLE)
+
+ async def moved() -> bool:
+ return await _client_in(brokers[1], tenant_id) == "second-client"
+
+ await _eventually(moved)
+ finally:
+ for bus in buses:
+ await bus.stop()
+
+ async def test_an_organizations_signal_leaves_the_servers_app_alone(
+ self,
+ postgres_url: str,
+ rls_harness: RLSHarness,
+ session_factory: async_sessionmaker[AsyncSession],
+ tmp_path: Path,
+ ) -> None:
+ server = _Server(rls_harness.restricted, tmp_path)
+ broker = await server.start()
+ forgotten: list[tuple[str, str]] = []
+ server.organization_apps.forget = ( # type: ignore[method-assign]
+ lambda organization, service: forgotten.append((organization, service))
+ )
+ signals = ConnectorAppSignals(
+ apps=server.apps,
+ organization_apps=server.organization_apps,
+ broker=broker,
+ state=State(),
+ bus=self._bus(postgres_url, session_factory),
+ )
+
+ signals._on_changed(
+ {
+ "kind": CONNECTOR_APP_CHANGED_SIGNAL,
+ "service": GOOGLE,
+ "organization_id": "org-1",
+ }
+ )
+
+ assert forgotten == [("org-1", GOOGLE)]
+ assert signals._owed.pending == 0
diff --git a/core/tests/switch_core/connections/test_registry.py b/core/tests/switch_core/connections/test_registry.py
index 4a863ce2b..c29b27e1b 100644
--- a/core/tests/switch_core/connections/test_registry.py
+++ b/core/tests/switch_core/connections/test_registry.py
@@ -201,6 +201,7 @@ def _broker(
keyring=TEST_KEYRING,
catalog=catalog,
adapters=adapters,
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
@@ -210,7 +211,7 @@ def _broker(
)
-def test_an_app_not_set_up_here_says_who_sets_it_up(
+async def test_an_app_not_set_up_here_says_who_sets_it_up(
tmp_path: Path, session_factory
) -> None:
broker = _broker(_catalog(tmp_path, STATIC), session_factory, {}, None)
@@ -218,30 +219,30 @@ def test_an_app_not_set_up_here_says_who_sets_it_up(
"{} is not set up on this server. Whoever runs your Switch server can "
"set it up in Switch Console, under Admin → Connector apps."
)
- assert broker.availability("example") == reason.format("Example")
- assert broker.availability("github") == reason.format("GitHub")
- assert broker.connectable("example") is False
+ assert await broker.availability("example") == reason.format("Example")
+ assert await broker.availability("github") == reason.format("GitHub")
+ assert await broker.connectable("example") is False
for service in ("example", "github"):
- unavailable = broker.unavailable(service)
+ unavailable = await broker.unavailable(service)
assert unavailable is not None and unavailable.code == "app_not_set_up"
- assert broker.server_setup_needed(service) is True
+ assert await broker.server_setup_needed(service) is True
-def test_a_service_signing_in_only_through_the_server_needs_its_public_url(
+async def test_a_service_signing_in_only_through_the_server_needs_its_public_url(
tmp_path: Path, session_factory
) -> None:
core_only = STATIC.replace("redirect: [loopback, core]", "redirect: [core]")
broker = _broker(_catalog(tmp_path, core_only), session_factory, {}, None)
- example = broker.unavailable("example")
+ example = await broker.unavailable("example")
assert example is not None
assert example.code == "needs_public_url"
assert example.reason == (
"Example can't be connected on this server: it needs the server's "
"public address (GATEWAY_PUBLIC_URL)."
)
- assert broker.server_setup_needed("example") is False
+ assert await broker.server_setup_needed("example") is False
# GitHub's App names its own callback, so it isn't held up the same way.
- github = broker.unavailable("github")
+ github = await broker.unavailable("github")
assert github is not None and github.code == "app_not_set_up"
with_url = _broker(
_catalog(tmp_path / "again", core_only),
@@ -249,48 +250,48 @@ def test_a_service_signing_in_only_through_the_server_needs_its_public_url(
{},
"https://switch.example.test",
)
- example = with_url.unavailable("example")
+ example = await with_url.unavailable("example")
assert example is not None and example.code == "app_not_set_up"
-def test_github_cannot_work_on_a_server_without_an_https_address(
+async def test_github_cannot_work_on_a_server_without_an_https_address(
tmp_path: Path, session_factory
) -> None:
catalog = _catalog(tmp_path, STATIC)
broker = _broker(catalog, session_factory, {}, "http://127.0.0.1:8000")
- github = broker.unavailable("github")
+ github = await broker.unavailable("github")
assert github is not None
assert github.code == "needs_https"
assert github.reason == (
"GitHub can't be used on this server: it needs the server to have an "
"https address."
)
- assert broker.server_setup_needed("github") is False
+ assert await broker.server_setup_needed("github") is False
# Google and the like take an http address back, so their app still can be.
- example = broker.unavailable("example")
+ example = await broker.unavailable("example")
assert example is not None and example.code == "app_not_set_up"
https = _broker(catalog, session_factory, {}, "https://switch.example.com")
- github = https.unavailable("github")
+ github = await https.unavailable("github")
assert github is not None and github.code == "app_not_set_up"
-def test_a_service_with_no_app_to_set_up_is_just_not_set_up(
+async def test_a_service_with_no_app_to_set_up_is_just_not_set_up(
tmp_path: Path, session_factory
) -> None:
broker = _broker(_catalog(tmp_path, STATIC), session_factory, {}, None)
- atlassian = broker.unavailable("atlassian")
+ atlassian = await broker.unavailable("atlassian")
assert atlassian is not None
assert (atlassian.code, atlassian.reason) == (
"not_set_up",
"Atlassian is not set up on this server.",
)
- assert broker.server_setup_needed("atlassian") is False
- asana = broker.unavailable("asana")
+ assert await broker.server_setup_needed("atlassian") is False
+ asana = await broker.unavailable("asana")
assert asana is not None and asana.code == "coming_soon"
-def test_a_key_that_never_expires_is_neither_connected_nor_granted(
+async def test_a_key_that_never_expires_is_neither_connected_nor_granted(
tmp_path: Path, session_factory
) -> None:
catalog = _catalog(tmp_path, STATIC)
@@ -298,18 +299,20 @@ def test_a_key_that_never_expires_is_neither_connected_nor_granted(
catalog, github_app=None, oauth_clients={}, http=HTTP, registration=REG
)
broker = _broker(catalog, session_factory, adapters, None)
- datadog = broker.unavailable("datadog")
+ datadog = await broker.unavailable("datadog")
assert datadog is not None
assert (datadog.code, datadog.reason) == (
"key_never_expires",
"Datadog keys do not expire and only Datadog can revoke them, so Switch "
"does not hand them to agents.",
)
- assert broker.connectable("datadog") is False
- assert broker.server_setup_needed("datadog") is False
+ assert await broker.connectable("datadog") is False
+ assert await broker.server_setup_needed("datadog") is False
-def test_a_replaced_adapter_is_used_at_once(tmp_path: Path, session_factory) -> None:
+async def test_a_replaced_adapter_is_used_at_once(
+ tmp_path: Path, session_factory
+) -> None:
catalog = _catalog(tmp_path, STATIC)
broker = _broker(catalog, session_factory, {}, None)
adapter = build_adapters(
@@ -323,12 +326,12 @@ def test_a_replaced_adapter_is_used_at_once(tmp_path: Path, session_factory) ->
)["example"]
broker.replace_adapter("example", adapter)
- assert broker.availability("example") is None
- assert broker.connectable("example") is True
+ assert await broker.availability("example") is None
+ assert await broker.connectable("example") is True
broker.replace_adapter("example", None)
- assert broker.availability("example") is not None
- assert broker.connectable("example") is False
+ assert await broker.availability("example") is not None
+ assert await broker.connectable("example") is False
with pytest.raises(ValueError, match="No service named"):
broker.replace_adapter("nowhere", None)
diff --git a/core/tests/switch_core/gateway/test_connector_apps.py b/core/tests/switch_core/gateway/test_connector_apps.py
index ca134c280..f9865e258 100644
--- a/core/tests/switch_core/gateway/test_connector_apps.py
+++ b/core/tests/switch_core/gateway/test_connector_apps.py
@@ -232,6 +232,7 @@ class World:
http=http,
registration=registration,
),
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/gateway/test_github_connections.py b/core/tests/switch_core/gateway/test_github_connections.py
index 8094a201a..74dce4b04 100644
--- a/core/tests/switch_core/gateway/test_github_connections.py
+++ b/core/tests/switch_core/gateway/test_github_connections.py
@@ -116,6 +116,7 @@ def _process(github, identity, session_factory, workspace_gate: bool) -> FastAPI
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={"github": GitHubAdapter(github, AsyncMock())},
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/gateway/test_google_workspace.py b/core/tests/switch_core/gateway/test_google_workspace.py
index 62fe897a0..0878e250f 100644
--- a/core/tests/switch_core/gateway/test_google_workspace.py
+++ b/core/tests/switch_core/gateway/test_google_workspace.py
@@ -106,6 +106,7 @@ def _world(
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters=adapters,
+ organization_apps=None,
disabled=disabled or {},
store=STORE,
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/gateway/test_organization_routes.py b/core/tests/switch_core/gateway/test_organization_routes.py
index c81d4353a..a62240df1 100644
--- a/core/tests/switch_core/gateway/test_organization_routes.py
+++ b/core/tests/switch_core/gateway/test_organization_routes.py
@@ -101,6 +101,7 @@ def _app(
keyring=_KEYRING,
catalog=CATALOG,
adapters={},
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/gateway/test_service_connections.py b/core/tests/switch_core/gateway/test_service_connections.py
index 7db418543..690a8bbbe 100644
--- a/core/tests/switch_core/gateway/test_service_connections.py
+++ b/core/tests/switch_core/gateway/test_service_connections.py
@@ -449,6 +449,7 @@ class TestMemberRemoval:
keyring=TEST_KEYRING,
catalog=CATALOG,
adapters={"github": vendor},
+ organization_apps=None,
disabled={},
store=STORE,
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/gateway/test_service_flows.py b/core/tests/switch_core/gateway/test_service_flows.py
index ff64fb16f..178cfe351 100644
--- a/core/tests/switch_core/gateway/test_service_flows.py
+++ b/core/tests/switch_core/gateway/test_service_flows.py
@@ -108,6 +108,7 @@ def _world(
catalog["example"].definition, client, vendor.client()
)
},
+ organization_apps=None,
disabled=disabled or {},
store=STORE,
token_retention=timedelta(days=30),
diff --git a/core/tests/switch_core/gateway/test_tenant_api_routes.py b/core/tests/switch_core/gateway/test_tenant_api_routes.py
index 47d74e12b..237c8c7fa 100644
--- a/core/tests/switch_core/gateway/test_tenant_api_routes.py
+++ b/core/tests/switch_core/gateway/test_tenant_api_routes.py
@@ -178,6 +178,7 @@ def _app(
keyring=_KEYRING,
catalog=CATALOG,
adapters={},
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
@@ -1613,6 +1614,7 @@ class TestMemberRoutes:
keyring=_KEYRING,
catalog=CATALOG,
adapters={"github": GitHubAdapter(github, AsyncMock())}, # type: ignore[arg-type]
+ organization_apps=None,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),