diff --git a/.github/workflows/sso-image.yml b/.github/workflows/sso-image.yml index 4cba8d090e..289db4b5c6 100644 --- a/.github/workflows/sso-image.yml +++ b/.github/workflows/sso-image.yml @@ -5,7 +5,7 @@ name: Build SSO image # GitHub Container Registry (ghcr.io//) so the cluster can pull it. # # Usage: Actions tab -> "Build SSO image" -> Run workflow -> pick the upstream -# app tag that matches the ClickStack chart's appVersion (e.g. @hyperdx/app@2.39.1). +# app tag that matches the ClickStack chart's appVersion (e.g. @hyperdx/app@2.42.0). on: workflow_dispatch: @@ -13,7 +13,7 @@ on: upstream_tag: description: 'Upstream HyperDX app tag to build (must match chart appVersion)' required: true - default: '@hyperdx/app@2.39.1' + default: '@hyperdx/app@2.42.0' push: branches: [main] paths: @@ -44,8 +44,8 @@ jobs: - name: Resolve tag & version id: ver run: | - TAG="${{ github.event.inputs.upstream_tag || '@hyperdx/app@2.39.1' }}" - VERSION="${TAG##*@}" # @hyperdx/app@2.39.1 -> 2.39.1 + TAG="${{ github.event.inputs.upstream_tag || '@hyperdx/app@2.42.0' }}" + VERSION="${TAG##*@}" # @hyperdx/app@2.42.0 -> 2.42.0 echo "tag=$TAG" >> "$GITHUB_OUTPUT" echo "version=$VERSION" >> "$GITHUB_OUTPUT" echo "Building $TAG (version $VERSION)" @@ -102,7 +102,7 @@ jobs: # stale cached image after a rebuild of the same upstream version. - name: Create multi-arch manifest run: | - TAG="${{ github.event.inputs.upstream_tag || '@hyperdx/app@2.39.1' }}" + TAG="${{ github.event.inputs.upstream_tag || '@hyperdx/app@2.42.0' }}" IMAGE="ghcr.io/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" SRC="${IMAGE}:${TAG##*@}-sso" docker buildx imagetools create \ diff --git a/sso/README.md b/sso/README.md index 90b0e47773..2f25d02b48 100644 --- a/sso/README.md +++ b/sso/README.md @@ -25,21 +25,23 @@ header is configurable via `PROXY_AUTH_EMAIL_HEADER`; in k8-mgmt we set it to `x-forwarded-email`. Files: -- `overlay/` — new files, mirrored to repo paths (only `proxyAuth.ts`). -- `integrate.mjs` — copies the overlay and applies an idempotent, anchor-based - codemod to `auth.ts`. Fails loudly if upstream changed that file's shape. +- `overlay/` — new files, mirrored to repo paths: `middleware/proxyAuth.ts` + (sign-in), `middleware/proxyAuthRoles.ts` (roles), `models/proxyAuthRole.ts`. +- `integrate.mjs` — copies the overlay and applies idempotent, anchor-based + codemods to `auth.ts`, `api-app.ts` and `mcp/utils/registerTool.ts`. Fails + loudly if upstream changed those files' shape. - `apply.sh` — checks out an upstream tag into a git worktree and runs the codemod. ## Build locally ```bash -sso/apply.sh '@hyperdx/app@2.28.0' build +sso/apply.sh '@hyperdx/app@2.42.0' build docker build -f build/docker/hyperdx/Dockerfile --target prod \ --build-context hyperdx=build/docker/hyperdx \ --build-context api=build/packages/api \ --build-context app=build/packages/app \ - --build-arg CODE_VERSION=2.28.0-sso \ - -t ghcr.io//hyperdx:2.28.0-sso build + --build-arg CODE_VERSION=2.42.0-sso \ + -t ghcr.io//hyperdx:2.42.0-sso build ``` ## Build in CI @@ -76,27 +78,70 @@ empty in Terraform — `oauth2-proxy.tf` then skips the dockerconfigjson secret | `PROXY_AUTH_DEFAULT_TEAM_NAME` | `HyperDX` | (default) | Team created on first login. | | `PROXY_AUTH_GROUPS_HEADER` | `x-forwarded-groups` | (default) | Groups header (oauth2-proxy GitHub: `org`, `org:team`). | | `PROXY_AUTH_ADMIN_GROUPS` | (empty → roles off) | `topia-io:devops,metaversecloud-com:devops` | Comma-separated groups with full access. | +| `PROXY_AUTH_MEMBER_GROUPS` | (empty) | to add: `topia-io:dev,metaversecloud-com:dev` | Groups that may author dashboards, searches, alerts, webhooks. | +| `PROXY_AUTH_ACCESS_KEY_MAX_IDLE_DAYS` | `30` | (default) | Access keys stop working when their owner hasn't signed in via SSO for this long. `0` disables. | ### Roles -HyperDX OSS has no RBAC; the overlay adds two roles, enforced app-wide by +HyperDX OSS has no RBAC; the overlay adds three roles, enforced app-wide by `proxyAuthRoleGuard` (mounted before every router): -- **admin** — any user in `PROXY_AUTH_ADMIN_GROUPS`: full access. -- **viewer** — everyone else: all `GET`s plus read-only `POST`s (search/charts via - `/clickhouse-proxy`, `/v1/prometheus/query*`, `/ai/assistant`, - `/me/onboarding/*`). Every other write returns 403. Viewer ClickHouse queries - are forced to `readonly=2`, so raw SQL through the proxy cannot modify data. +| Role | Who | Can | +|------|-----|-----| +| **admin** | in `PROXY_AUTH_ADMIN_GROUPS` | everything | +| **member** | in `PROXY_AUTH_MEMBER_GROUPS` | viewer + create/edit/delete dashboards, saved searches, alerts, webhooks, pinned filters | +| **viewer** | everyone else | read; run searches/charts (`/clickhouse-proxy`, PromQL, AI assistant); own favorites, onboarding, access key | -The UI is unchanged: viewers still see edit buttons, but saving fails. On an -upstream upgrade, re-check new `POST` routes viewers need and run -`yarn ci:unit src/middleware/__tests__/proxyAuth.test.ts` in the integrated tree. +Sources, connections, team settings, IaC and connection tests are admin-only. +Any write route not classified in `proxyAuthRoles.ts` is admin-only, so routes +added upstream fail closed. Non-admin ClickHouse queries are forced to +`readonly=2`, so raw SQL through the proxy cannot modify data. + +Write decisions are logged (`Proxy auth: write`, `... write denied`, +`... MCP write`, `... role changed`) and counted in +`hyperdx.proxy_auth.write_decisions{role,outcome}`. + +The UI is unchanged: non-admins still see edit buttons, but saving returns 403 +naming the required role. + +### Access keys, API v2 and MCP + +`/api/v2/*` and `/mcp` authenticate by personal access key, not by the proxy, +so group headers are ignored there. The role comes from the key owner's last SSO +sign-in, stored in the `proxyauthroles` collection (refreshed at most hourly +while they use the UI). A key is rejected with 403 if its owner never signed in +through SSO or not within `PROXY_AUTH_ACCESS_KEY_MAX_IDLE_DAYS`. + +MCP serves all tools over one `POST /mcp`, so it is gated per tool: tools +annotated `readOnlyHint` are open to viewers, `*_source` tools need admin, every +other write tool needs member. A denied call returns an MCP tool error. + +**Revoking someone:** remove them from the GitHub team (blocks new SSO logins; +existing oauth2-proxy cookies last until `cookie_expire`), and delete them in +Team Settings to kill their access key immediately. Otherwise the key dies +after the idle window. + +**oauth2-proxy must let bearer requests through** for headless clients (CLI, +MCP, API v2), since they have no GitHub cookie. In `oauth2-proxy.tf`: + +``` +skip_auth_routes = ["^/api/mcp$", "^/api/api/v2/[^.]*$"] +skip_auth_strip_headers = true # default; must stay on +``` + +The `[^.]*` keeps `..` out of the bypass. With header stripping on, a skipped +request cannot carry `X-Forwarded-*`, and those routes only trust access keys. + +On an upstream upgrade, classify any new write routes in `proxyAuthRoles.ts` +and run `yarn ci:unit src/middleware/__tests__/proxyAuthRoles.test.ts` in the +integrated tree. > **Security:** only enable when HyperDX is reachable *exclusively* through > oauth2-proxy, and the proxy strips client-supplied copies of these headers. > Otherwise the identity header can be spoofed. oauth2-proxy strips inbound > `X-Forwarded-*` / `X-Auth-Request-*` before setting its own, so this holds as -> long as nothing else can reach the app port directly. +> long as nothing else can reach the app port directly. Enforce that with a +> NetworkPolicy allowing ingress to the HyperDX pod only from oauth2-proxy. ## Deploy via Terraform (k8-mgmt) @@ -125,7 +170,7 @@ clickstack_sso = { github_client_secret = "" cookie_secret = "" image_repository = "ghcr.io//hyperdx" - image_tag = "2.28.0-sso" + image_tag = "2.42.0-sso" # ghcr_username / ghcr_pull_token omitted → public package # allowed_github_teams defaults to topia-io/metaversecloud-com :devops/:dev } diff --git a/sso/integrate.mjs b/sso/integrate.mjs index 7294964ae6..e815e2dc36 100755 --- a/sso/integrate.mjs +++ b/sso/integrate.mjs @@ -11,9 +11,11 @@ // if the expected anchors are missing (i.e. upstream changed that file's // shape) instead of silently producing a broken build. // 3. Anchor-based codemod of packages/api/src/api-app.ts mounting the -// app-wide role guard (viewer vs admin, from proxy-supplied groups). +// app-wide role guard (viewer/member/admin, from proxy-supplied groups). // 4. Anchor-based codemod of packages/app/src/HDXMultiSeriesTimeChart.tsx // so time-chart x-axis ticks show the date on ranges longer than a day. +// 5. Anchor-based codemod of packages/api/src/mcp/utils/registerTool.ts +// gating each MCP write tool by role (MCP multiplexes tools over one POST). import { cp, readFile, writeFile, stat } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; @@ -94,7 +96,7 @@ if (appSrc.includes('proxyAuthRoleGuard')) { appSrc = appSrc .replace( appImportAnchor, - `${appImportAnchor}\nimport { proxyAuthRoleGuard } from './middleware/proxyAuth';`, + `${appImportAnchor}\nimport { proxyAuthRoleGuard } from './middleware/proxyAuthRoles';`, ) .replace(corsAnchor, `${corsAnchor}\napp.use(proxyAuthRoleGuard);`); @@ -106,7 +108,10 @@ if (appSrc.includes('proxyAuthRoleGuard')) { // time-of-day only ('time'), whatever the range. Over a 30-day range that is // a row of identical "9:00:00 AM" labels with no dates. When the range spans // more than a day, label every tick with its date ('short' = MMM d h:mma). -const chartPath = path.join(target, 'packages/app/src/HDXMultiSeriesTimeChart.tsx'); +const chartPath = path.join( + target, + 'packages/app/src/HDXMultiSeriesTimeChart.tsx', +); if (!(await exists(chartPath))) { throw new Error(`[sso] expected file not found: ${chartPath}`); } @@ -114,7 +119,9 @@ if (!(await exists(chartPath))) { let chartSrc = await readFile(chartPath, 'utf8'); if (chartSrc.includes('xTickSpansDays')) { - console.log('[sso] HDXMultiSeriesTimeChart.tsx already integrated, skipping codemod'); + console.log( + '[sso] HDXMultiSeriesTimeChart.tsx already integrated, skipping codemod', + ); } else { const tickAnchor = ' const xTickFormatter = useCallback(\n' + @@ -145,7 +152,52 @@ if (chartSrc.includes('xTickSpansDays')) { ); await writeFile(chartPath, chartSrc); - console.log('[sso] patched x-axis tick format in HDXMultiSeriesTimeChart.tsx'); + console.log( + '[sso] patched x-axis tick format in HDXMultiSeriesTimeChart.tsx', + ); +} + +// 5) Gate MCP write tools by role. +const toolPath = path.join( + target, + 'packages/api/src/mcp/utils/registerTool.ts', +); +if (!(await exists(toolPath))) { + throw new Error(`[sso] expected file not found: ${toolPath}`); +} + +let toolSrc = await readFile(toolPath, 'utf8'); + +if (toolSrc.includes('guardMcpTool')) { + console.log('[sso] registerTool.ts already integrated, skipping codemod'); +} else { + const toolImportAnchor = "import { withToolTracing } from './tracing';"; + const registerAnchor = + 'server.registerTool(name, config, traced);'; + if ( + !toolSrc.includes(toolImportAnchor) || + !toolSrc.includes(registerAnchor) + ) { + throw new Error( + '[sso] anchors not found in registerTool.ts (layout changed)', + ); + } + toolSrc = toolSrc + .replace( + toolImportAnchor, + `import { guardMcpTool } from '@/middleware/proxyAuthRoles';\n\n${toolImportAnchor}`, + ) + .replace( + registerAnchor, + 'server.registerTool(\n' + + ' name,\n' + + ' config,\n' + + ' guardMcpTool(name, config.annotations, traced),\n' + + ' );', + ); + + await writeFile(toolPath, toolSrc); + console.log('[sso] gated MCP tools in registerTool.ts'); } console.log('[sso] integration complete'); diff --git a/sso/overlay/packages/api/src/middleware/__tests__/proxyAuth.test.ts b/sso/overlay/packages/api/src/middleware/__tests__/proxyAuth.test.ts deleted file mode 100644 index 450eb398d5..0000000000 --- a/sso/overlay/packages/api/src/middleware/__tests__/proxyAuth.test.ts +++ /dev/null @@ -1,93 +0,0 @@ -import express from 'express'; -import request from 'supertest'; - -const ADMIN = 'topia-io, topia-io:devops'; -const VIEWER = 'topia-io, topia-io:dev'; - -function buildApp(adminGroups = 'topia-io:devops,metaversecloud-com:devops') { - let app: express.Express; - jest.isolateModules(() => { - process.env.PROXY_AUTH_ENABLED = 'true'; - process.env.PROXY_AUTH_ADMIN_GROUPS = adminGroups; - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { proxyAuthRoleGuard } = require('../proxyAuth'); - app = express(); - app.use(proxyAuthRoleGuard); - app.use((req, res) => { - res.json({ url: req.url, originalUrl: req.originalUrl }); - }); - }); - return app!; -} - -describe('proxyAuthRoleGuard', () => { - const app = buildApp(); - - it('lets admins write', async () => { - await request(app) - .delete('/dashboards/123') - .set('x-forwarded-groups', ADMIN) - .expect(200); - }); - - it('lets viewers read', async () => { - await request(app) - .get('/dashboards') - .set('x-forwarded-groups', VIEWER) - .expect(200); - }); - - it.each([ - ['post', '/dashboards'], - ['patch', '/dashboards/123'], - ['delete', '/dashboards/123'], - ['post', '/alerts'], - ['put', '/sources/123'], - ['post', '/connections'], - ['post', '/clickhouse-proxy/test'], - ['post', '/mcp'], - ] as const)('denies viewer %s %s', async (method, path) => { - await request(app) - [method](path) - .set('x-forwarded-groups', VIEWER) - .expect(403); - }); - - it('denies writes when the groups header is missing', async () => { - await request(app).post('/dashboards').expect(403); - }); - - it.each([ - '/v1/prometheus/query_range', - '/ai/assistant', - '/me/onboarding/task', - ])('allows viewer read-only POST %s', async path => { - await request(app).post(path).set('x-forwarded-groups', VIEWER).expect(200); - }); - - it('forces readonly=2 on viewer ClickHouse queries', async () => { - const res = await request(app) - .post('/clickhouse-proxy/?query_id=1&readonly=0') - .set('x-forwarded-groups', VIEWER) - .expect(200); - const params = new URL(res.body.originalUrl, 'http://x').searchParams; - expect(params.getAll('readonly')).toEqual(['2']); - expect(params.get('query_id')).toBe('1'); - expect(res.body.url).toContain('readonly=2'); - }); - - it('leaves admin ClickHouse queries untouched', async () => { - const res = await request(app) - .post('/clickhouse-proxy/?query_id=1') - .set('x-forwarded-groups', ADMIN) - .expect(200); - expect(res.body.originalUrl).not.toContain('readonly'); - }); - - it('is a no-op when no admin groups are configured', async () => { - await request(buildApp('')) - .delete('/dashboards/123') - .set('x-forwarded-groups', VIEWER) - .expect(200); - }); -}); diff --git a/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts b/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts new file mode 100644 index 0000000000..3d7f53d4e3 --- /dev/null +++ b/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts @@ -0,0 +1,317 @@ +import express from 'express'; +import request from 'supertest'; + +const ADMIN = 'topia-io, topia-io:devops'; +const MEMBER = 'topia-io, topia-io:dev'; +const VIEWER = 'topia-io'; + +const mockFindUser = jest.fn(); +const mockFindRole = jest.fn(); +const mockUpsertRole = jest.fn(); + +jest.mock('@/models/user', () => ({ + __esModule: true, + default: { findOne: (...args: unknown[]) => mockFindUser(...args) }, +})); +jest.mock('@/models/proxyAuthRole', () => ({ + __esModule: true, + PROXY_AUTH_ROLES: ['viewer', 'member', 'admin'], + default: { + findOne: (...args: unknown[]) => mockFindRole(...args), + findOneAndUpdate: (...args: unknown[]) => mockUpsertRole(...args), + }, +})); + +type Roles = typeof import('../proxyAuthRoles'); + +function load(adminGroups = 'topia-io:devops,metaversecloud-com:devops') { + let mod: Roles; + jest.isolateModules(() => { + process.env.PROXY_AUTH_ENABLED = 'true'; + process.env.PROXY_AUTH_ADMIN_GROUPS = adminGroups; + process.env.PROXY_AUTH_MEMBER_GROUPS = 'topia-io:dev'; + process.env.PROXY_AUTH_EMAIL_HEADER = 'x-forwarded-email'; + // eslint-disable-next-line @typescript-eslint/no-require-imports + mod = require('../proxyAuthRoles'); + }); + return mod!; +} + +function buildApp( + adminGroups?: string, + sessionUser?: { _id: string; email: string }, +) { + const { proxyAuthRoleGuard } = load(adminGroups); + const app = express(); + app.use((req, _res, next) => { + // Stands in for passport.session(). + req.user = sessionUser as Express.User | undefined; + next(); + }); + app.use(proxyAuthRoleGuard); + app.use((req, res) => { + res.json({ + url: req.url, + originalUrl: req.originalUrl, + user: req.user ?? null, + auth: (req as { auth?: unknown }).auth ?? null, + }); + }); + return app; +} + +beforeEach(() => { + mockFindUser.mockReset(); + mockFindRole.mockReset(); + mockUpsertRole.mockReset().mockResolvedValue(null); +}); + +describe('proxyAuthRoleGuard (session)', () => { + const app = buildApp(); + + it('lets admins write anything', async () => { + for (const path of ['/dashboards/1', '/sources/1', '/team/member/1']) { + await request(app) + .delete(path) + .set('x-forwarded-groups', ADMIN) + .expect(200); + } + }); + + it('lets everyone read', async () => { + await request(app) + .get('/sources') + .set('x-forwarded-groups', VIEWER) + .expect(200); + }); + + it.each([ + ['post', '/dashboards'], + ['patch', '/dashboards/1'], + ['delete', '/saved-search/1'], + ['post', '/alerts'], + ['post', '/alerts/1/silenced'], + ['put', '/webhooks/1'], + ['post', '/webhooks/test'], + ['put', '/pinned-filters'], + ] as const)('lets members %s %s', async (method, path) => { + await request(app) + [method](path) + .set('x-forwarded-groups', MEMBER) + .expect(200); + }); + + it.each([ + ['put', '/sources/1'], + ['post', '/connections'], + ['patch', '/team/name'], + ['delete', '/team/member/1'], + ['post', '/clickhouse-proxy/test'], + ['post', '/iac/apply'], + ] as const)('denies members %s %s', async (method, path) => { + const res = await request(app) + [method](path) + .set('x-forwarded-groups', MEMBER) + .expect(403); + expect(res.body.message).toContain('requires admin'); + }); + + it.each([ + ['post', '/dashboards'], + ['delete', '/alerts/1'], + ['put', '/pinned-filters'], + ] as const)('denies viewers %s %s', async (method, path) => { + await request(app) + [method](path) + .set('x-forwarded-groups', VIEWER) + .expect(403); + }); + + it('treats a missing groups header as viewer', async () => { + await request(app).post('/dashboards').expect(403); + }); + + it.each([ + ['post', '/v1/prometheus/query_range'], + ['post', '/ai/assistant'], + ['post', '/me/onboarding/task'], + ['patch', '/me/onboarding/dismiss'], + ['patch', '/me/accessKey'], + ['put', '/favorites'], + ] as const)('lets viewers %s %s', async (method, path) => { + await request(app) + [method](path) + .set('x-forwarded-groups', VIEWER) + .expect(200); + }); + + it.each([VIEWER, MEMBER])( + 'forces readonly=2 on non-admin ClickHouse queries (%s)', + async groups => { + const res = await request(app) + .post('/clickhouse-proxy/?query_id=1&readonly=0') + .set('x-forwarded-groups', groups) + .expect(200); + const params = new URL(res.body.originalUrl, 'http://x').searchParams; + expect(params.getAll('readonly')).toEqual(['2']); + expect(params.get('query_id')).toBe('1'); + expect(res.body.url).toContain('readonly=2'); + }, + ); + + it('leaves admin ClickHouse queries untouched', async () => { + const res = await request(app) + .post('/clickhouse-proxy/?query_id=1') + .set('x-forwarded-groups', ADMIN) + .expect(200); + expect(res.body.originalUrl).not.toContain('readonly'); + }); + + it('lets everyone write when no admin groups are configured', async () => { + await request(buildApp('')) + .delete('/sources/1') + .set('x-forwarded-groups', VIEWER) + .expect(200); + }); +}); + +describe('proxyAuthRoleGuard (session identity)', () => { + const user = { _id: 'u1', email: 'dev@topia.io' }; + + it('records the role of the signed-in user once', async () => { + const app = buildApp(undefined, user); + for (let i = 0; i < 2; i++) { + await request(app) + .get('/dashboards') + .set('x-forwarded-email', 'Dev@topia.io') + .set('x-forwarded-groups', MEMBER) + .expect(200); + } + expect(mockUpsertRole).toHaveBeenCalledTimes(1); + expect(mockUpsertRole.mock.calls[0][0]).toEqual({ user: 'u1' }); + expect(mockUpsertRole.mock.calls[0][1]).toMatchObject({ + role: 'member', + groups: ['topia-io', 'topia-io:dev'], + }); + }); + + it('drops a session that belongs to another account', async () => { + const res = await request(buildApp(undefined, user)) + .get('/dashboards') + .set('x-forwarded-email', 'someone-else@topia.io') + .set('x-forwarded-groups', ADMIN) + .expect(200); + expect(res.body.user).toBeNull(); + expect(mockUpsertRole).not.toHaveBeenCalled(); + }); +}); + +describe('proxyAuthRoleGuard (access key)', () => { + const app = buildApp(); + const keyUser = { _id: 'u2', email: 'bot@topia.io' }; + + function withRole(role: string, lastSeenAt = new Date()) { + mockFindUser.mockResolvedValue(keyUser); + mockFindRole.mockResolvedValue({ role, lastSeenAt }); + } + + it('passes unknown keys through for validateUserAccessKey to reject', async () => { + mockFindUser.mockResolvedValue(null); + await request(app) + .post('/api/v2/dashboards') + .set('authorization', 'Bearer nope') + .expect(200); + expect(mockFindRole).not.toHaveBeenCalled(); + }); + + it('lets viewers query', async () => { + withRole('viewer'); + await request(app) + .post('/api/v2/search') + .set('authorization', 'Bearer k') + .expect(200); + await request(app) + .post('/api/v2/dashboards') + .set('authorization', 'Bearer k') + .expect(403); + }); + + it('lets members write content but not sources', async () => { + withRole('member'); + await request(app) + .put('/api/v2/dashboards/1') + .set('authorization', 'Bearer k') + .expect(200); + await request(app) + .post('/api/v2/sources') + .set('authorization', 'Bearer k') + .expect(403); + }); + + it('ignores group headers on access-key requests', async () => { + withRole('viewer'); + await request(app) + .post('/api/v2/sources') + .set('authorization', 'Bearer k') + .set('x-forwarded-groups', ADMIN) + .expect(403); + }); + + it('rejects keys whose owner never signed in', async () => { + mockFindUser.mockResolvedValue(keyUser); + mockFindRole.mockResolvedValue(null); + const res = await request(app) + .get('/api/v2/dashboards') + .set('authorization', 'Bearer k') + .expect(403); + expect(res.body.message).toContain('inactive'); + }); + + it('rejects keys whose owner has not signed in recently', async () => { + withRole('admin', new Date(Date.now() - 31 * 24 * 60 * 60 * 1000)); + await request(app) + .get('/api/v2/dashboards') + .set('authorization', 'Bearer k') + .expect(403); + }); + + it('passes the role to MCP tool handlers', async () => { + withRole('member'); + const res = await request(app) + .post('/mcp') + .set('authorization', 'Bearer k') + .expect(200); + expect(res.body.auth).toMatchObject({ + clientId: 'u2', + extra: { role: 'member', email: 'bot@topia.io' }, + }); + }); +}); + +describe('guardMcpTool', () => { + const { guardMcpTool } = load(); + const ok = { content: [{ type: 'text' as const, text: 'ok' }] }; + const handler = jest.fn().mockResolvedValue(ok); + + function call(name: string, readOnlyHint: boolean, role: string | undefined) { + const extra = { authInfo: role ? { extra: { role } } : undefined }; + + return guardMcpTool(name, { readOnlyHint }, handler)({}, extra as any); + } + + it.each([ + ['clickstack_search', true, 'viewer', false], + ['clickstack_save_dashboard', false, 'viewer', true], + ['clickstack_save_dashboard', false, undefined, true], + ['clickstack_save_dashboard', false, 'member', false], + ['clickstack_save_alert', false, 'member', false], + ['clickstack_delete_source', false, 'member', true], + ['clickstack_delete_source', false, 'admin', false], + ])( + '%s (readOnly=%s) as %s -> isError=%s', + async (name, ro, role, isError) => { + const res = await call(name, ro, role); + expect(Boolean((res as { isError?: boolean }).isError)).toBe(isError); + }, + ); +}); diff --git a/sso/overlay/packages/api/src/middleware/proxyAuth.ts b/sso/overlay/packages/api/src/middleware/proxyAuth.ts index 21ebf5a025..9fb98c081a 100644 --- a/sso/overlay/packages/api/src/middleware/proxyAuth.ts +++ b/sso/overlay/packages/api/src/middleware/proxyAuth.ts @@ -17,7 +17,7 @@ export const PROXY_AUTH_ENABLED = env.PROXY_AUTH_ENABLED === 'true'; // Header carrying the authenticated user's email. oauth2-proxy default is // X-Auth-Request-Email (Node lower-cases header keys in req.headers). -const EMAIL_HEADER = ( +export const EMAIL_HEADER = ( env.PROXY_AUTH_EMAIL_HEADER || 'x-auth-request-email' ).toLowerCase(); @@ -29,85 +29,15 @@ const NAME_HEADER = ( // HyperDX is single-team; first proxy login bootstraps it. const DEFAULT_TEAM_NAME = env.PROXY_AUTH_DEFAULT_TEAM_NAME || 'HyperDX'; -// Header carrying the user's groups. oauth2-proxy (GitHub provider) emits -// `org` and `org:team` entries in X-Forwarded-Groups. -const GROUPS_HEADER = ( - env.PROXY_AUTH_GROUPS_HEADER || 'x-forwarded-groups' -).toLowerCase(); - -// Groups granted full access (e.g. "topia-io:devops"). Everyone else is a -// read-only viewer. Empty disables roles: every user keeps full access. -const ADMIN_GROUPS = (env.PROXY_AUTH_ADMIN_GROUPS || '') - .split(',') - .map(g => g.trim().toLowerCase()) - .filter(Boolean); - -const SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS']; - -// POST endpoints that only read data (searches, charts, queries) or touch the -// viewer's own UI state, so viewers keep a working UI. -const VIEWER_POST_ALLOWLIST = [ - /^\/clickhouse-proxy\/(?!test(\/|$))/, - /^\/v1\/prometheus\/query/, - /^\/ai\/assistant$/, - /^\/me\/onboarding\//, -]; - export function proxyAuthEnabled() { return PROXY_AUTH_ENABLED; } -function headerValue(req: Request, name: string): string | undefined { +export function headerValue(req: Request, name: string): string | undefined { const v = req.headers[name]; return Array.isArray(v) ? v[0] : v; } -function isAdmin(req: Request) { - const raw = req.headers[GROUPS_HEADER]; - const groups = (Array.isArray(raw) ? raw.join(',') : raw || '') - .split(',') - .map(g => g.trim().toLowerCase()); - return groups.some(g => ADMIN_GROUPS.includes(g)); -} - -// App-level role guard: admins pass through; viewers may only read. Viewer -// ClickHouse queries are forced to readonly=2 so raw SQL sent through the -// proxy cannot modify data either (ClickHouse rejects raising it back). -export function proxyAuthRoleGuard( - req: Request, - res: Response, - next: NextFunction, -) { - if (!PROXY_AUTH_ENABLED || ADMIN_GROUPS.length === 0 || isAdmin(req)) { - return next(); - } - - if (req.path.startsWith('/clickhouse-proxy/')) { - const url = new URL(req.url, 'http://localhost'); - url.searchParams.set('readonly', '2'); - req.url = `${url.pathname}${url.search}`; - req.originalUrl = req.url; - } - - if ( - SAFE_METHODS.includes(req.method) || - (req.method === 'POST' && - VIEWER_POST_ALLOWLIST.some(re => re.test(req.path))) - ) { - return next(); - } - - logger.warn( - { - method: req.method, - path: req.path, - email: headerValue(req, EMAIL_HEADER), - }, - 'Proxy auth: write denied for viewer', - ); - return res.status(403).json({ message: 'Read-only access' }); -} - // Ensure the single HyperDX team exists, tolerating a concurrent first-login // race (createTeam throws if a team already exists). async function getOrCreateTeam() { diff --git a/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts b/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts new file mode 100644 index 0000000000..ace6d4b61e --- /dev/null +++ b/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts @@ -0,0 +1,275 @@ +import type { AuthInfo } from '@modelcontextprotocol/sdk/server/auth/types.js'; +import type { ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js'; +import type { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js'; +import type { NextFunction, Request, Response } from 'express'; +import type { AnyZodObject } from 'zod'; + +import ProxyAuthRole, { + PROXY_AUTH_ROLES, + type ProxyAuthRoleName, +} from '@/models/proxyAuthRole'; +import User from '@/models/user'; +import { getCounter } from '@/utils/instrumentation'; +import logger from '@/utils/logger'; + +import { EMAIL_HEADER, headerValue, PROXY_AUTH_ENABLED } from './proxyAuth'; + +const env = process.env; + +// Header carrying the user's groups. oauth2-proxy (GitHub provider) emits +// `org` and `org:team` entries in X-Forwarded-Groups. +const GROUPS_HEADER = ( + env.PROXY_AUTH_GROUPS_HEADER || 'x-forwarded-groups' +).toLowerCase(); + +function parseGroups(value: string | undefined) { + return (value || '') + .split(',') + .map(g => g.trim().toLowerCase()) + .filter(Boolean); +} + +// Groups granted full access (e.g. "topia-io:devops"). Empty disables roles: +// every user keeps full access. +const ADMIN_GROUPS = parseGroups(env.PROXY_AUTH_ADMIN_GROUPS); + +// Groups that may author shared content (dashboards, saved searches, alerts, +// webhooks) but not manage sources, connections or the team. +const MEMBER_GROUPS = parseGroups(env.PROXY_AUTH_MEMBER_GROUPS); + +// Access keys stop working once their owner hasn't signed in through the +// proxy for this long, so removing someone from the IdP group also revokes +// their key. 0 disables the check. +const ACCESS_KEY_MAX_IDLE_MS = + Number(env.PROXY_AUTH_ACCESS_KEY_MAX_IDLE_DAYS ?? 30) * 24 * 60 * 60 * 1000; + +// Routers authenticated by access key (validateUserAccessKey). Requests to +// them don't go through the proxy session, so the role comes from the key +// owner's last proxy sign-in and request headers are ignored. +const ACCESS_KEY_PATH = /^\/(mcp|api\/v2)(\/|$)/; + +const SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS']; + +// Non-GET routes that only read data or touch the caller's own state. +// /mcp is gated per tool by guardMcpTool. +const VIEWER_WRITES = [ + /^\/clickhouse-proxy\/(?!test(\/|$))/, + /^\/v1\/prometheus\/query/, + /^\/ai\/assistant$/, + /^\/me\/(onboarding\/|accessKey$)/, + /^\/favorites(\/|$)/, + /^\/api\/v2\/(search|charts\/series|dashboards\/validate)\/?$/, + /^\/mcp\/?$/, +]; + +// Team-shared content members may author. Every other write (sources, +// connections, team, iac, connection tests) needs admin, so routes added +// upstream are denied until they are classified here. +const MEMBER_WRITES = [ + /^\/(dashboards|saved-search|alerts|webhooks|pinned-filters)(\/|$)/, + /^\/api\/v2\/(dashboards|saved-searches|alerts|webhooks)(\/|$)/, +]; + +// Refresh the stored role at most this often per user unless it changes. +const RECORD_INTERVAL_MS = 60 * 60 * 1000; +const lastRecorded = new Map(); + +const writeDecisions = getCounter('hyperdx.proxy_auth.write_decisions', { + description: + 'Writes (HTTP and MCP tool calls) checked against proxy-auth roles, ' + + 'labeled by caller role and outcome (allowed/denied).', +}); + +function hasRole(role: ProxyAuthRoleName, needed: ProxyAuthRoleName) { + return PROXY_AUTH_ROLES.indexOf(role) >= PROXY_AUTH_ROLES.indexOf(needed); +} + +function roleForGroups(groups: string[]): ProxyAuthRoleName { + if (ADMIN_GROUPS.length === 0 || groups.some(g => ADMIN_GROUPS.includes(g))) { + return 'admin'; + } + return groups.some(g => MEMBER_GROUPS.includes(g)) ? 'member' : 'viewer'; +} + +function requiredRole(req: Request): ProxyAuthRoleName { + if ( + SAFE_METHODS.includes(req.method) || + VIEWER_WRITES.some(re => re.test(req.path)) + ) { + return 'viewer'; + } + return MEMBER_WRITES.some(re => re.test(req.path)) ? 'member' : 'admin'; +} + +function recordRole( + user: Express.User, + role: ProxyAuthRoleName, + groups: string[], +) { + const userId = user._id.toString(); + const state = `${role}|${groups.join(',')}`; + const prev = lastRecorded.get(userId); + if (prev?.state === state && Date.now() - prev.at < RECORD_INTERVAL_MS) { + return; + } + lastRecorded.set(userId, { state, at: Date.now() }); + + ProxyAuthRole.findOneAndUpdate( + { user: user._id }, + { email: user.email, role, groups, lastSeenAt: new Date() }, + { upsert: true }, + ) + .then(old => { + if (old?.role !== role) { + logger.info( + { email: user.email, from: old?.role, to: role, groups }, + 'Proxy auth: role changed', + ); + } + }) + .catch(err => { + lastRecorded.delete(userId); + logger.error({ err, email: user.email }, 'Proxy auth: role not recorded'); + }); +} + +// App-level role guard. Viewers may only read; members may also author +// shared content; admins pass. Non-admin ClickHouse queries are forced to +// readonly=2 so raw SQL sent through the proxy cannot modify data either +// (ClickHouse rejects raising it back). +export async function proxyAuthRoleGuard( + req: Request, + res: Response, + next: NextFunction, +) { + if (!PROXY_AUTH_ENABLED) { + return next(); + } + + try { + let role: ProxyAuthRoleName; + let email: string | undefined; + + if (ACCESS_KEY_PATH.test(req.path)) { + const key = req.headers.authorization?.split('Bearer ')[1]; + const user = key ? await User.findOne({ accessKey: key }) : null; + if (!key || !user) { + // validateUserAccessKey answers 401. + return next(); + } + const record = await ProxyAuthRole.findOne({ user: user._id }); + if ( + !record || + (ACCESS_KEY_MAX_IDLE_MS > 0 && + Date.now() - record.lastSeenAt.getTime() > ACCESS_KEY_MAX_IDLE_MS) + ) { + logger.warn( + { email: user.email, lastSeenAt: record?.lastSeenAt }, + 'Proxy auth: access key rejected, no recent sign-in', + ); + return res.status(403).json({ + message: + 'Access key inactive: sign in to HyperDX in the browser to reactivate it', + }); + } + role = record.role; + email = user.email; + // The MCP SDK hands req.auth to tool handlers as extra.authInfo. + const auth: AuthInfo = { + token: key, + clientId: user._id.toString(), + scopes: [], + extra: { role, email }, + }; + (req as Request & { auth?: AuthInfo }).auth = auth; + } else { + email = headerValue(req, EMAIL_HEADER)?.trim().toLowerCase(); + const groups = parseGroups( + [req.headers[GROUPS_HEADER] ?? []].flat().join(','), + ); + role = roleForGroups(groups); + if (req.user && req.user.email?.toLowerCase() !== email) { + // The session belongs to a different account than the proxy asserts + // (shared browser, switched IdP user). Drop it so isUserAuthenticated + // re-provisions from the header instead of mixing two identities. + req.user = undefined; + } else if (req.user) { + recordRole(req.user, role, groups); + } + } + + const needed = requiredRole(req); + const path = req.path; + if (!hasRole(role, needed)) { + writeDecisions.add(1, { role, outcome: 'denied' }); + logger.warn( + { method: req.method, path, email, role, needed }, + 'Proxy auth: write denied', + ); + return res.status(403).json({ + message: `Your role (${role}) cannot do this; it requires ${needed}.`, + }); + } + + if (role !== 'admin' && path.startsWith('/clickhouse-proxy/')) { + const url = new URL(req.url, 'http://localhost'); + url.searchParams.set('readonly', '2'); + req.url = `${url.pathname}${url.search}`; + req.originalUrl = req.url; + } + + if (needed !== 'viewer') { + writeDecisions.add(1, { role, outcome: 'allowed' }); + res.on('finish', () => { + logger.info( + { method: req.method, path, email, role, status: res.statusCode }, + 'Proxy auth: write', + ); + }); + } + + return next(); + } catch (err) { + return next(err); + } +} + +// MCP serves every tool over one POST /mcp, so writes are gated per tool: +// tools not marked read-only need member, source tools need admin. Fails +// closed when the guard resolved no role for the request. +export function guardMcpTool( + name: string, + annotations: ToolAnnotations | undefined, + handler: ToolCallback, +): ToolCallback { + const needed: ProxyAuthRoleName = annotations?.readOnlyHint + ? 'viewer' + : name.endsWith('_source') + ? 'admin' + : 'member'; + if (!PROXY_AUTH_ENABLED || needed === 'viewer') { + return handler; + } + + return async (args, extra) => { + const role = + PROXY_AUTH_ROLES.find(r => r === extra.authInfo?.extra?.role) ?? 'viewer'; + const email = extra.authInfo?.extra?.email; + if (!hasRole(role, needed)) { + writeDecisions.add(1, { role, outcome: 'denied' }); + logger.warn({ email, role, tool: name }, 'Proxy auth: MCP tool denied'); + return { + isError: true, + content: [ + { + type: 'text', + text: `Your role (${role}) cannot run ${name}; it requires ${needed}.`, + }, + ], + }; + } + writeDecisions.add(1, { role, outcome: 'allowed' }); + logger.info({ email, role, tool: name }, 'Proxy auth: MCP write'); + return handler(args, extra); + }; +} diff --git a/sso/overlay/packages/api/src/models/proxyAuthRole.ts b/sso/overlay/packages/api/src/models/proxyAuthRole.ts new file mode 100644 index 0000000000..760a6ce5f1 --- /dev/null +++ b/sso/overlay/packages/api/src/models/proxyAuthRole.ts @@ -0,0 +1,43 @@ +import mongoose, { Schema } from 'mongoose'; + +import type { ObjectId } from '.'; + +// Ordered least to most privileged. +export const PROXY_AUTH_ROLES = ['viewer', 'member', 'admin'] as const; +export type ProxyAuthRoleName = (typeof PROXY_AUTH_ROLES)[number]; + +// The role the proxy last asserted for a user. Access-key requests (API v2, +// MCP) carry no proxy headers, so they are authorized against this record. +interface IProxyAuthRole { + _id: ObjectId; + user: ObjectId; + email: string; + role: ProxyAuthRoleName; + groups: string[]; + lastSeenAt: Date; + createdAt: Date; + updatedAt: Date; +} + +const ProxyAuthRoleSchema = new Schema( + { + user: { + type: mongoose.Schema.Types.ObjectId, + required: true, + ref: 'User', + unique: true, + }, + email: { type: String, required: true }, + role: { type: String, required: true, enum: PROXY_AUTH_ROLES }, + groups: { type: [String], default: [] }, + lastSeenAt: { type: Date, required: true }, + }, + { + timestamps: true, + }, +); + +export default mongoose.model( + 'ProxyAuthRole', + ProxyAuthRoleSchema, +);