test(ci): add disposable real ZITADEL browser OIDC acceptance
This commit is contained in:
1 parent
f423c69654
commit
cbcd60e76e
32 files changed
+875
-22
No files matched your search
@@ -13,3 +13,4 @@
|
||||
| [backend_cleanup.sh](backend_cleanup.sh) | Always-run cleanup of marked CI-owned Compose project/volumes, with leftover verification | P05-CI-03 |
|
||||
| [security_advisory_gate.py](security_advisory_gate.py) | Strict CI registry JSON gate: rejects incomplete reports and any Moderate/High/Critical finding | P05-SEC-01 |
|
||||
| [transitive_override_contract.py](transitive_override_contract.py) | Verify exact seven parent-scoped patched PNPM overrides and no known vulnerable resolved lock entries | P05-SEC-02 |
|
||||
| [oidc_e2e/](oidc_e2e/) | Isolated real OIDC client/user/browser provisioning/negative checks, CI-only Compose owner PAT and resource cleanup | P05-OIDC-01 |
|
||||
@@ -22,3 +22,6 @@ Add new CI checks and update `MINIFEST.md`, `project/`, `docs/`, and `notes/` in
|
||||
|
||||
|
||||
**P05-SEC-02-A01 final acceptance:** Existing corrected `transitive_override_contract.py` successfully executed in **Gitea Run #89** (exact seven parent selector and patched lock markers). Frozen PNPM install, Next Login standalone build, and Vitest 84/84 files/1219/1219 cases PASS. Separate production `security_advisory_gate.py` reports **0 Moderate/High/Critical**, no suppressed findings. Two Account jobs intentionally skipped using `login_only=true`. No local test run; no real authenticated OIDC session/production deployment.
|
||||
|
||||
|
||||
**P05-OIDC-01** is the new CI-only directory `tests/ci/oidc_e2e/`: exact files and first-party instructions in `README.md` and `MINIFEST.md` inside that folder. It is triggered **only** via new manual `.gitea/workflows/p05-oidc-acceptance.yml`. `run.sh` protects numeric run ownership, starts no-host-port official ZITADEL Compose, provisions a real disposable client/human via official IAM_OWNER PAT and runs pinned Chromium with the original Login V2 and unmodified Account Portal. It expects real PKCE/state/nonce/callback/session positive and negative results. `cleanup.sh` runs even on failure and must prove removal of **only** owned test containers, volumes, database/bootstrap credentials. **CI execution pending, not local tests or production proof.**
|
||||
@@ -0,0 +1,14 @@
|
||||
# tests/ci/oidc_e2e/ — exact file manifest
|
||||
|
||||
| File | Responsibility |
|
||||
| --- | --- |
|
||||
| [README.md](README.md) | CI-only architecture, prerequisites, safety and flow boundaries |
|
||||
| [MINIFEST.md](MINIFEST.md) | Exact registered files in this new first-party directory |
|
||||
| [compose.owner.override.yaml](compose.owner.override.yaml) | Disposable first-instance IAM_OWNER PAT bootstrap only; no developer/prod change |
|
||||
| [run.sh](run.sh) | Run-ID Docker ownership preflight, portless stack startup, secret volume/provisioning, private browser launch |
|
||||
| [provision.py](provision.py) | Official ZITADEL project/OIDC client/human creation via ephemeral owner PAT; no secret output |
|
||||
| [browser.sh](browser.sh) | Pinned Playwright image CI-only npm installs, isolated Account app dependencies and browser harness |
|
||||
| [browser.cjs](browser.cjs) | Real Chromium OIDC PKCE/state/nonce/username/password/callback/session and negative contracts |
|
||||
| [cleanup.sh](cleanup.sh) | Always-run owned-only browser/forwarder/secret/Compose teardown and leftover checks |
|
||||
|
||||
Every child file is tracked in Git. Never add real credentials, screenshots, session recordings or user data.
|
||||
@@ -0,0 +1,15 @@
|
||||
# P05-OIDC-01 — disposable Gitea-only real browser OIDC verification
|
||||
|
||||
**Not for local development, production hosts, or real identities.** The new `.gitea/workflows/p05-oidc-acceptance.yml` manual workflow is the only approved entrypoint, and it runs on a disposable Gitea Docker runner.
|
||||
|
||||
The environment is a temporary `zitplane-p05-{GITHUB_RUN_ID}` Compose stack containing the **unchanged** P02 PostgreSQL/ZITADEL API/official prebuilt Login V2/Traefik. `compose.owner.override.yaml` adds **only** a first-instance ephemeral `IAM_OWNER` machine PAT for official API provisioning, with a short expiration; `compose.portless.override.yaml` clears inherited host ports. A tiny run-owned TCP loopback-forwarder exists only in the proxy Docker network namespace to preserve the official P02 `localhost:8080` issuer. No host port is published.
|
||||
|
||||
`run.sh` strictly refuses an existing fixed network, project, volume, credentials file or marker. `provision.py` uses the official API with a read-only temporary owner PAT to create an isolated project, OIDC Web authorization-code client and a verified disposable human with a strong random password; the app/user secrets are written with mode `0600` into **a separate disposable volume**. Admin PAT never reaches the browser runner. `browser.sh` installs the **unchanged** Account Portal using its committed npm lock and pinned Playwright 1.58.2 in a short-lived Chromium image, and invokes `browser.cjs` to launch the Account Portal with `next dev` **inside the isolated CI container only**. The HTTP loopback issuer is allowed in CI development mode but **not** in production. Do not claim HTTPS production topology was tested.
|
||||
|
||||
`browser.cjs` performs real native browser navigation, original ZITADEL Login V2 username/password, visible OIDC authorization code + PKCE S256/state/nonce request, Auth.js callback, nonempty matching subject in `GET /api/account/me`, protected Account page, anonymous/invalid-callback 401 negative tests and local Account Portal logout. No fake IdP, injected session cookies, mocked tokens or bypasses. It must reject failures rather than silently skip assertions.
|
||||
|
||||
`cleanup.sh` is mandatory `always()`: removes **only** run-labeled Playwright/forwarder containers and run-labeled secret volume, then invokes the pre-existing audited `tests/ci/backend_cleanup.sh` for the marked Compose project (including bootstrap PAT/Postgres), and verifies no owned resources remain. Failure of cleanup makes the CI job fail; never run it on a production host.
|
||||
|
||||
**Privacy:** never log user password, owner PAT, OIDC client secret, Auth.js cookies, codes, nonce, state, request query or screenshots. Browser diagnostics contain only safe stage IDs. No credentials/recordings uploaded as CI artifacts. The actual CI run and individual pass/blocked status are recorded in `notes/P05-OIDC-01.md` and `docs/P05-OIDC-01.md`; source existence alone is not runtime acceptance.
|
||||
|
||||
**Phase limitations:** MFA/passkeys, production HTTPS issuer, deployment, full Admin Console human login and global SSO logout are outside this phase. Only a real successful browser callback + verified teardown is enough for its narrower user/password OIDC acceptance.
|
||||
@@ -0,0 +1,194 @@
|
||||
"use strict";
|
||||
|
||||
// Real Chromium -> original Login V2 -> real ZITADEL -> unmodified Auth.js.
|
||||
// No mock authorization, fabricated JWT, injected session, or token exfiltration.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const { spawn } = require("node:child_process");
|
||||
const { chromium } = require("playwright");
|
||||
|
||||
const ACCOUNT = "http://localhost:3000";
|
||||
const ISSUER = "http://localhost:8080";
|
||||
let stage = "BROWSER_PREFLIGHT";
|
||||
let server;
|
||||
let browser;
|
||||
|
||||
function pass(marker) {
|
||||
process.stdout.write(marker + "\n");
|
||||
}
|
||||
|
||||
function fixture() {
|
||||
const data = JSON.parse(fs.readFileSync("/ci/secret/oidc.json", "utf8"));
|
||||
for (const key of ["authSecret", "clientId", "clientSecret", "userId", "loginName", "password"]) {
|
||||
assert.equal(typeof data[key], "string", "Missing temporary fixture field");
|
||||
assert.ok(data[key].length > 0, "Empty temporary fixture field");
|
||||
}
|
||||
assert.equal(data.issuer, ISSUER);
|
||||
return data;
|
||||
}
|
||||
|
||||
async function waitForPortal(maxMs = 180000) {
|
||||
const deadline = Date.now() + maxMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (server.exitCode !== null) throw Error("Disposable Account Portal exited early");
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const abort = setTimeout(() => controller.abort(), 3000);
|
||||
try {
|
||||
const res = await fetch(ACCOUNT + "/signin", { redirect: "manual", signal: controller.signal });
|
||||
if (res.status === 200 || res.status === 303 || res.status === 307) return;
|
||||
} finally {
|
||||
clearTimeout(abort);
|
||||
}
|
||||
} catch (_) {}
|
||||
await new Promise(resolve => setTimeout(resolve, 1500));
|
||||
}
|
||||
throw Error("Disposable Account Portal was not HTTP-ready");
|
||||
}
|
||||
|
||||
function assertAuthorization(requestUrl, expectedId) {
|
||||
assert.ok(requestUrl, "No genuine OIDC authorization navigation observed");
|
||||
const u = new URL(requestUrl);
|
||||
assert.equal(u.origin, ISSUER);
|
||||
assert.equal(u.pathname, "/oauth/v2/authorize");
|
||||
assert.equal(u.searchParams.get("client_id"), expectedId);
|
||||
assert.equal(u.searchParams.get("response_type"), "code");
|
||||
assert.equal(u.searchParams.get("code_challenge_method"), "S256");
|
||||
assert.ok((u.searchParams.get("code_challenge") || "").length >= 43);
|
||||
assert.ok((u.searchParams.get("state") || "").length >= 16);
|
||||
assert.ok((u.searchParams.get("nonce") || "").length >= 16);
|
||||
assert.equal(u.searchParams.get("redirect_uri"), ACCOUNT + "/api/auth/callback/zitadel");
|
||||
}
|
||||
|
||||
async function verifyAnonymous(context) {
|
||||
const reply = await context.request.get(ACCOUNT + "/api/account/me");
|
||||
assert.equal(reply.status(), 401, "Anonymous Account API must be denied");
|
||||
const cache = reply.headers()["cache-control"] || "";
|
||||
assert.ok(cache.includes("no-store"), "Anonymous identity response may be cached");
|
||||
const page = await context.newPage();
|
||||
await page.goto(ACCOUNT + "/account", { waitUntil: "domcontentloaded", timeout: 90000 });
|
||||
await page.waitForURL(url => url.pathname === "/signin", { timeout: 60000 });
|
||||
await page.close();
|
||||
pass("ZITPLANE_OIDC_ANONYMOUS_API_AND_PAGE_REJECTED_PASS");
|
||||
}
|
||||
|
||||
async function verifyInvalidCallback(context) {
|
||||
const page = await context.newPage();
|
||||
await page.goto(ACCOUNT + "/api/auth/callback/zitadel?code=invalid&state=invalid", {
|
||||
waitUntil: "domcontentloaded",
|
||||
timeout: 60000
|
||||
}).catch(() => {});
|
||||
const reply = await context.request.get(ACCOUNT + "/api/account/me");
|
||||
assert.equal(reply.status(), 401, "Tampered callback established an identity");
|
||||
await page.close();
|
||||
pass("ZITPLANE_OIDC_INVALID_CALLBACK_FAIL_CLOSED_PASS");
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const data = fixture();
|
||||
stage = "ACCOUNT_SERVER_START";
|
||||
// Only this isolated CI namespace uses HTTP loopback; production Auth.js
|
||||
// configuration (including its HTTPS requirement) stays untouched.
|
||||
const outFd = fs.openSync("/tmp/oidc-portal-server.log", "w", 0o600);
|
||||
server = spawn("npm", ["run", "dev", "--", "--hostname", "127.0.0.1", "--port", "3000"], {
|
||||
cwd: "/tmp/ui",
|
||||
detached: true,
|
||||
stdio: ["ignore", outFd, outFd],
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: "development",
|
||||
AUTH_URL: ACCOUNT,
|
||||
AUTH_TRUST_HOST: "true",
|
||||
AUTH_SECRET: data.authSecret,
|
||||
ZITADEL_ISSUER: ISSUER,
|
||||
ZITADEL_CLIENT_ID: data.clientId,
|
||||
ZITADEL_CLIENT_SECRET: data.clientSecret,
|
||||
NO_PROXY: "localhost,127.0.0.1"
|
||||
}
|
||||
});
|
||||
fs.closeSync(outFd);
|
||||
await waitForPortal();
|
||||
pass("ZITPLANE_OIDC_DISPOSABLE_ACCOUNT_HTTP_READY_PASS");
|
||||
|
||||
stage = "PLAYWRIGHT_CHROMIUM_START";
|
||||
browser = await chromium.launch({ headless: true, args: ["--no-sandbox"] });
|
||||
const anonymous = await browser.newContext();
|
||||
const signed = await browser.newContext();
|
||||
anonymous.setDefaultTimeout(70000);
|
||||
signed.setDefaultTimeout(70000);
|
||||
|
||||
stage = "ANONYMOUS_NEGATIVE";
|
||||
await verifyAnonymous(anonymous);
|
||||
stage = "INVALID_CALLBACK_NEGATIVE";
|
||||
await verifyInvalidCallback(anonymous);
|
||||
|
||||
stage = "REAL_OIDC_AUTH_REDIRECT";
|
||||
const page = await signed.newPage();
|
||||
let authorizeUrl = null;
|
||||
page.on("request", request => {
|
||||
try {
|
||||
const url = new URL(request.url());
|
||||
if (url.origin === ISSUER && url.pathname === "/oauth/v2/authorize") {
|
||||
// Captured in memory ONLY. Never print code challenge, state or nonce.
|
||||
authorizeUrl = request.url();
|
||||
}
|
||||
} catch (_) {}
|
||||
});
|
||||
await page.goto(ACCOUNT + "/signin", { waitUntil: "domcontentloaded", timeout: 90000 });
|
||||
await page.getByRole("button", { name: "Continue with ZITADEL" }).click();
|
||||
await page.getByTestId("username-text-input").waitFor({ state: "visible", timeout: 110000 });
|
||||
assertAuthorization(authorizeUrl, data.clientId);
|
||||
pass("ZITPLANE_OIDC_REAL_AUTHORIZE_PKCE_STATE_NONCE_PASS");
|
||||
|
||||
stage = "ORIGINAL_LOGIN_USERNAME";
|
||||
await page.getByTestId("username-text-input").fill(data.loginName);
|
||||
await page.getByTestId("submit-button").click();
|
||||
stage = "ORIGINAL_LOGIN_PASSWORD";
|
||||
await page.getByTestId("password-text-input").waitFor({ state: "visible", timeout: 80000 });
|
||||
await page.getByTestId("password-text-input").fill(data.password);
|
||||
await page.getByTestId("submit-button").click();
|
||||
|
||||
stage = "OIDC_CODE_CALLBACK";
|
||||
await page.waitForURL(url => url.origin === ACCOUNT && url.pathname === "/account", {
|
||||
timeout: 105000
|
||||
});
|
||||
pass("ZITPLANE_OIDC_REAL_PASSWORD_AND_CALLBACK_PASS");
|
||||
|
||||
stage = "AUTHENTICATED_SESSION_IDENTITY";
|
||||
const response = await signed.request.get(ACCOUNT + "/api/account/me");
|
||||
assert.equal(response.status(), 200, "Authenticated API did not return HTTP 200");
|
||||
assert.ok((response.headers()["cache-control"] || "").includes("no-store"));
|
||||
const identity = await response.json();
|
||||
assert.equal(identity?.user?.id, data.userId, "Authenticated subject mismatch");
|
||||
const cookies = await signed.cookies(ACCOUNT);
|
||||
assert.ok(cookies.some(c => c.name.includes("session-token") && c.httpOnly), "No HttpOnly Auth.js session cookie");
|
||||
await page.goto(ACCOUNT + "/account/security", { waitUntil: "domcontentloaded" });
|
||||
assert.equal(new URL(page.url()).pathname, "/account/security", "Protected Account page denied valid session");
|
||||
pass("ZITPLANE_OIDC_REAL_SESSION_PROTECTED_PAGE_AND_API_PASS");
|
||||
|
||||
stage = "LOCAL_SESSION_LOGOUT";
|
||||
await page.getByRole("button", { name: "Sign out of portal" }).click();
|
||||
await page.waitForURL(url => url.pathname === "/signin", { timeout: 90000 });
|
||||
const afterLogout = await signed.request.get(ACCOUNT + "/api/account/me");
|
||||
assert.equal(afterLogout.status(), 401, "Local sign out left Account API authenticated");
|
||||
pass("ZITPLANE_OIDC_PORTAL_LOGOUT_SESSION_REVOKED_PASS");
|
||||
|
||||
await anonymous.close();
|
||||
await signed.close();
|
||||
pass("ZITPLANE_OIDC_REAL_BROWSER_COMPLETE_PASS");
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await main();
|
||||
} catch (_) {
|
||||
// Never log browser URL/query, cookies, API bodies, passwords or client key.
|
||||
process.stderr.write("ZITPLANE_OIDC_BROWSER_BLOCKED_AT=" + stage + "\n");
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
if (browser) await browser.close().catch(() => {});
|
||||
if (server?.pid) {
|
||||
try { process.kill(-server.pid, "SIGTERM"); } catch (_) {}
|
||||
}
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# Ephemeral CI-only Playwright image; NEVER use on the developer Windows PC.
|
||||
set -euo pipefail
|
||||
test -r /ci/secret/oidc.json || { echo OIDC_CI_SECRET_FIXTURE_MISSING >&2; exit 1; }
|
||||
test -d /tmp/ui || { echo OIDC_CI_SOURCE_MISSING >&2; exit 1; }
|
||||
|
||||
# Account Portal is installed in a disposable browser container only. The
|
||||
# previously verified production build/unit suites are NOT repeated here.
|
||||
if ! (cd /tmp/ui && npm ci --no-audit --no-fund >/tmp/oidc-account-npm.log 2>&1); then
|
||||
echo ZITPLANE_OIDC_ACCOUNT_CI_DEPENDENCIES_BLOCKED >&2
|
||||
exit 1
|
||||
fi
|
||||
echo ZITPLANE_OIDC_DISPOSABLE_ACCOUNT_DEPENDENCIES_READY_PASS
|
||||
# Match the pinned Chromium image, never install arbitrary latest browser code.
|
||||
if ! npm install --prefix /tmp/oidc-playwright --no-save --ignore-scripts --no-audit --no-fund playwright@1.58.2 >/tmp/oidc-browser-npm.log 2>&1; then
|
||||
echo ZITPLANE_OIDC_PINNED_PLAYWRIGHT_INSTALL_BLOCKED >&2
|
||||
exit 1
|
||||
fi
|
||||
export NODE_PATH=/tmp/oidc-playwright/node_modules
|
||||
# Issuer and Account are private IPv4 loopback sockets in the proxy namespace.
|
||||
export NODE_OPTIONS=--dns-result-order=ipv4first
|
||||
exec node /tmp/oidc_e2e/browser.cjs
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/bin/sh
|
||||
# Run only as the always() step for the matching Gitea run's disposable job.
|
||||
set -eu
|
||||
: "${GITHUB_RUN_ID:?Gitea numeric run ID required for safe cleanup}"
|
||||
case "$GITHUB_RUN_ID" in *[!0-9]*|'') echo OIDC_CI_CLEANUP_INVALID_RUN_ID >&2; exit 1;; esac
|
||||
project="zitplane-p05-$GITHUB_RUN_ID"
|
||||
browser="zitplane-oidc-$GITHUB_RUN_ID-browser"
|
||||
forwarder="zitplane-oidc-$GITHUB_RUN_ID-loopback"
|
||||
secret_volume="zitplane-oidc-$GITHUB_RUN_ID-secrets"
|
||||
owner_marker=".p05-oidc-owned"
|
||||
rc=0
|
||||
|
||||
if [ -f "$owner_marker" ] && [ ! -f .p05-ci-owned ]; then
|
||||
# Pre-Compose preparation failed; this run claimed a clean checkout before
|
||||
# generating the disposable .env. No Docker resources were created yet.
|
||||
rm -f .env "$owner_marker"
|
||||
echo ZITPLANE_OIDC_PRECOMPOSE_CREDENTIAL_CLEANUP_PASS
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ ! -f "$owner_marker" ] && [ ! -f .p05-ci-owned ]; then
|
||||
# Preflight failed before claiming ownership; never touch Docker resources.
|
||||
if [ -e .env ]; then
|
||||
echo 'OIDC_CI_UNOWNED_ENV_PRESENT: cleanup refused' >&2
|
||||
exit 1
|
||||
fi
|
||||
echo ZITPLANE_OIDC_CLEANUP_NOT_REQUIRED
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Stop only exact run-labeled, CI-created sibling containers, before Compose
|
||||
# teardown, otherwise they could keep the fixed private network namespace alive.
|
||||
for name in "$browser" "$forwarder"; do
|
||||
if docker container inspect "$name" >/dev/null 2>&1; then
|
||||
label="$(docker inspect -f '{{ index .Config.Labels "zitplane.oidc.run" }}' "$name" 2>/dev/null || true)"
|
||||
if [ "$label" != "$GITHUB_RUN_ID" ]; then
|
||||
echo 'OIDC_CI_UNOWNED_CONTAINER_BLOCKED' >&2
|
||||
rc=1
|
||||
continue
|
||||
fi
|
||||
if ! docker rm -f "$name" >/dev/null; then
|
||||
echo 'OIDC_CI_BROWSER_OR_FORWARDER_CLEANUP_FAILED' >&2
|
||||
rc=1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
if docker volume inspect "$secret_volume" >/dev/null 2>&1; then
|
||||
label="$(docker volume inspect -f '{{ index .Labels "zitplane.oidc.run" }}' "$secret_volume" 2>/dev/null || true)"
|
||||
if [ "$label" != "$GITHUB_RUN_ID" ]; then
|
||||
echo 'OIDC_CI_UNOWNED_SECRET_VOLUME_BLOCKED' >&2
|
||||
rc=1
|
||||
elif ! docker volume rm "$secret_volume" >/dev/null; then
|
||||
echo 'OIDC_CI_DISPOSABLE_SECRETS_NOT_DESTROYED' >&2
|
||||
rc=1
|
||||
fi
|
||||
fi
|
||||
# Remove only the run-owned backend project using existing audited cleanup.
|
||||
if [ -f .p05-ci-owned ]; then
|
||||
if ! sh tests/ci/backend_cleanup.sh; then
|
||||
echo OIDC_CI_BACKEND_CLEANUP_NOT_VERIFIED >&2
|
||||
rc=1
|
||||
fi
|
||||
fi
|
||||
for name in "$browser" "$forwarder"; do
|
||||
if docker container inspect "$name" >/dev/null 2>&1; then
|
||||
echo OIDC_CI_CONTAINER_LEFTOVER >&2
|
||||
rc=1
|
||||
fi
|
||||
done
|
||||
if docker volume inspect "$secret_volume" >/dev/null 2>&1; then
|
||||
echo OIDC_CI_SECRET_VOLUME_LEFTOVER >&2
|
||||
rc=1
|
||||
fi
|
||||
if [ -n "$(docker ps -a -q --filter "label=com.docker.compose.project=$project")" ] ||
|
||||
[ -n "$(docker volume ls -q --filter "label=com.docker.compose.project=$project")" ] ||
|
||||
docker network inspect zitplane-backend-dev-internal >/dev/null 2>&1; then
|
||||
echo OIDC_CI_DISPOSABLE_BACKEND_RESOURCES_LEFTOVER >&2
|
||||
rc=1
|
||||
fi
|
||||
if [ "$rc" -ne 0 ]; then
|
||||
echo ZITPLANE_OIDC_CLEANUP_FAILED_MANUAL_REVIEW >&2
|
||||
exit 1
|
||||
fi
|
||||
rm -f "$owner_marker"
|
||||
echo ZITPLANE_OIDC_ALL_RESOURCES_CLEANED_PASS
|
||||
@@ -0,0 +1,8 @@
|
||||
# P05-OIDC-01 CI-only bootstrap. Never change P02 developer or production Compose.
|
||||
services:
|
||||
zitadel-api:
|
||||
environment:
|
||||
ZITADEL_FIRSTINSTANCE_PATPATH: /zitadel/bootstrap/oidc-ci-owner.pat
|
||||
ZITADEL_FIRSTINSTANCE_ORG_MACHINE_MACHINE_USERNAME: oidc-ci-owner
|
||||
ZITADEL_FIRSTINSTANCE_ORG_MACHINE_MACHINE_NAME: Disposable_OIDC_CI_IAM_OWNER
|
||||
ZITADEL_FIRSTINSTANCE_ORG_MACHINE_PAT_EXPIRATIONDATE: ${OIDC_CI_PAT_EXPIRATION:?Missing ephemeral owner PAT expiration}
|
||||
@@ -0,0 +1,178 @@
|
||||
"""Official ZITADEL API provisioning for ONE disposable, isolated Gitea run.
|
||||
|
||||
Read-only IAM_OWNER PAT from the ephemeral ZITADEL bootstrap volume.
|
||||
Only output state is a 0600 JSON fixture on a distinct CI-owned Docker volume.
|
||||
NEVER log PAT, client secrets, passwords, tokens, API JSON/error bodies.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import sys
|
||||
import time
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.request import HTTPRedirectHandler, ProxyHandler, Request, build_opener
|
||||
|
||||
OWNER_PAT = Path("/zitadel/bootstrap/oidc-ci-owner.pat")
|
||||
FIXTURE = Path("/ci/secret/oidc.json")
|
||||
ISSUER = "http://localhost:8080"
|
||||
ORIGIN = "http://127.0.0.1:8080"
|
||||
TIMEOUT = 20
|
||||
CLIENT_ID_KEYS = ("clientId",)
|
||||
OPENER = build_opener(ProxyHandler({}))
|
||||
|
||||
|
||||
class DenyRedirect(HTTPRedirectHandler):
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
raise RuntimeError("Unexpected redirect while provisioning local ZITADEL")
|
||||
|
||||
|
||||
OPENER.add_handler(DenyRedirect())
|
||||
|
||||
|
||||
def request(method: str, path: str, token: str | None = None, payload: dict | None = None) -> dict:
|
||||
if not path.startswith("/") or path.startswith("//") or "://" in path or "?" in path:
|
||||
raise RuntimeError("Unsafe provisioning endpoint")
|
||||
body = None if payload is None else json.dumps(payload, separators=(",", ":")).encode()
|
||||
headers = {"Host": "localhost:8080", "Accept": "application/json"}
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
try:
|
||||
with OPENER.open(Request(ORIGIN + path, data=body, headers=headers, method=method), timeout=TIMEOUT) as reply:
|
||||
if reply.status != 200:
|
||||
raise RuntimeError(f"Provisioning endpoint returned HTTP {reply.status}")
|
||||
data = reply.read(1_000_001)
|
||||
if len(data) > 1_000_000:
|
||||
raise RuntimeError("Oversized provisioning API response")
|
||||
except HTTPError as exc:
|
||||
# No error response payload (may include secrets, login names or details).
|
||||
raise RuntimeError(f"Official ZITADEL API {method} {path} returned HTTP {exc.code}") from None
|
||||
except URLError as exc:
|
||||
raise RuntimeError(f"ZITADEL API unavailable for {method} {path}") from None
|
||||
if not data:
|
||||
return {}
|
||||
result = json.loads(data)
|
||||
if not isinstance(result, dict):
|
||||
raise RuntimeError("Invalid API response shape")
|
||||
return result
|
||||
|
||||
|
||||
def wait_until_ready() -> None:
|
||||
deadline = time.monotonic() + 70
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
request("GET", "/debug/ready")
|
||||
return
|
||||
except RuntimeError:
|
||||
time.sleep(2)
|
||||
raise RuntimeError("Disposable localhost issuer port forwarding was not ready")
|
||||
|
||||
|
||||
def required_string(data: dict, name: str) -> str:
|
||||
value = data.get(name)
|
||||
if not isinstance(value, str) or not value or len(value) > 4096:
|
||||
raise RuntimeError(f"ZITADEL response missing required {name}")
|
||||
return value
|
||||
|
||||
|
||||
def main() -> None:
|
||||
run = os.environ.get("OIDC_CI_RUN_ID", "")
|
||||
if not run.isdecimal() or len(run) > 18:
|
||||
raise RuntimeError("Invalid disposable Gitea run ID")
|
||||
if not FIXTURE.parent.is_dir() or FIXTURE.exists():
|
||||
raise RuntimeError("Secret fixture volume missing or already used")
|
||||
wait_until_ready()
|
||||
print("ZITPLANE_OIDC_LOCAL_ORIGIN_READY_PASS", flush=True)
|
||||
discovery = request("GET", "/.well-known/openid-configuration")
|
||||
if discovery.get("issuer") != ISSUER:
|
||||
raise RuntimeError("Disposable OIDC issuer mismatch")
|
||||
for field in ("authorization_endpoint", "token_endpoint", "jwks_uri"):
|
||||
if not isinstance(discovery.get(field), str) or not discovery[field].startswith(ISSUER + "/"):
|
||||
raise RuntimeError(f"Invalid discovery {field} origin")
|
||||
print("ZITPLANE_OIDC_ISSUER_DISCOVERY_PASS", flush=True)
|
||||
|
||||
if not OWNER_PAT.is_file():
|
||||
raise RuntimeError("Disposable first-instance IAM_OWNER PAT was not issued")
|
||||
pat = OWNER_PAT.read_text(encoding="utf-8").strip()
|
||||
if len(pat) < 20:
|
||||
raise RuntimeError("Invalid disposable IAM_OWNER PAT length")
|
||||
|
||||
project = request(
|
||||
"POST", "/management/v1/projects", pat,
|
||||
{"name": f"zitplane-oidc-ci-{run}"},
|
||||
)
|
||||
project_id = required_string(project, "id")
|
||||
print("ZITPLANE_OIDC_PROJECT_CREATED_PASS", flush=True)
|
||||
|
||||
app = request(
|
||||
"POST", f"/management/v1/projects/{project_id}/apps/oidc", pat,
|
||||
{
|
||||
"name": "ZitPlane_Disposable_Account_OIDC",
|
||||
"redirectUris": ["http://localhost:3000/api/auth/callback/zitadel"],
|
||||
"postLogoutRedirectUris": ["http://localhost:3000"],
|
||||
"responseTypes": ["OIDC_RESPONSE_TYPE_CODE"],
|
||||
"grantTypes": ["OIDC_GRANT_TYPE_AUTHORIZATION_CODE"],
|
||||
"appType": "OIDC_APP_TYPE_WEB",
|
||||
"authMethodType": "OIDC_AUTH_METHOD_TYPE_BASIC",
|
||||
"version": "OIDC_VERSION_1_0",
|
||||
"devMode": True,
|
||||
},
|
||||
)
|
||||
client_id = required_string(app, "clientId")
|
||||
client_secret = required_string(app, "clientSecret")
|
||||
required_string(app, "appId")
|
||||
print("ZITPLANE_OIDC_REAL_CLIENT_CREATED_PASS", flush=True)
|
||||
|
||||
# Invalid TLD and verified email ensure no delivery to any real account.
|
||||
login_name = f"oidc-ci-{run}@example.invalid"
|
||||
password = "Zp!7" + secrets.token_urlsafe(38)
|
||||
human = request(
|
||||
"POST", "/v2/users/human", pat,
|
||||
{
|
||||
"username": login_name,
|
||||
"profile": {
|
||||
"givenName": "Disposable",
|
||||
"familyName": "Acceptance",
|
||||
"preferredLanguage": "en",
|
||||
},
|
||||
"email": {"email": login_name, "isVerified": True},
|
||||
"password": {"password": password, "changeRequired": False},
|
||||
},
|
||||
)
|
||||
user_id = required_string(human, "userId")
|
||||
print("ZITPLANE_OIDC_REAL_TEST_HUMAN_CREATED_PASS", flush=True)
|
||||
|
||||
# Write secrets only inside an explicit CI-created, non-reused Docker volume;
|
||||
# browser has this volume read-only, never the owner PAT. No stdout JSON.
|
||||
fixture = {
|
||||
"issuer": ISSUER,
|
||||
"clientId": client_id,
|
||||
"clientSecret": client_secret,
|
||||
"authSecret": secrets.token_urlsafe(48),
|
||||
"userId": user_id,
|
||||
"loginName": login_name,
|
||||
"password": password,
|
||||
}
|
||||
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
|
||||
fd = os.open(FIXTURE, flags, 0o600)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
||||
json.dump(fixture, handle)
|
||||
handle.write("\n")
|
||||
except BaseException:
|
||||
FIXTURE.unlink(missing_ok=True)
|
||||
raise
|
||||
print("ZITPLANE_OIDC_TEST_CREDENTIALS_ISOLATED_PASS", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as exc:
|
||||
# Only our intentionally redacted statuses, not raw traceback/request.
|
||||
print(f"ZITPLANE_OIDC_PROVISION_BLOCKED: {str(exc)[:210]}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,118 @@
|
||||
#!/bin/sh
|
||||
# One-shot isolated Gitea runner ONLY. No public host port; no real user data.
|
||||
set -eu
|
||||
: "${GITHUB_RUN_ID:?Gitea run ID required}"
|
||||
case "$GITHUB_RUN_ID" in *[!0-9]*|'') echo 'OIDC_CI_INVALID_RUN_ID' >&2; exit 1;; esac
|
||||
project="zitplane-p05-$GITHUB_RUN_ID"
|
||||
network="zitplane-backend-dev-internal"
|
||||
secret_volume="zitplane-oidc-$GITHUB_RUN_ID-secrets"
|
||||
browser="zitplane-oidc-$GITHUB_RUN_ID-browser"
|
||||
forwarder="zitplane-oidc-$GITHUB_RUN_ID-loopback"
|
||||
marker=".p05-oidc-owned"
|
||||
|
||||
dc() {
|
||||
docker compose --env-file .env -p "$project" \
|
||||
-f deployment/docker/compose.backend.dev.yaml \
|
||||
-f tests/ci/compose.portless.override.yaml \
|
||||
-f tests/ci/oidc_e2e/compose.owner.override.yaml "$@"
|
||||
}
|
||||
|
||||
docker --version
|
||||
docker compose version
|
||||
docker info --format '{{.Driver}}' >/dev/null
|
||||
echo ZITPLANE_OIDC_DOCKER_PREFLIGHT_PASS
|
||||
|
||||
# Refuse to assume ownership of any preexisting name, fixed shared network,
|
||||
# credential or prior project; cleanup must not delete unowned resources.
|
||||
if [ -e .env ] || [ -e "$marker" ] || [ -e .p05-ci-owned ] ||
|
||||
docker network inspect "$network" >/dev/null 2>&1 ||
|
||||
docker volume inspect "$secret_volume" >/dev/null 2>&1 ||
|
||||
docker container inspect "$browser" >/dev/null 2>&1 ||
|
||||
docker container inspect "$forwarder" >/dev/null 2>&1 ||
|
||||
[ -n "$(docker ps -a -q --filter "label=com.docker.compose.project=$project")" ] ||
|
||||
[ -n "$(docker volume ls -q --filter "label=com.docker.compose.project=$project")" ]; then
|
||||
echo 'OIDC_CI_OWNERSHIP_PREFLIGHT_BLOCKED: existing state' >&2
|
||||
exit 1
|
||||
fi
|
||||
for v in "${project}_postgres-data" "${project}_zitadel-bootstrap"; do
|
||||
if docker volume inspect "$v" >/dev/null 2>&1; then
|
||||
echo OIDC_CI_PREEXISTING_VOLUME_BLOCKED >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
# Mark only CI ownership after verifying a fresh checkout and NO Docker
|
||||
# resources. If env/config generation fails, cleanup deletes only our env.
|
||||
touch "$marker"
|
||||
python3 core/backend/zitadel/dev.py init
|
||||
python3 - <<'PY'
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
p = Path('.env')
|
||||
if not p.is_file():
|
||||
raise SystemExit('Disposable env missing')
|
||||
expiration = (datetime.now(timezone.utc) + timedelta(hours=2)).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
with p.open('a', encoding='utf-8') as out:
|
||||
out.write('OIDC_CI_PAT_EXPIRATION=' + expiration + '\n')
|
||||
p.chmod(0o600)
|
||||
PY
|
||||
dc config --quiet
|
||||
dc config --format json |
|
||||
python3 -c 'import json,sys; v=json.load(sys.stdin); assert set(v["services"])=={"postgres","zitadel-api","zitadel-login","proxy"}; assert not v["services"]["proxy"].get("ports"), "A public host port was configured"; assert v["services"]["zitadel-api"]["environment"]["ZITADEL_FIRSTINSTANCE_PATPATH"]=="/zitadel/bootstrap/oidc-ci-owner.pat"; print("ZITPLANE_OIDC_NO_PUBLISHED_PORT_AND_OWNER_CONFIG_PASS")'
|
||||
|
||||
# Mark ownership only AFTER all preflight/config checks; always() cleanup knows it.
|
||||
touch .p05-ci-owned
|
||||
dc up -d --wait
|
||||
for name in postgres zitadel-api zitadel-login proxy; do
|
||||
cid="$(dc ps -q "$name")"
|
||||
[ -n "$cid" ] && [ "$(docker inspect -f '{{.State.Running}}' "$cid")" = true ] || {
|
||||
echo "OIDC_CI_SERVICE_NOT_RUNNING=$name" >&2; exit 1;
|
||||
}
|
||||
if [ "$name" != proxy ]; then
|
||||
[ "$(docker inspect -f '{{.State.Health.Status}}' "$cid")" = healthy ] || {
|
||||
echo "OIDC_CI_SERVICE_NOT_HEALTHY=$name" >&2; exit 1;
|
||||
}
|
||||
fi
|
||||
done
|
||||
echo ZITPLANE_OIDC_FOUR_SERVICES_READY_PASS
|
||||
|
||||
proxy_id="$(dc ps -q proxy)"
|
||||
test -n "$proxy_id"
|
||||
bootstrap_volume="${project}_zitadel-bootstrap"
|
||||
|
||||
# Everything below shares the private proxy network namespace (not the host).
|
||||
# Loopback-8080 preserves ZITADEL's P02 external issuer and Host routing.
|
||||
docker run -d --name "$forwarder" \
|
||||
--label "zitplane.oidc.run=$GITHUB_RUN_ID" \
|
||||
--network "container:$proxy_id" alpine:3.21 \
|
||||
sh -ec 'apk add --no-cache socat >/dev/null 2>&1; exec socat TCP-LISTEN:8080,bind=127.0.0.1,reuseaddr,fork TCP:127.0.0.1:80' >/dev/null
|
||||
docker volume create --name "$secret_volume" \
|
||||
--label "zitplane.oidc.run=$GITHUB_RUN_ID" >/dev/null
|
||||
|
||||
# Provisioning container only: holds ephemeral IAM_OWNER PAT READ-ONLY and
|
||||
# writes a CI-only client/user credential file to a separate short-lived volume.
|
||||
docker run --rm -i --network "container:$proxy_id" \
|
||||
-e "OIDC_CI_RUN_ID=$GITHUB_RUN_ID" \
|
||||
--volume "$bootstrap_volume:/zitadel/bootstrap:ro" \
|
||||
--volume "$secret_volume:/ci/secret" \
|
||||
python:3.12-alpine python - < tests/ci/oidc_e2e/provision.py
|
||||
echo ZITPLANE_OIDC_DISPOSABLE_PROVISIONING_PASS
|
||||
|
||||
# Copy only nonsecret repository app/source and test harness files into a
|
||||
# throwaway browser container. Docker cp does NOT depend on runner host paths.
|
||||
docker create --name "$browser" \
|
||||
--label "zitplane.oidc.run=$GITHUB_RUN_ID" \
|
||||
--network "container:$proxy_id" \
|
||||
--volume "$secret_volume:/ci/secret:ro" \
|
||||
mcr.microsoft.com/playwright:v1.58.2-noble \
|
||||
bash /tmp/oidc_e2e/browser.sh >/dev/null
|
||||
docker cp users/ui "$browser:/tmp/ui"
|
||||
docker cp tests/ci/oidc_e2e "$browser:/tmp/oidc_e2e"
|
||||
|
||||
# Logs only safe named PASS/BLOCKED markers, never passwords/tokens/query URLs.
|
||||
docker start -a "$browser"
|
||||
exit_code="$(docker inspect -f '{{.State.ExitCode}}' "$browser")"
|
||||
if [ "$exit_code" != 0 ]; then
|
||||
echo 'ZITPLANE_OIDC_BROWSER_ACCEPTANCE_BLOCKED' >&2
|
||||
exit 1
|
||||
fi
|
||||
echo ZITPLANE_OIDC_REAL_BROWSER_ACCEPTANCE_PASS
|
||||
Reference in new issue
Block a user