From f4c77f5b9352263b9c76e3e5b9a58542dfb587bf Mon Sep 17 00:00:00 2001 From: Jesper Jumziey Vesterberg Date: Sat, 21 Nov 2026 11:39:05 +0100 Subject: [PATCH] feat(deploy): pass the restricted Pod Security Standard Each shipped container (backend, frontend, bundled Valkey) now sets allowPrivilegeEscalation: false, drops all capabilities, sets runAsNonRoot: true, and uses the RuntimeDefault seccomp profile. - Backend: read-only root filesystem with an emptyDir at /tmp. Vert.x creates its file cache there at start, and the backend does not start without it. - Frontend: the image moves to nginxinc/nginx-unprivileged (UID 101, port 8080). The served env-config.js is a link to /tmp/env-config.js, so the root filesystem is read-only with an emptyDir at /tmp. The Service keeps port 80. - Valkey: the upstream image starts as root, so the manifest sets runAsUser 999 and runAsGroup 1000. - compose: map the frontend port 8080, and start nginx through the image entrypoint so that env-config.js is written. - CI: the manifests job applies the quickstart overlay to a kind cluster as a server-side dry run and fails on a Pod Security warning. The frontend image check starts the image with the limits of the Deployment. The frontend image now listens on port 8080, not port 80. The manifests do not start a frontend image from before this change, so the image pins must move to an image with this change before the manifests are released. See docs/adr/0044. Closes #134 --- .github/workflows/pr.yml | 15 +++- CONTRIBUTING.md | 3 +- ci/README.md | 15 +++- ci/check-restricted-pod-security.sh | 51 ++++++++++++ compose.quickstart.yml | 2 +- compose.yml | 8 +- deploy/k8s/base/backend-deployment.yaml | 17 ++++ deploy/k8s/base/frontend-deployment.yaml | 23 +++++- deploy/k8s/components/valkey/valkey.yaml | 13 +++ ...ss-the-restricted-pod-security-standard.md | 80 +++++++++++++++++++ docs/deployment.md | 48 +++++++++++ frontend/Dockerfile | 11 ++- frontend/README.md | 8 +- frontend/ci/bin/test-build-version-image.sh | 59 +++++++++++--- frontend/docker-entrypoint.d/40-env-config.sh | 6 +- frontend/nginx.conf | 2 +- frontend/nginx.quickstart.conf | 2 +- 17 files changed, 331 insertions(+), 32 deletions(-) create mode 100755 ci/check-restricted-pod-security.sh create mode 100644 docs/adr/0044-shipped-workloads-pass-the-restricted-pod-security-standard.md diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 5d4d07cd..5298ddc3 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -67,7 +67,7 @@ jobs: working-directory: frontend run: yarn test - - name: Frontend build identity (both shipped nginx images, no publishing) + - name: Frontend image contract (build identity, both nginx configs, restricted runtime; no publishing) run: bash frontend/ci/bin/test-build-version-image.sh # The frontend image is published for linux/arm64 too (docs/adr/0043), but @@ -91,7 +91,7 @@ jobs: with: node-version: 'lts/*' - - name: Frontend build identity (both shipped nginx images, no publishing) + - name: Frontend image contract (build identity, both nginx configs, restricted runtime; no publishing) run: bash frontend/ci/bin/test-build-version-image.sh # The GraalVM native build plus the native @QuarkusIntegrationTest suite @@ -199,11 +199,13 @@ jobs: # (base alone, the quickstart overlay, and base + each component — the rbac # component only composes at apply time, so CI fabricates the throwaway # overlay a user would write) and schema-validate the output against the - # upstream Kubernetes OpenAPI schemas with kubeconform. + # upstream Kubernetes OpenAPI schemas with kubeconform. Then check on a real + # API server (kind) that the quickstart overlay passes the `restricted` Pod + # Security Standard (docs/adr/0044). Schema validation cannot see that. manifests: name: manifests runs-on: ubuntu-latest - timeout-minutes: 5 + timeout-minutes: 10 steps: - name: Check out repository uses: actions/checkout@v7 @@ -232,3 +234,8 @@ jobs: kubectl kustomize "deploy/k8s/$target" \ | kubeconform -strict -summary done + + # kind and kubectl are part of the runner image. The script creates its + # own cluster and deletes it. + - name: Restricted Pod Security Standard (quickstart overlay) + run: bash ci/check-restricted-pod-security.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3802510d..7a1a31d7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -96,7 +96,8 @@ Every pull request must pass five required checks before it can merge: - **`frontend image (arm64)`** builds the frontend image on an arm64 runner and runs the same image checks that `fast` runs on amd64. - **`manifests`** builds every shipped Kubernetes composition and validates - the rendered resources with kubeconform. + the rendered resources with kubeconform. It also checks on a kind cluster + that the quickstart overlay passes the `restricted` Pod Security Standard. All five jobs are defined in `.github/workflows/pr.yml`. diff --git a/ci/README.md b/ci/README.md index 2e0362be..dc06b6a6 100644 --- a/ci/README.md +++ b/ci/README.md @@ -9,6 +9,7 @@ bash ci/check-backend-build-version.sh bash frontend/ci/bin/test-build-version-image.sh bash ci/test-native-dockerfile-arch.sh bash ci/check-image-platforms.sh linux/amd64,linux/arm64 ... +bash ci/check-restricted-pod-security.sh ``` - `resolve-build-version.sh release v` validates strict SemVer and @@ -23,9 +24,11 @@ bash ci/check-image-platforms.sh linux/amd64,linux/arm64 ... and checking `/api/version`. Valkey and version-source URLs are deliberately unreachable. Runtime identity overrides must be ignored. - The frontend check requires Docker, curl, and Node; it checks the static - artifact through both shipped nginx configurations. It never publishes images. - It builds for the architecture of its host; PR checks run it on amd64 and on - arm64. + artifact through both shipped nginx configurations. It also starts the image + with the limits of the shipped Deployment: a read-only root filesystem with + a writable `/tmp`, no capabilities, and a user that is not root. It never + publishes images. It builds for the architecture of its host; PR checks run + it on amd64 and on arm64. - The native Dockerfile test requires Docker (BuildKit) and `readelf`. It builds only the `patcher` stage of `Dockerfile.native-ci` with a fixture binary: each supported target platform gets its ELF loader, `/nix/store` paths are removed, @@ -34,6 +37,12 @@ bash ci/check-image-platforms.sh linux/amd64,linux/arm64 ... - `check-image-platforms.sh` reads each given published tag from the registry and fails unless its manifest list carries every given platform. The publish workflow runs it on each tag it writes (ADR-0043). +- `check-restricted-pod-security.sh` requires Docker, kind, and kubectl. It + creates a kind cluster with its own kubeconfig, and deletes both on exit. It + does not use the current kube context. It applies the quickstart overlay as + a server-side dry run to a namespace that has the `restricted` Pod Security + label, and fails on each Pod Security warning (ADR-0044). It starts no pods, + so it does not prove that the images run with these limits. Publishing resolves once in `build-images.yml`'s upstream identity job. Every build job (the backend job of each architecture and the frontend job) checks diff --git a/ci/check-restricted-pod-security.sh b/ci/check-restricted-pod-security.sh new file mode 100755 index 00000000..46346ba2 --- /dev/null +++ b/ci/check-restricted-pod-security.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Checks that the quickstart overlay passes the `restricted` Pod Security +# Standard. Run from any directory. Requires Docker, kind and kubectl. +# +# The script creates its own kind cluster with its own kubeconfig and deletes +# both on exit. It never reads or changes the current kube context. +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" +OVERLAY="$REPO_ROOT/deploy/k8s/overlays/quickstart" +NAMESPACE=platformup2date +CLUSTER="pu2d-pss-$$" +TMP_DIR="$(mktemp -d)" +export KUBECONFIG="$TMP_DIR/kubeconfig" + +cleanup() { + kind delete cluster --name "$CLUSTER" >/dev/null 2>&1 || true + rm -rf "$TMP_DIR" +} +trap cleanup EXIT + +kind create cluster --name "$CLUSTER" --wait 120s + +# Pod Security admission rejects only Pods in `enforce` mode: a Deployment +# with a bad pod template is still admitted. `warn` mode evaluates the pod +# template of a workload resource, and --warnings-as-errors makes kubectl fail +# on that warning. A server-side dry run is sufficient, because admission runs +# on a dry run. +kubectl create namespace "$NAMESPACE" --save-config +kubectl label namespace "$NAMESPACE" \ + pod-security.kubernetes.io/enforce=restricted \ + pod-security.kubernetes.io/warn=restricted + +# Control: a Deployment with no securityContext must fail. If it passes, the +# check below proves nothing. +if kubectl --namespace "$NAMESPACE" create deployment pss-control \ + --image=registry.k8s.io/pause:3.10 --dry-run=server --warnings-as-errors \ + >"$TMP_DIR/control.log" 2>&1; then + cat "$TMP_DIR/control.log" >&2 + echo 'FAIL: the control Deployment has no securityContext, but the cluster accepted it' >&2 + exit 1 +fi +grep -q 'violate PodSecurity "restricted' "$TMP_DIR/control.log" || { + cat "$TMP_DIR/control.log" >&2 + echo 'FAIL: the control Deployment failed for a reason other than Pod Security' >&2 + exit 1 +} +echo 'PASS: control Deployment without securityContext is rejected' + +kubectl apply --dry-run=server --warnings-as-errors -k "$OVERLAY" +echo 'PASS: the quickstart overlay passes the restricted Pod Security Standard' diff --git a/compose.quickstart.yml b/compose.quickstart.yml index 141ec8f8..08afee2c 100644 --- a/compose.quickstart.yml +++ b/compose.quickstart.yml @@ -37,6 +37,6 @@ services: volumes: - ./frontend/nginx.quickstart.conf:/etc/nginx/conf.d/default.conf:ro ports: - - "3000:80" + - "3000:8080" depends_on: - backend diff --git a/compose.yml b/compose.yml index 0ab6f724..19af4e91 100644 --- a/compose.yml +++ b/compose.yml @@ -43,16 +43,18 @@ services: volumes: - ./frontend/nginx.quickstart.conf:/etc/nginx/conf.d/default.conf:ro ports: - - "3000:80" + - "3000:8080" command: - /bin/sh - -c - - echo "PlatformUp2Date is available at http://localhost:3000" && exec nginx -g 'daemon off;' + # Start nginx through the image entrypoint. The entrypoint writes + # env-config.js only when its first argument is `nginx`. + - echo "PlatformUp2Date is available at http://localhost:3000" && exec /docker-entrypoint.sh nginx -g 'daemon off;' depends_on: backend: condition: service_healthy healthcheck: - test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://127.0.0.1/api/v1/version"] + test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://127.0.0.1:8080/api/v1/version"] interval: 3s timeout: 2s retries: 30 diff --git a/deploy/k8s/base/backend-deployment.yaml b/deploy/k8s/base/backend-deployment.yaml index bc1c8fe1..9f680962 100644 --- a/deploy/k8s/base/backend-deployment.yaml +++ b/deploy/k8s/base/backend-deployment.yaml @@ -68,6 +68,10 @@ spec: - name: platform-config mountPath: /config readOnly: true + # The only writable path. Vert.x creates its file cache in /tmp + # at start, and the backend does not start without it. + - name: tmp + mountPath: /tmp # Readiness reports DOWN when Valkey is unreachable (the backend # fails closed with 503s without it), pulling the replica out of # rotation for exactly the window it cannot serve. @@ -88,7 +92,20 @@ spec: memory: 256Mi limits: memory: 512Mi + # Passes the `restricted` Pod Security Standard (docs/adr/0044). The + # image runs as UID 1001. All state is in Valkey, so the root + # filesystem is read-only. + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + readOnlyRootFilesystem: true + runAsNonRoot: true + seccompProfile: + type: RuntimeDefault volumes: - name: platform-config configMap: name: platform-config + - name: tmp + emptyDir: {} diff --git a/deploy/k8s/base/frontend-deployment.yaml b/deploy/k8s/base/frontend-deployment.yaml index e6d600d3..9750cb72 100644 --- a/deploy/k8s/base/frontend-deployment.yaml +++ b/deploy/k8s/base/frontend-deployment.yaml @@ -29,8 +29,10 @@ spec: - name: frontend image: ghcr.io/sreyardship/platformup2date/frontend ports: + # The image runs unprivileged nginx, which cannot bind a port + # below 1024. The Service still exposes port 80. - name: http - containerPort: 80 + containerPort: 8080 env: # Injected into window._env_ at container start. Empty means # same-origin: the browser calls /api on whatever host served the @@ -69,3 +71,22 @@ spec: memory: 32Mi limits: memory: 64Mi + # Passes the `restricted` Pod Security Standard (docs/adr/0044). The + # image runs as UID 101. + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + readOnlyRootFilesystem: true + runAsNonRoot: true + seccompProfile: + type: RuntimeDefault + # The only writable path. It holds the env-config.js that is + # generated at container start, the nginx pid file, and the nginx + # temp paths. + volumeMounts: + - name: tmp + mountPath: /tmp + volumes: + - name: tmp + emptyDir: {} diff --git a/deploy/k8s/components/valkey/valkey.yaml b/deploy/k8s/components/valkey/valkey.yaml index 28113477..e7e9c4bc 100644 --- a/deploy/k8s/components/valkey/valkey.yaml +++ b/deploy/k8s/components/valkey/valkey.yaml @@ -42,6 +42,19 @@ spec: memory: 32Mi limits: memory: 128Mi + # Passes the `restricted` Pod Security Standard (docs/adr/0044). The + # upstream image starts as root and then changes to its `valkey` + # user. That change needs capabilities, so start as that user + # instead: UID 999 and GID 1000 in the alpine image. + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + runAsNonRoot: true + runAsUser: 999 + runAsGroup: 1000 + seccompProfile: + type: RuntimeDefault --- apiVersion: v1 kind: Service diff --git a/docs/adr/0044-shipped-workloads-pass-the-restricted-pod-security-standard.md b/docs/adr/0044-shipped-workloads-pass-the-restricted-pod-security-standard.md new file mode 100644 index 00000000..eadd6cda --- /dev/null +++ b/docs/adr/0044-shipped-workloads-pass-the-restricted-pod-security-standard.md @@ -0,0 +1,80 @@ +--- +status: accepted +--- + +# Shipped workloads pass the `restricted` Pod Security Standard + +The shipped Deployments set no `securityContext`. A namespace that enforces the +`restricted` Pod Security Standard rejected the pods. Each shipped container +(backend, frontend, bundled Valkey) now sets `allowPrivilegeEscalation: false`, +drops all capabilities, sets `runAsNonRoot: true`, and uses the `RuntimeDefault` +seccomp profile. + +The profile does not require a read-only root filesystem. The backend and the +frontend set `readOnlyRootFilesystem: true` nevertheless, because each of them +needs only `/tmp` to be writable. The cost was different for each container: + +- **Backend.** The image already runs as UID 1001. With a read-only root + filesystem and no writable `/tmp`, the backend does not start: Vert.x creates + its file cache in `/tmp/vertx-cache` at start. The Deployment mounts an + `emptyDir` at `/tmp`. +- **Frontend.** The image used `nginx:alpine`. It ran as root, listened on + port 80, and wrote `env-config.js` into the static file directory at start. + The image now uses `nginxinc/nginx-unprivileged:alpine`. It runs as UID 101, + listens on port 8080, and keeps the nginx pid file and temp paths in `/tmp`. + The served `env-config.js` is a symbolic link to `/tmp/env-config.js`. The + Deployment mounts an `emptyDir` at `/tmp`. The Service still exposes port 80. +- **Valkey.** The upstream image has no `USER`. It starts as root and its + entrypoint changes to the `valkey` user, which needs capabilities. The + manifest sets `runAsUser: 999` and `runAsGroup: 1000`, the IDs of that user + in the alpine image. Its root filesystem stays writable. The profile does + not require a read-only one, and the default configuration writes snapshots + to `/data`. + +A PR check applies the quickstart overlay to a kind cluster as a server-side +dry run. Pod Security admission rejects only Pods in `enforce` mode, and a +Deployment with a bad pod template is admitted. Thus, the check labels the +namespace with `warn` also and runs `kubectl` with `--warnings-as-errors`. A +control Deployment with no `securityContext` must fail first, so the check +cannot pass on a cluster that does not evaluate the profile. + +## Considered Options + +- **Serve `env-config.js` from `/tmp` with an nginx `location` (rejected).** + The path would then be part of each nginx configuration. The compose quick + start mounts its own configuration over the one in the image, and it can pin + an image from before this change, which writes the file to the static file + directory. With the link, the configuration does not know where the file is, + so the two image generations work with the same configuration. +- **Create the link in the runtime stage (rejected).** The runtime stage has + no `RUN` step, so that a multi-arch build needs no emulation (ADR-0043). The + link is created in the build stage and copied. +- **Disable the Vert.x file cache instead of a writable `/tmp` (rejected).** + This is a change to the backend build, with behavior that must be proved in + the native image. An `emptyDir` is a change to the manifest only, and it + also covers other temp files. +- **Start the pods in the PR check (rejected for now).** It is the only proof + that the images run with these limits. But the backend image of a PR exists + only in the native jobs. The frontend image check starts the frontend with + the same limits (`--read-only`, `--tmpfs /tmp`, `--cap-drop ALL`, + `no-new-privileges`). The backend limits were proved once, by hand, on a + kind cluster that enforces the profile. + +## Consequences + +- The frontend image listens on port 8080, not port 80. This is a breaking + change for operators who run the image with their own manifest or port + mapping. The shipped Service keeps port 80, so Ingress and HTTPRoute rules + do not change. +- The manifests and the frontend image must come from the same release. The + manifests do not start an image from before this change, because that image + runs as root. The image pins in `deploy/k8s/base/kustomization.yaml` must + point at an image with this change before the manifests are released. +- The entrypoint of the nginx image runs `40-env-config.sh` only when the + container command starts with `nginx`. A command that does not start with + `nginx` leaves the link without a target. `compose.yml` thus starts nginx + through `/docker-entrypoint.sh`. +- The Valkey IDs are those of the `8-alpine` image. A different image can need + different IDs. +- The backend has no automatic check for the read-only root filesystem. A new + dependency that writes outside `/tmp` fails only in a cluster. diff --git a/docs/deployment.md b/docs/deployment.md index c534a636..487a00e2 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -176,6 +176,48 @@ patches: target: {kind: ClusterRoleBinding, name: platformup2date-read-workloads} ``` +### Pod security + +All shipped containers pass the `restricted` +[Pod Security Standard](https://kubernetes.io/docs/concepts/security/pod-security-standards/). +This applies to the releases after `v1.0.0-rc.8`. You can deploy these +manifests to a namespace that enforces the standard: + +```bash +kubectl label namespace my-namespace \ + pod-security.kubernetes.io/enforce=restricted +``` + +Each container sets `allowPrivilegeEscalation: false`, drops all capabilities, +sets `runAsNonRoot: true`, and uses the `RuntimeDefault` seccomp profile. See +[`docs/adr/0044`](adr/0044-shipped-workloads-pass-the-restricted-pod-security-standard.md). + +| Container | User | Root filesystem | Writable path | +|---|---|---|---| +| backend | UID 1001, from the image | read-only | `/tmp` (`emptyDir`) | +| frontend | UID 101, from the image | read-only | `/tmp` (`emptyDir`) | +| valkey | UID 999, GID 1000, from the manifest | writable | `/data` | + +- **Backend**: Vert.x creates its file cache in `/tmp` at start. Without a + writable `/tmp`, the backend does not start. +- **Frontend**: nginx listens on port 8080 in the container, because a + process that is not root cannot bind port 80. The Service still exposes + port 80. Thus, the routing contract and the examples on this page do not + change. `/tmp` holds the generated `env-config.js`, the nginx pid file, and + the nginx temp paths. +- **Valkey**: the upstream image starts as root and then changes to its + `valkey` user. The manifest starts the container as that user instead. If + you replace the image, make sure that the UID and the GID are still correct + for the new image. + +The frontend image and the manifests must come from the same release. The +manifests do not start a frontend image from before this change, because that +image runs as root. If you write your own Deployment for the frontend, use +`containerPort: 8080`. + +If your overlay adds a container, for example a sidecar, that container must +also pass the `restricted` profile. + ## What the backend needs (outside Kubernetes) You can run the backend on Docker, Podman, or another OCI runtime. The base @@ -505,3 +547,9 @@ The frontend image injects `API_BASE_URL` into `window._env_` at container start (`docker-entrypoint.d/40-env-config.sh`), not at build time. Set it as a standard environment variable on the container or Pod. Point it at the address where a browser can reach the backend. + +The two images do not run as root. The backend runs as UID 1001 and listens on +port 8080. The frontend runs unprivileged nginx as UID 101 and also listens on +port 8080 (for example, `docker run -p 3000:8080`). The two images can run +with a read-only root filesystem if `/tmp` is writable (for example, +`docker run --read-only --tmpfs /tmp`). diff --git a/frontend/Dockerfile b/frontend/Dockerfile index aed23905..4fb06935 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -12,13 +12,20 @@ COPY . . ARG BUILD_VERSION=dev ARG BUILD_VERSION_REQUIRED=false RUN BUILD_VERSION="$BUILD_VERSION" BUILD_VERSION_REQUIRED="$BUILD_VERSION_REQUIRED" yarn build +# The runtime config is written at container start, but the static files must +# stay read-only. Thus, the served path is a link into /tmp, the one writable +# path of the runtime image (docs/adr/0044). The link is made here, because +# the runtime stage has no RUN step (docs/adr/0043). +RUN ln -sf /tmp/env-config.js build/env-config.js -FROM nginx:alpine +# The unprivileged nginx image runs as UID 101, listens on 8080, and keeps its +# pid file and temp paths in /tmp. +FROM nginxinc/nginx-unprivileged:alpine COPY nginx.conf /etc/nginx/conf.d/default.conf COPY --from=build /app/build /usr/share/nginx/html COPY --chmod=755 docker-entrypoint.d/40-env-config.sh /docker-entrypoint.d/40-env-config.sh -EXPOSE 80 +EXPOSE 8080 CMD ["nginx", "-g", "daemon off;"] diff --git a/frontend/README.md b/frontend/README.md index 47381cf9..a8dda42e 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -56,7 +56,9 @@ bash ci/bin/test-build-version-image.sh # requires Docker, curl, and Node The image check builds local and required-injection release/edge variants, rejects missing/dev/invalid required injection, and checks both shipped nginx -configurations without publishing images. +configurations without publishing images. It also starts the image with the +limits of the shipped Deployment: a read-only root filesystem with a writable +`/tmp`, no capabilities, and a user that is not root. ## Runtime configuration @@ -71,6 +73,10 @@ these environment variables before nginx starts: | `OIDC_CLIENT_ID` | empty | Public client ID registered for the SPA. | | `OIDC_SCOPE` | `openid profile` | Space-separated scopes sent with the authorization request. | +The image runs unprivileged nginx as UID 101 and listens on port 8080. The +served `env-config.js` is a link to `/tmp/env-config.js`, so `/tmp` is the only +path that must be writable. + Web login is enabled only when both `OIDC_AUTHORITY` and `OIDC_CLIENT_ID` are non-blank. The settings are injected at container startup, so changing them does not require rebuilding the image. See diff --git a/frontend/ci/bin/test-build-version-image.sh b/frontend/ci/bin/test-build-version-image.sh index 063121af..cf95e9e2 100755 --- a/frontend/ci/bin/test-build-version-image.sh +++ b/frontend/ci/bin/test-build-version-image.sh @@ -40,8 +40,26 @@ assert.deepEqual(JSON.parse(readFileSync(process.argv[2], 'utf8')), { version: p JS } +# Starts a frontend container and waits for nginx. Sets $container and $url. +start_frontend() { + local port attempt + container="$(docker run -d --network "$NETWORK" -p 127.0.0.1::8080 "$@")" + containers+=("$container") + port="$(docker inspect -f '{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' "$container")" + url="http://127.0.0.1:$port" + for ((attempt = 0; attempt < 50; attempt++)); do + if curl --silent --fail --max-time 1 "$url/" >/dev/null; then break; fi + sleep 0.1 + done +} + +stop_frontend() { + docker rm -f "$container" >/dev/null + containers=() +} + check_image() { - local image=$1 version=$2 config container port url status + local image=$1 version=$2 config container url status local -a config_args for config in nginx.conf nginx.quickstart.conf; do # Test the image's own default config, and the quickstart bind-mount variant. @@ -51,16 +69,9 @@ check_image() { fi # No backend is attached to this private network. Runtime variables must not # replace embedded identity, even with the existing UI runtime-config hook. - container="$(docker run -d --network "$NETWORK" -p 127.0.0.1::80 \ + start_frontend \ -e BUILD_VERSION=9.9.9 -e BUILD_VERSION_REQUIRED=true -e API_BASE_URL=http://backend:8080 \ - "${config_args[@]}" "$image")" - containers+=("$container") - port="$(docker inspect -f '{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' "$container")" - url="http://127.0.0.1:$port" - for ((attempt = 0; attempt < 50; attempt++)); do - if curl --silent --fail --max-time 1 "$url/" >/dev/null; then break; fi - sleep 0.1 - done + "${config_args[@]}" "$image" assert_version "$url" "$version" [[ "$(request "$url/a-client-side-route")" == 200 ]] grep -qi '&2; return 1; } assert_version "$url" "$version" fi - docker exec "$container" rm /usr/share/nginx/html/version.json + # The image user cannot change the static files, so remove the file as root. + docker exec -u 0 "$container" rm /usr/share/nginx/html/version.json [[ "$(request "$url/version.json")" == 404 ]] grep -Eiq '^Cache-Control: no-store[[:space:]]*$' "$TMP_DIR/headers" if grep -q '
&2 return 1 fi - docker rm -f "$container" >/dev/null - containers=() + stop_frontend echo "PASS: $config reports $version, ignores runtime override, and returns 404 when missing" done } +# The limits of the frontend container in deploy/k8s/base: the image user is +# not root, the root filesystem is read-only, /tmp is the only writable path, +# and the container has no capabilities and no privilege escalation. +check_restricted_runtime() { + local image=$1 user container url + user="$(docker inspect -f '{{.Config.User}}' "$image")" + # runAsNonRoot needs a numeric UID. The kubelet cannot verify a user name. + [[ "$user" =~ ^[1-9][0-9]*$ ]] || { echo "Expected a numeric non-root image user, got '$user'" >&2; return 1; } + start_frontend --read-only --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges \ + -e API_BASE_URL=https://api.example.test -e OIDC_CLIENT_ID=pu2d-spa "$image" + [[ "$(request "$url/")" == 200 ]] + grep -qi ' /usr/share/nginx/html/env-config.js <