Skip to content

Add OIDC identity and OPA entitlement; remove the shared-password stopgaps - #6

Merged
emphilip merged 6 commits into
upgrade-admin-ui-next-16from
add-identity-and-opa
Nov 23, 2026
Merged

emphilip merged 6 commits into
upgrade-admin-ui-next-16from
add-identity-and-opa

Conversation

@emphilip

Copy link
Copy Markdown
Collaborator

Stacked on #5 (which is stacked on #4). The base retargets to main as those merge.

What and why

openCG had no real identity: a shared admin-UI password, a self-issued MCP OAuth server with a shared password, and a stub identity the pipeline trusted from the request body. Entitlement was a hard-coded role map. This replaces all of it with standard OIDC and OPA policy, so an enterprise pilot can start.

OpenSpec change

openspec/changes/add-identity-and-opa/, approved by the owner on 2026-11-12. The owner decided:

  • Default mode none.
  • API keys only when OPENCG__ENVIRONMENT=dev.
  • Pre-registered MCP clients for Entra ID.
  • Keycloak everywhere, bundled in the same workload.
  • Classifications public / internal / confidential:<domain>.

All tasks are done except 11.3 (CI on this PR) and 13.3 (a manual Claude Code OAuth sign-in against the bundled Keycloak, for the owner).

Breaking:

  • OPENCG__ADMIN_UI__PASSWORD and OPENCG__MCP__OAUTH_PASSWORD are removed, and services refuse to start if either is set.
  • OPENCG__MCP__HTTP_TOKEN now works only in dev, and an unset OPENCG__ENVIRONMENT means production.
  • none mode refuses a non-loopback bind address.

See CHANGELOG and docs/OPERATIONS.md "Migrating from the stopgaps".

Highlights:

  • Identity: generic OIDC in every service, with one claim mapping. Python uses PyJWT; the MCP server uses jose; the admin UI uses openid-client with sessions in Valkey.
  • Policy: OPA 1.21.4 decides every retrieval candidate in one batched call before the token budget, and every API action. A route without an action fails a unit test. It fails closed.
  • Audit: a new append-only access_audit_log. Retrieval audit rows record auth_method and each candidate's policy decision and revision.
  • Bundled Keycloak 26.8.1: it runs in the same compose project with its own Postgres database, and starts only in oidc mode with the bundled issuer.
  • Ingestion: runs take a classification and record requested_by.

Checklist

  • OpenSpec change exists, passes openspec validate add-identity-and-opa --strict, and was agreed before implementation
  • tasks.md updated
  • Unit tests added; make ci-static passes:
    • Python: 66 shared, 118 pipeline, 95 ingestion
    • TypeScript: 63 MCP server, 154 admin UI
    • OPA: 26/26
  • make smoke passes in oidc mode (36 s) and in none mode (11 s); make smoke-obs passes (oidc)
  • make e2e passes in oidc mode (23 passed, 1 skipped) and in none mode (19 passed, 5 skipped)
  • New admin UI components have stories (UserBadge, PermissionDenied)
  • Docs: README, OPERATIONS (auth, IdP and Entra ID, API keys, OPA, access audit, migration), SECURITY, CONTRIBUTING, CHANGELOG, agent guides
  • No secrets: the dev-only Keycloak values in .env.example are public by design and marked as such; gitleaks is clean
  • Archived after merge

Notes for reviewers:

  • The admin UI uses Next 16's experimental.authInterrupts for the 403 page. A non-experimental fallback is possible if preferred.
  • confidential:<domain> is readable only for domains listed in infra/opa/data/opencg.json (currently legal). Other domains are denied as unknown_classification, which fails safe.
  • In staging/production, Keycloak runs plain start (it builds at boot). A pre-built optimized image is a follow-up.

🤖 Generated with Claude Code

Edward and others added 3 commits November 12, 2026 12:55
Owner-approved 2026-11-12: default mode none, API keys dev-only, Keycloak
bundled in the same workload, pre-registered MCP clients for Entra ID,
public/internal/confidential:<domain> classifications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- OPENCG__AUTH__MODE (none | oidc) in every service; OIDC access tokens
  validated against one issuer's JWKS (RS256/ES256, iss/aud/exp/nbf, 60 s
  skew); identity only from the token in oidc mode.
- OPA (1.21.4) decides every retrieval candidate (one batched call, before
  the token budget) and every API action, in both modes; fails closed with
  503 policy_unavailable. Policies, data and tests in infra/opa/.
