Skip to content

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

feat(organizations): org admins switch services on or off for their workspaces, and lock them

Petr Bauchfda78dcce6eb
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.py
index 9a3e87484..8b8487f74 100644
--- a/core/switch_core/connections/broker.py
+++ b/core/switch_core/connections/broker.py
@@ -172,6 +172,14 @@ UnavailableCode = Literal[
]
+def organization_switched_off(name: str) -> str:
+ """Why a service is off where the workspace's organization locked it so."""
+ return (
+ f"Your organization has switched {name} off for its workspaces. An "
+ "organization admin can change it under Organization, Services."
+ )
+
+
# 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"]
@@ -470,6 +478,9 @@ class ServiceBroker:
self._adapters = adapters
# Organizations' own apps, with `organization_connector_apps` on.
self._organization_apps = organization_apps
+ # Organizations' service settings count with the same flag as their
+ # apps (`organization_connector_apps`): a phase ships whole.
+ self._organization_settings = organization_apps is not None
# Switched off here, by service, with the reason people are shown.
self._disabled = {
service: reason or SWITCHED_OFF for service, reason in disabled.items()
@@ -533,21 +544,31 @@ class ServiceBroker:
if not self._workspace_gate:
return
self._require_switched_on(service)
- if not await self._workspace_services.is_enabled(session, service):
- raise ServiceError(
- 403,
- FORBIDDEN,
- f"{name} is not enabled in this workspace. Ask a workspace admin; "
- "they switch it on under Connectors in Switch Console.",
- retryable=False,
+ switch = (
+ await self._workspace_services.switches(
+ session, [service], organizations=self._organization_settings
)
+ )[service]
+ if switch.enabled:
+ return
+ raise ServiceError(
+ 403,
+ FORBIDDEN,
+ organization_switched_off(name)
+ if switch.locked
+ else f"{name} is not enabled in this workspace. Ask a workspace admin; "
+ "they switch it on under Connectors in Switch Console.",
+ retryable=False,
+ )
async def workspace_disabled(self, session: AsyncSession) -> set[str]:
"""The services off in this workspace, by an admin's switch or the
workspace's default; none with the gate off."""
if not self._workspace_gate:
return set()
- return await self._workspace_services.disabled_services(session, self._catalog)
+ return await self._workspace_services.disabled_services(
+ session, self._catalog, organizations=self._organization_settings
+ )
async def _choose(self, session: AsyncSession, service: str) -> _Choice:
"""The adapter `service` goes through in the bound workspace: its
diff --git a/core/switch_core/db/audit.py b/core/switch_core/db/audit.py
index 7c5f7e943..fccbd1746 100644
--- a/core/switch_core/db/audit.py
+++ b/core/switch_core/db/audit.py
@@ -80,6 +80,8 @@ class OrganizationAuditAction(StrEnum):
MEMBER_REMOVED = "organization.member_removed"
CONNECTOR_APP_SAVED = "organization.connector_app_saved"
CONNECTOR_APP_REMOVED = "organization.connector_app_removed"
+ SERVICE_SET = "organization.service_set"
+ SERVICE_CLEARED = "organization.service_cleared"
class ServerAuditAction(StrEnum):
diff --git a/core/switch_core/db/stores/workspace_service_store.py b/core/switch_core/db/stores/workspace_service_store.py
index 50a72169a..0c879e24d 100644
--- a/core/switch_core/db/stores/workspace_service_store.py
+++ b/core/switch_core/db/stores/workspace_service_store.py
@@ -3,17 +3,39 @@
No row means the workspace's default, `Tenant.services_on_by_default`: off
for a workspace made since services started off. Every query runs in the
bound tenant.
+
+With `organizations`, the workspace's organization has a say too
+(`organization_service_settings`), and whether a service is on is the first
+of: the organization's setting, if locked; the workspace's own; the
+organization's, unlocked; the workspace's default. Without, the organization's
+settings are never read: that is a server with `organization_connector_apps`
+off.
"""
from __future__ import annotations
from collections.abc import Iterable
+from dataclasses import dataclass
from sqlalchemy import func, select
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.ext.asyncio import AsyncSession
-from switch_core.db.models import Tenant, WorkspaceServiceSetting, require_tenant_id
+from switch_core.db.models import (
+ OrganizationServiceSetting,
+ Tenant,
+ WorkspaceServiceSetting,
+ require_tenant_id,
+)
+
+
+@dataclass(frozen=True)
+class ServiceSwitch:
+ """Whether a service is on in the workspace, and whether its organization
+ locked it so, out of the workspace admin's reach."""
+
+ enabled: bool
+ locked: bool
class WorkspaceServiceStore:
@@ -27,28 +49,65 @@ class WorkspaceServiceStore:
raise LookupError(f"No workspace {tenant_id!r}.")
return on
+ async def switches(
+ self, session: AsyncSession, services: Iterable[str], *, organizations: bool
+ ) -> dict[str, ServiceSwitch]:
+ """Each of `services`, on or off in the workspace, and whether its
+ organization locked it."""
+ default = await self.on_by_default(session)
+ own = await self.list_settings(session)
+ organization = (
+ await self.organization_settings(session) if organizations else {}
+ )
+ switches: dict[str, ServiceSwitch] = {}
+ for service in services:
+ set_above = organization.get(service)
+ if set_above is not None and set_above.locked:
+ switches[service] = ServiceSwitch(set_above.enabled, locked=True)
+ elif service in own:
+ switches[service] = ServiceSwitch(own[service].enabled, locked=False)
+ elif set_above is not None:
+ switches[service] = ServiceSwitch(set_above.enabled, locked=False)
+ else:
+ switches[service] = ServiceSwitch(default, locked=False)
+ return switches
+
async def disabled_services(
- self, session: AsyncSession, services: Iterable[str]
+ self, session: AsyncSession, services: Iterable[str], *, organizations: bool
) -> set[str]:
"""Those of `services` that are off in the workspace."""
- default = await self.on_by_default(session)
- settings = await self.list_settings(session)
return {
service
- for service in services
- if not (settings[service].enabled if service in settings else default)
+ for service, switch in (
+ await self.switches(session, services, organizations=organizations)
+ ).items()
+ if not switch.enabled
}
- async def is_enabled(self, session: AsyncSession, service: str) -> bool:
- enabled = await session.scalar(
- select(WorkspaceServiceSetting.enabled).where(
- WorkspaceServiceSetting.tenant_id == require_tenant_id(),
- WorkspaceServiceSetting.service == service,
+ async def is_enabled(
+ self, session: AsyncSession, service: str, *, organizations: bool
+ ) -> bool:
+ switches = await self.switches(session, [service], organizations=organizations)
+ return switches[service].enabled
+
+ async def organization_settings(
+ self, session: AsyncSession
+ ) -> dict[str, OrganizationServiceSetting]:
+ """What the workspace's organization set, by service; none for a
+ workspace in no organization."""
+ return {
+ setting.service: setting
+ for setting in await session.scalars(
+ select(OrganizationServiceSetting)
+ .join(
+ Tenant,
+ Tenant.organization_id
+ == OrganizationServiceSetting.organization_id,
+ )
+ .where(Tenant.id == require_tenant_id())
+ .execution_options(populate_existing=True)
)
- )
- if enabled is None:
- return await self.on_by_default(session)
- return enabled
+ }
async def list_settings(
self, session: AsyncSession
diff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.py
index 94248a410..b9e0a31f1 100644
--- a/core/switch_core/gateway/app.py
+++ b/core/switch_core/gateway/app.py
@@ -62,6 +62,9 @@ from 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.organization_services import (
+ router as organization_services_router,
+)
from switch_core.gateway.organizations import router as organizations_router
from switch_core.gateway.packages import router as packages_router
from switch_core.gateway.providers import router as providers_router
@@ -186,6 +189,9 @@ def create_gateway_app(
app.include_router(
organization_connector_apps_router, tags=["connector-apps"]
)
+ app.include_router(
+ organization_services_router, tags=["service-connections"]
+ )
# authlib's OIDC client stores transient state/nonce/PKCE in the request
# session across the IdP redirect round-trip; SameSite=Lax lets the cookie
diff --git a/core/switch_core/gateway/organization_services.py b/core/switch_core/gateway/organization_services.py
new file mode 100644
index 000000000..927f30641
--- /dev/null
+++ b/core/switch_core/gateway/organization_services.py
@@ -0,0 +1,303 @@
+"""An organization's admins switch catalog services on or off for every
+workspace in it, and lock them there.
+
+Whether a service is on in a workspace is the first of
+(`db/stores/workspace_service_store.py`): the organization's setting, if
+locked; the workspace admin's own; the organization's, unlocked; the
+workspace's default. So an unlocked setting is a default each workspace may
+override, and a locked one is out of every workspace admin's reach: off and
+locked is how an organization keeps a service out of all its workspaces.
+Clearing the setting gives up the organization's say.
+
+A change that leaves the service off in a workspace withdraws what it was
+issued there, as a workspace admin's switch does: tokens are queued for
+revocation, revoked before the answer where the vendor allows, and the
+warning says what could not be. Grants are kept.
+
+`org_admin` only, acting on the bound workspace's organization. Mounted only
+with both `service_connections` and `organization_connector_apps` on, the
+flag the rest of the organization's say in services ships under.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from typing import Annotated, Any
+
+from fastapi import APIRouter, Depends, HTTPException
+from pydantic import BaseModel, ConfigDict
+from sqlalchemy import func, select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.connections.broker import ServiceBroker, get_service_broker
+from switch_core.connections.loader import CATALOG
+from switch_core.db.audit import (
+ OrganizationAuditAction,
+ record_organization_audit_event,
+)
+from switch_core.db.models import (
+ OrganizationServiceSetting,
+ ServiceConnection,
+ ServiceTokenIssuance,
+ User,
+)
+from switch_core.db.session_scope import tenant_session
+from switch_core.db.stores.organization_service_store import OrganizationServiceStore
+from switch_core.db.stores.organization_store import OrganizationStore
+from switch_core.db.stores.user_store import UserStore
+from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore
+from switch_core.gateway.auth import get_current_user, organization_workspace_ids
+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
+
+router = APIRouter(prefix="/organizations/current/services")
+SETTINGS = OrganizationServiceStore()
+WORKSPACES = WorkspaceServiceStore()
+
+
+def _services() -> list[str]:
+ return [
+ entry.definition.slug for entry in CATALOG.values() if entry.definition.enabled
+ ]
+
+
+def _definition_name(service: str) -> str:
+ entry = CATALOG.get(service)
+ if entry is None or not entry.definition.enabled:
+ raise HTTPException(status_code=404, detail=f"No service named {service!r}.")
+ return entry.definition.name
+
+
+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 _reach(
+ session_factory: async_sessionmaker[AsyncSession], workspace_ids: list[str]
+) -> tuple[dict[str, int], dict[str, int]]:
+ """In how many of the workspaces each service is on, and how many people
+ there have it connected: what an admin weighs before switching it. One
+ short session per workspace, after the request's has let its go."""
+ on: dict[str, int] = {}
+ connected: dict[str, int] = {}
+ for tenant_id in workspace_ids:
+ async with tenant_session(session_factory, tenant_id) as session:
+ switches = await WORKSPACES.switches(
+ session, _services(), organizations=True
+ )
+ counts = await session.execute(
+ select(
+ ServiceConnection.service,
+ func.count(func.distinct(ServiceConnection.user_id)),
+ )
+ .where(
+ ServiceConnection.tenant_id == tenant_id,
+ ServiceConnection.status == "active",
+ )
+ .group_by(ServiceConnection.service)
+ )
+ for service, count in counts.tuples():
+ connected[service] = connected.get(service, 0) + count
+ for service, switch in switches.items():
+ if switch.enabled:
+ on[service] = on.get(service, 0) + 1
+ return on, connected
+
+
+async def _withdraw_where_off(
+ session_factory: async_sessionmaker[AsyncSession],
+ broker: ServiceBroker,
+ workspace_ids: list[str],
+ service: str,
+) -> str | None:
+ """After a change has committed: in each workspace where `service` is now
+ off, withdraw what it was issued there, as a workspace admin's switch
+ does. The warnings, once each, of what could not be taken back yet."""
+ issued_for_it = (ServiceTokenIssuance.service == service,)
+ warnings: list[str] = []
+ for tenant_id in workspace_ids:
+ async with tenant_session(session_factory, tenant_id) as session:
+ if await WORKSPACES.is_enabled(session, service, organizations=True):
+ continue
+ await broker.queue_revocation(session, *issued_for_it)
+ await session.commit()
+ warning = await broker.revoke_withdrawn(session, issued_for_it)
+ if warning is not None and warning not in warnings:
+ warnings.append(warning)
+ return " ".join(warnings) or None
+
+
+def _setting_body(setting: OrganizationServiceSetting | None) -> dict[str, bool] | None:
+ return (
+ None
+ if setting is None
+ else {"enabled": setting.enabled, "locked": setting.locked}
+ )
+
+
+async def list_organization_services(
+ session: Annotated[AsyncSession, Depends(get_session)],
+ session_factory: Annotated[Any, Depends(get_session_factory)],
+ 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]:
+ """Every catalog service that can be connected, what the organization set
+ for it, if anything, and how far it reaches across the organization."""
+ organization_id = await _admin_of(session, user_store, organization_store, user)
+ settings = await SETTINGS.list_settings(session, organization_id)
+ authors = {
+ author.id: author.name
+ for author in await session.scalars(
+ select(User).where(
+ User.id.in_(
+ {
+ setting.updated_by
+ for setting in settings.values()
+ if setting.updated_by is not None
+ }
+ )
+ )
+ )
+ }
+ # The request's connection goes back before the scan opens one per workspace.
+ await session.commit()
+ workspace_ids = await organization_workspace_ids(
+ session_factory, user.id, organization_id
+ )
+ on, connected = await _reach(session_factory, workspace_ids)
+ services = []
+ for entry in CATALOG.values():
+ definition = entry.definition
+ if not definition.enabled:
+ continue
+ setting = settings.get(definition.slug)
+ updated_at: datetime | None = None if setting is None else setting.updated_at
+ services.append(
+ {
+ "slug": definition.slug,
+ "name": definition.name,
+ "setting": _setting_body(setting),
+ "updated_by_name": (
+ None
+ if setting is None or setting.updated_by is None
+ else authors.get(setting.updated_by)
+ ),
+ "updated_at": None if updated_at is None else updated_at.isoformat(),
+ "workspaces_on": on.get(definition.slug, 0),
+ "connected_members": connected.get(definition.slug, 0),
+ }
+ )
+ return {"workspaces": len(workspace_ids), "services": services}
+
+
+class SettingBody(BaseModel):
+ model_config = ConfigDict(extra="forbid", strict=True)
+ enabled: bool
+ locked: bool
+
+
[email protected]("/{service}")
+async def set_organization_service(
+ service: str,
+ body: SettingBody,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ session_factory: Annotated[Any, Depends(get_session_factory)],
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
+ 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]:
+ """Set `service` on or off for the organization's workspaces, locked or
+ not. Where that leaves it off, what was issued for it is withdrawn."""
+ organization_id = await _admin_of(session, user_store, organization_store, user)
+ _definition_name(service)
+ previous = (await SETTINGS.list_settings(session, organization_id)).get(service)
+ details: dict[str, Any] = {
+ "service": service,
+ "enabled": body.enabled,
+ "locked": body.locked,
+ "previous": _setting_body(previous),
+ }
+ await SETTINGS.set(
+ session,
+ organization_id,
+ service,
+ enabled=body.enabled,
+ locked=body.locked,
+ updated_by=user.id,
+ )
+ await record_organization_audit_event(
+ session,
+ organization_id=organization_id,
+ actor_user_id=user.id,
+ action=OrganizationAuditAction.SERVICE_SET,
+ target_type="service",
+ target_id=service,
+ details=details,
+ )
+ await session.commit()
+ workspace_ids = await organization_workspace_ids(
+ session_factory, user.id, organization_id
+ )
+ warning = await _withdraw_where_off(session_factory, broker, workspace_ids, service)
+ return {
+ "slug": service,
+ "setting": {"enabled": body.enabled, "locked": body.locked},
+ "warning": warning,
+ }
+
+
[email protected]("/{service}")
+async def clear_organization_service(
+ service: str,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ session_factory: Annotated[Any, Depends(get_session_factory)],
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
+ 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]:
+ """Give up the organization's say in `service`: each workspace is back to
+ its own switch, or its default. Where that leaves it off, what was issued
+ for it is withdrawn."""
+ organization_id = await _admin_of(session, user_store, organization_store, user)
+ name = _definition_name(service)
+ previous = (await SETTINGS.list_settings(session, organization_id)).get(service)
+ if previous is None:
+ raise HTTPException(
+ status_code=404,
+ detail=f"Your organization has no setting for {name}.",
+ )
+ details = {"service": service, "previous": _setting_body(previous)}
+ await SETTINGS.clear(session, organization_id, service)
+ await record_organization_audit_event(
+ session,
+ organization_id=organization_id,
+ actor_user_id=user.id,
+ action=OrganizationAuditAction.SERVICE_CLEARED,
+ target_type="service",
+ target_id=service,
+ details=details,
+ )
+ await session.commit()
+ workspace_ids = await organization_workspace_ids(
+ session_factory, user.id, organization_id
+ )
+ warning = await _withdraw_where_off(session_factory, broker, workspace_ids, service)
+ return {"slug": service, "setting": None, "warning": warning}
diff --git a/core/switch_core/gateway/workspace_services.py b/core/switch_core/gateway/workspace_services.py
index bf2f89f1f..3e725261e 100644
--- a/core/switch_core/gateway/workspace_services.py
+++ b/core/switch_core/gateway/workspace_services.py
@@ -10,6 +10,10 @@ switching it back on returns them. A token already handed out may be used
until that restart; one the vendor can revoke (GitHub's) is revoked here, and
one no vendor can revoke alone is marked withdrawn and stays valid until it
expires, which the answer's warning says.
+
+With `organization_connector_apps` on, the workspace's organization has a say
+(`gateway/organization_services.py`): a service it locked is out of the
+workspace admin's reach, and one it set unlocked is the workspace's default.
"""
from __future__ import annotations
@@ -34,6 +38,7 @@ from switch_core.db.models import (
)
from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore
from switch_core.gateway.auth import require_tenant_admin
+from switch_core.gateway.connector_apps import organization_apps_on
from switch_core.gateway.dependencies import get_session
router = APIRouter(prefix="/workspace/services")
@@ -53,11 +58,16 @@ async def _counts(session: AsyncSession, column: Any, model: Any) -> dict[str, i
async def list_workspace_services(
_: Annotated[User, Depends(require_tenant_admin)],
session: Annotated[AsyncSession, Depends(get_session)],
+ organizations: Annotated[bool, Depends(organization_apps_on)],
) -> dict[str, Any]:
"""Every catalog service that can be connected, whether the workspace has
it on, and who it reaches: what an admin weighs before switching it off."""
settings = await STORE.list_settings(session)
- on_by_default = await STORE.on_by_default(session)
+ switches = await STORE.switches(
+ session,
+ [entry.definition.slug for entry in CATALOG.values()],
+ organizations=organizations,
+ )
names = {
user.id: user.name
for user in await session.scalars(
@@ -79,7 +89,10 @@ async def list_workspace_services(
{
"slug": definition.slug,
"name": definition.name,
- "enabled": on_by_default if setting is None else setting.enabled,
+ "enabled": switches[definition.slug].enabled,
+ # Locked by the workspace's organization: the admin's switch
+ # does nothing until an organization admin unlocks it.
+ "organization_locked": switches[definition.slug].locked,
"updated_by_name": (
None if setting is None else names.get(setting.updated_by)
),
@@ -103,6 +116,7 @@ async def set_workspace_service(
user: Annotated[User, Depends(require_tenant_admin)],
session: Annotated[AsyncSession, Depends(get_session)],
broker: Annotated[ServiceBroker, Depends(get_service_broker)],
+ organizations: Annotated[bool, Depends(organization_apps_on)],
) -> dict[str, Any]:
"""Switch `service` on or off for the workspace.
@@ -114,6 +128,17 @@ async def set_workspace_service(
entry = CATALOG.get(service)
if entry is None or not entry.definition.enabled:
raise HTTPException(status_code=404, detail=f"No service named {service!r}.")
+ switch = (await STORE.switches(session, [service], organizations=organizations))[
+ service
+ ]
+ if switch.locked:
+ state = "on" if switch.enabled else "off"
+ raise HTTPException(
+ status_code=409,
+ detail=f"Your organization has {entry.definition.name} locked {state} "
+ "for its workspaces. An organization admin can change it under "
+ "Organization, Services.",
+ )
await STORE.set_enabled(session, service, body.enabled, user.id)
issued_for_it = (ServiceTokenIssuance.service == service,)
if not body.enabled:
diff --git a/core/tests/switch_core/db/test_workspace_service_store.py b/core/tests/switch_core/db/test_workspace_service_store.py
index 9595f5a36..403363cd7 100644
--- a/core/tests/switch_core/db/test_workspace_service_store.py
+++ b/core/tests/switch_core/db/test_workspace_service_store.py
@@ -16,7 +16,11 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.db.models import TENANT_ZERO_ID, Tenant, User, WorkspaceServiceSetting
from switch_core.db.session_scope import tenant_session
-from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore
+from switch_core.db.stores.organization_service_store import OrganizationServiceStore
+from switch_core.db.stores.workspace_service_store import (
+ ServiceSwitch,
+ WorkspaceServiceStore,
+)
from tests.conftest import RLSHarness
STORE = WorkspaceServiceStore()
@@ -51,8 +55,12 @@ class TestSwitch:
) -> None:
async with session_factory() as session:
assert await STORE.on_by_default(session) is False
- assert await STORE.is_enabled(session, "datadog") is False
- assert await STORE.disabled_services(session, SERVICES) == set(SERVICES)
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is False
+ )
+ assert await STORE.disabled_services(
+ session, SERVICES, organizations=False
+ ) == set(SERVICES)
assert await STORE.list_settings(session) == {}
async def test_a_workspace_on_by_default_with_no_row_has_every_service_on(
@@ -60,8 +68,13 @@ class TestSwitch:
) -> None:
await _on_by_default(session_factory)
async with session_factory() as session:
- assert await STORE.is_enabled(session, "datadog") is True
- assert await STORE.disabled_services(session, SERVICES) == set()
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is True
+ )
+ assert (
+ await STORE.disabled_services(session, SERVICES, organizations=False)
+ == set()
+ )
async def test_on_then_off_round_trips_in_a_new_workspace(
self, session_factory
@@ -70,17 +83,28 @@ class TestSwitch:
async with session_factory() as session:
await STORE.set_enabled(session, "datadog", True, admin)
await session.commit()
- assert await STORE.is_enabled(session, "datadog") is True
- assert await STORE.is_enabled(session, "atlassian") is False
- assert await STORE.disabled_services(session, SERVICES) == {
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is True
+ )
+ assert (
+ await STORE.is_enabled(session, "atlassian", organizations=False)
+ is False
+ )
+ assert await STORE.disabled_services(
+ session, SERVICES, organizations=False
+ ) == {
"atlassian",
"github",
}
await STORE.set_enabled(session, "datadog", False, admin)
await session.commit()
- assert await STORE.is_enabled(session, "datadog") is False
- assert await STORE.disabled_services(session, SERVICES) == set(SERVICES)
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is False
+ )
+ assert await STORE.disabled_services(
+ session, SERVICES, organizations=False
+ ) == set(SERVICES)
async def test_off_then_on_round_trips_in_a_workspace_on_by_default(
self, session_factory
@@ -90,14 +114,26 @@ class TestSwitch:
async with session_factory() as session:
await STORE.set_enabled(session, "datadog", False, admin)
await session.commit()
- assert await STORE.is_enabled(session, "datadog") is False
- assert await STORE.is_enabled(session, "atlassian") is True
- assert await STORE.disabled_services(session, SERVICES) == {"datadog"}
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is False
+ )
+ assert (
+ await STORE.is_enabled(session, "atlassian", organizations=False)
+ is True
+ )
+ assert await STORE.disabled_services(
+ session, SERVICES, organizations=False
+ ) == {"datadog"}
await STORE.set_enabled(session, "datadog", True, admin)
await session.commit()
- assert await STORE.is_enabled(session, "datadog") is True
- assert await STORE.disabled_services(session, SERVICES) == set()
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is True
+ )
+ assert (
+ await STORE.disabled_services(session, SERVICES, organizations=False)
+ == set()
+ )
async def test_the_same_switch_twice_is_one_row_naming_the_last_admin(
self, session_factory
@@ -147,8 +183,75 @@ class TestIsolation:
text("SELECT service FROM workspace_service_settings")
)
assert raw.all() == []
- assert await STORE.is_enabled(session, "datadog") is True
- assert await STORE.disabled_services(session, SERVICES) == set()
+ assert (
+ await STORE.is_enabled(session, "datadog", organizations=False) is True
+ )
+ assert (
+ await STORE.disabled_services(session, SERVICES, organizations=False)
+ == set()
+ )
async with tenant_session(rls_harness.restricted, mine) as session:
- assert await STORE.disabled_services(session, SERVICES) == {"datadog"}
+ assert await STORE.disabled_services(
+ session, SERVICES, organizations=False
+ ) == {"datadog"}
+
+
+class TestTheOrganizationsSay:
+ """The first of: the organization's setting, if locked; the workspace's
+ own; the organization's, unlocked; the workspace's default."""
+
+ async def _organization(self, session_factory, *, enabled: bool, locked: bool):
+ admin = await _user(session_factory, "org-admin")
+ async with session_factory() as session:
+ await OrganizationServiceStore().set(
+ session,
+ TENANT_ZERO_ID,
+ "datadog",
+ enabled=enabled,
+ locked=locked,
+ updated_by=admin,
+ )
+ await session.commit()
+
+ async def _own(self, session_factory, enabled: bool) -> None:
+ admin = await _user(session_factory, "workspace-admin")
+ async with tenant_session(session_factory, TENANT_ZERO_ID) as session:
+ await STORE.set_enabled(session, "datadog", enabled, admin)
+ await session.commit()
+
+ async def _switch(self, session_factory, *, organizations: bool) -> ServiceSwitch:
+ async with tenant_session(session_factory, TENANT_ZERO_ID) as session:
+ return (
+ await STORE.switches(session, ["datadog"], organizations=organizations)
+ )["datadog"]
+
+ async def test_locked_wins_over_the_workspaces_own(self, session_factory) -> None:
+ await self._own(session_factory, True)
+ await self._organization(session_factory, enabled=False, locked=True)
+ assert await self._switch(session_factory, organizations=True) == ServiceSwitch(
+ enabled=False, locked=True
+ )
+
+ async def test_the_workspaces_own_wins_over_unlocked(self, session_factory) -> None:
+ await self._own(session_factory, False)
+ await self._organization(session_factory, enabled=True, locked=False)
+ assert await self._switch(session_factory, organizations=True) == ServiceSwitch(
+ enabled=False, locked=False
+ )
+
+ async def test_unlocked_wins_over_the_workspaces_default(
+ self, session_factory
+ ) -> None:
+ await self._organization(session_factory, enabled=True, locked=False)
+ assert await self._switch(session_factory, organizations=True) == ServiceSwitch(
+ enabled=True, locked=False
+ )
+
+ async def test_without_organizations_their_settings_are_not_read(
+ self, session_factory
+ ) -> None:
+ await self._organization(session_factory, enabled=True, locked=True)
+ assert await self._switch(
+ session_factory, organizations=False
+ ) == ServiceSwitch(enabled=False, locked=False)
diff --git a/core/tests/switch_core/gateway/test_app_route_mounting.py b/core/tests/switch_core/gateway/test_app_route_mounting.py
index 64f9b6991..9a1a21dfe 100644
--- a/core/tests/switch_core/gateway/test_app_route_mounting.py
+++ b/core/tests/switch_core/gateway/test_app_route_mounting.py
@@ -110,3 +110,19 @@ def test_organizations_connector_apps_are_mounted_only_with_both_flags() -> None
assert ORGANIZATION_APP_ROUTES <= paths(
"service_connections,organization_connector_apps"
)
+
+
+def test_organizations_service_settings_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]
+ }
+
+ routes = {
+ "/organizations/current/services",
+ "/organizations/current/services/{service}",
+ }
+ assert not routes & paths("service_connections")
+ assert not routes & paths("organization_connector_apps")
+ assert routes <= paths("service_connections,organization_connector_apps")
diff --git a/core/tests/switch_core/gateway/test_organization_services.py b/core/tests/switch_core/gateway/test_organization_services.py
new file mode 100644
index 000000000..d26dcfbe4
--- /dev/null
+++ b/core/tests/switch_core/gateway/test_organization_services.py
@@ -0,0 +1,315 @@
+"""An organization's switch per service, for every workspace in it, through
+the real gateway on Postgres.
+
+Locked, it is out of every workspace admin's reach; unlocked, it is the
+default each workspace may override; cleared, the organization has no say.
+Where a change leaves a service off, what it was issued is withdrawn in each
+workspace, as a workspace admin's switch does.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from typing import cast
+
+import pytest
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.connections.app_credentials import ConnectorApps
+from switch_core.connections.broker import (
+ ServiceBroker,
+ ServiceError,
+ organization_switched_off,
+)
+from switch_core.connections.organization_apps import OrganizationApps
+from switch_core.db.audit import OrganizationAuditAction
+from switch_core.db.models import (
+ TENANT_ZERO_ID,
+ OrganizationAuditEvent,
+ OrganizationMember,
+ Tenant,
+ 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.gateway import dependencies as gw_deps
+from switch_core.gateway.organization_services import (
+ router as organization_services_router,
+)
+from tests.conftest import TEST_KEYRING
+from tests.switch_core.connections.fake_vendor import FakeVendor
+from tests.switch_core.connections.service_harness import (
+ agent_with_key,
+ build_service_harness,
+)
+from tests.switch_core.gateway.test_workspace_services import (
+ _granted,
+ _issuance,
+ _issuances,
+ _person,
+)
+from tests.switch_core.management.harness import Harness, add_member, cookies_for
+
+ORGANIZATION = "/gateway/organizations/current/services"
+WORKSPACE = "/gateway/workspace/services"
+# A second workspace of tenant zero's organization.
+SIBLING = "tenant-organization-services-sibling"
+
+
+def vendor() -> FakeVendor:
+ return FakeVendor()
+
+
+async def harness(
+ session_factory: async_sessionmaker[AsyncSession], vendor: FakeVendor
+) -> Harness:
+ """Services gated per workspace, with organizations' say in force: the
+ broker holds organizations' apps (none saved), as with
+ `organization_connector_apps` on."""
+ harness, broker = build_service_harness(
+ session_factory, vendor, workspace_gate=True
+ )
+ organization_apps = OrganizationApps(
+ apps=cast(ConnectorApps, SimpleNamespace(specs={})),
+ store=OrganizationAppCredentialStore(TEST_KEYRING),
+ )
+ broker._organization_apps = organization_apps
+ broker._organization_settings = True
+ harness.gateway_app.state.organization_apps = organization_apps
+ harness.gateway_app.dependency_overrides[gw_deps.get_organization_store] = lambda: (
+ OrganizationStore()
+ )
+ harness.gateway_app.include_router(organization_services_router)
+ async with session_factory() as session:
+ session.add(
+ Tenant(
+ id=SIBLING, slug=SIBLING, name="Sibling", organization_id=TENANT_ZERO_ID
+ )
+ )
+ await session.commit()
+ return harness
+
+
+async def _org_admin(session_factory: async_sessionmaker[AsyncSession]) -> User:
+ user = await _person(session_factory, "org-admin", member_role=None)
+ 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 _call(harness: Harness, user: User, method: str, path: str, body=None):
+ async with harness.client() as client:
+ return await client.request(method, path, json=body, cookies=cookies_for(user))
+
+
+def _by_slug(response) -> dict[str, dict]:
+ assert response.status_code == 200, response.text
+ return {service["slug"]: service for service in response.json()["services"]}
+
+
+async def _events(session_factory) -> list[OrganizationAuditEvent]:
+ async with session_factory() as session:
+ return list(
+ await session.scalars(
+ select(OrganizationAuditEvent)
+ .where(OrganizationAuditEvent.action.like("organization.service_%"))
+ .order_by(OrganizationAuditEvent.occurred_at)
+ )
+ )
+
+
+class TestWhoMayCall:
+ async def test_a_workspace_owner_is_refused(self, harness: Harness) -> None:
+ owner = await _person(harness.session_factory, "owner", member_role="owner")
+ answers = [
+ await _call(harness, owner, "GET", ORGANIZATION),
+ await _call(
+ harness,
+ owner,
+ "PUT",
+ f"{ORGANIZATION}/github",
+ {"enabled": False, "locked": True},
+ ),
+ await _call(harness, owner, "DELETE", f"{ORGANIZATION}/github"),
+ ]
+ assert [answer.status_code for answer in answers] == [403] * 3
+
+ async def test_an_unknown_service_is_not_found(self, harness: Harness) -> None:
+ admin = await _org_admin(harness.session_factory)
+ answer = await _call(
+ harness,
+ admin,
+ "PUT",
+ f"{ORGANIZATION}/no-such-service",
+ {"enabled": True, "locked": False},
+ )
+ assert answer.status_code == 404
+
+
+class TestLocked:
+ async def test_off_and_locked_keeps_it_out_of_every_workspace(
+ self, harness: Harness, vendor: FakeVendor, session_factory
+ ) -> None:
+ admin = await _org_admin(session_factory)
+ workspace_admin = await _person(
+ session_factory, "workspace-admin", member_role="admin"
+ )
+ ada = await add_member(session_factory, "ada")
+ agent_id, _ = await agent_with_key(session_factory, ada, "builder")
+ github = await _granted(session_factory, ada, agent_id, "github")
+ async with session_factory() as session:
+ session.add(_issuance(github, ada.id, "github", "ghs_here"))
+ await session.commit()
+ async with tenant_session(session_factory, SIBLING) as session:
+ session.add(
+ _issuance(None, ada.id, "github", "ghs_elsewhere", tenant_id=SIBLING)
+ )
+ await session.commit()
+ # The workspace admin had it on.
+ assert (
+ await _call(
+ harness,
+ workspace_admin,
+ "PUT",
+ f"{WORKSPACE}/github",
+ {"enabled": True},
+ )
+ ).status_code == 200
+
+ locked = await _call(
+ harness,
+ admin,
+ "PUT",
+ f"{ORGANIZATION}/github",
+ {"enabled": False, "locked": True},
+ )
+ listed = await _call(harness, workspace_admin, "GET", WORKSPACE)
+ refused = await _call(
+ harness, workspace_admin, "PUT", f"{WORKSPACE}/github", {"enabled": True}
+ )
+ organization = await _call(harness, admin, "GET", ORGANIZATION)
+
+ assert locked.status_code == 200, locked.text
+ assert locked.json()["warning"] is None
+ assert sorted(vendor.revoked) == ["ghs_elsewhere", "ghs_here"]
+ assert (await _issuances(session_factory, SIBLING))[
+ "ghs_elsewhere"
+ ].encrypted_token is None
+ github_here = _by_slug(listed)["github"]
+ assert github_here["enabled"] is False
+ assert github_here["organization_locked"] is True
+ assert refused.status_code == 409
+ assert "locked off" in refused.json()["detail"]
+ assert organization.json()["workspaces"] == 2
+ github_above = _by_slug(organization)["github"]
+ assert github_above["setting"] == {"enabled": False, "locked": True}
+ assert github_above["workspaces_on"] == 0
+ assert github_above["updated_by_name"] == "org-admin"
+ [event] = await _events(session_factory)
+ assert event.action == OrganizationAuditAction.SERVICE_SET
+ assert event.details == {
+ "service": "github",
+ "enabled": False,
+ "locked": True,
+ "previous": None,
+ }
+
+ async def test_the_refusal_names_the_organization(
+ self, harness: Harness, session_factory
+ ) -> None:
+ admin = await _org_admin(session_factory)
+ await _call(
+ harness,
+ admin,
+ "PUT",
+ f"{ORGANIZATION}/github",
+ {"enabled": False, "locked": True},
+ )
+ broker: ServiceBroker = harness.gateway_app.state.service_broker
+ async with tenant_session(session_factory, TENANT_ZERO_ID) as session:
+ with pytest.raises(ServiceError) as refused:
+ await broker.require_workspace_enabled(session, "github")
+ assert refused.value.message == organization_switched_off("GitHub")
+
+
+class TestUnlocked:
+ async def test_it_is_the_default_each_workspace_may_override(
+ self, harness: Harness, session_factory
+ ) -> None:
+ admin = await _org_admin(session_factory)
+ workspace_admin = await _person(
+ session_factory, "workspace-admin", member_role="admin"
+ )
+ on = await _call(
+ harness,
+ admin,
+ "PUT",
+ f"{ORGANIZATION}/github",
+ {"enabled": True, "locked": False},
+ )
+ before = _by_slug(await _call(harness, workspace_admin, "GET", WORKSPACE))
+ switched = await _call(
+ harness, workspace_admin, "PUT", f"{WORKSPACE}/github", {"enabled": False}
+ )
+ after = _by_slug(await _call(harness, workspace_admin, "GET", WORKSPACE))
+ organization = _by_slug(await _call(harness, admin, "GET", ORGANIZATION))
+
+ assert on.status_code == 200, on.text
+ assert before["github"] == {
+ **before["github"],
+ "enabled": True,
+ "organization_locked": False,
+ }
+ assert switched.status_code == 200, switched.text
+ assert after["github"]["enabled"] is False
+ # On in the sibling, which never switched it, off here.
+ assert organization["github"]["workspaces_on"] == 1
+
+
+class TestClearing:
+ async def test_clearing_gives_up_the_organizations_say(
+ self, harness: Harness, session_factory
+ ) -> None:
+ admin = await _org_admin(session_factory)
+ workspace_admin = await _person(
+ session_factory, "workspace-admin", member_role="admin"
+ )
+ await _call(
+ harness,
+ admin,
+ "PUT",
+ f"{ORGANIZATION}/github",
+ {"enabled": True, "locked": True},
+ )
+ cleared = await _call(harness, admin, "DELETE", f"{ORGANIZATION}/github")
+ again = await _call(harness, admin, "DELETE", f"{ORGANIZATION}/github")
+ listed = _by_slug(await _call(harness, workspace_admin, "GET", WORKSPACE))
+ organization = _by_slug(await _call(harness, admin, "GET", ORGANIZATION))
+
+ assert cleared.status_code == 200, cleared.text
+ assert again.status_code == 404
+ # Back to the workspace's default: off, in a workspace made since
+ # services started off.
+ assert listed["github"]["enabled"] is False
+ assert listed["github"]["organization_locked"] is False
+ assert organization["github"]["setting"] is None
+ events = await _events(session_factory)
+ assert [event.action for event in events] == [
+ OrganizationAuditAction.SERVICE_SET,
+ OrganizationAuditAction.SERVICE_CLEARED,
+ ]
+ assert events[-1].details == {
+ "service": "github",
+ "previous": {"enabled": True, "locked": True},
+ }
diff --git a/core/tests/switch_core/gateway/test_workspace_services.py b/core/tests/switch_core/gateway/test_workspace_services.py
index 28641c9e0..cf069851c 100644
--- a/core/tests/switch_core/gateway/test_workspace_services.py
+++ b/core/tests/switch_core/gateway/test_workspace_services.py
@@ -251,6 +251,7 @@ class TestResults:
"slug": "datadog",
"name": "Datadog",
"enabled": on_by_default,
+ "organization_locked": False,
"updated_by_name": None,
"updated_at": None,
"connected_members": 0,