Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
15 changes: 12 additions & 3 deletions ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <image-ref>...
bash ci/check-restricted-pod-security.sh
```

- `resolve-build-version.sh release v<semver>` validates strict SemVer and
Expand All @@ -23,9 +24,11 @@ bash ci/check-image-platforms.sh linux/amd64,linux/arm64 <image-ref>...
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,
Expand All @@ -34,6 +37,12 @@ bash ci/check-image-platforms.sh linux/amd64,linux/arm64 <image-ref>...
- `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
Expand Down
51 changes: 51 additions & 0 deletions ci/check-restricted-pod-security.sh
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion compose.quickstart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions deploy/k8s/base/backend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: {}
23 changes: 22 additions & 1 deletion deploy/k8s/base/frontend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {}
13 changes: 13 additions & 0 deletions deploy/k8s/components/valkey/valkey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
48 changes: 48 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`).
Loading
Loading