Skip to content

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

Sign in to GitHub through the workspace's organization's App

Petr Bauch3490e2e30cf5
diff --git a/core/switch_core/connections/broker.py b/core/switch_core/connections/broker.py
index b04083834..cace27c89 100644
--- a/core/switch_core/connections/broker.py
+++ b/core/switch_core/connections/broker.py
@@ -65,7 +65,10 @@ from switch_core.connections.loader import (
ConnectionDefinition,
TokenPolicy,
)
-from switch_core.connections.organization_apps import OrganizationApps
+from switch_core.connections.organization_apps import (
+ OrganizationApp,
+ OrganizationApps,
+)
from switch_core.connections.shielded import finish_shielded
from switch_core.db.audit import AuditAction, record_audit_event
from switch_core.db.models import (
@@ -543,14 +546,34 @@ class ServiceBroker:
"""The adapter `service` goes through in the bound workspace: its
organization's app where it saved one, else the server's. `session`
is bound to the workspace."""
- if self._organization_apps is not None:
- app = await self._organization_apps.for_workspace(
- session, require_tenant_id(), service
- )
- if app is not None:
- return _Choice(app.adapter, app.problem)
+ 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)
+ async def organization_app(
+ self, session: AsyncSession, service: str
+ ) -> OrganizationApp | None:
+ """The app the bound workspace's organization saved for `service`, or
+ None where the server's applies. `session` is bound to the workspace."""
+ if self._organization_apps is None:
+ return None
+ return await self._organization_apps.for_workspace(
+ session, require_tenant_id(), service
+ )
+
+ async def organization_app_of(
+ self, tenant_id: str, service: str
+ ) -> OrganizationApp | None:
+ """`organization_app` for a workspace no signed-in request has bound,
+ such as a browser's step of a sign-in, read in a session of its own."""
+ if self._organization_apps is None:
+ return None
+ async with tenant_session(self._session_factory, tenant_id) as session:
+ return await self._organization_apps.for_workspace(
+ session, tenant_id, service
+ )
+
async def _here(self, session: AsyncSession, service: str) -> ServiceAdapter | None:
"""`service`'s adapter in the bound workspace, or None."""
return (await self._choose(session, service)).adapter
diff --git a/core/switch_core/connections/organization_apps.py b/core/switch_core/connections/organization_apps.py
index 9ac6f3a90..19e33cffb 100644
--- a/core/switch_core/connections/organization_apps.py
+++ b/core/switch_core/connections/organization_apps.py
@@ -3,15 +3,12 @@
A workspace whose organization saved an app for a service
(`organization_app_credentials`) signs in, is issued and is refreshed through
that app; one whose organization saved none goes through the server's, as
-`connections/app_credentials.py` resolves it. All of this is behind the
+`connections/app_credentials.py` resolves it. That covers GitHub's App,
+whose sign-in routes ask here too (`gateway/github_connections.py`), and
+every static OAuth client. All of this is behind the
`organization_connector_apps` flag: with it off the broker holds no
`OrganizationApps`, and no workspace looks past the server's apps.
-Only the static OAuth clients are looked up here so far. GitHub's App is
-not: its sign-in still goes through the App on the gateway's state, which is
-the server's, so a connection made there would never match an
-organization's App and would be sent back to reconnect forever.
-
An organization's adapter is built the first time one of its workspaces
needs it, and kept. The process that changes an organization's app forgets
the adapter at once and announces the change (`app_signals.py`), and every
@@ -42,6 +39,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from switch_core.connections.adapters import ServiceAdapter
+from switch_core.connections.adapters.github import GitHubApp
from switch_core.connections.app_credentials import (
AppLoadError,
ConnectorApps,
@@ -79,10 +77,12 @@ class OrganizationApp:
"""The app an organization saved for a service, as built here.
`adapter` is None when the app cannot be used, and `problem` then says
- why, for members to read."""
+ why, for members to read. `github_app` is the App itself, for GitHub's
+ sign-in routes."""
organization_id: str
adapter: ServiceAdapter | None
+ github_app: GitHubApp | None
problem: str | None
changed_at: datetime
@@ -120,8 +120,7 @@ class OrganizationApps:
"""The app `tenant_id`'s organization saved for `service`, or None
when it saved none and the server's applies. `session` is bound to
`tenant_id`; it is read through only when nothing kept answers."""
- spec = self._apps.specs.get(service)
- if spec is None or spec.kind != "oauth_client":
+ if service not in self._apps.specs:
return None
if tenant_id in self._organization_of:
organization_id = self._organization_of[tenant_id]
@@ -205,6 +204,7 @@ class OrganizationApps:
return OrganizationApp(
organization_id=row.organization_id,
adapter=None,
+ github_app=None,
problem=problem,
changed_at=row.updated_at,
)
@@ -215,6 +215,7 @@ class OrganizationApps:
github_app=loaded.github_app,
oauth_client=loaded.oauth_client,
),
+ github_app=loaded.github_app,
problem=None,
changed_at=row.updated_at,
)
diff --git a/core/switch_core/gateway/github_connections.py b/core/switch_core/gateway/github_connections.py
index 55054dd61..568b88967 100644
--- a/core/switch_core/gateway/github_connections.py
+++ b/core/switch_core/gateway/github_connections.py
@@ -18,6 +18,7 @@ from switch_core.connections.broker import (
ServiceError,
get_service_broker,
)
+from switch_core.connections.organization_apps import OrganizationApp
from switch_core.connections.sign_in_flows import (
FlowGoneError,
FlowsBusyError,
@@ -53,12 +54,36 @@ INTERRUPTED = "Sign-in was interrupted. Start it again from Switch Console."
def service(request: Request) -> GitHubConnections:
+ """The server's own GitHub App."""
value = request.app.state.github_connections
if value is None:
raise HTTPException(503, "GitHub connections are not enabled on this server.")
return cast(GitHubConnections, value)
+def app_for_workspace(
+ request: Request, app: OrganizationApp | None
+) -> GitHubConnections:
+ """The GitHub App a workspace signs in through: its organization's App
+ `app`, where it saved one, else the server's. An organization's App that
+ cannot be used is refused with the reason, never swapped for the
+ server's."""
+ if app is None:
+ return service(request)
+ if app.github_app is None:
+ raise HTTPException(503, app.problem or "GitHub is not available here.")
+ return app.github_app.connections
+
+
+async def workspace_app(
+ request: Request,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
+) -> GitHubConnections:
+ """The signed-in person's workspace's GitHub App (`app_for_workspace`)."""
+ return app_for_workspace(request, await broker.organization_app(session, "github"))
+
+
def get_sign_in_flows(request: Request) -> SignInFlows:
flows = getattr(request.app.state, "sign_in_flows", None)
if not isinstance(flows, SignInFlows):
@@ -124,7 +149,7 @@ async def start(
session: Annotated[AsyncSession, Depends(get_session)],
user: Annotated[User, Depends(get_current_user)],
broker: Annotated[ServiceBroker, Depends(get_service_broker)],
- github: Annotated[GitHubConnections, Depends(service)],
+ github: Annotated[GitHubConnections, Depends(workspace_app)],
flows: Annotated[SignInFlows, Depends(get_sign_in_flows)],
) -> dict | JSONResponse:
try:
@@ -167,12 +192,24 @@ async def start(
@router.get("/authorize")
async def authorize(
state: str,
- github: Annotated[GitHubConnections, Depends(service)],
+ request: Request,
+ broker: Annotated[ServiceBroker, Depends(get_service_broker)],
flows: Annotated[SignInFlows, Depends(get_sign_in_flows)],
) -> Response:
+ """The browser leaves for GitHub through Core. Nobody is signed in here,
+ so the App is the flow's workspace's."""
nonce = secrets.token_urlsafe(32)
try:
flow = await browser_flow(flows, state)
+ except GitHubError:
+ return page(INTERRUPTED, 400)
+ github = app_for_workspace(
+ request, await broker.organization_app_of(flow.tenant_id, "github")
+ )
+ # Started under another App: the code GitHub issued would be refused.
+ if flow.details.get("client_id") not in (None, github.client_id):
+ return page(INTERRUPTED, 400)
+ try:
taken = await flows.advance(
flow,
expect=("pending",),
@@ -202,7 +239,6 @@ async def authorize(
@router.get("/callback")
async def callback(
request: Request,
- github: Annotated[GitHubConnections, Depends(service)],
flows: Annotated[SignInFlows, Depends(get_sign_in_flows)],
) -> Response:
"""Back from GitHub: the browser takes the code on to Switch Console, on
@@ -264,7 +300,7 @@ async def complete(
user: Annotated[User, Depends(get_current_user)],
session: Annotated[AsyncSession, Depends(get_session)],
broker: Annotated[ServiceBroker, Depends(get_service_broker)],
- github: Annotated[GitHubConnections, Depends(service)],
+ github: Annotated[GitHubConnections, Depends(workspace_app)],
flows: Annotated[SignInFlows, Depends(get_sign_in_flows)],
) -> Response:
"""Console hands back the code its listener received, with its secret:
@@ -277,9 +313,7 @@ async def complete(
if recorded is not None and recorded != github.client_id:
# The code was issued to the App the flow started with: with another
# in its place, GitHub would refuse it, and less clearly.
- changed = (
- "This server's GitHub App changed during your sign-in. Start it again."
- )
+ changed = "The GitHub App changed during your sign-in. Start it again."
await flows.advance(
flow, expect=("delivered",), to="failed", changes={"error": changed}
)
@@ -358,7 +392,6 @@ async def complete(
async def flow_status(
flow_id: str,
user: Annotated[User, Depends(get_current_user)],
- github: Annotated[GitHubConnections, Depends(service)],
flows: Annotated[SignInFlows, Depends(get_sign_in_flows)],
) -> dict:
flow = await owned_flow(flows, flow_id, user.id)
@@ -376,7 +409,6 @@ async def flow_status(
async def drop(
flow_id: str,
user: Annotated[User, Depends(get_current_user)],
- github: Annotated[GitHubConnections, Depends(service)],
flows: Annotated[SignInFlows, Depends(get_sign_in_flows)],
) -> Response:
"""Console is done with the flow: cancelled, or its outcome read."""
@@ -389,7 +421,7 @@ async def connection(
user: Annotated[User, Depends(get_current_user)],
session: Annotated[AsyncSession, Depends(get_session)],
broker: Annotated[ServiceBroker, Depends(get_service_broker)],
- github: Annotated[GitHubConnections, Depends(service)],
+ github: Annotated[GitHubConnections, Depends(workspace_app)],
) -> dict:
return await connection_status(user.id, session, broker, github)
diff --git a/core/tests/switch_core/connections/test_organization_apps.py b/core/tests/switch_core/connections/test_organization_apps.py
index 8a16c8b15..0968f0b45 100644
--- a/core/tests/switch_core/connections/test_organization_apps.py
+++ b/core/tests/switch_core/connections/test_organization_apps.py
@@ -50,6 +50,7 @@ from switch_core.keys import Keyring
from switch_core.signals import SignalBus
from tests.conftest import TEST_KEYRING, RLSHarness
from tests.switch_core.connections.adapters.test_github import _config
+from tests.switch_core.connections.test_app_credentials import _rsa_pem
pytestmark = pytest.mark.no_ambient_tenant
@@ -212,6 +213,40 @@ class TestWhichApp:
assert await _client_in(broker, without_app) == SERVER_CLIENT
assert await _client_in(broker, no_organization) == SERVER_CLIENT
+ async def test_github_goes_through_the_organizations_app(
+ self, rls_harness: RLSHarness, tmp_path: Path
+ ) -> None:
+ organization_id, tenant_id = await _workspace(
+ rls_harness.owner, in_organization=True
+ )
+ assert organization_id is not None
+ async with rls_harness.owner() as session:
+ user = User(name="admin", email=f"{_id('a')}@example.invalid", role="user")
+ session.add(user)
+ await session.flush()
+ await OrganizationAppCredentialStore(TEST_KEYRING).save(
+ session,
+ organization_id=organization_id,
+ service="github",
+ client_id="organization-app-client",
+ settings={"slug": "organization-app"},
+ secrets={"client_secret": "example-secret", "private_key": _rsa_pem()},
+ secret_names=SECRET_NAMES["github_app"],
+ updated_by=user.id,
+ )
+ await session.commit()
+ server = _Server(rls_harness.restricted, tmp_path)
+ await server.start()
+
+ app = await server.lookup(tenant_id, "github")
+
+ assert app is not None and app.github_app is not None
+ assert app.github_app.connections.client_id == "organization-app-client"
+ assert app.github_app.connections.callback == (
+ f"{PUBLIC_URL}/gateway/provider-connections/github/callback"
+ )
+ assert app.adapter is not None and app.adapter.can_issue
+
async def test_an_unusable_organization_app_does_not_fall_back_to_the_servers(
self, rls_harness: RLSHarness, tmp_path: Path
) -> None:
@@ -241,7 +276,8 @@ class TestWhichApp:
server = _Server(rls_harness.restricted, tmp_path)
await server.start()
- for service in ("atlassian", "github"):
+ # Atlassian registers itself with the vendor, and Datadog takes keys.
+ for service in ("atlassian", "datadog"):
assert (
await server.organization_apps.for_workspace(
_Unread(), "no-such", service
diff --git a/core/tests/switch_core/gateway/test_github_connections.py b/core/tests/switch_core/gateway/test_github_connections.py
index 74dce4b04..ab29e16df 100644
--- a/core/tests/switch_core/gateway/test_github_connections.py
+++ b/core/tests/switch_core/gateway/test_github_connections.py
@@ -12,9 +12,10 @@ import pytest
from fastapi import FastAPI
from sqlalchemy import select, update
-from switch_core.connections.adapters.github import GitHubAdapter
+from switch_core.connections.adapters.github import GitHubAdapter, GitHubApp
from switch_core.connections.broker import ServiceBroker
from switch_core.connections.loader import CATALOG
+from switch_core.connections.organization_apps import OrganizationApp
from switch_core.connections.sign_in_flows import SignInFlows
from switch_core.db.models import (
ServiceConnection,
@@ -100,7 +101,9 @@ async def github_app(request, tmp_path, session_factory):
yield client, github, identity, session_factory
-def _process(github, identity, session_factory, workspace_gate: bool) -> FastAPI:
+def _process(
+ github, identity, session_factory, workspace_gate: bool, organization_apps=None
+) -> FastAPI:
"""One switch-core process's gateway: its own app, broker and flow store,
on the shared database."""
@@ -115,8 +118,10 @@ def _process(github, identity, session_factory, workspace_gate: bool) -> FastAPI
session_factory=session_factory,
keyring=TEST_KEYRING,
catalog=CATALOG,
- adapters={"github": GitHubAdapter(github, AsyncMock())},
- organization_apps=None,
+ adapters={}
+ if github is None
+ else {"github": GitHubAdapter(github, AsyncMock())},
+ organization_apps=organization_apps,
disabled={},
store=ServiceConnectionStore(),
token_retention=timedelta(days=30),
@@ -680,6 +685,144 @@ async def test_an_app_replaced_during_the_sign_in_is_refused(github_app):
response = await complete(client, flow_id)
assert response.status_code == 400
assert response.json()["detail"] == (
- "This server's GitHub App changed during your sign-in. Start it again."
+ "The GitHub App changed during your sign-in. Start it again."
)
github.exchange.assert_not_called()
+
+
+def _organization_github(client_id: str) -> GitHubConnections:
+ """An organization's own GitHub App, called back at the server's address."""
+ github = GitHubConnections(
+ client_id=client_id,
+ client_secret="SYNTHETIC-ORGANIZATION",
+ slug="organization-app",
+ origin="https://switch.example.com",
+ )
+ github.exchange = AsyncMock(
+ return_value={
+ "access_token": "SYNTHETIC-ORG-ACCESS",
+ "refresh_token": "SYNTHETIC-ORG-REFRESH",
+ "expires_at": time.time() + 3600,
+ "refresh_expires_at": time.time() + 7200,
+ }
+ )
+ github.revoke = AsyncMock()
+ github.request = AsyncMock(return_value={"id": 321, "login": "org-user"})
+ return github
+
+
+class _Organizations:
+ """Organizations' apps on: the App the workspace's organization saved,
+ or None, and which workspace each lookup was for."""
+
+ def __init__(self, app: OrganizationApp | None) -> None:
+ self.app = app
+ self.tenants: list[str] = []
+
+ async def for_workspace(self, session, tenant_id: str, service: str):
+ self.tenants.append(tenant_id)
+ return self.app if service == "github" else None
+
+
+def _saved(
+ github: GitHubConnections | None, problem: str | None = None
+) -> OrganizationApp:
+ return OrganizationApp(
+ organization_id="org-example",
+ adapter=None if github is None else GitHubAdapter(github, AsyncMock()),
+ github_app=None if github is None else GitHubApp(github, None),
+ problem=problem,
+ changed_at=datetime.now(UTC),
+ )
+
+
+async def _client(app: FastAPI) -> httpx.AsyncClient:
+ return httpx.AsyncClient(
+ transport=httpx.ASGITransport(app=app), base_url="https://switch.example.com"
+ )
+
+
+class TestOrganizationsApp:
+ async def test_a_workspace_signs_in_through_its_organizations_app(
+ self, github_app
+ ) -> None:
+ _, server_github, identity, factory = github_app
+ own = _organization_github("organization-client")
+ organizations = _Organizations(_saved(own))
+ # The server has no App of its own: the organization's is all there is.
+ app = _process(None, identity, factory, False, organizations)
+ app.state.github_connections = None
+
+ async with await _client(app) as client:
+ state = secrets.token_urlsafe(32)
+ started = await client.post(
+ BASE + "/flows",
+ json={"port": 12345, "state": state, "completion_secret": SECRET},
+ )
+ assert started.status_code == 200, started.text
+ left = await client.get(started.json()["url"])
+ assert parse_qs(urlsplit(left.headers["location"]).query)["client_id"] == [
+ "organization-client"
+ ]
+ await relay(client, state)
+ completed = await complete(client, state)
+ assert completed.status_code == 200, completed.text
+ assert (await client.get(BASE + f"/flows/{state}")).json()[
+ "status"
+ ] == "connected"
+
+ own.exchange.assert_awaited_once()
+ server_github.exchange.assert_not_awaited()
+ flow_tenant = organizations.tenants[1]
+ assert set(organizations.tenants) == {flow_tenant}
+ async with factory() as session:
+ row = await session.scalar(
+ select(ServiceConnection).where(ServiceConnection.service == "github")
+ )
+ assert row is not None and row.oauth_client_id == "organization-client"
+
+ async def test_an_unusable_organization_app_is_refused_not_replaced(
+ self, github_app
+ ) -> None:
+ _, server_github, identity, factory = github_app
+ problem = "Your organization's GitHub app can't be used: it is broken."
+ app = _process(
+ server_github,
+ identity,
+ factory,
+ False,
+ _Organizations(_saved(None, problem)),
+ )
+
+ async with await _client(app) as client:
+ refused = await client.post(
+ BASE + "/flows",
+ json={
+ "port": 12345,
+ "state": secrets.token_urlsafe(32),
+ "completion_secret": SECRET,
+ },
+ )
+
+ assert refused.status_code == 503
+ assert refused.json()["detail"] == problem
+
+ async def test_a_sign_in_begun_under_another_app_is_refused_at_authorize(
+ self, github_app
+ ) -> None:
+ _, server_github, identity, factory = github_app
+ organizations = _Organizations(None)
+ app = _process(server_github, identity, factory, False, organizations)
+
+ async with await _client(app) as client:
+ state = secrets.token_urlsafe(32)
+ started = await client.post(
+ BASE + "/flows",
+ json={"port": 12345, "state": state, "completion_secret": SECRET},
+ )
+ assert started.status_code == 200
+ # The organization saves its own App while the browser is on its way.
+ organizations.app = _saved(_organization_github("organization-client"))
+ left = await client.get(started.json()["url"])
+
+ assert left.status_code == 400