Skip to content

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

fix(organizations): connector-app review fixes for #1197

Petr Bauche004a1fac21d
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.py
index 53eb93912..9a3e87484 100644
--- a/core/switch_core/connections/broker.py
+++ b/core/switch_core/connections/broker.py
@@ -1354,6 +1354,16 @@ class ServiceBroker:
else unavailable.reason
)
+ def server_serves(self, service: str) -> bool:
+ """Whether a workspace that goes through the server's app for
+ `service` can be granted it: switched on, set up, and able to issue."""
+ return (
+ self._unavailable(
+ service, _Choice(self._adapters.get(service), None, "server")
+ )
+ is None
+ )
+
def _unavailable(self, service: str, choice: _Choice) -> Unavailable | None:
"""Why `service` cannot be granted through `choice`, as a code and
the sentence people read, or None if it can."""
diff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.py
index f8f591f2d..8afd30c29 100644
--- a/core/switch_core/gateway/auth.py
+++ b/core/switch_core/gateway/auth.py
@@ -370,16 +370,17 @@ async def tenants_bindable_by(
return await tenants_bindable_by_user(session_factory, user_id)
-async def workspaces_in_organization(
+async def _in_organization(
session_factory: async_sessionmaker[AsyncSession],
- user_id: str,
+ candidates: list[str],
organization_id: str,
) -> list[Tenant]:
- """The workspaces of `organization_id` that `user_id` holds a membership
- row in. Each workspace is read on its own short session, one after
- another, so the caller holds no other connection while this runs."""
+ """Those of `candidates` in `organization_id`, in their order. A
+ workspace's organization is readable only bound to it, so each is read
+ on its own short session, one after another: the caller holds no other
+ connection while this runs."""
found: list[Tenant] = []
- for tenant_id in await tenants_of_user(session_factory, user_id):
+ for tenant_id in candidates:
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:
@@ -387,6 +388,21 @@ async def workspaces_in_organization(
return found
+async def workspaces_in_organization(
+ session_factory: async_sessionmaker[AsyncSession],
+ user_id: str,
+ organization_id: str,
+) -> list[Tenant]:
+ """The workspaces of `organization_id` that `user_id` holds a membership
+ row in. Each workspace is read on its own short session, one after
+ another, so the caller holds no other connection while this runs."""
+ return await _in_organization(
+ session_factory,
+ await tenants_of_user(session_factory, user_id),
+ organization_id,
+ )
+
+
async def organization_workspace_ids(
session_factory: async_sessionmaker[AsyncSession],
admin_id: str,
@@ -395,13 +411,14 @@ async def organization_workspace_ids(
"""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
+ return [
+ tenant.id
+ for tenant in await _in_organization(
+ session_factory,
+ await tenants_bindable_by_user(session_factory, admin_id),
+ organization_id,
+ )
+ ]
async def workspaces_reached_through_organization(
@@ -414,15 +431,15 @@ async def workspaces_reached_through_organization(
demoted or removed. Each read on its own short session, as
`workspaces_in_organization` does."""
members_of = set(await tenants_of_user(session_factory, user_id))
- found: list[Tenant] = []
- for tenant_id in await tenants_bindable_by_user(session_factory, user_id):
- if tenant_id in members_of:
- continue
- 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)
- return found
+ return await _in_organization(
+ session_factory,
+ [
+ tenant_id
+ for tenant_id in await tenants_bindable_by_user(session_factory, user_id)
+ if tenant_id not in members_of
+ ],
+ organization_id,
+ )
async def organizations_bindable_by(
@@ -489,11 +506,9 @@ async def organization_workspaces_bindable_by(
else await tenants_bindable_by(session_factory, user_id)
)
found: list[TenantMembershipResponse] = []
- for tenant_id in candidates:
+ for tenant in await _in_organization(session_factory, candidates, organization_id):
+ tenant_id = tenant.id
async with tenant_session(session_factory, tenant_id) as session:
- tenant = await session.get(Tenant, tenant_id)
- if tenant is None or tenant.organization_id != organization_id:
- continue
role = await user_store.effective_tenant_role(session, tenant_id, user_id)
organization_role = await user_store.organization_role(
session, tenant_id, user_id
diff --git a/core/switch_core/gateway/connector_apps.py b/core/switch_core/gateway/connector_apps.py
index f80bb3d3d..633676676 100644
--- a/core/switch_core/gateway/connector_apps.py
+++ b/core/switch_core/gateway/connector_apps.py
@@ -19,7 +19,9 @@ a private key its fingerprint.
from __future__ import annotations
import logging
+from collections.abc import Callable
from dataclasses import dataclass
+from datetime import datetime
from typing import Annotated, Any, cast
from fastapi import APIRouter, Depends, HTTPException, Query, Request
@@ -47,9 +49,11 @@ from switch_core.connections.oauth_clients import redirect_uris
from switch_core.connections.registry import every_scope
from switch_core.db.audit import ServerAuditAction, record_server_audit_event
from switch_core.db.models import (
+ OrganizationAppCredential,
ServiceAppCredential,
ServiceConnection,
ServiceOAuthClient,
+ Tenant,
User,
)
from switch_core.db.session_scope import tenant_session
@@ -125,34 +129,70 @@ class Usage:
)
-async def usage_in(
+@dataclass(frozen=True)
+class Usages:
+ """Every service's connections across some workspaces."""
+
+ rows: dict[str, list[tuple[str, str, str | None]]]
+
+ def of(self, service: str) -> Usage:
+ return Usage(self.rows.get(service, []))
+
+
+async def usages_in(
session_factory: async_sessionmaker[AsyncSession],
- service: str,
tenant_ids: list[str],
-) -> Usage:
- """`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]] = []
+ *,
+ leave_out_organization_apps: bool,
+) -> Usages:
+ """Every service's connections in `tenant_ids`, read one workspace at a
+ time, each bound before it is touched. With `leave_out_organization_apps`,
+ a workspace's connections to a service its organization saved its own app
+ for are left out: they never went through the server's."""
+ rows: dict[str, list[tuple[str, str, str | None]]] = {}
for tenant_id in tenant_ids:
async with tenant_session(session_factory, tenant_id) as session:
+ own: set[str] = set()
+ if leave_out_organization_apps:
+ own = set(
+ await session.scalars(
+ select(OrganizationAppCredential.service)
+ .join(
+ Tenant,
+ Tenant.organization_id
+ == OrganizationAppCredential.organization_id,
+ )
+ .where(Tenant.id == tenant_id)
+ )
+ )
result = await session.execute(
select(
- ServiceConnection.status, ServiceConnection.oauth_client_id
- ).where(
- ServiceConnection.tenant_id == tenant_id,
- ServiceConnection.service == service,
- )
+ ServiceConnection.service,
+ ServiceConnection.status,
+ ServiceConnection.oauth_client_id,
+ ).where(ServiceConnection.tenant_id == tenant_id)
)
- rows.extend((tenant_id, status, client) for status, client in result)
- return Usage(rows)
-
-
-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)
+ for service, status, client in result:
+ if service not in own:
+ rows.setdefault(service, []).append((tenant_id, status, client))
+ return Usages(rows)
+
+
+def organization_apps_on(request: Request) -> bool:
+ """Whether workspaces may go through their organization's own apps
+ (`organization_connector_apps`), so the server's apps serve only the
+ rest."""
+ return getattr(request.app.state, "organization_apps", None) is not None
+
+
+async def _usages(
+ session_factory: async_sessionmaker[AsyncSession], organization_apps: bool
+) -> Usages:
+ """Every connection made through the server's apps, in every workspace."""
+ return await usages_in(
+ session_factory,
+ await all_tenant_ids(session_factory),
+ leave_out_organization_apps=organization_apps,
)
@@ -250,55 +290,78 @@ def secret_views(
return views
-async def _entry(
+@dataclass(frozen=True)
+class SavedApp:
+ """A saved app's row, the server's or an organization's, as its entry
+ shows it."""
+
+ client_id: str
+ settings: dict[str, Any]
+ updated_at: datetime
+ updated_by_name: str | None
+
+
+async def saved_app(
session: AsyncSession,
- session_factory: async_sessionmaker[AsyncSession],
+ row: ServiceAppCredential | OrganizationAppCredential | None,
+) -> SavedApp | None:
+ if row is None:
+ return None
+ author = None if row.updated_by is None else await session.get(User, row.updated_by)
+ return SavedApp(
+ client_id=row.client_id,
+ settings=dict(row.settings),
+ updated_at=row.updated_at,
+ updated_by_name=None if author is None else author.name,
+ )
+
+
+def entry_body(
apps: ConnectorApps,
spec: AppSpec,
+ *,
+ source: str,
+ configuration_variables: list[str],
+ deprecated: bool,
+ current: str | None,
+ saved: SavedApp | None,
+ shown: AppValues | None,
+ saved_ignored: bool,
+ unreadable: bool,
+ load_error: str | None,
+ redirects: list[str],
+ usage: Usage,
) -> dict[str, Any]:
- effective = apps.effective(spec.service)
- row = await apps.store.get(session, spec.service)
- values = effective.values
- saved: AppValues | None = None
- if row is not None:
- try:
- saved = AppValues(
- row.client_id, dict(row.settings), apps.store.secrets(row)
- )
- except (UndecryptableError, ValueError):
- saved = None
- shown = values or saved
- current = None if values is None else values.client_id
- usage = await _usage(session_factory, spec.service)
+ """One service's app as a connector-apps page shows it, the server's or
+ an organization's: one shape, read by one set of cards. `current` is the
+ client in use, if any; `shown` the values whose secrets are described."""
people, workspaces = usage.through(current) if current is not None else (0, 0)
- updated_by = None
- if row is not None:
- user = await session.get(User, row.updated_by)
- updated_by = None if user is None else user.name
- slug = None if shown is None else shown.settings.get("slug")
- redirects = _redirects(apps, spec, effective)
+ settings = (
+ shown.settings
+ if shown is not None
+ else ({} if saved is None else saved.settings)
+ )
+ slug = settings.get("slug")
return {
"slug": spec.service,
"name": spec.name,
"kind": spec.kind,
- "source": effective.source,
- "configuration_variables": list(
- effective.configuration_variables
- if effective.source == "configuration"
- else spec.variables
- ),
- "deprecated": effective.deprecated,
+ "source": source,
+ "configuration_variables": configuration_variables,
+ "deprecated": deprecated,
"client_id": (
- current if current is not None else (None if row is None else row.client_id)
+ current
+ if current is not None
+ else (None if saved is None else saved.client_id)
),
- "saved_client_id": None if row is None else row.client_id,
+ "saved_client_id": None if saved is None else saved.client_id,
"fields": {"slug": slug} if spec.kind == "github_app" else {},
"secrets": secret_views(
- spec, None if shown is None else shown.secrets, row is not None
+ spec, None if shown is None else shown.secrets, saved is not None
),
- "saved_ignored": effective.saved_ignored,
- "unreadable": effective.unreadable,
- "load_error": effective.load_error,
+ "saved_ignored": saved_ignored,
+ "unreadable": unreadable,
+ "load_error": load_error,
# The first of `redirect_uris`, for Consoles that read only one.
"redirect_uri": next(iter(redirects), None),
"redirect_uris": redirects,
@@ -310,14 +373,84 @@ async def _entry(
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(),
+ "updated_by_name": None if saved is None else saved.updated_by_name,
+ "updated_at": None if saved is None else saved.updated_at.isoformat(),
"connected_members": people,
"workspaces": workspaces,
"stale_connections": usage.stale(current),
}
+@dataclass(frozen=True)
+class _ServerApp:
+ """What the server's entry for a service reads through the request's
+ session, before its connection is let go for the usage scan."""
+
+ effective: EffectiveCredentials
+ saved: SavedApp | None
+ shown: AppValues | None
+
+
+async def _read(
+ session: AsyncSession, apps: ConnectorApps, spec: AppSpec
+) -> _ServerApp:
+ effective = apps.effective(spec.service)
+ row = await apps.store.get(session, spec.service)
+ saved_values: AppValues | None = None
+ if row is not None:
+ try:
+ saved_values = AppValues(
+ row.client_id, dict(row.settings), apps.store.secrets(row)
+ )
+ except (UndecryptableError, ValueError):
+ saved_values = None
+ return _ServerApp(
+ effective=effective,
+ saved=await saved_app(session, row),
+ shown=effective.values or saved_values,
+ )
+
+
+def _entry(
+ apps: ConnectorApps, spec: AppSpec, read: _ServerApp, usage: Usage
+) -> dict[str, Any]:
+ effective = read.effective
+ return entry_body(
+ apps,
+ spec,
+ source=effective.source,
+ configuration_variables=list(
+ effective.configuration_variables
+ if effective.source == "configuration"
+ else spec.variables
+ ),
+ deprecated=effective.deprecated,
+ current=None if effective.values is None else effective.values.client_id,
+ saved=read.saved,
+ shown=read.shown,
+ saved_ignored=effective.saved_ignored,
+ unreadable=effective.unreadable,
+ load_error=effective.load_error,
+ redirects=_redirects(apps, spec, effective),
+ usage=usage,
+ )
+
+
+async def _one_entry(
+ session: AsyncSession,
+ session_factory: async_sessionmaker[AsyncSession],
+ apps: ConnectorApps,
+ spec: AppSpec,
+ organization_apps: bool,
+) -> dict[str, Any]:
+ """`spec`'s entry, the request's connection let go before the usage
+ scan opens one per workspace."""
+ read = await _read(session, apps, spec)
+ await session.commit()
+ usages = await _usages(session_factory, organization_apps)
+ return _entry(apps, spec, read, usages.of(spec.service))
+
+
async def _registered(session: AsyncSession) -> list[dict[str, Any]]:
"""Services whose client Core registers for itself: nothing to set up."""
rows = {
@@ -412,38 +545,51 @@ def fields_changed(
return sorted(changed)
-async def list_connector_apps(
- session: Annotated[AsyncSession, Depends(get_session)],
- session_factory: Annotated[Any, Depends(get_session_factory)],
- apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
- _admin: Annotated[User, Depends(require_admin)],
-) -> dict[str, Any]:
- entries = [
- await _entry(session, session_factory, apps, spec)
- for spec in apps.specs.values()
- ]
- return {
- "public_url": apps.public_url,
- "apps": entries,
- "registered": await _registered(session),
- }
+@dataclass(frozen=True)
+class StoredApp:
+ """The app saved for a service before a save, the server's or an
+ organization's. `secrets` raises `UndecryptableError` when no key opens
+ them."""
+ client_id: str
+ settings: dict[str, Any]
+ secrets: Callable[[], dict[str, str]]
[email protected]("/{service}", response_model=None)
-async def save_connector_app(
- service: str,
+
+@dataclass(frozen=True)
+class CheckedSave:
+ """A save the vendor accepted: what to store, and what it changed."""
+
+ result: CheckResult
+ previous: AppValues | None
+ given: dict[str, str]
+ previous_client_id: str | None
+
+ def details(self, service: str, client_id: str) -> dict[str, Any]:
+ """The save as its audit event records it: never a secret."""
+ details: dict[str, Any] = {
+ "service": service,
+ "fields_changed": fields_changed(
+ self.previous, client_id, self.result.settings, self.given
+ ),
+ "client_id": client_id,
+ }
+ if self.previous_client_id not in (None, client_id):
+ details["previous_client_id"] = self.previous_client_id
+ return details
+
+
+async def checked_save(
+ apps: ConnectorApps,
+ spec: AppSpec,
body: SaveApp,
- request: Request,
- session: Annotated[AsyncSession, Depends(get_session)],
- session_factory: Annotated[Any, Depends(get_session_factory)],
- apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
- broker: Annotated[ServiceBroker, Depends(get_service_broker)],
- admin: Annotated[User, Depends(require_admin)],
-) -> dict[str, Any] | JSONResponse:
- spec = app_spec(apps, service)
- _refuse_while_configured(apps.effective(service))
- refuse_while_blocked(spec, apps.public_url)
+ stored: StoredApp | None,
+ redirects: list[str],
+) -> CheckedSave | JSONResponse:
+ """Everything a save asks before anything is stored, the server's or an
+ organization's: the secrets it names, that a different app is meant, and
+ what the vendor says. A refusal from the vendor comes back as the
+ response to send."""
given = {
name: value
for name, value in (
@@ -455,18 +601,17 @@ async def save_connector_app(
unknown = set(given) - spec.secret_names
if unknown:
raise HTTPException(422, f"{spec.name} has no {', '.join(sorted(unknown))}.")
- row = await apps.store.get(session, service)
- if row is not None and row.client_id != body.client_id and not body.replace:
+ if stored is not None and stored.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:
+ if stored is not None:
try:
previous = AppValues(
- row.client_id, dict(row.settings), apps.store.secrets(row)
+ stored.client_id, dict(stored.settings), stored.secrets()
)
except UndecryptableError:
if not spec.secret_names <= set(given):
@@ -494,7 +639,7 @@ async def save_connector_app(
{} if previous is None else previous.settings,
candidate_secrets,
),
- _redirects(apps, spec, apps.effective(service)),
+ redirects,
)
if not result.ok:
failure = result.first_failure()
@@ -507,28 +652,76 @@ async def save_connector_app(
"checks": checks_body(result),
},
)
- previous_client_id = None if row is None else row.client_id
+ return CheckedSave(
+ result=result,
+ previous=previous,
+ given=given,
+ previous_client_id=None if stored is None else stored.client_id,
+ )
+
+
+async def list_connector_apps(
+ session: Annotated[AsyncSession, Depends(get_session)],
+ session_factory: Annotated[Any, Depends(get_session_factory)],
+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
+ organization_apps: Annotated[bool, Depends(organization_apps_on)],
+ _admin: Annotated[User, Depends(require_admin)],
+) -> dict[str, Any]:
+ reads = [(spec, await _read(session, apps, spec)) for spec in apps.specs.values()]
+ registered = await _registered(session)
+ # The request's connection goes back before the scan opens one per workspace.
+ await session.commit()
+ usages = await _usages(session_factory, organization_apps)
+ return {
+ "public_url": apps.public_url,
+ "apps": [
+ _entry(apps, spec, read, usages.of(spec.service)) for spec, read in reads
+ ],
+ "registered": registered,
+ }
+
+
[email protected]("/{service}", response_model=None)
+async def save_connector_app(
+ service: str,
+ body: SaveApp,
+ request: Request,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ session_factory: Annotated[Any, Depends(get_session_factory)],
+ apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
+ organization_apps: Annotated[bool, Depends(organization_apps_on)],
+ admin: Annotated[User, Depends(require_admin)],
+) -> dict[str, Any] | JSONResponse:
+ spec = app_spec(apps, service)
+ _refuse_while_configured(apps.effective(service))
+ refuse_while_blocked(spec, apps.public_url)
+ row = await apps.store.get(session, service)
+ checked = await checked_save(
+ apps,
+ spec,
+ body,
+ None
+ if row is None
+ else StoredApp(row.client_id, row.settings, lambda: apps.store.secrets(row)),
+ _redirects(apps, spec, apps.effective(service)),
+ )
+ if isinstance(checked, JSONResponse):
+ return checked
try:
await apps.store.save(
session,
service=service,
client_id=body.client_id,
- settings=result.settings,
- secrets=given,
+ settings=checked.result.settings,
+ secrets=checked.given,
secret_names=spec.secret_names,
updated_by=admin.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
+ details = checked.details(service, body.client_id)
await record_server_audit_event(
session,
actor_user_id=admin.id,
@@ -548,8 +741,8 @@ 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),
+ **await _one_entry(session, session_factory, apps, spec, organization_apps),
+ "checks": checks_body(checked.result),
}
@@ -559,6 +752,7 @@ async def import_connector_app(
session: Annotated[AsyncSession, Depends(get_session)],
session_factory: Annotated[Any, Depends(get_session_factory)],
apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
+ organization_apps: Annotated[bool, Depends(organization_apps_on)],
admin: Annotated[User, Depends(require_admin)],
) -> dict[str, Any]:
"""Copy the app configuration names into a saved one, so the
@@ -613,7 +807,7 @@ async def import_connector_app(
await session.commit()
await apps.reload(service)
apps.announce(service)
- return await _entry(session, session_factory, apps, spec)
+ return await _one_entry(session, session_factory, apps, spec, organization_apps)
@router.post("/{service}/check")
@@ -646,12 +840,13 @@ async def connector_app_impact(
client_id: Annotated[str, Query(min_length=1, max_length=512)],
session_factory: Annotated[Any, Depends(get_session_factory)],
apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
+ organization_apps: Annotated[bool, Depends(organization_apps_on)],
_admin: Annotated[User, Depends(require_admin)],
) -> dict[str, int]:
"""Who would have to reconnect if `client_id` replaced the app in use."""
app_spec(apps, service)
values = apps.effective(service).values
- usage = await _usage(session_factory, service)
+ usage = (await _usages(session_factory, organization_apps)).of(service)
people, workspaces = usage.not_through(
client_id, None if values is None else values.client_id
)
@@ -666,6 +861,7 @@ async def remove_connector_app(
session_factory: Annotated[Any, Depends(get_session_factory)],
apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
broker: Annotated[ServiceBroker, Depends(get_service_broker)],
+ organization_apps: Annotated[bool, Depends(organization_apps_on)],
admin: Annotated[User, Depends(require_admin)],
) -> dict[str, int]:
"""Remove the saved app. The connections made through it are kept, and
@@ -687,7 +883,9 @@ async def remove_connector_app(
await apps.apply(service, broker=broker, state=request.app.state)
apps.announce(service)
logger.warning("The operator removed %s's app: user=%s", spec.name, admin.id)
- people, workspaces = (await _usage(session_factory, service)).through(
- removed.client_id
+ people, workspaces = (
+ (await _usages(session_factory, organization_apps))
+ .of(service)
+ .through(removed.client_id)
)
return {"connected_members": people, "workspaces": workspaces}
diff --git a/core/switch_core/gateway/organization_connector_apps.py b/core/switch_core/gateway/organization_connector_apps.py
index 4d677a04a..0a9f3ca36 100644
--- a/core/switch_core/gateway/organization_connector_apps.py
+++ b/core/switch_core/gateway/organization_connector_apps.py
@@ -1,17 +1,17 @@
"""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.
+The organization's counterpart of `gateway/connector_apps.py`, sharing its
+entry shape (`entry_body`) and everything a save asks before it is stored
+(`checked_save`): the vendor checks a save first, 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.
+page shows, beside each of its own apps, whether the server's would serve
+its workspaces meanwhile (`server_app`). 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
@@ -22,6 +22,7 @@ organization's audit log.
from __future__ import annotations
import logging
+from dataclasses import dataclass
from typing import Annotated, Any, cast
from fastapi import APIRouter, Depends, HTTPException, Query, Request
@@ -36,12 +37,13 @@ from switch_core.connections.app_credentials import (
UnreadableAppError,
load_saved_app,
)
+from switch_core.connections.broker import ServiceBroker, get_service_broker
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.models import User
from switch_core.db.stores.app_secrets import MissingSecretsError
from switch_core.db.stores.organization_app_credential_store import (
OrganizationAppCredentialStore,
@@ -51,19 +53,20 @@ 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,
+ SavedApp,
+ StoredApp,
Usage,
- app_scopes,
+ Usages,
app_spec,
check_values,
+ checked_save,
checks_body,
- fields_changed,
+ entry_body,
get_connector_apps,
- needs_public_url,
refuse_while_blocked,
+ saved_app,
saved_app_redirects,
- secret_views,
- setup_blocked,
- usage_in,
+ usages_in,
)
from switch_core.gateway.dependencies import (
get_organization_store,
@@ -104,104 +107,116 @@ async def _admin_of(
return access.organization_id
-async def _usage(
+async def _usages(
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(
+) -> Usages:
+ """Every connection in the organization's workspaces. The caller's
+ session has let its connection go: this opens one per workspace."""
+ return await usages_in(
session_factory,
- service,
await organization_workspace_ids(session_factory, user.id, organization_id),
+ leave_out_organization_apps=False,
)
-def _opened(
+@dataclass(frozen=True)
+class _OrganizationApp:
+ """What the organization saved for a service, read through the request's
+ session before its connection is let go for the usage scan."""
+
+ saved: SavedApp | None
+ values: AppValues | None
+ load_error: str | None
+ unreadable: bool
+
+ @property
+ def current(self) -> str | None:
+ """The client its workspaces go through, if the app can be used."""
+ if self.saved is None or self.load_error is not None:
+ return None
+ return self.saved.client_id
+
+
+async def _read(
+ session: AsyncSession,
apps: ConnectorApps,
store: OrganizationAppCredentialStore,
+ organization_id: str,
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."""
+) -> _OrganizationApp:
+ row = await store.get(session, organization_id, spec.service)
+ if row is None:
+ return _OrganizationApp(None, None, None, False)
+ values: AppValues | None = None
+ load_error: str | None = None
+ unreadable = False
try:
- loaded = load_saved_app(
+ values = load_saved_app(
spec,
client_id=row.client_id,
settings=row.settings,
secrets=lambda: store.secrets(row),
public_url=apps.public_url,
- )
+ ).values
except UnreadableAppError as error:
- return None, str(error), True
+ load_error, unreadable = str(error), True
except AppLoadError as error:
- return None, str(error), False
- return loaded.values, None, False
+ load_error = str(error)
+ return _OrganizationApp(
+ saved=await saved_app(session, row),
+ values=values,
+ load_error=load_error,
+ unreadable=unreadable,
+ )
-async def _entry(
+def _entry(
+ apps: ConnectorApps,
+ spec: AppSpec,
+ read: _OrganizationApp,
+ usage: Usage,
+ server_serves: bool,
+) -> dict[str, Any]:
+ return {
+ **entry_body(
+ apps,
+ spec,
+ source="saved" if read.current is not None else "none",
+ configuration_variables=[],
+ deprecated=False,
+ current=read.current,
+ saved=read.saved,
+ shown=read.values,
+ saved_ignored=False,
+ unreadable=read.unreadable,
+ load_error=read.load_error,
+ redirects=saved_app_redirects(apps, spec),
+ usage=usage,
+ ),
+ # Whether the server's app serves the organization's workspaces while
+ # it saves none of its own.
+ "server_app": server_serves,
+ }
+
+
+async def _one_entry(
session: AsyncSession,
session_factory: async_sessionmaker[AsyncSession],
apps: ConnectorApps,
store: OrganizationAppCredentialStore,
+ broker: ServiceBroker,
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)
+ read = await _read(session, apps, store, organization_id, spec)
+ await session.commit()
+ usages = await _usages(session_factory, user, organization_id)
+ return _entry(
+ apps, spec, read, usages.of(spec.service), broker.server_serves(spec.service)
)
- 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",
- }
@router.get("")
@@ -209,6 +224,7 @@ 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)],
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
store: Annotated[
OrganizationAppCredentialStore, Depends(get_organization_app_store)
],
@@ -217,13 +233,24 @@ async def list_organization_connector_apps(
user: Annotated[User, Depends(get_current_user)],
) -> dict[str, Any]:
organization_id = await _admin_of(session, user_store, organization_store, user)
+ reads = [
+ (spec, await _read(session, apps, store, organization_id, spec))
+ for spec in apps.specs.values()
+ ]
+ # The request's connection goes back before the scan opens one per workspace.
+ await session.commit()
+ usages = await _usages(session_factory, user, organization_id)
return {
"public_url": apps.public_url,
"apps": [
- await _entry(
- session, session_factory, apps, store, user, organization_id, spec
+ _entry(
+ apps,
+ spec,
+ read,
+ usages.of(spec.service),
+ broker.server_serves(spec.service),
)
- for spec in apps.specs.values()
+ for spec, read in reads
],
}
@@ -235,6 +262,7 @@ async def save_organization_connector_app(
session: Annotated[AsyncSession, Depends(get_session)],
session_factory: Annotated[Any, Depends(get_session_factory)],
apps: Annotated[ConnectorApps, Depends(get_connector_apps)],
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
organization_apps: Annotated[OrganizationApps, Depends(get_organization_apps)],
store: Annotated[
OrganizationAppCredentialStore, Depends(get_organization_app_store)
@@ -246,93 +274,32 @@ async def save_organization_connector_app(
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(
+ checked = await checked_save(
apps,
spec,
- AppValues(
- body.client_id,
- {} if previous is None else previous.settings,
- candidate_secrets,
- ),
+ body,
+ None
+ if row is None
+ else StoredApp(row.client_id, row.settings, lambda: store.secrets(row)),
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
+ if isinstance(checked, JSONResponse):
+ return checked
try:
await store.save(
session,
organization_id=organization_id,
service=service,
client_id=body.client_id,
- settings=result.settings,
- secrets=given,
+ settings=checked.result.settings,
+ secrets=checked.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
+ details = checked.details(service, body.client_id)
await record_organization_audit_event(
session,
organization_id=organization_id,
@@ -352,10 +319,17 @@ async def save_organization_connector_app(
details["fields_changed"],
)
return {
- **await _entry(
- session, session_factory, apps, store, user, organization_id, spec
+ **await _one_entry(
+ session,
+ session_factory,
+ apps,
+ store,
+ broker,
+ user,
+ organization_id,
+ spec,
),
- "checks": checks_body(result),
+ "checks": checks_body(checked.result),
}
@@ -375,13 +349,17 @@ async def check_organization_connector_app(
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:
+ read = await _read(session, apps, store, organization_id, spec)
+ await session.commit()
+ if read.saved 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))
+ if read.values is None:
+ raise HTTPException(
+ 409, read.load_error or f"{spec.name}'s app cannot be read."
+ )
+ result = await check_values(
+ apps, spec, read.values, saved_app_redirects(apps, spec)
+ )
return {"ok": result.ok, "checks": checks_body(result)}
@@ -410,7 +388,7 @@ async def organization_connector_app_impact(
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)
+ usage = (await _usages(session_factory, user, organization_id)).of(service)
people, workspaces = usage.not_through(client_id, current)
return {"connected_members": people, "workspaces": workspaces}
@@ -455,6 +433,8 @@ async def remove_organization_connector_app(
user.id,
)
people, workspaces = (
- await _usage(session_factory, user, organization_id, service)
- ).through(removed.client_id)
+ (await _usages(session_factory, user, organization_id))
+ .of(service)
+ .through(removed.client_id)
+ )
return {"connected_members": people, "workspaces": workspaces}
diff --git a/core/tests/switch_core/gateway/test_organization_connector_apps.py b/core/tests/switch_core/gateway/test_organization_connector_apps.py
index 6f72d6626..9ac32961b 100644
--- a/core/tests/switch_core/gateway/test_organization_connector_apps.py
+++ b/core/tests/switch_core/gateway/test_organization_connector_apps.py
@@ -22,10 +22,12 @@ from switch_core.db.models import (
OrganizationMember,
User,
)
+from switch_core.db.session_scope import tenant_session
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.service_connection_store import ServiceConnectionStore
from switch_core.gateway import dependencies as gw_deps
from switch_core.gateway.organization_connector_apps import (
router as organization_connector_apps_router,
@@ -41,6 +43,7 @@ from tests.switch_core.gateway.test_connector_apps import (
PUBLIC,
World,
_google_body,
+ _tenant_id,
_user,
)
from tests.switch_core.management.harness import cookies_for
@@ -339,3 +342,82 @@ class TestRemoving:
OrganizationAuditAction.CONNECTOR_APP_REMOVED,
]
assert events[-1].details == {"service": GOOGLE, "client_id": STATIC_CLIENT_ID}
+
+
+async def _connected(session_factory, name: str, tenant: str, client_id: str) -> None:
+ user = await _user(session_factory, name, tenant_id=tenant)
+ async with session_factory() as session:
+ tenant_id = await _tenant_id(session, tenant)
+ async with tenant_session(session_factory, tenant_id) as session:
+ await ServiceConnectionStore().save_connection(
+ session,
+ user_id=user.id,
+ service=GOOGLE,
+ consent="read",
+ granted_scopes=[],
+ account_id=f"account-{name}",
+ external_identity=f"{name}@example.invalid",
+ encrypted_secret=TEST_KEYRING.encrypt("{}"),
+ oauth_client_id=client_id,
+ )
+ await session.commit()
+
+
+class TestTheServersAppAlongside:
+ async def test_the_server_counts_no_connection_made_through_an_organizations_app(
+ 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:
+ await client.put(
+ f"{APPS}/{GOOGLE}", json=_google_body(), cookies=cookies_for(operator)
+ )
+ world.google.client_id = "organization-client"
+ saved = await client.put(
+ f"{ORGANIZATION_APPS}/{GOOGLE}",
+ json=_google_body(client_id="organization-client"),
+ cookies=cookies_for(admin),
+ )
+ assert saved.status_code == 200, saved.text
+ await _connected(session_factory, "ada", "default", "organization-client")
+ await _connected(session_factory, "bo", "elsewhere", STATIC_CLIENT_ID)
+ servers = await client.get(APPS, cookies=cookies_for(operator))
+ impact = await client.get(
+ f"{APPS}/{GOOGLE}/impact",
+ params={"client_id": "another-client-id"},
+ cookies=cookies_for(operator),
+ )
+ organizations = await client.get(
+ ORGANIZATION_APPS, cookies=cookies_for(admin)
+ )
+ [server] = [a for a in servers.json()["apps"] if a["slug"] == GOOGLE]
+ assert server["stale_connections"] == 0
+ assert (server["connected_members"], server["workspaces"]) == (1, 1)
+ assert impact.json() == {"connected_members": 1, "workspaces": 1}
+ [own] = [a for a in organizations.json()["apps"] if a["slug"] == GOOGLE]
+ assert (own["connected_members"], own["workspaces"]) == (1, 1)
+
+ async def test_a_server_app_switched_off_is_not_one_to_fall_back_to(
+ 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")
+ broker: ServiceBroker = world.harness.gateway_app.state.service_broker
+ async with world.harness.client() as client:
+ await client.put(
+ f"{APPS}/{GOOGLE}", json=_google_body(), cookies=cookies_for(operator)
+ )
+ serving = await client.get(ORGANIZATION_APPS, cookies=cookies_for(admin))
+ # As DISABLED_SERVICES does: refused before any app is looked at.
+ broker._disabled[GOOGLE] = "Google Workspace is switched off here."
+ off = await client.get(ORGANIZATION_APPS, cookies=cookies_for(admin))
+
+ def google(answer) -> dict:
+ [entry] = [a for a in answer.json()["apps"] if a["slug"] == GOOGLE]
+ return entry
+
+ assert google(serving)["server_app"] is True
+ assert google(off)["server_app"] is False
diff --git a/gateway/src/pages/agents/ServiceAccessSection.test.tsx b/gateway/src/pages/agents/ServiceAccessSection.test.tsx
index a40913729..f672b117d 100644
--- a/gateway/src/pages/agents/ServiceAccessSection.test.tsx
+++ b/gateway/src/pages/agents/ServiceAccessSection.test.tsx
@@ -277,6 +277,22 @@ describe("ServiceAccessSection", () => {
expect((screen.getByLabelText("Other for helper") as HTMLInputElement).disabled).toBe(true);
});
+ it("says a service signs in through the organization's app only while that app works", async () => {
+ const through = "Signs in through your organization's Example app.";
+ mockServer(NONE, [onOff("active", { app_source: "organization" })]);
+ render(<ServiceAccessSection agent={agent} onAgentUpdated={vi.fn()} />);
+ expect(await screen.findByText(through)).toBeTruthy();
+ cleanup();
+
+ const broken = "Your organization's Example app can't be used: it is broken.";
+ mockServer(NONE, [
+ onOff("active", { app_source: "organization", unavailable_reason: broken }),
+ ]);
+ render(<ServiceAccessSection agent={agent} onAgentUpdated={vi.fn()} />);
+ expect(await screen.findByText(broken)).toBeTruthy();
+ expect(screen.queryByText(through)).toBeNull();
+ });
+
const NOT_SET_UP =
"Example is not set up on this server. Ask whoever runs your Switch server to add it under Connectors in the Switch dashboard.";
diff --git a/gateway/src/pages/agents/ServiceAccessSection.tsx b/gateway/src/pages/agents/ServiceAccessSection.tsx
index 71ae3dd62..306058613 100644
--- a/gateway/src/pages/agents/ServiceAccessSection.tsx
+++ b/gateway/src/pages/agents/ServiceAccessSection.tsx
@@ -332,7 +332,8 @@ function OnOffGrant({
{grant.summary}
</Typography>
)}
- {service.app_source === "organization" && (
+ {/* Only while it works: a broken organization app says why below. */}
+ {service.app_source === "organization" && service.unavailable_reason === null && (
<Typography variant="body2" color="text.secondary" sx={{ mt: 0.5 }}>
Signs in through your organization&apos;s {service.name} app.
</Typography>