- Append-only opencg.access_audit_log (denials and allowed writes, in the
  write's transaction); auth_method and per-candidate policy decisions on
  retrieval audit rows; real actor on graph audit rows.
- MCP server is an OAuth protected resource; the shared-password OAuth
  stopgap is removed. Admin UI signs in with code + PKCE and Valkey
  sessions; the password gate is removed. Removed settings refuse startup.
- Bundled Keycloak (26.8.1) in the same compose project, Postgres-backed,
  dev users only in dev/test. API keys only when OPENCG__ENVIRONMENT=dev.
- Ingestion classification (CLI and API) and requested_by.
- Smoke and e2e run in oidc mode by default (OPENCG_TEST_AUTH_MODE), and
  in CI's stack and observability jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The root .gitignore's data/ rule excluded infra/opa/data/opencg.json, so
CI's opa check failed with 'stat data: no such file or directory'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrabS-EM PrabS-EM left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, with fixes requested before merge:

  1. The dev-only secrets are accepted in production. The Keycloak entrypoint only checks that secrets are set. If you copy .env.example and set OPENCG__ENVIRONMENT=production, Keycloak imports the publicly known dev-only-* client secret and admin password. Please refuse to start on any dev-only-* value outside dev/test, the same way the removed stopgap variables are refused.
  2. The monitoring overlay is exposed in OIDC mode. none mode now requires a loopback bind, so OIDC deployments will set OPENCG__BIND_ADDRESS to a non-loopback address. That also publishes Prometheus and Tempo, which have no auth, and Tempo's traces include principal and tenant names. Grafana is also published, on its default password. Please either keep the overlay ports on loopback whatever the bind address, or require auth for them.
  3. The readiness check misses a missing column. /readyz checks for access_audit_log, but not for the new audit_log.auth_method column or #4's cost columns. If those scripts weren't applied, the service still reports ready and /retrieve returns 500s.
  4. The audit routes ignore the token's tenant. With TENANT_CLAIM now supported, /audit/recent still filters on cfg.tenant, and /audit/{id} isn't tenant-scoped. A token missing the tenant claim also silently falls back to the default tenant. Please scope these routes by the caller's tenant and reject a token that's missing a configured tenant claim.

Minor, fine as follow-ups:

  • entitle.run indexes result.decisions[...] directly. A candidate missing from OPA's answer causes a 500 instead of a deny.
  • The fallback access-audit write after the handler finishes can fail after the client already has its 200.
  • Please confirm stack and observability pass once this is retargeted to main. They were skipped on the stacked base.

Edward and others added 3 commits November 23, 2026 10:04
- Refuse any OPENCG__*/KC_* variable still holding a dev-only-* value from
  .env.example outside OPENCG__ENVIRONMENT=dev/test, in every service and
  the Keycloak entrypoint (names only, never values).
- A configured tenant claim is now required (401 token_invalid without
  it); /audit/recent and /audit/{id} are scoped to the caller's tenant.
- /readyz (pipeline and ingestion) checks the tables and columns from
  060_audit_cost.sql and 070_access_audit.sql and names the script.
- entitle.run fails closed (PolicyUnavailable) on a missing decision.
- Guard registered with scope="function": the fallback access-audit
  write finishes before the response, so a failed write is a 500.

Spec deltas, tasks (section 14), OPERATIONS, .env.example and CHANGELOG
updated. The overlay-port fix is on add-observability-stack, merged in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… defaults

Must-fix findings from a full review of the PR's code:
- GET /entities and /entities/{id} now apply opencg.entitlement (shared
  with /search/vector): readers no longer see confidential rows, and a
  denied entity is a 404.
- OPA runs with --authorization=basic (policies/system_authz.rego allows
  only decision queries and /health), and its host port is always
  loopback: its API no longer accepts policy or data writes.
- Compose defaults OPENCG__ENVIRONMENT to production, as the services do.
- The admin UI refuses cross-origin writes to /api/** in both modes.
- Admin UI refresh keeps a session another instance already rotated,
  ends it only on invalid_grant, and survives brief IdP outages.

Also: issuer matched exactly as configured (trailing slash), unused auth
glue removed, entitle skips OPA for an empty candidate list. Spec deltas,
tasks (section 15, with three follow-ups), OPERATIONS and CHANGELOG updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@emphilip
emphilip merged commit 5591d7c into upgrade-admin-ui-next-16 Nov 23, 2026
4 checks passed
emphilip added a commit that referenced this pull request Nov 23, 2026
* Propose the Next 16 upgrade for the admin UI (upgrade-admin-ui-next-16)

Owner-approved: Next 16.4.5 as a security-release exception, a nightly
hydration stress pass in CI, Tremor kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Upgrade the admin UI to Next.js 16.4.5 and React 19 (fixes #2)

- next / eslint-config-next 16.4.5 (security release, owner-approved
  exception to the two-week rule), react / react-dom 19.3.1, types 19.3.0.
- Native flat ESLint config; four commented inline disables for the new
  react-hooks/set-state-in-effect rule; Tremor kept with a peer allowance.
- middleware.ts -> proxy.ts (Node runtime); tsconfig.json and next-env.d.ts
  as rewritten by `next build`.
- New `make hydration-stress` check (services/admin-ui/scripts) and a
  nightly 4 x 10 pass in the CI stack job.
- Issue #2: 0 hydration errors in 1,680 stressed loads on Next 16 vs 34 in
  560 on 15.5.29; the e2e reload workaround is removed (3 green e2e runs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Propose identity (OIDC) and OPA entitlement (add-identity-and-opa)

Owner-approved 2026-11-12: default mode none, API keys dev-only, Keycloak
bundled in the same workload, pre-registered MCP clients for Entra ID,
public/internal/confidential:<domain> classifications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drop the fixed hydration error from README known issues (#2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Add OIDC identity and OPA entitlement (add-identity-and-opa)

- OPENCG__AUTH__MODE (none | oidc) in every service; OIDC access tokens
  validated against one issuer's JWKS (RS256/ES256, iss/aud/exp/nbf, 60 s
  skew); identity only from the token in oidc mode.
- OPA (1.21.4) decides every retrieval candidate (one batched call, before
  the token budget) and every API action, in both modes; fails closed with
  503 policy_unavailable. Policies, data and tests in infra/opa/.
- Append-only opencg.access_audit_log (denials and allowed writes, in the
  write's transaction); auth_method and per-candidate policy decisions on
  retrieval audit rows; real actor on graph audit rows.
- MCP server is an OAuth protected resource; the shared-password OAuth
  stopgap is removed. Admin UI signs in with code + PKCE and Valkey
  sessions; the password gate is removed. Removed settings refuse startup.
- Bundled Keycloak (26.8.1) in the same compose project, Postgres-backed,
  dev users only in dev/test. API keys only when OPENCG__ENVIRONMENT=dev.
- Ingestion classification (CLI and API) and requested_by.
- Smoke and e2e run in oidc mode by default (OPENCG_TEST_AUTH_MODE), and
  in CI's stack and observability jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Track infra/opa/data (was caught by the data/ ignore rule)

The root .gitignore's data/ rule excluded infra/opa/data/opencg.json, so
CI's opa check failed with 'stat data: no such file or directory'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address PR #6 review: dev-only secrets, tenant scoping, schema readiness

- Refuse any OPENCG__*/KC_* variable still holding a dev-only-* value from
  .env.example outside OPENCG__ENVIRONMENT=dev/test, in every service and
  the Keycloak entrypoint (names only, never values).
- A configured tenant claim is now required (401 token_invalid without
  it); /audit/recent and /audit/{id} are scoped to the caller's tenant.
- /readyz (pipeline and ingestion) checks the tables and columns from
  060_audit_cost.sql and 070_access_audit.sql and names the script.
- entitle.run fails closed (PolicyUnavailable) on a missing decision.
- Guard registered with scope="function": the fallback access-audit
  write finishes before the response, so a failed write is a 500.

Spec deltas, tasks (section 14), OPERATIONS, .env.example and CHANGELOG
updated. The overlay-port fix is on add-observability-stack, merged in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address Ponytail review of #6: entity entitlement, OPA lockdown, safe defaults

Must-fix findings from a full review of the PR's code:
- GET /entities and /entities/{id} now apply opencg.entitlement (shared
  with /search/vector): readers no longer see confidential rows, and a
  denied entity is a 404.
- OPA runs with --authorization=basic (policies/system_authz.rego allows
  only decision queries and /health), and its host port is always
  loopback: its API no longer accepts policy or data writes.
- Compose defaults OPENCG__ENVIRONMENT to production, as the services do.
- The admin UI refuses cross-origin writes to /api/** in both modes.
- Admin UI refresh keeps a session another instance already rotated,
  ends it only on invalid_grant, and survives brief IdP outages.

Also: issuer matched exactly as configured (trailing slash), unused auth
glue removed, entitle skips OPA for an empty candidate list. Spec deltas,
tasks (section 15, with three follow-ups), OPERATIONS and CHANGELOG updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Make the nightly hydration stress run work in oidc mode

With add-identity-and-opa merged into this branch, CI's stack job runs
every service in oidc mode, so the nightly stress run's browsers landed
on the Keycloak sign-in page and failed every night with a misleading
'no seed data' message. The script now reuses the signed-in alice
session that make e2e saves (or STORAGE_STATE), defaults BASE_URL to
localhost like e2e so that session's cookie applies, and names the
sign-in redirect when it has no session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Edward <edward@mac.home>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants