119 lines
5.1 KiB
Bash
119 lines
5.1 KiB
Bash
#!/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
|