# Deploy — implementation spec

One `podman compose` file brings up **three** containers: `pds`, `pds-keycloak`,
`web-platform-stub`. The Angular SPA runs on the host via `ng serve`; the Outil Local as a
native `node dist/index.js` process launched by Claude Desktop's config (not `npx`/a published
package — [architecture.md §12.0](../architecture.md)); Claude Desktop is the Windows app.
Rationale: [architecture.md §7/§12](../architecture.md). Shared config matrix:
[`../contracts.md`](../contracts.md) §6.

```
deploy/
  compose.yaml
  gen-keys.sh                 # generates deploy/keys/platform-signing.jwk.json (git-ignored)
  keys/                       # git-ignored
```

No separate `keycloak/bqt-realm.json` — the `demo` → `bqt` realm swap already happened
*in place* at step 3 ([`pds/docker/realm-export.json`](../pds/docker/realm-export.json)), so
`compose.yaml` mounts that file directly instead of keeping a second copy that could drift.

---

## 1. Scope & build order

Global **step 8** — after the PDS image builds ([pds §3](../pds/spec.md), step 3) and the
isolated filter is done (step 6), and the stub exists (step 1). Its job is only wiring:
addresses, the `bqt` realm, and key provisioning. **✅ done 2026-09-26** — `compose.yaml`,
`pds/Dockerfile` (new; the platform stub's `Dockerfile` already existed from step 1) and
`pds/.dockerignore` written; `gen-keys.sh` had already been run (step 1) so
`deploy/keys/platform-signing.jwk.json` pre-existed. **Live-tested 2026-10-09** on the WSL2 box
(step 9's first real `compose up`) — two healthcheck bugs found and fixed then, see §2. *(Corrected
2026-11-30: this paragraph previously still said "not yet live-tested".)*

---

## 2. `compose.yaml` services

| Service | Image / build | Ports (host:container) | Key env | Volumes | Health / deps |
|---|---|---|---|---|---|
| `pds-keycloak` | `quay.io/keycloak/keycloak:26.0`, cmd `start-dev --import-realm` | `8080:8080` | `KEYCLOAK_ADMIN=admin`, `KEYCLOAK_ADMIN_PASSWORD=admin`, `KC_DB=dev-mem`, `KC_HEALTH_ENABLED=true`, `KC_HOSTNAME=pds-keycloak`, `KC_HOSTNAME_PORT=8080`, `KC_HOSTNAME_STRICT_HTTPS=false` | `../pds/docker/realm-export.json:/opt/keycloak/data/import/bqt-realm.json:ro` (reused as-is, §4) | healthcheck `bash -c 'exec 3<>/dev/tcp/127.0.0.1/9000'` (TCP-connect probe against the management port, not an HTTP check — see "Attention points" below), `interval: 10s`, `retries: 12`, `start_period: 60s` |
| `web-platform-stub` | build `../platform/stub` | `3000:3000` | `PLATFORM_ISSUER=https://veritia.eu`, `PDS_MCP_URI=http://localhost:8081/mcp`, `PDS_DISCOVERY_URI=http://pds:8081/mcp`, `SIGNING_KEY_PATH=/keys/platform-signing.jwk.json`, `PDS_ASSERT_URI=http://pds:8081/account-link/assert`, `PDS_ASSERTION_JWKS_URI=http://pds:8081/pds-jwks.json`, `PDS_ASSERT_DEV_SECRET=poc-dev-secret` | `./keys:/keys:ro` | — |
| `pds` | build `../pds` | `8081:8081` | `KC_ISSUER_URI=http://pds-keycloak:8080/realms/bqt`, `MCP_CANONICAL_URI=http://localhost:8081/mcp`, `PLATFORM_JWKS_URI=http://web-platform-stub:3000/jwks.json`, `PLATFORM_INTROSPECT_BASE=http://web-platform-stub:3000/introspect`, `PLATFORM_EXPECTED_ISS=https://veritia.eu`, `PDS_PROVIDER_ID=9b7c1e40-2f6a-4c11-9e2a-000000000bqt`, `PDS_ASSERT_DEV_SECRET=poc-dev-secret` | `./data:/app/data` (H2 file mode + `pds-assertion.jwk.json`, survives restart) | `depends_on: pds-keycloak (service_healthy)` |

Notes
- `MCP_CANONICAL_URI` stays `http://localhost:8081/mcp` **inside** the `pds` container — it is
  the token `aud`/`htu` identifier the host-side Outil Local signs, not a routable address.
- The stub reaches the PDS for discovery by **service name** (`pds:8081`); the PDS reaches the
  stub by **service name** (`web-platform-stub:3000`). Only the host (SPA, Outil Local,
  browser) uses `localhost`.
- **Keycloak `issuer-uri` mismatch, resolved.** The PDS's Spring resource server validates the
  `iss` claim string-for-string against `KC_ISSUER_URI` (`http://pds-keycloak:8080/realms/bqt`,
  the *container-network* view — see `SecurityConfig.jwtDecoder()`, [pds §3](../pds/spec.md)).
  Without pinning, Keycloak's `start-dev` derives its own issuer from whatever `Host` header the
  request arrived with, so a demo-step-1 `curl` run from the **host** at `localhost:8080` would
  get back a token with `iss=http://localhost:8080/realms/bqt` — a mismatch, 401. Fixed by
  setting `KC_HOSTNAME=pds-keycloak` (+ `KC_HOSTNAME_STRICT_HTTPS=false`, since this is plain
  `http`, and `KC_HOSTNAME_PORT=8080` pinned explicitly — Keycloak's hostname-v2 provider
  otherwise derives the default port from the scheme (80/443) when none is given, which would
  silently drop `:8080` from the issuer): Keycloak then always mints
  `iss=http://pds-keycloak:8080/realms/bqt` regardless of which host:port a client used to
  reach it, so the host-side curl in `demo/script.md` works with no `podman compose exec`
  indirection. (Previously left as "pin this in the spike" — the spike notes never actually
  addressed it; resolved here instead.)
- **Healthcheck, resolved — two real bugs, found live on 2026-10-09 (step 9's first real
  `podman compose up`, not reasoned about in advance).** `pds/TESTING.md` §7 already documented
  that the inherited `docker/docker-compose.yml`'s `/health/ready` healthcheck doesn't reliably
  report `healthy` without `KC_HEALTH_ENABLED=true` — added here since `pds`'s
  `depends_on: service_healthy` is load-bearing (it wasn't, at step 3). That alone turned out to
  be **not enough** once actually run:
  1. Keycloak 26 serves health/metrics on a separate **management interface, port 9000 by
     default** (visible in its own boot log: `"Management interface listening on
     http://0.0.0.0:9000"`) — not on the main `:8080` HTTP port the original healthcheck curled.
  2. Independently, `quay.io/keycloak/keycloak:26.0` ships **no `curl`, no `wget`, not even
     `which`** — confirmed live via `podman exec ... which curl` → "executable file not found".
     An HTTP-based healthcheck was never going to work on this image at all, regardless of port.
  A hand-built raw HTTP/1.1 request against `:9000` via `bash`'s `/dev/tcp` (`kc.sh` itself needs
  `bash`, so it's always present) got a `400 Bad Request` from Quarkus's HTTP parser for reasons
  not visible from outside the container (tried both with and without a `Host` header matching
  `KC_HOSTNAME` — same result both times). Rather than keep guessing at raw request formatting,
  the healthcheck now just confirms the management port **accepts a TCP connection** at all
  (`bash -c 'exec 3<>/dev/tcp/127.0.0.1/9000'`) — weaker than a true `/health/ready` probe, but
  sufficient to gate `pds`'s startup, and live-confirmed (`podman ps -a` → `(healthy)`) on the
  real machine. If `podman compose ps`/`podman ps -a` still never shows `healthy`,
  `podman compose logs pds-keycloak` for `"Imported realm bqt"` remains the fallback signal
  (pds/TESTING.md §7, and a parallel row in its Troubleshooting table now covers this exact
  symptom too).

---

## 3. Address matrix (concrete)

| Meaning | Host value (SPA / Outil Local / browser) | Compose value (container→container) |
|---|---|---|
| Stub base | `http://localhost:3000` | `http://web-platform-stub:3000` |
| PDS MCP | `http://localhost:8081/mcp` | `http://pds:8081/mcp` |
| PDS REST `/accounts/me` | `http://localhost:8081/accounts/me` | `http://pds:8081/accounts/me` |
| PDS assertion JWKS (stub→PDS) | `http://localhost:8081/pds-jwks.json` | `http://pds:8081/pds-jwks.json` |
| PDS account-link assert (stub→PDS) | `http://localhost:8081/account-link/assert` | `http://pds:8081/account-link/assert` |
| Platform JWKS (PDS→stub) | — | `http://web-platform-stub:3000/jwks.json` |
| Introspection base (PDS→stub) | — | `http://web-platform-stub:3000/introspect` |
| Discovery (stub→PDS) | — | `http://pds:8081/mcp` |
| Keycloak | `http://localhost:8080` | `http://pds-keycloak:8080` |
| SPA | `http://localhost:4200` | n/a (host only) |

Token `aud` = `http://localhost:8081/mcp` on **every** side (identifier, [contracts §6](../contracts.md)).

---

## 4. The `bqt` realm import

The `demo` → `bqt` realm swap already happened **in place** at step 3, not as a separate
deploy-side file: [`pds/docker/realm-export.json`](../pds/docker/realm-export.json) already
*is* the `bqt` realm, and `KeycloakRoleConverter.KEYCLOAK_SYSTEM_ROLES` already lists
`default-roles-bqt`. `compose.yaml` mounts that file directly (target path
`/opt/keycloak/data/import/bqt-realm.json`, matching [`pds/docker/docker-compose.yml`](../pds/docker/docker-compose.yml)'s
own mount) instead of keeping a second copy in `deploy/` that could drift out of sync.

Actual content, for reference:

| Element | Value |
|---|---|
| `realm` | `bqt`, `enabled: true`, `accessTokenLifespan: 900` |
| client | `clientId: bqt-rest`, `secret: bqt-secret`, `directAccessGrantsEnabled: true`, `protocol: openid-connect`, `redirectUris: ["http://localhost:8081/*"]` |
| realm roles | `CUSTOMER` only (the base demo's `USER`/`ADMIN` roles were dropped, not carried forward) |
| users | `pieter` / `sofie` / `noah`, all password `demo` (`temporary: false`), realm role `CUSTOMER` — matches [contracts.md §4](../contracts.md)'s seed data |

The **platform** side has no Keycloak (fake itsme is a SPA screen — [architecture.md §5](../architecture.md)).

---

## 5. `gen-keys.sh`

Run **once** before the first `compose up`. Generates the platform signing key so the JWKS
(and thus the PDS's cached verification key) is stable across restarts. **Already run** (step
1, 2026-09-09) — `deploy/keys/platform-signing.jwk.json` exists and is idempotent-skipped by
the script (delete `keys/` yourself to rotate).

```
# node one-liner using jose: generate RSA-2048, kid "platform-poc-1", write private JWK
node -e '(async()=>{const {generateKeyPair,exportJWK}=require("jose");
  const {privateKey}=await generateKeyPair("RS256",{modulusLength:2048});
  const jwk=await exportJWK(privateKey); jwk.kid="platform-poc-1"; jwk.alg="RS256"; jwk.use="sig";
  require("fs").mkdirSync("keys",{recursive:true});
  require("fs").writeFileSync("keys/platform-signing.jwk.json",JSON.stringify(jwk,null,2));
  console.log("wrote keys/platform-signing.jwk.json");})()'
```
`deploy/keys/` and `deploy/data/` are already covered by `.gitignore` (`poc-code/**/data/`;
add `poc-code/deploy/keys/`).

---

## 6. Startup / teardown

```
cd poc-code/deploy
./gen-keys.sh                       # once
podman compose up -d --build        # --build: avoids reusing a stale cached image after a
                                     # source change (jar/dist/); pds-keycloak → (healthy) → pds ; stub in parallel
podman compose ps                   # all healthy?
( cd ../platform/spa && ng serve )  # host, :4200
# configure Claude Desktop (outil-local §4.5), then run the demo

podman compose down -v && rm -rf data keys/*   # full wipe at project close
```

---

## 7. Scenarios

| # | Case | Expected |
|---|---|---|
| D-1 | cold `compose up` | stub up immediately; `pds` waits for Keycloak `service_healthy`; no crash-loop |
| D-2 | `gen-keys.sh` not run | stub fails fast with "signing key not found" (don't auto-generate — that would drift the JWKS) |
| S13 | restart `web-platform-stub` mid-demo | in-memory state gone → prior `jti` introspects `404` → PDS fail-closes (`403`). Recover by re-running SPA registration + mandate |
| D-3 | restart `pds` | H2 file volume intact → `customer`/`account`/`account_link` still seeded; `mcp_call_audit` retained |
| D-4 | `iss` mismatch on demo step 1 | resolved: `KC_HOSTNAME=pds-keycloak` (§2) pins the issued token's `iss` regardless of how the curl reaches Keycloak — host `localhost:8080` or in-network `pds-keycloak:8080` both mint the same `iss`, so no `podman compose exec` indirection is needed |

---

## 8. Environment prerequisites

Everything except Claude Desktop runs inside WSL2 (Ubuntu). Full table:
[architecture.md §12.0](../architecture.md). Essentials: JDK 21 (Temurin), Maven 3.9,
Podman ≥ 4.4 with `podman-compose`, Node 20 LTS, repo on the Linux filesystem (never
`/mnt/c/...`), WSL2 systemd enabled for rootless Podman, `.wslconfig` memory ≥ 8 GB.
