Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
feat(organizations): org admins set up their organization's connector apps
Petr Bauch3750e7d61ca1
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.pyindex cace27c89..53eb93912 100644--- a/core/switch_core/connections/broker.py+++ b/core/switch_core/connections/broker.py@@ -172,12 +172,18 @@ UnavailableCode = Literal[]+# Whose app a service goes through in a workspace: its organization's, or+# the server's (which includes services with no app at all).+AppSource = Literal["organization", "server"]++@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+ source: AppSourcedef _client_replaced(@@ -203,6 +209,7 @@ class ServiceStanding:unavailable: Unavailable | Noneconnectable: booladapter: ServiceAdapter | None+ source: AppSource@propertydef server_setup_needed(self) -> bool:@@ -548,8 +555,8 @@ class ServiceBroker:is bound to the workspace."""app = await self.organization_app(session, service)if app is not None:- return _Choice(app.adapter, app.problem)- return _Choice(self._adapters.get(service), None)+ return _Choice(app.adapter, app.problem, "organization")+ return _Choice(self._adapters.get(service), None, "server")async def organization_app(self, session: AsyncSession, service: str@@ -1314,6 +1321,7 @@ class ServiceBroker:and choice.adapter is not None),adapter=choice.adapter,+ source=choice.source,)def replace_adapter(self, service: str, adapter: ServiceAdapter | None) -> None:@@ -1831,7 +1839,7 @@ class ServiceBroker:raise ServiceError(503,INTERNAL,- self._reason(service, _Choice(adapter, None)),+ self._reason(service, _Choice(adapter, None, "server")),retryable=False,)try:diff --git a/core/switch_core/connections/organization_apps.py b/core/switch_core/connections/organization_apps.pyindex 19e33cffb..cf66b8892 100644--- a/core/switch_core/connections/organization_apps.py+++ b/core/switch_core/connections/organization_apps.py@@ -109,6 +109,10 @@ class OrganizationApps:self._epoch = 0self._signals: ConnectorAppSignals | None = None+ @property+ def store(self) -> OrganizationAppCredentialStore:+ return self._store+def use_signals(self, signals: ConnectorAppSignals) -> None:"""Announce each change made here, so every other process forgetsthe adapter too (`app_signals.py`)."""diff --git a/core/switch_core/db/audit.py b/core/switch_core/db/audit.pyindex 73179b91d..7c5f7e943 100644--- a/core/switch_core/db/audit.py+++ b/core/switch_core/db/audit.py@@ -78,6 +78,8 @@ class OrganizationAuditAction(StrEnum):WORKSPACE_CREATED = "organization.workspace_created"ROLE_CHANGED = "organization.role_changed"MEMBER_REMOVED = "organization.member_removed"+ CONNECTOR_APP_SAVED = "organization.connector_app_saved"+ CONNECTOR_APP_REMOVED = "organization.connector_app_removed"class ServerAuditAction(StrEnum):diff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.pyindex 2ea32ef3d..94248a410 100644--- a/core/switch_core/gateway/app.py+++ b/core/switch_core/gateway/app.py@@ -59,6 +59,9 @@ from switch_core.gateway.messaging_installs import ()from switch_core.gateway.oidc_routes import register_oidc_clientfrom switch_core.gateway.oidc_routes import router as oidc_router+from switch_core.gateway.organization_connector_apps import (+ router as organization_connector_apps_router,+)from switch_core.gateway.organizations import router as organizations_routerfrom switch_core.gateway.packages import router as packages_routerfrom switch_core.gateway.providers import router as providers_router@@ -179,6 +182,10 @@ def create_gateway_app(app.include_router(service_flows_router, tags=["service-connections"])app.include_router(workspace_services_router, tags=["service-connections"])app.include_router(connector_apps_router, tags=["connector-apps"])+ if config.organization_connector_apps_enabled:+ app.include_router(+ organization_connector_apps_router, tags=["connector-apps"]+ )# authlib's OIDC client stores transient state/nonce/PKCE in the request# session across the IdP redirect round-trip; SameSite=Lax lets the cookiediff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.pyindex c806cb3ad..f8f591f2d 100644--- a/core/switch_core/gateway/auth.py+++ b/core/switch_core/gateway/auth.py@@ -387,6 +387,23 @@ async def workspaces_in_organization(return found+async def organization_workspace_ids(+ session_factory: async_sessionmaker[AsyncSession],+ admin_id: str,+ organization_id: str,+) -> list[str]:+ """Every workspace of `organization_id`, as its `org_admin` `admin_id`+ reaches them: an `org_admin` may bind each one. Each read on its own+ short session, as `workspaces_in_organization` does."""+ found: list[str] = []+ for tenant_id in await tenants_bindable_by_user(session_factory, admin_id):+ async with tenant_session(session_factory, tenant_id) as session:+ tenant = await session.get(Tenant, tenant_id)+ if tenant is not None and tenant.organization_id == organization_id:+ found.append(tenant_id)+ return found++async def workspaces_reached_through_organization(session_factory: async_sessionmaker[AsyncSession],user_id: str,diff --git a/core/switch_core/gateway/connector_apps.py b/core/switch_core/gateway/connector_apps.pyindex 611211602..f80bb3d3d 100644--- a/core/switch_core/gateway/connector_apps.py+++ b/core/switch_core/gateway/connector_apps.py@@ -125,12 +125,15 @@ class Usage:)-async def _usage(- session_factory: async_sessionmaker[AsyncSession], service: str+async def usage_in(+ session_factory: async_sessionmaker[AsyncSession],+ service: str,+ tenant_ids: list[str],) -> Usage:- """Read one workspace at a time, each bound before it is touched."""+ """`service`'s connections in `tenant_ids`, read one workspace at a time,+ each bound before it is touched."""rows: list[tuple[str, str, str | None]] = []- for tenant_id in await all_tenant_ids(session_factory):+ for tenant_id in tenant_ids:async with tenant_session(session_factory, tenant_id) as session:result = await session.execute(select(@@ -144,14 +147,23 @@ async def _usage(return Usage(rows)-def _spec(apps: ConnectorApps, service: str) -> AppSpec:+async def _usage(+ session_factory: async_sessionmaker[AsyncSession], service: str+) -> Usage:+ """`service`'s connections in every workspace on the server."""+ return await usage_in(+ session_factory, service, await all_tenant_ids(session_factory)+ )+++def app_spec(apps: ConnectorApps, service: str) -> AppSpec:spec = apps.specs.get(service)if spec is None:raise HTTPException(404, f"{service} has no app to set up on this server.")return spec-def _setup_blocked(spec: AppSpec, public_url: str | None) -> str | None:+def setup_blocked(spec: AppSpec, public_url: str | None) -> str | None:"""Why the operator can't set this app up here, known before any vendoris asked. An unset GATEWAY_PUBLIC_URL is the page's own banner; Googleaccepts an http redirect for local development, GitHub does not."""@@ -160,8 +172,8 @@ def _setup_blocked(spec: AppSpec, public_url: str | None) -> str | None:return None-def _refuse_while_blocked(spec: AppSpec, public_url: str | None) -> None:- blocked = _setup_blocked(spec, public_url)+def refuse_while_blocked(spec: AppSpec, public_url: str | None) -> None:+ blocked = setup_blocked(spec, public_url)if blocked is not None:raise HTTPException(409, blocked)@@ -172,13 +184,20 @@ def _redirects("""Every address the vendor sends people back to Switch at, for theoperator to register: Core's own callback, which needs the public URL,or Switch Console's listener on loopback, which does not."""+ if spec.kind == "github_app" and effective.github_app is not None:+ return [effective.github_app.connections.callback]+ return saved_app_redirects(apps, spec)+++def saved_app_redirects(apps: ConnectorApps, spec: AppSpec) -> list[str]:+ """Where the vendor sends people back to an app saved here, the server's+ or an organization's: a saved GitHub App is called back at+ GATEWAY_PUBLIC_URL."""public_url = apps.public_urlif spec.kind == "github_app":- if effective.github_app is not None:- return [effective.github_app.connections.callback]return ([]- if public_url is None or _setup_blocked(spec, public_url) is not None+ if public_url is None or setup_blocked(spec, public_url) is not Noneelse [public_url.rstrip("/") + GITHUB_CALLBACK_PATH])oauth = apps.definition(spec.service).auth.oauth@@ -186,7 +205,7 @@ def _redirects(return redirect_uris(spec.service, oauth.redirect, public_url, oauth.loopback_ports)-def _needs_public_url(apps: ConnectorApps, spec: AppSpec) -> bool:+def needs_public_url(apps: ConnectorApps, spec: AppSpec) -> bool:if spec.kind == "github_app":return Trueoauth = apps.definition(spec.service).auth.oauth@@ -194,14 +213,14 @@ def _needs_public_url(apps: ConnectorApps, spec: AppSpec) -> bool:return "core" in oauth.redirect-def _scopes(apps: ConnectorApps, spec: AppSpec) -> list[str]:+def app_scopes(apps: ConnectorApps, spec: AppSpec) -> list[str]:"""What a sign-in asks for, for a vendor whose app lists its scopes."""if spec.kind == "github_app":return []return every_scope(apps.definition(spec.service))-def _secret_views(+def secret_views(spec: AppSpec, secrets: dict[str, str] | None, stored: bool) -> dict[str, dict[str, Any]]:"""Whether each secret is set, and how the operator recognises it: never@@ -274,7 +293,7 @@ async def _entry(),"saved_client_id": None if row is None else row.client_id,"fields": {"slug": slug} if spec.kind == "github_app" else {},- "secrets": _secret_views(+ "secrets": secret_views(spec, None if shown is None else shown.secrets, row is not None),"saved_ignored": effective.saved_ignored,@@ -283,9 +302,9 @@ async def _entry(# The first of `redirect_uris`, for Consoles that read only one."redirect_uri": next(iter(redirects), None),"redirect_uris": redirects,- "needs_public_url": _needs_public_url(apps, spec),- "scopes": _scopes(apps, spec),- "setup_blocked": _setup_blocked(spec, apps.public_url),+ "needs_public_url": needs_public_url(apps, spec),+ "scopes": app_scopes(apps, spec),+ "setup_blocked": setup_blocked(spec, apps.public_url),"install_url": (f"https://github.com/apps/{slug}/installations/new"if spec.kind == "github_app" and slug@@ -338,7 +357,7 @@ def _refuse_while_configured(effective: EffectiveCredentials) -> None:)-def _checks_body(result: CheckResult) -> list[dict[str, Any]]:+def checks_body(result: CheckResult) -> list[dict[str, Any]]:return [{"name": check.name,@@ -351,8 +370,11 @@ def _checks_body(result: CheckResult) -> list[dict[str, Any]]:]-async def _check(apps: ConnectorApps, spec: AppSpec, values: AppValues) -> CheckResult:- redirects = _redirects(apps, spec, apps.effective(spec.service))+async def check_values(+ apps: ConnectorApps, spec: AppSpec, values: AppValues, redirects: list[str]+) -> CheckResult:+ """Ask the vendor whether `values` work as an app sending people back to+ `redirects`; nothing is stored."""if not redirects:raise HTTPException(409,@@ -371,7 +393,7 @@ async def _check(apps: ConnectorApps, spec: AppSpec, values: AppValues) -> Checkraise HTTPException(503, str(error)) from None-def _fields_changed(+def fields_changed(previous: AppValues | None,client_id: str,settings: dict[str, str],@@ -419,9 +441,9 @@ async def save_connector_app(broker: Annotated[ServiceBroker, Depends(get_service_broker)],admin: Annotated[User, Depends(require_admin)],) -> dict[str, Any] | JSONResponse:- spec = _spec(apps, service)+ spec = app_spec(apps, service)_refuse_while_configured(apps.effective(service))- _refuse_while_blocked(spec, apps.public_url)+ refuse_while_blocked(spec, apps.public_url)given = {name: valuefor name, value in (@@ -464,7 +486,7 @@ async def save_connector_app(if missing:names = " and ".join(name.replace("_", " ") for name in sorted(missing))raise HTTPException(422, f"Enter the {names}.")- result = await _check(+ result = await check_values(apps,spec,AppValues(@@ -472,6 +494,7 @@ async def save_connector_app({} if previous is None else previous.settings,candidate_secrets,),+ _redirects(apps, spec, apps.effective(service)),)if not result.ok:failure = result.first_failure()@@ -481,7 +504,7 @@ async def save_connector_app(content={"detail": failure.message,"field": failure.field,- "checks": _checks_body(result),+ "checks": checks_body(result),},)previous_client_id = None if row is None else row.client_id@@ -499,7 +522,7 @@ async def save_connector_app(raise HTTPException(422, str(error)) from Nonedetails: dict[str, Any] = {"service": service,- "fields_changed": _fields_changed(+ "fields_changed": fields_changed(previous, body.client_id, result.settings, given),"client_id": body.client_id,@@ -526,7 +549,7 @@ async def save_connector_app(# What the vendor said, notes included, for the page to show as a check's.return {**await _entry(session, session_factory, apps, spec),- "checks": _checks_body(result),+ "checks": checks_body(result),}@@ -540,11 +563,11 @@ async def import_connector_app() -> dict[str, Any]:"""Copy the app configuration names into a saved one, so theconfiguration can be removed with nothing retyped."""- spec = _spec(apps, service)+ spec = app_spec(apps, service)effective = apps.effective(service)if effective.source != "configuration" or effective.values is None:raise HTTPException(409, f"{spec.name} is not set from configuration.")- _refuse_while_blocked(spec, apps.public_url)+ refuse_while_blocked(spec, apps.public_url)if effective.deprecated:raise HTTPException(409,@@ -600,19 +623,21 @@ async def check_connector_app(_admin: Annotated[User, Depends(require_admin)],) -> dict[str, Any]:"""Ask the vendor about the app in use; nothing is stored."""- spec = _spec(apps, service)+ spec = app_spec(apps, service)effective = apps.effective(service)# A configured GitHub App is called back at its own origin, not this URL.if effective.source != "configuration":- _refuse_while_blocked(spec, apps.public_url)+ refuse_while_blocked(spec, apps.public_url)if effective.values is None:raise HTTPException(409, f"{spec.name} has no app to check.")if effective.deprecated:raise HTTPException(409, "HOSTED_GITHUB_CONFIG_PATH names no private key to check.")- result = await _check(apps, spec, effective.values)- return {"ok": result.ok, "checks": _checks_body(result)}+ result = await check_values(+ apps, spec, effective.values, _redirects(apps, spec, effective)+ )+ return {"ok": result.ok, "checks": checks_body(result)}@router.get("/{service}/impact")@@ -624,7 +649,7 @@ async def connector_app_impact(_admin: Annotated[User, Depends(require_admin)],) -> dict[str, int]:"""Who would have to reconnect if `client_id` replaced the app in use."""- _spec(apps, service)+ app_spec(apps, service)values = apps.effective(service).valuesusage = await _usage(session_factory, service)people, workspaces = usage.not_through(@@ -645,7 +670,7 @@ async def remove_connector_app() -> dict[str, int]:"""Remove the saved app. The connections made through it are kept, andunusable until an app is set up again; they can still be disconnected."""- spec = _spec(apps, service)+ spec = app_spec(apps, service)_refuse_while_configured(apps.effective(service))removed: ServiceAppCredential | None = await apps.store.remove(session, service)if removed is None:diff --git a/core/switch_core/gateway/organization_connector_apps.py b/core/switch_core/gateway/organization_connector_apps.pynew file mode 100644index 000000000..4d677a04a--- /dev/null+++ b/core/switch_core/gateway/organization_connector_apps.py@@ -0,0 +1,460 @@+"""An organization's admins set up the apps its workspaces sign in through.++The organization's counterpart of `gateway/connector_apps.py`, and the same+rules: a save is checked with the vendor first and stored only if the vendor+accepts it, no response carries a secret, and a different app is taken only+when asked for on purpose. `org_admin` only, acting on the bound+workspace's organization; the platform operator has no say here unless they+are one.++Which app a workspace then uses is `connections/organization_apps.py`: the+organization's where it saved one, else the server's. So an organization's+page shows, beside each of its own apps, whether the server offers one its+workspaces fall back to. There is nothing to import: configuration is the+server's alone.++Mounted only with both `service_connections` and+`organization_connector_apps` on. A save or removal takes effect at once in+every process (`OrganizationApps.changed`), and is recorded in the+organization's audit log.+"""++from __future__ import annotations++import logging+from typing import Annotated, Any, cast++from fastapi import APIRouter, Depends, HTTPException, Query, Request+from fastapi.responses import JSONResponse+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.connections.app_credentials import (+ AppLoadError,+ AppSpec,+ AppValues,+ ConnectorApps,+ UnreadableAppError,+ load_saved_app,+)+from switch_core.connections.organization_apps import OrganizationApps+from switch_core.db.audit import (+ OrganizationAuditAction,+ record_organization_audit_event,+)+from switch_core.db.models import OrganizationAppCredential, User+from switch_core.db.stores.app_secrets import MissingSecretsError+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.user_store import UserStore+from switch_core.gateway.auth import get_current_user, organization_workspace_ids+from switch_core.gateway.connector_apps import (+ SaveApp,+ Usage,+ app_scopes,+ app_spec,+ check_values,+ checks_body,+ fields_changed,+ get_connector_apps,+ needs_public_url,+ refuse_while_blocked,+ saved_app_redirects,+ secret_views,+ setup_blocked,+ usage_in,+)+from switch_core.gateway.dependencies import (+ get_organization_store,+ get_session,+ get_session_factory,+ get_user_store,+)+from switch_core.gateway.organizations import require_organization_admin++logger = logging.getLogger(__name__)++router = APIRouter(prefix="/organizations/current/connector-apps")+++def get_organization_apps(request: Request) -> OrganizationApps:+ apps = getattr(request.app.state, "organization_apps", None)+ if not isinstance(apps, OrganizationApps):+ raise RuntimeError("No organizations' apps are installed on this app.")+ return cast(OrganizationApps, apps)+++def get_organization_app_store(+ organization_apps: Annotated[OrganizationApps, Depends(get_organization_apps)],+) -> OrganizationAppCredentialStore:+ return organization_apps.store+++async def _admin_of(+ session: AsyncSession,+ user_store: UserStore,+ organization_store: OrganizationStore,+ user: User,+) -> str:+ """The bound workspace's organization, which `user` must administer."""+ access = await require_organization_admin(+ session, user_store, organization_store, user, operator_restores=False+ )+ return access.organization_id+++async def _usage(+ session_factory: async_sessionmaker[AsyncSession],+ user: User,+ organization_id: str,+ service: str,+) -> Usage:+ """`service`'s connections in the organization's workspaces."""+ return await usage_in(+ session_factory,+ service,+ await organization_workspace_ids(session_factory, user.id, organization_id),+ )+++def _opened(+ apps: ConnectorApps,+ store: OrganizationAppCredentialStore,+ spec: AppSpec,+ row: OrganizationAppCredential,+) -> tuple[AppValues | None, str | None, bool]:+ """The saved row's values, why it cannot be used, and whether no key+ opens it."""+ try:+ loaded = load_saved_app(+ spec,+ client_id=row.client_id,+ settings=row.settings,+ secrets=lambda: store.secrets(row),+ public_url=apps.public_url,+ )+ except UnreadableAppError as error:+ return None, str(error), True+ except AppLoadError as error:+ return None, str(error), False+ return loaded.values, None, False+++async def _entry(+ session: AsyncSession,+ session_factory: async_sessionmaker[AsyncSession],+ apps: ConnectorApps,+ store: OrganizationAppCredentialStore,+ user: User,+ organization_id: str,+ spec: AppSpec,+) -> dict[str, Any]:+ """One service's app, in the shape of the operator's page+ (`connector_apps._entry`), and whether the server has one to fall back+ to."""+ row = await store.get(session, organization_id, spec.service)+ values, load_error, unreadable = (+ (None, None, False) if row is None else _opened(apps, store, spec, row)+ )+ in_use = row is not None and load_error is None+ current = row.client_id if in_use and row is not None else None+ usage = await _usage(session_factory, user, organization_id, spec.service)+ people, workspaces = usage.through(current) if current is not None else (0, 0)+ updated_by = None+ if row is not None and row.updated_by is not None:+ author = await session.get(User, row.updated_by)+ updated_by = None if author is None else author.name+ slug = None if row is None else row.settings.get("slug")+ redirects = saved_app_redirects(apps, spec)+ return {+ "slug": spec.service,+ "name": spec.name,+ "kind": spec.kind,+ "source": "saved" if in_use else "none",+ "configuration_variables": [],+ "deprecated": False,+ "client_id": None if row is None else row.client_id,+ "saved_client_id": None if row is None else row.client_id,+ "fields": {"slug": slug} if spec.kind == "github_app" else {},+ "secrets": secret_views(+ spec, None if values is None else values.secrets, row is not None+ ),+ "saved_ignored": False,+ "unreadable": unreadable,+ "load_error": load_error,+ "redirect_uri": next(iter(redirects), None),+ "redirect_uris": redirects,+ "needs_public_url": needs_public_url(apps, spec),+ "scopes": app_scopes(apps, spec),+ "setup_blocked": setup_blocked(spec, apps.public_url),+ "install_url": (+ f"https://github.com/apps/{slug}/installations/new"+ if spec.kind == "github_app" and slug+ else None+ ),+ "updated_by_name": updated_by,+ "updated_at": None if row is None else row.updated_at.isoformat(),+ "connected_members": people,+ "workspaces": workspaces,+ "stale_connections": usage.stale(current),+ # Whether the organization's workspaces use the server's app while it+ # saves none of its own.+ "server_app": apps.effective(spec.service).source != "none",+ }+++async def list_organization_connector_apps(+ session: Annotated[AsyncSession, Depends(get_session)],+ session_factory: Annotated[Any, Depends(get_session_factory)],+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],+ store: Annotated[+ OrganizationAppCredentialStore, Depends(get_organization_app_store)+ ],+ user_store: Annotated[UserStore, Depends(get_user_store)],+ organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],+ user: Annotated[User, Depends(get_current_user)],+) -> dict[str, Any]:+ organization_id = await _admin_of(session, user_store, organization_store, user)+ return {+ "public_url": apps.public_url,+ "apps": [+ await _entry(+ session, session_factory, apps, store, user, organization_id, spec+ )+ for spec in apps.specs.values()+ ],+ }++[email protected]("/{service}", response_model=None)+async def save_organization_connector_app(+ service: str,+ body: SaveApp,+ session: Annotated[AsyncSession, Depends(get_session)],+ session_factory: Annotated[Any, Depends(get_session_factory)],+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],+ organization_apps: Annotated[OrganizationApps, Depends(get_organization_apps)],+ store: Annotated[+ OrganizationAppCredentialStore, Depends(get_organization_app_store)+ ],+ user_store: Annotated[UserStore, Depends(get_user_store)],+ organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],+ user: Annotated[User, Depends(get_current_user)],+) -> dict[str, Any] | JSONResponse:+ organization_id = await _admin_of(session, user_store, organization_store, user)+ spec = app_spec(apps, service)+ refuse_while_blocked(spec, apps.public_url)+ given = {+ name: value+ for name, value in (+ ("client_secret", body.client_secret),+ ("private_key", body.private_key),+ )+ if value is not None+ }+ unknown = set(given) - spec.secret_names+ if unknown:+ raise HTTPException(422, f"{spec.name} has no {', '.join(sorted(unknown))}.")+ row = await store.get(session, organization_id, service)+ if row is not None and row.client_id != body.client_id and not body.replace:+ raise HTTPException(+ 409,+ "This is a different app: everyone connected through the current one "+ "would have to reconnect. Use a different app to switch on purpose.",+ )+ previous: AppValues | None = None+ if row is not None:+ values, _, unreadable = _opened(apps, store, spec, row)+ if unreadable and not spec.secret_names <= set(given):+ raise HTTPException(+ 422,+ "Switch can't read the saved secret any more. Enter every secret "+ "again.",+ )+ previous = values or (+ None+ if unreadable+ else AppValues(row.client_id, dict(row.settings), store.secrets(row))+ )+ # Another app's secrets are no use to this one: switching names them all.+ kept = (+ {}+ if previous is None or previous.client_id != body.client_id+ else previous.secrets+ )+ candidate_secrets = {**kept, **given}+ missing = spec.secret_names - set(candidate_secrets)+ if missing:+ names = " and ".join(name.replace("_", " ") for name in sorted(missing))+ raise HTTPException(422, f"Enter the {names}.")+ result = await check_values(+ apps,+ spec,+ AppValues(+ body.client_id,+ {} if previous is None else previous.settings,+ candidate_secrets,+ ),+ saved_app_redirects(apps, spec),+ )+ if not result.ok:+ failure = result.first_failure()+ assert failure is not None+ return JSONResponse(+ status_code=422,+ content={+ "detail": failure.message,+ "field": failure.field,+ "checks": checks_body(result),+ },+ )+ previous_client_id = None if row is None else row.client_id+ try:+ await store.save(+ session,+ organization_id=organization_id,+ service=service,+ client_id=body.client_id,+ settings=result.settings,+ secrets=given,+ secret_names=spec.secret_names,+ updated_by=user.id,+ )+ except MissingSecretsError as error:+ raise HTTPException(422, str(error)) from None+ details: dict[str, Any] = {+ "service": service,+ "fields_changed": fields_changed(+ previous, body.client_id, result.settings, given+ ),+ "client_id": body.client_id,+ }+ if previous_client_id is not None and previous_client_id != body.client_id:+ details["previous_client_id"] = previous_client_id+ await record_organization_audit_event(+ session,+ organization_id=organization_id,+ actor_user_id=user.id,+ action=OrganizationAuditAction.CONNECTOR_APP_SAVED,+ target_type="service",+ target_id=service,+ details=details,+ )+ await session.commit()+ organization_apps.changed(organization_id, service)+ logger.warning(+ "An organization admin saved %s's app: organization=%s user=%s fields=%s",+ spec.name,+ organization_id,+ user.id,+ details["fields_changed"],+ )+ return {+ **await _entry(+ session, session_factory, apps, store, user, organization_id, spec+ ),+ "checks": checks_body(result),+ }++[email protected]("/{service}/check")+async def check_organization_connector_app(+ service: str,+ session: Annotated[AsyncSession, Depends(get_session)],+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],+ store: Annotated[+ OrganizationAppCredentialStore, Depends(get_organization_app_store)+ ],+ user_store: Annotated[UserStore, Depends(get_user_store)],+ organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],+ user: Annotated[User, Depends(get_current_user)],+) -> dict[str, Any]:+ """Ask the vendor about the organization's app; nothing is stored."""+ organization_id = await _admin_of(session, user_store, organization_store, user)+ spec = app_spec(apps, service)+ refuse_while_blocked(spec, apps.public_url)+ row = await store.get(session, organization_id, service)+ if row is None:+ raise HTTPException(409, f"Your organization has no {spec.name} app to check.")+ values, load_error, _ = _opened(apps, store, spec, row)+ if values is None:+ raise HTTPException(409, load_error or f"{spec.name}'s app cannot be read.")+ result = await check_values(apps, spec, values, saved_app_redirects(apps, spec))+ return {"ok": result.ok, "checks": checks_body(result)}++[email protected]("/{service}/impact")+async def organization_connector_app_impact(+ service: str,+ client_id: Annotated[str, Query(min_length=1, max_length=512)],+ session: Annotated[AsyncSession, Depends(get_session)],+ session_factory: Annotated[Any, Depends(get_session_factory)],+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],+ store: Annotated[+ OrganizationAppCredentialStore, Depends(get_organization_app_store)+ ],+ user_store: Annotated[UserStore, Depends(get_user_store)],+ organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],+ user: Annotated[User, Depends(get_current_user)],+) -> dict[str, int]:+ """Who in the organization would have to reconnect if `client_id`+ became the app its workspaces use."""+ organization_id = await _admin_of(session, user_store, organization_store, user)+ app_spec(apps, service)+ row = await store.get(session, organization_id, service)+ if row is not None:+ current: str | None = row.client_id+ else:+ server = apps.effective(service).values+ current = None if server is None else server.client_id+ await session.commit()+ usage = await _usage(session_factory, user, organization_id, service)+ people, workspaces = usage.not_through(client_id, current)+ return {"connected_members": people, "workspaces": workspaces}++[email protected]("/{service}")+async def remove_organization_connector_app(+ service: str,+ session: Annotated[AsyncSession, Depends(get_session)],+ session_factory: Annotated[Any, Depends(get_session_factory)],+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],+ organization_apps: Annotated[OrganizationApps, Depends(get_organization_apps)],+ store: Annotated[+ OrganizationAppCredentialStore, Depends(get_organization_app_store)+ ],+ user_store: Annotated[UserStore, Depends(get_user_store)],+ organization_store: Annotated[OrganizationStore, Depends(get_organization_store)],+ user: Annotated[User, Depends(get_current_user)],+) -> dict[str, int]:+ """Remove the organization's app. Its workspaces go back to the server's+ app, where the server has one; the connections made through the+ organization's are kept, and must reconnect before they are used."""+ organization_id = await _admin_of(session, user_store, organization_store, user)+ spec = app_spec(apps, service)+ removed = await store.remove(session, organization_id, service)+ if removed is None:+ raise HTTPException(404, f"Your organization has no saved {spec.name} app.")+ await record_organization_audit_event(+ session,+ organization_id=organization_id,+ actor_user_id=user.id,+ action=OrganizationAuditAction.CONNECTOR_APP_REMOVED,+ target_type="service",+ target_id=service,+ details={"service": service, "client_id": removed.client_id},+ )+ await session.commit()+ organization_apps.changed(organization_id, service)+ logger.warning(+ "An organization admin removed %s's app: organization=%s user=%s",+ spec.name,+ organization_id,+ user.id,+ )+ people, workspaces = (+ await _usage(session_factory, user, organization_id, service)+ ).through(removed.client_id)+ return {"connected_members": people, "workspaces": workspaces}diff --git a/core/switch_core/gateway/organizations.py b/core/switch_core/gateway/organizations.pyindex 5c5d4c8a3..215508275 100644--- a/core/switch_core/gateway/organizations.py+++ b/core/switch_core/gateway/organizations.py@@ -132,7 +132,7 @@ class _OrganizationAccess(NamedTuple):platform_access: bool-async def _require_organization_admin(+async def require_organization_admin(session: AsyncSession,user_store: UserStore,organization_store: OrganizationStore,@@ -288,7 +288,7 @@ async def create_organization_workspace(sign-up, which governs who comes onto the server rather than what anorganization adds. The platform operator is exempt, as everywhere."""- access = await _require_organization_admin(+ access = await require_organization_admin(session, user_store, organization_store, user, operator_restores=False)organization_id = access.organization_id@@ -348,7 +348,7 @@ async def list_organization_audit_log(actor, and a target that is a person, come with their names, read from`users`, which outlives membership."""- access = await _require_organization_admin(+ access = await require_organization_admin(session, user_store, organization_store, user, operator_restores=False)organization_id = access.organization_id@@ -393,7 +393,7 @@ async def list_organization_members() -> list[OrganizationMemberDetail]:"""Every member of the organization and their role in it. `org_admin` only:the list spans every workspace of the organization, not only this one."""- access = await _require_organization_admin(+ access = await require_organization_admin(session, user_store, organization_store, user, operator_restores=True)organization_id = access.organization_id@@ -500,7 +500,7 @@ async def update_organization_member_role(they were in only as `org_admin` (`_end_access_through_organization`).Recorded in the organization's audit log."""- access = await _require_organization_admin(+ access = await require_organization_admin(session, user_store, organization_store, user, operator_restores=True)organization_id = access.organization_id@@ -597,7 +597,7 @@ async def remove_organization_member((`_end_access_through_organization`). Refuses the last `org_admin` too.Recorded in the organization's audit log."""- access = await _require_organization_admin(+ access = await require_organization_admin(session, user_store, organization_store, user, operator_restores=False)organization_id = access.organization_iddiff --git a/core/switch_core/gateway/service_connections.py b/core/switch_core/gateway/service_connections.pyindex a11cfbcb7..a6ba511eb 100644--- a/core/switch_core/gateway/service_connections.py+++ b/core/switch_core/gateway/service_connections.py@@ -140,6 +140,9 @@ async def list_service_connections(# Whether it waits on the server's operator setting up its app# under Connectors, so a client can offer them that."server_setup_needed": standing.server_setup_needed,+ # Whose app it signs in through here: "organization" where the+ # workspace's organization saved its own, else "server".+ "app_source": standing.source,# 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 ordiff --git a/core/switch_core/main.py b/core/switch_core/main.pyindex 5f5364d6b..a3b908e5b 100644--- a/core/switch_core/main.py+++ b/core/switch_core/main.py@@ -1398,6 +1398,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:)agent_bridge_app.state.service_broker = service_brokergateway_app.state.service_broker = service_broker+ gateway_app.state.organization_apps = organization_apps# A connector app the operator saves or removes, put to work in every process.ConnectorAppSignals(apps=connector_apps,diff --git a/core/tests/switch_core/gateway/test_app_route_mounting.py b/core/tests/switch_core/gateway/test_app_route_mounting.pyindex d0c3b276e..64f9b6991 100644--- a/core/tests/switch_core/gateway/test_app_route_mounting.py+++ b/core/tests/switch_core/gateway/test_app_route_mounting.py@@ -90,3 +90,23 @@ def test_service_connections_are_mounted_only_with_their_flag() -> None:assert SERVICE_ROUTES <= on# GitHub's own link was there before service connections, and stays.assert "/provider-connections/github" in off+++ORGANIZATION_APP_ROUTES = {+ "/organizations/current/connector-apps",+ "/organizations/current/connector-apps/{service}",+}+++def test_organizations_connector_apps_are_mounted_only_with_both_flags() -> None:+ def paths(flags: str) -> set[str]:+ return {+ route.path+ for route in _app(feature_flags_enabled=flags).routes # type: ignore[attr-defined]+ }++ assert not ORGANIZATION_APP_ROUTES & paths("service_connections")+ assert not ORGANIZATION_APP_ROUTES & paths("organization_connector_apps")+ assert ORGANIZATION_APP_ROUTES <= paths(+ "service_connections,organization_connector_apps"+ )diff --git a/core/tests/switch_core/gateway/test_organization_connector_apps.py b/core/tests/switch_core/gateway/test_organization_connector_apps.pynew file mode 100644index 000000000..6f72d6626--- /dev/null+++ b/core/tests/switch_core/gateway/test_organization_connector_apps.py@@ -0,0 +1,341 @@+"""An organization's connector-app routes, against Postgres and fake vendors.++Only the organization's `org_admin`s reach them; a save is checked with the+vendor and stored only when the vendor accepts it; its workspaces use it at+once and the server's app no longer; and each change lands in the+organization's audit log.+"""++from __future__ import annotations++import pytest+from sqlalchemy import select+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.connections.broker import ServiceBroker+from switch_core.connections.organization_apps import OrganizationApps+from switch_core.db.audit import OrganizationAuditAction+from switch_core.db.models import (+ TENANT_ZERO_ID,+ OrganizationAppCredential,+ OrganizationAuditEvent,+ OrganizationMember,+ User,+)+from switch_core.db.stores.organization_app_credential_store import (+ OrganizationAppCredentialStore,+)+from switch_core.db.stores.organization_store import OrganizationStore+from switch_core.gateway import dependencies as gw_deps+from switch_core.gateway.organization_connector_apps import (+ router as organization_connector_apps_router,+)+from tests.conftest import TEST_KEYRING+from tests.switch_core.connections.fake_vendor import (+ STATIC_CLIENT_ID,+ STATIC_CLIENT_SECRET,+)+from tests.switch_core.gateway.test_connector_apps import (+ APPS,+ GOOGLE,+ PUBLIC,+ World,+ _google_body,+ _user,+)+from tests.switch_core.management.harness import cookies_for++ORGANIZATION_APPS = "/gateway/organizations/current/connector-apps"+CONNECTIONS = "/gateway/service-connections"+++async def world(session_factory) -> World:+ return World(session_factory, PUBLIC)+++class _Recording(OrganizationApps):+ def __init__(self, **kwargs: object) -> None:+ super().__init__(**kwargs) # type: ignore[arg-type]+ self.changes: list[tuple[str, str]] = []++ def changed(self, organization_id: str, service: str) -> None:+ self.changes.append((organization_id, service))+ super().changed(organization_id, service)+++async def _install(world: World) -> _Recording:+ """The operator's routes and the organization's, with a broker that+ looks up the organization's apps."""+ apps = await world.install()+ organization_apps = _Recording(+ apps=apps, store=OrganizationAppCredentialStore(TEST_KEYRING)+ )+ server: ServiceBroker = world.harness.gateway_app.state.service_broker+ server._organization_apps = organization_apps+ world.harness.gateway_app.state.organization_apps = organization_apps+ world.harness.gateway_app.dependency_overrides[gw_deps.get_organization_store] = (+ lambda: OrganizationStore()+ )+ world.harness.gateway_app.include_router(organization_connector_apps_router)+ return organization_apps+++async def _org_admin(+ session_factory: async_sessionmaker[AsyncSession], name: str+) -> User:+ user = await _user(session_factory, name)+ async with session_factory() as session:+ session.add(+ OrganizationMember(+ organization_id=TENANT_ZERO_ID, user_id=user.id, role="org_admin"+ )+ )+ await session.commit()+ return user+++async def _rows(session_factory) -> list[OrganizationAppCredential]:+ async with session_factory() as session:+ return list(await session.scalars(select(OrganizationAppCredential)))+++async def _audit(session_factory) -> list[OrganizationAuditEvent]:+ async with session_factory() as session:+ return list(+ await session.scalars(+ select(OrganizationAuditEvent)+ .where(+ OrganizationAuditEvent.action.like("organization.connector_app%")+ )+ .order_by(OrganizationAuditEvent.occurred_at)+ )+ )+++async def _google(client, cookies: dict[str, str]) -> dict[str, object]:+ listed = await client.get(CONNECTIONS, cookies=cookies)+ assert listed.status_code == 200, listed.text+ [entry] = [+ entry for entry in listed.json()["connections"] if entry["slug"] == GOOGLE+ ]+ return entry+++class TestOnlyOrgAdmins:+ async def test_workspace_admins_and_the_operator_are_refused(+ self, world: World, session_factory+ ) -> None:+ await _install(world)+ owner = await _user(session_factory, "owner", member_role="owner")+ operator = await _user(session_factory, "operator", role="admin")+ async with world.harness.client() as client:+ answers = [+ await call(cookies_for(someone))+ for someone in (owner, operator)+ for call in (+ lambda cookies: client.get(ORGANIZATION_APPS, cookies=cookies),+ lambda cookies: client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}",+ json=_google_body(),+ cookies=cookies,+ ),+ lambda cookies: client.post(+ f"{ORGANIZATION_APPS}/{GOOGLE}/check", cookies=cookies+ ),+ lambda cookies: client.get(+ f"{ORGANIZATION_APPS}/{GOOGLE}/impact",+ params={"client_id": "x"},+ cookies=cookies,+ ),+ lambda cookies: client.delete(+ f"{ORGANIZATION_APPS}/{GOOGLE}", cookies=cookies+ ),+ )+ ]+ assert [answer.status_code for answer in answers] == [403] * 10+ assert await _rows(session_factory) == []++ async def test_an_org_admin_reads_every_app_and_whether_the_server_has_one(+ self, world: World, session_factory+ ) -> None:+ await _install(world)+ admin = await _org_admin(session_factory, "org-admin")+ operator = await _user(session_factory, "operator", role="admin")+ async with world.harness.client() as client:+ before = await client.get(ORGANIZATION_APPS, cookies=cookies_for(admin))+ await client.put(+ f"{APPS}/{GOOGLE}", json=_google_body(), cookies=cookies_for(operator)+ )+ after = await client.get(ORGANIZATION_APPS, cookies=cookies_for(admin))+ assert before.status_code == 200, before.text+ assert before.json()["public_url"] == PUBLIC+ apps = {entry["slug"]: entry for entry in before.json()["apps"]}+ assert apps[GOOGLE]["source"] == "none"+ assert apps[GOOGLE]["server_app"] is False+ assert apps[GOOGLE]["redirect_uris"] == [+ f"{PUBLIC}/gateway/service-connections/{GOOGLE}/flows/callback"+ ]+ [google] = [e for e in after.json()["apps"] if e["slug"] == GOOGLE]+ assert google["source"] == "none"+ assert google["server_app"] is True+++class TestSaving:+ async def test_a_checked_client_is_saved_audited_and_used_by_the_workspace(+ self, world: World, session_factory+ ) -> None:+ organization_apps = await _install(world)+ admin = await _org_admin(session_factory, "org-admin")+ cookies = cookies_for(admin)+ async with world.harness.client() as client:+ before = await _google(client, cookies)+ saved = await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}", json=_google_body(), cookies=cookies+ )+ after = await _google(client, cookies)+ assert before["app_source"] == "server"+ assert before["configured"] is False+ assert saved.status_code == 200, saved.text+ entry = saved.json()+ assert entry["source"] == "saved"+ assert entry["client_id"] == STATIC_CLIENT_ID+ assert entry["secrets"]["client_secret"] == {+ "set": True,+ "last4": STATIC_CLIENT_SECRET[-4:],+ }+ assert entry["updated_by_name"] == "org-admin"+ assert STATIC_CLIENT_SECRET not in saved.text+ assert after["app_source"] == "organization"+ assert after["configured"] is True+ assert organization_apps.changes == [(TENANT_ZERO_ID, GOOGLE)]+ [row] = await _rows(session_factory)+ assert row.organization_id == TENANT_ZERO_ID+ [event] = await _audit(session_factory)+ assert event.action == OrganizationAuditAction.CONNECTOR_APP_SAVED+ assert event.actor_user_id == admin.id+ assert event.details == {+ "service": GOOGLE,+ "fields_changed": ["client_id", "client_secret"],+ "client_id": STATIC_CLIENT_ID,+ }++ async def test_a_refusal_stores_nothing(+ self, world: World, session_factory+ ) -> None:+ organization_apps = await _install(world)+ admin = await _org_admin(session_factory, "org-admin")+ async with world.harness.client() as client:+ answer = await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}",+ json=_google_body(client_secret="example-wrong-secret"),+ cookies=cookies_for(admin),+ )+ assert answer.status_code == 422+ assert answer.json()["field"] == "client_secret"+ assert await _rows(session_factory) == []+ assert await _audit(session_factory) == []+ assert organization_apps.changes == []++ async def test_another_client_id_needs_replace_and_keeps_none_of_the_old_secrets(+ self, world: World, session_factory+ ) -> None:+ await _install(world)+ admin = await _org_admin(session_factory, "org-admin")+ cookies = cookies_for(admin)+ async with world.harness.client() as client:+ assert (+ await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}",+ json=_google_body(),+ cookies=cookies,+ )+ ).status_code == 200+ world.google.client_id = "another-client-id"+ refused = await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}",+ json=_google_body(client_id="another-client-id"),+ cookies=cookies,+ )+ unnamed = await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}",+ json=_google_body(+ client_id="another-client-id", client_secret=None, replace=True+ ),+ cookies=cookies,+ )+ switched = await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}",+ json=_google_body(client_id="another-client-id", replace=True),+ cookies=cookies,+ )+ assert refused.status_code == 409+ assert unnamed.status_code == 422+ assert unnamed.json()["detail"] == "Enter the client secret."+ assert switched.status_code == 200, switched.text+ [row] = await _rows(session_factory)+ assert row.client_id == "another-client-id"+ events = await _audit(session_factory)+ assert events[-1].details["previous_client_id"] == STATIC_CLIENT_ID++ async def test_the_saved_app_is_checked_again_on_request(+ self, world: World, session_factory+ ) -> None:+ await _install(world)+ admin = await _org_admin(session_factory, "org-admin")+ cookies = cookies_for(admin)+ async with world.harness.client() as client:+ nothing = await client.post(+ f"{ORGANIZATION_APPS}/{GOOGLE}/check", cookies=cookies+ )+ await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}", json=_google_body(), cookies=cookies+ )+ checked = await client.post(+ f"{ORGANIZATION_APPS}/{GOOGLE}/check", cookies=cookies+ )+ assert nothing.status_code == 409+ assert checked.status_code == 200, checked.text+ assert checked.json()["ok"] is True+++class TestRemoving:+ async def test_removing_goes_back_to_the_servers_app(+ self, world: World, session_factory+ ) -> None:+ organization_apps = await _install(world)+ admin = await _org_admin(session_factory, "org-admin")+ operator = await _user(session_factory, "operator", role="admin")+ cookies = cookies_for(admin)+ async with world.harness.client() as client:+ await client.put(+ f"{APPS}/{GOOGLE}", json=_google_body(), cookies=cookies_for(operator)+ )+ await client.put(+ f"{ORGANIZATION_APPS}/{GOOGLE}", json=_google_body(), cookies=cookies+ )+ during = await _google(client, cookies)+ removed = await client.delete(+ f"{ORGANIZATION_APPS}/{GOOGLE}", cookies=cookies+ )+ again = await client.delete(+ f"{ORGANIZATION_APPS}/{GOOGLE}", cookies=cookies+ )+ after = await _google(client, cookies)+ assert during["app_source"] == "organization"+ assert removed.status_code == 200, removed.text+ assert removed.json() == {"connected_members": 0, "workspaces": 0}+ assert again.status_code == 404+ assert after["app_source"] == "server"+ assert after["configured"] is True+ assert await _rows(session_factory) == []+ assert organization_apps.changes == [+ (TENANT_ZERO_ID, GOOGLE),+ (TENANT_ZERO_ID, GOOGLE),+ ]+ events = await _audit(session_factory)+ assert [event.action for event in events] == [+ OrganizationAuditAction.CONNECTOR_APP_SAVED,+ OrganizationAuditAction.CONNECTOR_APP_REMOVED,+ ]+ assert events[-1].details == {"service": GOOGLE, "client_id": STATIC_CLIENT_ID}