From d3dd5d0095ac9305cc19d44fb01507f8bd697ca1 Mon Sep 17 00:00:00 2001 From: David Collom Date: Wed, 18 Nov 2026 13:17:37 +0000 Subject: [PATCH] Harden Dockerfile, manifests and Helm chart defaults Image and manifests: - Pin both Dockerfile base images by digest and run as UID/GID 65534 rather than root. - deploy/yaml/deploy.yaml gains a restrictive securityContext (non-root, read-only root filesystem, no capabilities, RuntimeDefault seccomp) and resource requests and a memory limit, and no longer forces a pull on a pinned tag. Helm chart: - Default resources to requests of 100m CPU / 128Mi and a 512Mi memory limit. Raise the limit for clusters with many thousands of pods, which are all cached. - Add an optional NetworkPolicy (networkPolicy.enabled / ingressFrom) for the unauthenticated metrics port, following metricsServingAddress. - Add versionChecker.fallbackAllowPrivateHosts and ecr.allowCrossAccount for the new opt-out flags. - Map ghcr.hostname and selfhosted token_path to the environment: both were written to the Secret but never passed to the container. - Bump the Prometheus used by the chart's optional Prometheus resource from v2.20.1 to v2.55.1. - Regenerate the chart README, add tests for the above, and document the registry access restrictions in docs/installation.md. The metrics endpoint itself is still unauthenticated; the NetworkPolicy limits who can reach it. Signed-off-by: David Collom --- Dockerfile | 6 ++- deploy/charts/version-checker/README.md | 9 ++-- .../templates/_pod_helpers.tpl | 20 ++++++++ .../templates/networkpolicy.yaml | 24 +++++++++ .../version-checker/templates/prometheus.yaml | 2 +- .../version-checker/templates/secret.yaml | 3 ++ .../tests/deployment_test.yaml | 50 +++++++++++++++++-- .../tests/networkpolicy_test.yaml | 34 +++++++++++++ .../tests/prometheus_test.yaml | 2 +- deploy/charts/version-checker/values.yaml | 27 ++++++---- deploy/yaml/deploy.yaml | 17 ++++++- docs/installation.md | 9 ++++ 12 files changed, 183 insertions(+), 20 deletions(-) create mode 100644 deploy/charts/version-checker/templates/networkpolicy.yaml create mode 100644 deploy/charts/version-checker/tests/networkpolicy_test.yaml diff --git a/Dockerfile b/Dockerfile index 90354036..f0b94c9b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=$BUILDPLATFORM golang:1.27-alpine AS builder +FROM --platform=$BUILDPLATFORM golang:1.27-alpine@sha256:70bd7ddea8b6cbe5d6943cde279d6b67feb10cc924fb9100d1d8757c1575b280 AS builder COPY . /app/ WORKDIR /app/ @@ -9,11 +9,13 @@ ARG TARGETOS TARGETARCH RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o ./bin/version-checker ./cmd/. -FROM alpine:3.24 +FROM alpine:3.24@sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf LABEL description="Kubernetes utility for exposing used image versions compared to the latest version, as metrics." RUN apk --no-cache upgrade && apk --no-cache add ca-certificates COPY --from=builder /app/bin/version-checker /usr/bin/version-checker +USER 65534:65534 + ENTRYPOINT ["/usr/bin/version-checker"] diff --git a/deploy/charts/version-checker/README.md b/deploy/charts/version-checker/README.md index cb7e9a4a..8d897c1d 100644 --- a/deploy/charts/version-checker/README.md +++ b/deploy/charts/version-checker/README.md @@ -30,6 +30,7 @@ A Helm chart for version-checker | docker.token | string | `nil` | Token to authenticate with docker registry. Cannot be used with `docker.username` / `docker.password`. | | docker.username | string | `nil` | Username to authenticate with docker registry | | ecr.accessKeyID | string | `nil` | ECR access key ID for read access to private registries | +| ecr.allowCrossAccount | bool | `false` | Allow lookups in ECR registries owned by other AWS accounts than the one version-checker authenticates as. Off by default. | | ecr.iamRoleArn | string | `nil` | Provide AWS EKS Iam Role ARN following: [Specify A ServiceAccount Role](https://docs.aws.amazon.com/eks/latest/userguide/specify-service-account-role.html) | | ecr.secretAccessKey | string | `nil` | ECR secret access key for read access to private registries | | ecr.sessionToken | string | `nil` | ECR session token for read access to private registries | @@ -50,6 +51,9 @@ A Helm chart for version-checker | livenessProbe.initialDelaySeconds | int | `3` | Number of seconds after the container has started before liveness probes are initiated. | | livenessProbe.periodSeconds | int | `3` | How often (in seconds) to perform the livenessProbe. | | nameOverride | string | `""` | Override the Chart Name | +| networkPolicy | object | `{"enabled":false,"ingressFrom":[]}` | Restrict ingress to the metrics port with a NetworkPolicy (the metrics endpoint is unauthenticated) | +| networkPolicy.enabled | bool | `false` | Create a NetworkPolicy | +| networkPolicy.ingressFrom | list | `[]` | Peers (NetworkPolicyPeer list) allowed to reach the metrics port, e.g. your Prometheus. Empty allows no ingress. | | nodeSelector | object | `{}` | Configure nodeSelector | | podSecurityContext | object | `{}` | Set pod-level security context | | prometheus.enabled | bool | `false` | Deploy a Prometheus-Operator Prometheus Object to collect version-checker metrics | @@ -62,7 +66,7 @@ A Helm chart for version-checker | readinessProbe.initialDelaySeconds | int | `3` | Number of seconds after the container has started before readiness probes are initiated. | | readinessProbe.periodSeconds | int | `3` | How often (in seconds) to perform the readinessProbe. | | replicaCount | int | `1` | Replica Count for version-checker | -| resources | object | `{}` | Setup version-checkers resource requests/limits | +| resources | object | `{"limits":{"memory":"512Mi"},"requests":{"cpu":"100m","memory":"128Mi"}}` | Setup version-checkers resource requests/limits. Raise the memory limit for clusters with many thousands of pods (they are all cached). | | securityContext | object | `{"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]},"readOnlyRootFilesystem":true,"runAsNonRoot":true,"runAsUser":65534,"seccompProfile":{"type":"RuntimeDefault"}}` | Set container-level security context | | securityContext.allowPrivilegeEscalation | bool | `false` | Prevent the container from PrivilegeEscalation | | securityContext.capabilities | object | `{"drop":["ALL"]}` | Ensure that we run with the capabilities we explicitly need to run | @@ -78,10 +82,9 @@ A Helm chart for version-checker | serviceMonitor.enabled | bool | `true` | Disable/Enable ServiceMonitor Object | | tolerations | list | `[]` | Configure tolerations | | topologySpreadConstraints | list | `[]` | Set topologySpreadConstraints | +| versionChecker.fallbackAllowPrivateHosts | bool | `false` | Allow anonymous lookups against registries on loopback, private or link-local addresses (e.g. an in-cluster registry). Off by default so pod annotations and image hosts cannot make version-checker call internal services. | | versionChecker.imageCacheTimeout | string | `"30m"` | How long to hold on to image tags and their versions | | versionChecker.logLevel | string | `"info"` | Configure version-checkers logging, valid options are: debug, info, warn, error, fatal, panic | | versionChecker.metricsServingAddress | string | `"0.0.0.0:8080"` | Port/interface to which version-checker should bind too | | versionChecker.testAllContainers | bool | `true` | Enable/Disable the requirement for an enable.version-checker.io annotation on pods. | ----------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.14.2](https://github.com/norwoodj/helm-docs/releases/v1.14.2) diff --git a/deploy/charts/version-checker/templates/_pod_helpers.tpl b/deploy/charts/version-checker/templates/_pod_helpers.tpl index adfd4402..9dc1fb85 100644 --- a/deploy/charts/version-checker/templates/_pod_helpers.tpl +++ b/deploy/charts/version-checker/templates/_pod_helpers.tpl @@ -3,6 +3,12 @@ - "--log-level={{.Values.versionChecker.logLevel}}" - "--metrics-serving-address={{.Values.versionChecker.metricsServingAddress}}" - "--test-all-containers={{.Values.versionChecker.testAllContainers}}" +{{- if .Values.versionChecker.fallbackAllowPrivateHosts }} +- "--fallback-allow-private-hosts=true" +{{- end }} +{{- if .Values.ecr.allowCrossAccount }} +- "--ecr-allow-cross-account=true" +{{- end }} {{- end -}} {{- define "version-checker.pod.envs.selfhosted" -}} @@ -37,6 +43,13 @@ name: {{ $chartname }} key: selfhosted.{{ $element.name }}.token {{- end -}} + {{- if and (hasKey $element "token_path") $element.token_path }} + - name: VERSION_CHECKER_SELFHOSTED_TOKEN_PATH_{{ $element.name }} + valueFrom: + secretKeyRef: + name: {{ $chartname }} + key: selfhosted.{{ $element.name }}.token_path + {{- end -}} {{- if and (hasKey $element "ca_path") $element.ca_path }} - name: VERSION_CHECKER_SELFHOSTED_CA_PATH_{{ $element.name }} valueFrom: @@ -153,6 +166,13 @@ secretKeyRef: name: {{ $chartname }} key: ghcr.token + {{- end }} + {{- if .Values.ghcr.hostname }} + - name: VERSION_CHECKER_GHCR_HOSTNAME + valueFrom: + secretKeyRef: + name: {{ $chartname }} + key: ghcr.hostname {{- end -}} {{- end -}} diff --git a/deploy/charts/version-checker/templates/networkpolicy.yaml b/deploy/charts/version-checker/templates/networkpolicy.yaml new file mode 100644 index 00000000..754fa2c1 --- /dev/null +++ b/deploy/charts/version-checker/templates/networkpolicy.yaml @@ -0,0 +1,24 @@ +{{- if .Values.networkPolicy.enabled }} +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "version-checker.name" . }} + labels: + {{- include "version-checker.labels" . | nindent 4 }} +spec: + podSelector: + matchLabels: + {{- include "version-checker.selector" . | nindent 6 }} + policyTypes: + - Ingress + ingress: + {{- with .Values.networkPolicy.ingressFrom }} + - from: + {{- toYaml . | nindent 8 }} + ports: + - port: {{ splitList ":" $.Values.versionChecker.metricsServingAddress | last }} + protocol: TCP + {{- else }} + [] + {{- end }} +{{- end }} diff --git a/deploy/charts/version-checker/templates/prometheus.yaml b/deploy/charts/version-checker/templates/prometheus.yaml index cede0a48..3aa5bbcb 100644 --- a/deploy/charts/version-checker/templates/prometheus.yaml +++ b/deploy/charts/version-checker/templates/prometheus.yaml @@ -43,7 +43,7 @@ metadata: spec: replicas: {{ .Values.prometheus.replicas }} serviceAccountName: {{ .Values.prometheus.serviceAccountName }} - version: v2.20.1 + version: v2.55.1 serviceMonitorSelector: matchLabels: app: {{ include "version-checker.name" . }} diff --git a/deploy/charts/version-checker/templates/secret.yaml b/deploy/charts/version-checker/templates/secret.yaml index f918c7aa..1022bc1d 100644 --- a/deploy/charts/version-checker/templates/secret.yaml +++ b/deploy/charts/version-checker/templates/secret.yaml @@ -69,6 +69,9 @@ data: {{- end }} {{- if and (hasKey $element "insecure") $element.insecure }} selfhosted.{{ $element.name }}.insecure: {{ $element.insecure | b64enc }} + {{- end }} + {{- if and (hasKey $element "token_path") $element.token_path }} + selfhosted.{{ $element.name }}.token_path: {{ $element.token_path | b64enc }} {{- end }} {{- if and (hasKey $element "ca_path") $element.ca_path }} selfhosted.{{ $element.name }}.ca_path: {{ $element.ca_path | b64enc }} diff --git a/deploy/charts/version-checker/tests/deployment_test.yaml b/deploy/charts/version-checker/tests/deployment_test.yaml index 31e1122c..46fab255 100644 --- a/deploy/charts/version-checker/tests/deployment_test.yaml +++ b/deploy/charts/version-checker/tests/deployment_test.yaml @@ -16,7 +16,12 @@ tests: value: quay.io/jetstack/version-checker:latest - equal: path: spec.template.spec.containers[0].resources - value: {} + value: + limits: + memory: 512Mi + requests: + cpu: 100m + memory: 128Mi - equal: path: spec.template.metadata.labels["app.kubernetes.io/name"] value: version-checker @@ -105,6 +110,30 @@ tests: count: 1 content: "--test-all-containers=false" + - it: fallbackAllowPrivateHosts is off by default + asserts: + - notContains: + path: spec.template.spec.containers[0].args + content: "--fallback-allow-private-hosts=true" + + - it: fallbackAllowPrivateHosts + set: + versionChecker.fallbackAllowPrivateHosts: true + asserts: + - contains: + path: spec.template.spec.containers[0].args + count: 1 + content: "--fallback-allow-private-hosts=true" + + - it: ecr.allowCrossAccount + set: + ecr.allowCrossAccount: true + asserts: + - contains: + path: spec.template.spec.containers[0].args + count: 1 + content: "--ecr-allow-cross-account=true" + # ACR - it: ACR should work set: @@ -247,6 +276,20 @@ tests: key: ghcr.token name: version-checker + - it: GHCR hostname + set: + ghcr.hostname: ghcr.example.com + asserts: + - contains: + path: spec.template.spec.containers[0].env + count: 1 + content: + name: VERSION_CHECKER_GHCR_HOSTNAME + valueFrom: + secretKeyRef: + key: ghcr.hostname + name: version-checker + # Quay - it: Quay should work set: @@ -488,14 +531,15 @@ tests: resources: limits: cpu: 100m - memory: 512Mi + memory: 1Gi + requests: null asserts: - equal: path: spec.template.spec.containers[0].resources value: limits: cpu: 100m - memory: 512Mi + memory: 1Gi - it: Custom Labels to Pod set: diff --git a/deploy/charts/version-checker/tests/networkpolicy_test.yaml b/deploy/charts/version-checker/tests/networkpolicy_test.yaml new file mode 100644 index 00000000..51eefa0e --- /dev/null +++ b/deploy/charts/version-checker/tests/networkpolicy_test.yaml @@ -0,0 +1,34 @@ +suite: test networkpolicy +templates: + - networkpolicy.yaml +tests: + - it: is not created by default + asserts: + - hasDocuments: + count: 0 + + - it: denies all ingress when no peers are given + set: + networkPolicy.enabled: true + asserts: + - isKind: + of: NetworkPolicy + - equal: + path: spec.ingress + value: [] + + - it: allows the configured peers on the metrics port + set: + networkPolicy.enabled: true + versionChecker.metricsServingAddress: 0.0.0.0:9999 + networkPolicy.ingressFrom: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: monitoring + asserts: + - equal: + path: spec.ingress[0].ports[0].port + value: 9999 + - equal: + path: spec.ingress[0].from[0].namespaceSelector.matchLabels["kubernetes.io/metadata.name"] + value: monitoring diff --git a/deploy/charts/version-checker/tests/prometheus_test.yaml b/deploy/charts/version-checker/tests/prometheus_test.yaml index e24035c9..cf3638d5 100644 --- a/deploy/charts/version-checker/tests/prometheus_test.yaml +++ b/deploy/charts/version-checker/tests/prometheus_test.yaml @@ -60,7 +60,7 @@ tests: - documentIndex: 3 equal: path: spec.version - value: v2.20.1 + value: v2.55.1 - documentIndex: 3 equal: path: spec.serviceMonitorSelector.matchLabels diff --git a/deploy/charts/version-checker/values.yaml b/deploy/charts/version-checker/values.yaml index 46ce5364..e3932ebb 100644 --- a/deploy/charts/version-checker/values.yaml +++ b/deploy/charts/version-checker/values.yaml @@ -48,6 +48,8 @@ versionChecker: metricsServingAddress: 0.0.0.0:8080 # -- Enable/Disable the requirement for an enable.version-checker.io annotation on pods. testAllContainers: true + # -- Allow anonymous lookups against registries on loopback, private or link-local addresses (e.g. an in-cluster registry). Off by default so pod annotations and image hosts cannot make version-checker call internal services. + fallbackAllowPrivateHosts: false # Azure Container Registry Credentials Configuration acr: @@ -78,6 +80,8 @@ ecr: secretAccessKey: # -- (string) ECR session token for read access to private registries sessionToken: + # -- Allow lookups in ECR registries owned by other AWS accounts than the one version-checker authenticates as. Off by default. + allowCrossAccount: false # Google Container Registry Credentials Configuration gcr: @@ -110,6 +114,7 @@ selfhosted: # username: # password: # token: + # token_path: /v2/token #- name: EXAMPLE # host: https://artifactory.example.com # username: foo @@ -121,16 +126,20 @@ selfhosted: # -- Provide an existing Secret within the cluster to use for authentication and configuration of version-checker existingSecret: "" -# -- Setup version-checkers resource requests/limits +# -- Setup version-checkers resource requests/limits. Raise the memory limit for clusters with many thousands of pods (they are all cached). resources: - {} - # limits: - # cpu: 100m - # memory: 128Mi - # requests: - # cpu: 100m - # memory: 128Mi - # + limits: + memory: 512Mi + requests: + cpu: 100m + memory: 128Mi + +# -- Restrict ingress to the metrics port with a NetworkPolicy (the metrics endpoint is unauthenticated) +networkPolicy: + # -- Create a NetworkPolicy + enabled: false + # -- Peers (NetworkPolicyPeer list) allowed to reach the metrics port, e.g. your Prometheus. Empty allows no ingress. + ingressFrom: [] # -- Set container-level security context securityContext: diff --git a/deploy/yaml/deploy.yaml b/deploy/yaml/deploy.yaml index 738a2ffa..95888392 100644 --- a/deploy/yaml/deploy.yaml +++ b/deploy/yaml/deploy.yaml @@ -51,7 +51,22 @@ spec: serviceAccountName: version-checker containers: - image: quay.io/jetstack/version-checker:v0.11.0 - imagePullPolicy: Always + imagePullPolicy: IfNotPresent + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: ["ALL"] + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 65534 + seccompProfile: + type: RuntimeDefault + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + memory: 512Mi ports: - containerPort: 8080 name: version-checker diff --git a/docs/installation.md b/docs/installation.md index 42b91e81..d3c33e4b 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -45,6 +45,15 @@ of the container in the pod. However, by passing the following flag,`-a, --test-all-containers` version-checker will test all containers within the cluster. +### Registry access restrictions +Image hosts and the `override-url.version-checker.io` annotation come from pod specs, so version-checker restricts where it will connect: + +- Anonymous lookups (images whose registry is not one of the configured ones) refuse loopback, private, link-local and CGNAT addresses, including redirects to them. An in-cluster registry that needs this can either be configured as a `selfhosted` registry or allowed with `--fallback-allow-private-hosts` (`versionChecker.fallbackAllowPrivateHosts` in the Helm chart). +- ECR lookups only target the AWS account version-checker authenticates as. Registries in other accounts need `--ecr-allow-cross-account` (`ecr.allowCrossAccount`). +- Selfhosted registry `host` values must be full URLs, e.g. `https://registry.example.com`. + +The metrics endpoint is unauthenticated. Set `networkPolicy.enabled=true` and `networkPolicy.ingressFrom` in the Helm chart to limit who can reach it. + ### Supported Annotations `version-checker` supports the following annotations to enrich version checking on image tags: