diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index d62e746..c9b2a41 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -1,11 +1,21 @@ name: CI/CD -on: [push, workflow_dispatch] +on: + push: + branches: [master] + pull_request: + workflow_dispatch: + schedule: + - cron: '23 5 * * 1' + +permissions: + contents: read jobs: build: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: version: [php, proxy, static] @@ -16,32 +26,43 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4.2.0 - - name: Cache Docker layers - uses: actions/cache@v6.1.0 - with: - path: /tmp/.buildx-cache - key: docker-buildx-${{ matrix.version }}-${{ github.sha }} - restore-keys: docker-buildx-${{ matrix.version }}- - - name: Build run: | docker buildx build \ - --cache-from type=local,src=/tmp/.buildx-cache/${{ matrix.version }} \ - --cache-to type=local,dest=/tmp/.buildx-cache/${{ matrix.version }} \ + --pull --no-cache \ + --platform linux/amd64 \ --output type=docker \ --tag kooldev/nginx:${{ matrix.version }} \ ${{ matrix.version }} - name: Tests - run: docker run kooldev/nginx:${{ matrix.version }} nginx -v + run: bash tests/smoke.sh "${{ matrix.version }}" "kooldev/nginx:${{ matrix.version }}" + + - name: Scan image + uses: anchore/scan-action@8576240a41341d866dbd7ebc72cd9fc0ef023f54 # v7.4.2 + with: + image: kooldev/nginx:${{ matrix.version }} + grype-version: v0.121.3 + severity-cutoff: high + only-fixed: false + output-format: json + output-file: grype-${{ matrix.version }}.json + + - name: Retain vulnerability report + if: always() + uses: actions/upload-artifact@v7.0.2 + with: + name: grype-${{ matrix.version }} + path: grype-${{ matrix.version }}.json + if-no-files-found: warn - name: Login to DockerHub uses: docker/login-action@v4.4.0 - if: github.ref == 'refs/heads/master' && github.repository == 'kool-dev/docker-nginx' + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/master' && github.repository == 'kool-dev/docker-nginx' with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Push to DockerHub - if: github.ref == 'refs/heads/master' && github.repository == 'kool-dev/docker-nginx' + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/master' && github.repository == 'kool-dev/docker-nginx' run: docker push kooldev/nginx:${{ matrix.version }} diff --git a/README.md b/README.md index b9155d1..581f96c 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,31 @@ app: ## Contributing +### Image checks + +CI builds all three variants for **linux/amd64**. ARM is not supported by the +bundled dockerize binary. Weekly Monday rebuilds pull the latest `nginx:alpine` +and refresh Alpine packages without reusing build layers. + +Run the same configuration and HTTP checks locally after building an image: + +```sh +docker build --pull --no-cache -t nginx-test:php php +bash tests/smoke.sh php nginx-test:php +``` + +Repeat for `static` and `proxy`. Tests require Docker and curl, publish only a +random loopback port, and remove their containers/network afterward. PHP uses a +real PHP-FPM fixture; proxy uses a Docker DNS upstream. Tests check default and +overridden config, invalid config rejection, static assets, routing/query +handling, denied `.ht` files and request body limits. + +Before publication, Grype blocks high/critical findings (including unfixed ones). +Full JSON reports, including lower severities, are retained as workflow artifacts. +There are no vulnerability exclusions. Scan failures require investigation and +an image/package fix, not disabling the gate. A successful scan is not proof +that an image is vulnerability-free. + ### Dependencies You should change `fwd-template.json` and `template` folder. diff --git a/php/Dockerfile b/php/Dockerfile index f4ef761..56b83ad 100644 --- a/php/Dockerfile +++ b/php/Dockerfile @@ -1,5 +1,8 @@ FROM nginx:alpine +# Refresh security fixes that have not yet reached the upstream image. +RUN apk upgrade --no-cache + ARG DOCKERIZE_VERSION=v0.6.1 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ diff --git a/proxy/Dockerfile b/proxy/Dockerfile index 7c79e72..d6dfe90 100644 --- a/proxy/Dockerfile +++ b/proxy/Dockerfile @@ -1,5 +1,8 @@ FROM nginx:alpine +# Refresh security fixes that have not yet reached the upstream image. +RUN apk upgrade --no-cache + ARG DOCKERIZE_VERSION=v0.6.1 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ diff --git a/static/Dockerfile b/static/Dockerfile index a296b1c..507563a 100644 --- a/static/Dockerfile +++ b/static/Dockerfile @@ -1,5 +1,8 @@ FROM nginx:alpine +# Refresh security fixes that have not yet reached the upstream image. +RUN apk upgrade --no-cache + ARG DOCKERIZE_VERSION=v0.6.1 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ diff --git a/template/Dockerfile.blade.php b/template/Dockerfile.blade.php index ee8e059..b1993f4 100644 --- a/template/Dockerfile.blade.php +++ b/template/Dockerfile.blade.php @@ -1,5 +1,8 @@ FROM nginx:alpine +# Refresh security fixes that have not yet reached the upstream image. +RUN apk upgrade --no-cache + ARG DOCKERIZE_VERSION=v0.6.1 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ diff --git a/tests/fixtures/.htpasswd b/tests/fixtures/.htpasswd new file mode 100644 index 0000000..cdc5d54 --- /dev/null +++ b/tests/fixtures/.htpasswd @@ -0,0 +1 @@ +must not be served diff --git a/tests/fixtures/asset.txt b/tests/fixtures/asset.txt new file mode 100644 index 0000000..a737a3d --- /dev/null +++ b/tests/fixtures/asset.txt @@ -0,0 +1 @@ +kool asset fixture diff --git a/tests/fixtures/front.php b/tests/fixtures/front.php new file mode 100644 index 0000000..380d305 --- /dev/null +++ b/tests/fixtures/front.php @@ -0,0 +1,3 @@ +/dev/null || true + docker logs "$backend" 2>/dev/null || true + fi + docker rm -f "$name" "$backend" >/dev/null 2>&1 || true + docker network rm "$network" >/dev/null 2>&1 || true + rm -rf "$tmp" + exit "$status" +} +trap cleanup EXIT + +docker network create "$network" >/dev/null +if [[ $variant == php ]]; then + docker run -d --pull always --name "$backend" --network "$network" --network-alias app \ + -v "$fixtures:/srv/site:ro" php:8.4-fpm-alpine >/dev/null +elif [[ $variant == proxy ]]; then + docker run -d --pull always --name "$backend" --network "$network" --network-alias app \ + -v "$fixtures:/usr/share/nginx/html:ro" nginx:alpine >/dev/null +fi + +# Validate the default rendered configuration (PHP needs a resolvable upstream). +docker run --rm --network "$network" "$image" nginx -v +docker run --rm --network "$network" "$image" nginx -t + +env_args=(-e LISTEN=8080 -e DOMAIN=example.test -e CLIENT_MAX_BODY_SIZE=1k) +if [[ $variant != proxy ]]; then + env_args+=(-e ROOT=/srv/site) +fi +if [[ $variant == php ]]; then + env_args+=(-e INDEX=front.php -e PHP_FPM=app:9000 -e FASTCGI_READ_TIMEOUT=10s + -e 'FASTCGI_BUFFERS=4 16k' -e FASTCGI_BUFFER_SIZE=16k) +fi +docker run --rm --network "$network" "${env_args[@]}" "$image" nginx -t + +# Invalid configuration must fail, rather than accidentally testing nginx -v again. +if docker run --rm --network "$network" -e LISTEN=not-a-port "$image" nginx -t; then + echo "Invalid LISTEN unexpectedly accepted" >&2 + exit 1 +fi + +docker run -d --name "$name" --network "$network" "${env_args[@]}" \ + -p 127.0.0.1::8080 -v "$fixtures:/srv/site:ro" "$image" >/dev/null +port=$(docker port "$name" 8080/tcp | cut -d: -f2) +url="http://127.0.0.1:$port" +host=example.test +[[ $variant != proxy ]] || host=app.example.test +request() { + curl --silent --show-error --max-time 5 -H "Host: $host" "$@" +} +ready=false +for attempt in {1..30}; do + if request --fail "$url/asset.txt" >"$tmp/body"; then + ready=true + break + fi + echo "Waiting for $variant HTTP readiness ($attempt/30)" + sleep 1 +done +[[ $ready == true ]] +grep -qx 'kool asset fixture' "$tmp/body" + +case "$variant" in + php) + request --fail "$url/missing-route?value=query-ok" >"$tmp/body" + grep -qx 'kool php fixture|query-ok|/srv/site/front.php' "$tmp/body" + ;; + static) + request --fail -D "$tmp/headers" "$url/missing-route" >"$tmp/body" + grep -qx 'kool static fixture' "$tmp/body" + grep -qi '^X-Served-By: kool.dev' "$tmp/headers" + ;; + proxy) + request --fail "$url/" >"$tmp/body" + grep -qx 'kool static fixture' "$tmp/body" + [[ $(request -o /dev/null -w '%{http_code}' "$url/missing-route") == 404 ]] + ;; +esac +[[ $(request -o /dev/null -w '%{http_code}' "$url/.htpasswd") == 403 ]] +head -c 2048 /dev/zero >"$tmp/large-body" +[[ $(request -o /dev/null -w '%{http_code}' --data-binary @"$tmp/large-body" "$url/") == 413 ]] +echo "$variant: configuration and HTTP tests passed"