diff --git a/.github/scripts/assert-minimal-image.sh b/.github/scripts/assert-minimal-image.sh new file mode 100755 index 000000000..c58c851d2 --- /dev/null +++ b/.github/scripts/assert-minimal-image.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# +# Fails if a local image ships an SSH client or server, a shell, or a package +# manager, so a distro base and its OpenSSH (SSHole, CVE-2026-123158) can't +# return to the operator image unnoticed. Matches file names only. +# +# Usage: assert-minimal-image.sh +set -euo pipefail + +IMAGE="${1:?usage: $0 }" + +# The dummy command lets images without ENTRYPOINT or CMD be created; nothing runs. +container=$(docker create "$IMAGE" unused) +trap 'docker rm -f "$container" >/dev/null' EXIT + +# Listed separately so a failed export fails the check instead of passing it. +files=$(docker export "$container" | tar -tf -) +[[ -n "$files" ]] || { echo "::error::$IMAGE exported no files" >&2; exit 1; } + +# Directories end in "/", so only files and links can match; "(^|/)" also covers "./" prefixes. +rc=0 +found=$(grep -E \ + '(^|/)(ssh[-.a-z0-9]*|scp|sftp|sftp-server|dropbear[a-z]*|sh|ash|bash|rbash|dash|zsh|ksh|mksh|csh|tcsh|fish|busybox|toybox|apk|apk\.static|apt|apt-get|dpkg|rpm|dnf|tdnf|yum|microdnf|zypper|pacman|opkg)$' \ + <<<"$files") || rc=$? +# grep exits 1 for no match; anything above that is an error, not a pass. +(( rc <= 1 )) || { echo "::error::grep failed with exit $rc" >&2; exit "$rc"; } + +if [[ -n "$found" ]]; then + echo "::error::$IMAGE ships files a distroless image must not have:" >&2 + echo "$found" >&2 + exit 1 +fi +echo "OK: $IMAGE has no SSH, shell or package manager" diff --git a/.github/scripts/assert-minimal-image_test.sh b/.github/scripts/assert-minimal-image_test.sh new file mode 100755 index 000000000..b51d2cc84 --- /dev/null +++ b/.github/scripts/assert-minimal-image_test.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# +# Negative tests for assert-minimal-image.sh, using throwaway images built +# with `docker import` so no registry access is needed. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +WORK=$(mktemp -d) +# Unique per run so parallel runs on one daemon don't delete each other's images. +REPO="assert-minimal-image-test-$$" +cleanup() { + rm -rf "$WORK" + docker images -q "$REPO" | sort -u | xargs -r docker rmi -f >/dev/null +} +trap cleanup EXIT + +# make_image ... : image with an empty file at each path. No +# ENTRYPOINT or CMD, so every case also covers images that have neither. +make_image() { + local name="$REPO:$1" root="$WORK/$1"; shift + mkdir -p "$root/usr/sbin" "$root/tmp" + for p in "$@"; do mkdir -p "$root/$(dirname "$p")"; : > "$root/$p"; done + tar -C "$root" -c . | docker import - "$name" >/dev/null +} + +# check [path] : a clean image must pass; with the path added, the +# check must fail and name that path, so an unrelated error can't count. +check() { + local name="$1" path="${2:-}" out + if [[ -z "$path" ]]; then + make_image "$name" manager etc/ssl/certs/ca.pem usr/lib/rpm/macros var/cache/tdnf/x + bash "$SCRIPT_DIR/assert-minimal-image.sh" "$REPO:$name" >/dev/null + echo "PASS: clean image passes" + return + fi + make_image "$name" manager "$path" + if out=$(bash "$SCRIPT_DIR/assert-minimal-image.sh" "$REPO:$name" 2>&1); then + echo "FAIL: $path was not reported" >&2 + exit 1 + fi + # docker export may or may not prefix paths with "./". + if [[ $'\n'"$out"$'\n' != *$'\n'"$path"$'\n'* && $'\n'"$out"$'\n' != *$'\n'"./$path"$'\n'* ]]; then + printf 'FAIL: %s failed for another reason:\n%s\n' "$path" "$out" >&2 + exit 1 + fi + echo "PASS: $path reported" +} + +check clean +check sshd usr/sbin/sshd +check sshd-session usr/lib/ssh/sshd-session +check sshd-pam usr/sbin/sshd.pam +check ssh-keysign usr/lib/ssh/ssh-keysign +check ssh-client usr/bin/ssh +check dropbear usr/sbin/dropbear +check shell bin/sh +check mksh bin/mksh +check busybox bin/busybox +check toybox bin/toybox +check apk sbin/apk +check apk-static sbin/apk.static +check tdnf usr/bin/tdnf +check pacman usr/bin/pacman diff --git a/.github/workflows/build_operator_images.yml b/.github/workflows/build_operator_images.yml index 243647917..72067c22a 100644 --- a/.github/workflows/build_operator_images.yml +++ b/.github/workflows/build_operator_images.yml @@ -19,6 +19,7 @@ on: - 'operator/src/**' - 'operator/cnpg-plugins/**' - '.github/workflows/build_operator_images.yml' + - '.github/scripts/assert-minimal-image.sh' permissions: packages: write @@ -75,6 +76,7 @@ jobs: docker build --pull --build-arg ARCH=${{ matrix.base_arch }} \ -t $IMAGE \ -f ${{ matrix.image.dockerfile }} ${{ matrix.image.context }} + bash .github/scripts/assert-minimal-image.sh "$IMAGE" docker push $IMAGE # --------------------------------------------------------------------------- diff --git a/.github/workflows/test-build-and-package.yml b/.github/workflows/test-build-and-package.yml index 97c63e400..6778d1dc5 100644 --- a/.github/workflows/test-build-and-package.yml +++ b/.github/workflows/test-build-and-package.yml @@ -202,6 +202,12 @@ jobs: echo "✓ Operator Docker image built successfully for ${{ matrix.arch }}" + - name: Check operator image has no SSH, shell or package manager + run: | + bash .github/scripts/assert-minimal-image_test.sh + bash .github/scripts/assert-minimal-image.sh \ + ghcr.io/${{ github.repository_owner }}/documentdb-kubernetes-operator/operator:${{ env.IMAGE_TAG }}-${{ matrix.arch }} + - name: Save operator Docker image as artifact run: | echo "Saving operator ${{ matrix.arch }} Docker image as tar file..." @@ -863,20 +869,22 @@ jobs: steps: - name: Generate Build Summary run: | - echo "## Build Summary" >> $GITHUB_STEP_SUMMARY - echo "" >> $GITHUB_STEP_SUMMARY - echo "### Configuration:" >> $GITHUB_STEP_SUMMARY - echo "- **Version**: ${{ inputs.version }}" >> $GITHUB_STEP_SUMMARY - echo "- **Generated Tag**: ${{ env.VERSION }}" >> $GITHUB_STEP_SUMMARY - echo "- **Build Mode**: Platform-specific artifacts with architecture suffixes" >> $GITHUB_STEP_SUMMARY - echo "" >> $GITHUB_STEP_SUMMARY - - echo "### Built Artifacts:" >> $GITHUB_STEP_SUMMARY - echo "- **Operator Images**: Platform-specific (AMD64 + ARM64) with suffix tags" >> $GITHUB_STEP_SUMMARY - echo "- **Sidecar Images**: Platform-specific (AMD64 + ARM64) with suffix tags" >> $GITHUB_STEP_SUMMARY - echo "- **Helm Charts**: Platform-specific charts for AMD64 and ARM64" >> $GITHUB_STEP_SUMMARY - - echo "" >> $GITHUB_STEP_SUMMARY - echo "### Job Results:" >> $GITHUB_STEP_SUMMARY - echo "- **Consolidate Images**: ${{ needs.consolidate-images.result }}" >> $GITHUB_STEP_SUMMARY - echo "- **Create Charts**: ${{ needs.create-helm-chart.result }}" >> $GITHUB_STEP_SUMMARY + { + echo "## Build Summary" + echo "" + echo "### Configuration:" + echo "- **Version**: ${{ inputs.version }}" + echo "- **Generated Tag**: ${{ env.VERSION }}" + echo "- **Build Mode**: Platform-specific artifacts with architecture suffixes" + echo "" + + echo "### Built Artifacts:" + echo "- **Operator Images**: Platform-specific (AMD64 + ARM64) with suffix tags" + echo "- **Sidecar Images**: Platform-specific (AMD64 + ARM64) with suffix tags" + echo "- **Helm Charts**: Platform-specific charts for AMD64 and ARM64" + + echo "" + echo "### Job Results:" + echo "- **Consolidate Images**: ${{ needs.consolidate-images.result }}" + echo "- **Create Charts**: ${{ needs.create-helm-chart.result }}" + } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index f7c5b47ca..038e1a8ca 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -27,6 +27,8 @@ on: - '.github/actions/**' - '.github/dockerfiles/Dockerfile_combined' - '.github/scripts/assert-combined-image.sh' + - '.github/scripts/assert-minimal-image.sh' + - '.github/scripts/assert-minimal-image_test.sh' - '.github/scripts/resolve-documentdb-version.sh' workflow_dispatch: inputs: diff --git a/CHANGELOG.md b/CHANGELOG.md index c0365a812..3d15a1166 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +### Security +- The operator image moves from the Alpine dev container base to Azure Linux distroless, so it no longer ships OpenSSH (SSHole, CVE-2026-123158), BusyBox, `apk`, `sudo` or `git`. Earlier images ship only the OpenSSH client and never run `sshd`, so they were not exploitable. The image no longer has a shell; use `kubectl debug` to troubleshoot the operator pod. + ## [1.2.1] - 2026-11-20 Security release. The default DocumentDB engine moves from `1.0.0` to `1.0.1`: the same engine (extension schema `1.0-0`) rebuilt on patched Debian packages. Unpinned clusters roll onto the new images when the operator is upgraded; there is no schema migration. The CRDs are unchanged from `1.2.0`; when upgrading from `1.1.x` or earlier, apply the `1.2.1` CRDs before `helm upgrade`. diff --git a/docs/designs/image-management.md b/docs/designs/image-management.md index d170dc9ef..e725e1c61 100644 --- a/docs/designs/image-management.md +++ b/docs/designs/image-management.md @@ -235,8 +235,8 @@ The build process: ### Dockerfile Details #### Operator (`operator/src/Dockerfile`) -- **Base**: `mcr.microsoft.com/oss/go/microsoft/golang:1.26.11-azurelinux3.0` → `scratch` -- **Multi-stage**: 2 stages (builder → scratch) +- **Base**: `mcr.microsoft.com/oss/go/microsoft/golang:1.26.11-azurelinux3.0` → `mcr.microsoft.com/azurelinux/distroless/minimal:3.0` +- **Multi-stage**: 2 stages (builder → distroless, which keeps CA certificates, tzdata and `/tmp`) - **Entrypoint**: `/manager` #### Sidecar (`operator/cnpg-plugins/sidecar-injector/Dockerfile`) diff --git a/operator/documentdb-helm-chart/values.yaml b/operator/documentdb-helm-chart/values.yaml index 69fe152b7..7bb00f709 100644 --- a/operator/documentdb-helm-chart/values.yaml +++ b/operator/documentdb-helm-chart/values.yaml @@ -147,9 +147,9 @@ operator: seccompProfile: type: RuntimeDefault containerSecurityContext: - # The operator image (operator/src/Dockerfile) runs as the Alpine `manager` - # user, which is uid 100. We pin runAsUser explicitly so Kubernetes can - # verify the user is non-root without depending on the image's USER directive. + # The operator image (operator/src/Dockerfile) runs as USER 100:101. We pin + # runAsUser explicitly so Kubernetes can verify the user is non-root + # without depending on the image's USER directive. # If the operator Dockerfile is changed to use a different uid, update this # value in lockstep with the appVersion bump. runAsUser: 100 diff --git a/operator/src/Dockerfile b/operator/src/Dockerfile index 96803cece..4152e5f6a 100644 --- a/operator/src/Dockerfile +++ b/operator/src/Dockerfile @@ -28,13 +28,12 @@ COPY internal/ internal/ # The ARCH build arg can be used to override the target architecture (e.g., amd64, arm64) RUN GOEXPERIMENT=nosystemcrypto CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${ARCH:-${TARGETARCH}} go build -a -o manager cmd/main.go -# Use Microsoft official image from MCR as minimal base image to package the manager binary -FROM --platform=${TARGETOS:-linux}/${ARCH:-${TARGETARCH}} mcr.microsoft.com/devcontainers/base:alpine +# The manager is static, so the runtime image needs no shell, package manager +# or SSH, only CA certificates, tzdata and /tmp. +FROM --platform=${TARGETOS:-linux}/${ARCH:-${TARGETARCH}} mcr.microsoft.com/azurelinux/distroless/minimal:3.0 WORKDIR / -# Pick up package fixes published since the base image was built. -RUN apk upgrade --no-cache COPY --from=builder /workspace/manager . -RUN addgroup -S manager && adduser -S manager -G manager -USER manager +# Same uid/gid as the old Alpine `manager` user; the chart pins runAsUser/runAsGroup to it. +USER 100:101 ENTRYPOINT ["/manager"]