From 6ef5139ad9df3afcacac098909f758b14a068b1c Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 5 Dec 2026 12:13:09 +0100 Subject: [PATCH 1/5] Improve the Helm chart Add a ServiceAccount, a values schema, a guard against more than one replica on SQLite, a PodDisruptionBudget, a Gateway API HTTPRoute, a helm test pod, a README and a few common values. The container port now follows config.data.listen. CI renders the new values, checks the cases the chart refuses and validates the manifests with kubeconform. --- .github/workflows/ci.yml | 72 +++ README.md | 2 + deploy/charts/proxy/README.md | 82 +++ deploy/charts/proxy/templates/NOTES.txt | 9 + deploy/charts/proxy/templates/_helpers.tpl | 58 +++ deploy/charts/proxy/templates/deployment.yaml | 22 +- deploy/charts/proxy/templates/httproute.yaml | 38 ++ .../proxy/templates/poddisruptionbudget.yaml | 20 + deploy/charts/proxy/templates/service.yaml | 19 + .../proxy/templates/serviceaccount.yaml | 17 + .../charts/proxy/templates/tests/health.yaml | 40 ++ deploy/charts/proxy/values.schema.json | 486 ++++++++++++++++++ deploy/charts/proxy/values.yaml | 76 ++- 13 files changed, 935 insertions(+), 6 deletions(-) create mode 100644 deploy/charts/proxy/README.md create mode 100644 deploy/charts/proxy/templates/httproute.yaml create mode 100644 deploy/charts/proxy/templates/poddisruptionbudget.yaml create mode 100644 deploy/charts/proxy/templates/serviceaccount.yaml create mode 100644 deploy/charts/proxy/templates/tests/health.yaml create mode 100644 deploy/charts/proxy/values.schema.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fbbc265c..fd8d19d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -149,3 +149,75 @@ jobs: --set serviceMonitor.interval=30s \ --set 'serviceMonitor.labels.release=prometheus' \ >/dev/null + helm template proxy deploy/charts/proxy \ + --set replicaCount=3 \ + --set config.data.database.driver=postgres \ + --set deploymentStrategy.type=RollingUpdate \ + --set persistence.enabled=false \ + --set persistence.emptyDirSizeLimit=1Gi \ + --set podDisruptionBudget.enabled=true \ + --set serviceAccount.create=true \ + --set-string 'serviceAccount.annotations.eks\.amazonaws\.com/role-arn=arn:aws:iam::123456789012:role/proxy' \ + --set httpRoute.enabled=true \ + --set 'httpRoute.parentRefs[0].name=public' \ + --set 'httpRoute.hostnames[0]=proxy.example.com' \ + --set httpRoute.timeouts.request=600s \ + --set service.type=LoadBalancer \ + --set 'service.loadBalancerSourceRanges[0]=10.0.0.0/8' \ + --set priorityClassName=high \ + --set revisionHistoryLimit=3 \ + --set 'extraContainers[0].name=sidecar' \ + --set 'extraContainers[0].image=busybox:1.37' \ + >build-chart-full.yaml + grep -F 'kind: PodDisruptionBudget' build-chart-full.yaml >/dev/null + grep -F 'kind: HTTPRoute' build-chart-full.yaml >/dev/null + grep -F 'serviceAccountName: proxy' build-chart-full.yaml >/dev/null + + - name: Check the values the chart derives and refuses + run: | + set -euo pipefail + fails() { + if "$@" >/dev/null 2>&1; then + echo "expected to fail: $*" >&2 + return 1 + fi + } + # The container port follows the listen address. + helm template proxy deploy/charts/proxy --set-string config.data.listen=:9090 | + grep -F 'containerPort: 9090' >/dev/null + # Replicas can't share SQLite, unless something else sets the database. + fails helm template proxy deploy/charts/proxy --set replicaCount=2 + helm template proxy deploy/charts/proxy --set replicaCount=2 \ + --set 'extraEnv[0].name=PROXY_DATABASE_DRIVER' \ + --set 'extraEnv[0].value=postgres' >/dev/null + # A budget for one replica would block node drains. + if helm template proxy deploy/charts/proxy --set podDisruptionBudget.enabled=true | + grep -F 'kind: PodDisruptionBudget' >/dev/null; then + echo "a PodDisruptionBudget was rendered for one replica" >&2 + exit 1 + fi + # The schema rejects a misspelled key. + fails helm template proxy deploy/charts/proxy --set persistance.enabled=false + fails helm template proxy deploy/charts/proxy --set httpRoute.enabled=true + fails helm template proxy deploy/charts/proxy \ + --set httpRoute.enabled=true \ + --set 'httpRoute.parentRefs[0].name=public' \ + --set ingress.enabled=true \ + --set 'ingress.hosts[0].host=proxy.example.com' \ + --set 'ingress.hosts[0].paths[0].path=/' \ + --set 'ingress.hosts[0].paths[0].pathType=Prefix' + + - uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0 + with: + go-version-file: go.mod + + # HTTPRoute and ServiceMonitor are CRDs without a schema here, so they + # are skipped. + - name: Validate manifests against the Kubernetes schemas + run: | + set -euo pipefail + go install github.com/yannh/kubeconform/cmd/kubeconform@v0.7.0 + kubeconform="$(go env GOPATH)/bin/kubeconform" + helm template proxy deploy/charts/proxy | + "$kubeconform" -strict -summary -ignore-missing-schemas + "$kubeconform" -strict -summary -ignore-missing-schemas build-chart-full.yaml diff --git a/README.md b/README.md index ab38b4da..eae26092 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ The default chart deploys one replica backed by a 10 GiB persistent volume, using SQLite and filesystem artifact storage under `/data`. See [`deploy/charts/proxy/values.yaml`](deploy/charts/proxy/values.yaml) for ingress, external database and object-storage configuration options. +The [chart's README](deploy/charts/proxy/README.md) covers running more than one +replica, cloud credentials through a ServiceAccount, and exposing the proxy. Set `deploymentAnnotations` to add annotations to the Deployment itself, separately from `podAnnotations`. diff --git a/deploy/charts/proxy/README.md b/deploy/charts/proxy/README.md new file mode 100644 index 00000000..b2cf6426 --- /dev/null +++ b/deploy/charts/proxy/README.md @@ -0,0 +1,82 @@ +# git-pkgs proxy + +A caching proxy for package registries. This chart runs it as a Deployment +with a Service, and optionally an Ingress or HTTPRoute, a ServiceMonitor, a +ServiceAccount and a PodDisruptionBudget. + +```bash +helm install proxy oci://ghcr.io/git-pkgs/charts/proxy \ + --set config.data.base_url=https://proxy.example.com +``` + +The defaults give one replica with a 10 GiB volume at `/data`, holding a +SQLite database and the cached artifacts. [`values.yaml`](values.yaml) +describes every value; `values.schema.json` rejects a value the chart doesn't +know, so a misspelled key fails the install instead of being ignored. + +## Configuration + +`config.data` is the proxy's configuration file, passed through as it is; the +[configuration reference](https://github.com/git-pkgs/proxy/blob/main/docs/configuration.md) +lists the keys. Environment variables override it, and are the place for +secrets: + +```yaml +config: + data: + base_url: https://proxy.example.com + database: + driver: postgres + +extraEnv: + - name: PROXY_DATABASE_URL + valueFrom: + secretKeyRef: + name: proxy-database + key: url +``` + +Set `config.existingConfigMap` to use a ConfigMap of your own instead. + +The container port follows `config.data.listen`. Set `service.containerPort` +when the listen address comes from an existing ConfigMap or `PROXY_LISTEN`. + +## More than one replica + +Replicas can't share a SQLite file, so they need: + +- `config.data.database.driver: postgres`, with the URL in `PROXY_DATABASE_URL` +- `deploymentStrategy.type: RollingUpdate` +- artifact storage all replicas reach: object storage with + `persistence.enabled: false`, or a `file://` URL on a `ReadWriteMany` volume + +The chart refuses to render more than one replica when the generated +configuration uses SQLite and nothing else can set the database. +`podDisruptionBudget.enabled` adds a budget once there is more than one. + +## Cloud credentials + +The `ecr` upstream auth type and S3 storage take AWS credentials from the +SDK's default chain. To bind them to the pod, give it a ServiceAccount: + +```yaml +serviceAccount: + create: true + annotations: + eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/proxy +``` + +## Exposing the proxy + +Use `ingress` or `httpRoute` (Gateway API), not both. Artifacts and hosted +uploads can be large, and most controllers cap the request body and time out +long responses by default; `values.yaml` shows the annotations for +ingress-nginx, and `httpRoute.timeouts` sets the route's. + +## Checking an install + +```bash +helm test proxy +``` + +runs a pod that asks the proxy for `/health` through its Service. diff --git a/deploy/charts/proxy/templates/NOTES.txt b/deploy/charts/proxy/templates/NOTES.txt index 3b29f515..82da7beb 100644 --- a/deploy/charts/proxy/templates/NOTES.txt +++ b/deploy/charts/proxy/templates/NOTES.txt @@ -9,7 +9,16 @@ To access the proxy locally: Then visit http://localhost:{{ .Values.service.port }}/. +To check that it answers: + + helm -n {{ .Release.Namespace }} test {{ .Release.Name }} {{- if not .Values.persistence.enabled }} + WARNING: persistence is disabled. Cached artifacts and the default SQLite database will be lost when the pod is replaced. {{- end }} +{{- if and (gt (int .Values.replicaCount) 1) (eq (toString .Values.deploymentStrategy.type) "Recreate") }} + +WARNING: deploymentStrategy is Recreate with {{ .Values.replicaCount }} replicas, so every +upgrade stops all of them first. Set deploymentStrategy.type to RollingUpdate. +{{- end }} diff --git a/deploy/charts/proxy/templates/_helpers.tpl b/deploy/charts/proxy/templates/_helpers.tpl index 9408a9f8..94afeb17 100644 --- a/deploy/charts/proxy/templates/_helpers.tpl +++ b/deploy/charts/proxy/templates/_helpers.tpl @@ -44,3 +44,61 @@ app.kubernetes.io/instance: {{ .Release.Name }} {{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}} {{- end -}} {{- end }} + +{{/* +The name is always rendered, "default" included. Leaving the field out doesn't +take a pod back to the default account: an upgrade that drops +serviceAccountName keeps the old name in the deprecated serviceAccount field. +*/}} +{{- define "proxy.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} +{{- default (include "proxy.fullname" .) .Values.serviceAccount.name -}} +{{- else -}} +{{- default "default" .Values.serviceAccount.name -}} +{{- end -}} +{{- end }} + +{{/* +The port the container listens on: service.containerPort when set, else the +port of config.data.listen in the generated configuration, else 8080. +*/}} +{{- define "proxy.containerPort" -}} +{{- $data := .Values.config.data | default dict -}} +{{- $listen := get $data "listen" | default "" | toString -}} +{{- if .Values.service.containerPort -}} +{{- .Values.service.containerPort -}} +{{- else if and (not .Values.config.existingConfigMap) (regexMatch ":[0-9]+$" $listen) -}} +{{- regexFind "[0-9]+$" $listen -}} +{{- else -}} +8080 +{{- end -}} +{{- end }} + +{{/* +proxy.sqliteForCertain is "true" when the generated configuration uses SQLite +and nothing outside it can set the database. +*/}} +{{- define "proxy.sqliteForCertain" -}} +{{- $data := .Values.config.data | default dict -}} +{{- $database := get $data "database" | default dict -}} +{{- $driver := get $database "driver" | default "sqlite" | toString | lower -}} +{{- $fromEnv := false -}} +{{- range .Values.extraEnv -}} +{{- if hasPrefix "PROXY_DATABASE_" (toString .name) -}} +{{- $fromEnv = true -}} +{{- end -}} +{{- end -}} +{{- if and (eq $driver "sqlite") (not .Values.config.existingConfigMap) (not $fromEnv) (not .Values.extraEnvFrom) -}} +true +{{- end -}} +{{- end }} + +{{/* Fail on values that can't work, before anything is installed. */}} +{{- define "proxy.validate" -}} +{{- if and (gt (int .Values.replicaCount) 1) (include "proxy.sqliteForCertain" .) -}} +{{- fail "replicaCount > 1 needs Postgres: set config.data.database.driver to postgres and supply PROXY_DATABASE_URL through extraEnv. Replicas can't share a SQLite file." -}} +{{- end -}} +{{- if and .Values.ingress.enabled .Values.httpRoute.enabled -}} +{{- fail "ingress.enabled and httpRoute.enabled are both set; pick one" -}} +{{- end -}} +{{- end }} diff --git a/deploy/charts/proxy/templates/deployment.yaml b/deploy/charts/proxy/templates/deployment.yaml index 67285b95..3f56ee5d 100644 --- a/deploy/charts/proxy/templates/deployment.yaml +++ b/deploy/charts/proxy/templates/deployment.yaml @@ -1,3 +1,4 @@ +{{- include "proxy.validate" . -}} apiVersion: apps/v1 kind: Deployment metadata: @@ -11,6 +12,9 @@ metadata: {{- include "proxy.labels" . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} + {{- if not (kindIs "invalid" .Values.revisionHistoryLimit) }} + revisionHistoryLimit: {{ .Values.revisionHistoryLimit }} + {{- end }} strategy: {{- toYaml .Values.deploymentStrategy | nindent 4 }} selector: @@ -29,7 +33,11 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} spec: - automountServiceAccountToken: false + serviceAccountName: {{ include "proxy.serviceAccountName" . }} + automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }} + {{- with .Values.priorityClassName }} + priorityClassName: {{ . }} + {{- end }} terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} @@ -61,7 +69,7 @@ spec: {{- end }} ports: - name: http - containerPort: {{ .Values.service.containerPort }} + containerPort: {{ include "proxy.containerPort" . }} protocol: TCP startupProbe: {{- toYaml .Values.startupProbe | nindent 12 }} @@ -83,6 +91,9 @@ spec: {{- with .Values.extraVolumeMounts }} {{- toYaml . | nindent 12 }} {{- end }} + {{- with .Values.extraContainers }} + {{- toYaml . | nindent 8 }} + {{- end }} volumes: - name: config configMap: @@ -95,7 +106,12 @@ spec: persistentVolumeClaim: claimName: {{ include "proxy.claimName" . }} {{- else }} - emptyDir: {} + emptyDir: + {{- with .Values.persistence.emptyDirSizeLimit }} + sizeLimit: {{ . }} + {{- else }} + {} + {{- end }} {{- end }} - name: tmp emptyDir: diff --git a/deploy/charts/proxy/templates/httproute.yaml b/deploy/charts/proxy/templates/httproute.yaml new file mode 100644 index 00000000..934f9985 --- /dev/null +++ b/deploy/charts/proxy/templates/httproute.yaml @@ -0,0 +1,38 @@ +{{- if .Values.httpRoute.enabled }} +{{- if not .Values.httpRoute.parentRefs }} +{{- fail "httpRoute.parentRefs must be set when httpRoute.enabled=true" }} +{{- end }} +apiVersion: gateway.networking.k8s.io/v1 +kind: HTTPRoute +metadata: + name: {{ include "proxy.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} + {{- with .Values.httpRoute.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.httpRoute.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + parentRefs: + {{- toYaml .Values.httpRoute.parentRefs | nindent 4 }} + {{- with .Values.httpRoute.hostnames }} + hostnames: + {{- toYaml . | nindent 4 }} + {{- end }} + rules: + - backendRefs: + - name: {{ include "proxy.fullname" . }} + port: {{ .Values.service.port }} + {{- with .Values.httpRoute.matches }} + matches: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.httpRoute.timeouts }} + timeouts: + {{- toYaml . | nindent 8 }} + {{- end }} +{{- end }} diff --git a/deploy/charts/proxy/templates/poddisruptionbudget.yaml b/deploy/charts/proxy/templates/poddisruptionbudget.yaml new file mode 100644 index 00000000..2e4bda93 --- /dev/null +++ b/deploy/charts/proxy/templates/poddisruptionbudget.yaml @@ -0,0 +1,20 @@ +{{- if and .Values.podDisruptionBudget.enabled (gt (int .Values.replicaCount) 1) }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "proxy.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} +spec: + {{- if not (kindIs "invalid" .Values.podDisruptionBudget.minAvailable) }} + minAvailable: {{ .Values.podDisruptionBudget.minAvailable }} + {{- else if not (kindIs "invalid" .Values.podDisruptionBudget.maxUnavailable) }} + maxUnavailable: {{ .Values.podDisruptionBudget.maxUnavailable }} + {{- else }} + maxUnavailable: 1 + {{- end }} + selector: + matchLabels: + {{- include "proxy.selectorLabels" . | nindent 6 }} +{{- end }} diff --git a/deploy/charts/proxy/templates/service.yaml b/deploy/charts/proxy/templates/service.yaml index ee5931e6..8e80d151 100644 --- a/deploy/charts/proxy/templates/service.yaml +++ b/deploy/charts/proxy/templates/service.yaml @@ -5,12 +5,31 @@ metadata: namespace: {{ .Release.Namespace }} labels: {{- include "proxy.labels" . | nindent 4 }} + {{- with .Values.service.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} spec: type: {{ .Values.service.type }} + {{- if eq .Values.service.type "LoadBalancer" }} + {{- with .Values.service.loadBalancerClass }} + loadBalancerClass: {{ . }} + {{- end }} + {{- with .Values.service.loadBalancerSourceRanges }} + loadBalancerSourceRanges: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- end }} + {{- with .Values.service.externalTrafficPolicy }} + externalTrafficPolicy: {{ . }} + {{- end }} ports: - name: http port: {{ .Values.service.port }} targetPort: http protocol: TCP + {{- if and .Values.service.nodePort (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) }} + nodePort: {{ .Values.service.nodePort }} + {{- end }} selector: {{- include "proxy.selectorLabels" . | nindent 4 }} diff --git a/deploy/charts/proxy/templates/serviceaccount.yaml b/deploy/charts/proxy/templates/serviceaccount.yaml new file mode 100644 index 00000000..7612d700 --- /dev/null +++ b/deploy/charts/proxy/templates/serviceaccount.yaml @@ -0,0 +1,17 @@ +{{- if .Values.serviceAccount.create }} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "proxy.serviceAccountName" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }} +{{- end }} diff --git a/deploy/charts/proxy/templates/tests/health.yaml b/deploy/charts/proxy/templates/tests/health.yaml new file mode 100644 index 00000000..95c616dd --- /dev/null +++ b/deploy/charts/proxy/templates/tests/health.yaml @@ -0,0 +1,40 @@ +# helm test: asks the proxy for /health through its Service, with the proxy's +# own image, which has wget. The pod doesn't carry the selector labels, or the +# Service would count it as a backend. +apiVersion: v1 +kind: Pod +metadata: + name: {{ printf "%s-test-health" (include "proxy.fullname" . | trunc 51 | trimSuffix "-") }} + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: {{ include "proxy.name" . }}-test + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + annotations: + helm.sh/hook: test + # Removed once it succeeds, so an uninstall leaves no pod behind. A failed + # one stays for its logs until the next run. + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded +spec: + restartPolicy: Never + automountServiceAccountToken: false + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 4 }} + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 4 }} + {{- end }} + containers: + - name: health + image: {{ include "proxy.image" . | quote }} + imagePullPolicy: {{ .Values.image.pullPolicy }} + securityContext: + {{- toYaml .Values.containerSecurityContext | nindent 8 }} + command: + - wget + - -q + - -T + - "15" + - -O + - /dev/null + - http://{{ include "proxy.fullname" . }}:{{ .Values.service.port }}/health diff --git a/deploy/charts/proxy/values.schema.json b/deploy/charts/proxy/values.schema.json new file mode 100644 index 00000000..1a77cba6 --- /dev/null +++ b/deploy/charts/proxy/values.schema.json @@ -0,0 +1,486 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "git-pkgs proxy chart values", + "type": "object", + "additionalProperties": false, + "properties": { + "global": { + "type": "object" + }, + "replicaCount": { + "type": "integer", + "minimum": 0 + }, + "revisionHistoryLimit": { + "type": [ + "integer", + "null" + ], + "minimum": 0 + }, + "nameOverride": { + "type": "string" + }, + "fullnameOverride": { + "type": "string" + }, + "image": { + "type": "object", + "properties": { + "repository": { + "type": "string", + "minLength": 1 + }, + "tag": { + "type": "string" + }, + "digest": { + "type": "string" + }, + "pullPolicy": { + "type": "string", + "enum": [ + "Always", + "IfNotPresent", + "Never" + ] + } + }, + "additionalProperties": false + }, + "imagePullSecrets": { + "type": "array", + "items": { + "type": "object" + } + }, + "service": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "ClusterIP", + "NodePort", + "LoadBalancer" + ] + }, + "port": { + "type": "integer", + "minimum": 1, + "maximum": 65535 + }, + "containerPort": { + "type": [ + "integer", + "null" + ], + "minimum": 1, + "maximum": 65535 + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "nodePort": { + "type": [ + "integer", + "null" + ] + }, + "loadBalancerClass": { + "type": "string" + }, + "loadBalancerSourceRanges": { + "type": "array", + "items": { + "type": "string" + } + }, + "externalTrafficPolicy": { + "type": "string", + "enum": [ + "", + "Cluster", + "Local" + ] + } + }, + "additionalProperties": false + }, + "serviceAccount": { + "type": "object", + "properties": { + "create": { + "type": "boolean" + }, + "name": { + "type": "string" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "automountToken": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "config": { + "type": "object", + "properties": { + "existingConfigMap": { + "type": "string" + }, + "existingConfigMapKey": { + "type": "string", + "minLength": 1 + }, + "data": { + "type": [ + "object", + "null" + ] + } + }, + "additionalProperties": false + }, + "extraEnv": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraEnvFrom": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraVolumes": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraVolumeMounts": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraInitContainers": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraContainers": { + "type": "array", + "items": { + "type": "object" + } + }, + "persistence": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "mountPath": { + "type": "string", + "minLength": 1 + }, + "emptyDirSizeLimit": { + "type": "string" + }, + "existingClaim": { + "type": "string" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "accessModes": { + "type": "array", + "items": { + "type": "string" + } + }, + "size": { + "type": "string", + "minLength": 1 + }, + "storageClass": { + "type": "string" + } + }, + "additionalProperties": false + }, + "deploymentStrategy": { + "type": "object" + }, + "podDisruptionBudget": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "minAvailable": { + "type": [ + "integer", + "string", + "null" + ] + }, + "maxUnavailable": { + "type": [ + "integer", + "string", + "null" + ] + } + }, + "additionalProperties": false + }, + "ingress": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "className": { + "type": "string" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "hosts": { + "type": "array", + "items": { + "type": "object", + "properties": { + "host": { + "type": "string" + }, + "paths": { + "type": "array", + "items": { + "type": "object", + "properties": { + "path": { + "type": "string" + }, + "pathType": { + "type": "string", + "enum": [ + "Prefix", + "Exact", + "ImplementationSpecific" + ] + } + }, + "additionalProperties": false, + "required": [ + "path", + "pathType" + ] + } + } + }, + "additionalProperties": false, + "required": [ + "paths" + ] + } + }, + "tls": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "additionalProperties": false + }, + "httpRoute": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "parentRefs": { + "type": "array", + "items": { + "type": "object" + } + }, + "hostnames": { + "type": "array", + "items": { + "type": "string" + } + }, + "matches": { + "type": "array", + "items": { + "type": "object" + } + }, + "timeouts": { + "type": "object" + } + }, + "additionalProperties": false + }, + "serviceMonitor": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "namespace": { + "type": "string" + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "path": { + "type": "string" + }, + "interval": { + "type": "string" + }, + "scrapeTimeout": { + "type": "string" + }, + "honorLabels": { + "type": "boolean" + }, + "relabelings": { + "type": "array", + "items": { + "type": "object" + } + }, + "metricRelabelings": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "additionalProperties": false + }, + "deploymentAnnotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "podAnnotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "podLabels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "podSecurityContext": { + "type": "object" + }, + "containerSecurityContext": { + "type": "object" + }, + "tmpDir": { + "type": "object", + "properties": { + "sizeLimit": { + "type": "string" + } + }, + "additionalProperties": false + }, + "resources": { + "type": "object" + }, + "startupProbe": { + "type": "object" + }, + "readinessProbe": { + "type": "object" + }, + "livenessProbe": { + "type": "object" + }, + "terminationGracePeriodSeconds": { + "type": "integer", + "minimum": 0 + }, + "priorityClassName": { + "type": "string" + }, + "nodeSelector": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "tolerations": { + "type": "array", + "items": { + "type": "object" + } + }, + "affinity": { + "type": "object" + }, + "topologySpreadConstraints": { + "type": "array", + "items": { + "type": "object" + } + } + } +} diff --git a/deploy/charts/proxy/values.yaml b/deploy/charts/proxy/values.yaml index 9458556a..c8a83b81 100644 --- a/deploy/charts/proxy/values.yaml +++ b/deploy/charts/proxy/values.yaml @@ -3,8 +3,15 @@ # persistence disabled) or a file:// URL on a ReadWriteMany volume # (persistence.accessModes: [ReadWriteMany]). With upstream.git, each # replica also needs its own git.cache_dir. +# +# The chart refuses to render more than one replica when the generated +# configuration uses SQLite and nothing else can set the database (an existing +# ConfigMap, a PROXY_DATABASE_* variable in extraEnv, or extraEnvFrom). replicaCount: 1 +# Old ReplicaSets kept for rollback. Empty uses the Kubernetes default. +revisionHistoryLimit: + nameOverride: "" fullnameOverride: "" @@ -21,9 +28,31 @@ imagePullSecrets: [] service: type: ClusterIP port: 8080 - # Keep this aligned with config.data.listen (or the listen address in an - # existing ConfigMap). - containerPort: 8080 + # Empty takes the port from config.data.listen. Set it when the listen + # address comes from an existing ConfigMap or PROXY_LISTEN and isn't 8080. + containerPort: + annotations: {} + # Only for type NodePort or LoadBalancer. + nodePort: + # Only for type LoadBalancer. + loadBalancerClass: "" + loadBalancerSourceRanges: [] + externalTrafficPolicy: "" + +# A ServiceAccount of the proxy's own, for cloud credentials bound to one: +# IAM roles for service accounts on EKS, or Workload Identity on GKE and AKS. +# The ecr upstream auth type and S3 storage get their credentials this way. +serviceAccount: + create: false + # With create, empty uses the release's full name. Without it, the name of + # an existing ServiceAccount; empty uses the namespace default. + name: "" + annotations: {} + # eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/proxy + labels: {} + # The proxy doesn't call the Kubernetes API, so the token stays unmounted. + # The cloud identity webhooks mount their own. + automountToken: false # The generated configuration is ignored when existingConfigMap is set. config: @@ -90,6 +119,8 @@ persistence: # Keep this aligned with config.data.storage.url and config.data.database.path # (or the equivalent paths in an existing ConfigMap). mountPath: /data + # Size limit of the emptyDir used when persistence is disabled. + emptyDirSizeLimit: "" existingClaim: "" annotations: {} accessModes: @@ -100,6 +131,19 @@ persistence: deploymentStrategy: type: Recreate +# Only rendered with more than one replica: with one, a budget either blocks +# node drains or protects nothing. +podDisruptionBudget: + enabled: false + # Set one of the two. + minAvailable: + maxUnavailable: 1 + +# Artifacts and hosted uploads can be large and slow. Most ingress controllers +# cap the request body and time out long responses by default; raise both, for +# example with ingress-nginx: +# nginx.ingress.kubernetes.io/proxy-body-size: "0" +# nginx.ingress.kubernetes.io/proxy-read-timeout: "600" ingress: enabled: false className: "" @@ -114,6 +158,26 @@ ingress: # hosts: # - proxy.example.com +# Gateway API HTTPRoute, as an alternative to ingress. Requires the +# gateway.networking.k8s.io/v1 CRDs and a Gateway to attach to. +httpRoute: + enabled: false + annotations: {} + labels: {} + parentRefs: [] + # - name: public + # namespace: gateway-system + hostnames: [] + # - proxy.example.com + matches: + - path: + type: PathPrefix + value: / + # Request timeouts of the route's rule. Large downloads can need more than + # a gateway's default. + timeouts: {} + # request: 600s + # Prometheus Operator ServiceMonitor for the /metrics endpoint. Requires the # monitoring.coreos.com/v1 CRDs to be installed in the cluster. serviceMonitor: @@ -186,6 +250,12 @@ livenessProbe: terminationGracePeriodSeconds: 30 +priorityClassName: "" + +# Additional containers for the pod, for example a database proxy sidecar. +# They can mount extraVolumes. +extraContainers: [] + nodeSelector: {} tolerations: [] affinity: {} From 3322ff85ed8cbfae41abfc65385695c6fdf07b70 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 5 Dec 2026 12:26:56 +0100 Subject: [PATCH 2/5] Harden the chart's rendering of names, ports and the test pod Quote the ServiceAccount, load balancer class and priority class names, render the container port without leading zeros, keep the test pod's label within 63 characters and give it the node selector and tolerations, and leave externalTrafficPolicy off a ClusterIP Service. --- .github/workflows/ci.yml | 2 +- deploy/charts/proxy/templates/_helpers.tpl | 4 ++-- deploy/charts/proxy/templates/deployment.yaml | 4 ++-- deploy/charts/proxy/templates/service.yaml | 4 +++- deploy/charts/proxy/templates/serviceaccount.yaml | 2 +- deploy/charts/proxy/templates/tests/health.yaml | 10 +++++++++- deploy/charts/proxy/values.yaml | 6 ++++-- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fd8d19d8..50a72fa5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -171,7 +171,7 @@ jobs: >build-chart-full.yaml grep -F 'kind: PodDisruptionBudget' build-chart-full.yaml >/dev/null grep -F 'kind: HTTPRoute' build-chart-full.yaml >/dev/null - grep -F 'serviceAccountName: proxy' build-chart-full.yaml >/dev/null + grep -F 'serviceAccountName: "proxy"' build-chart-full.yaml >/dev/null - name: Check the values the chart derives and refuses run: | diff --git a/deploy/charts/proxy/templates/_helpers.tpl b/deploy/charts/proxy/templates/_helpers.tpl index 94afeb17..6011b0d0 100644 --- a/deploy/charts/proxy/templates/_helpers.tpl +++ b/deploy/charts/proxy/templates/_helpers.tpl @@ -66,9 +66,9 @@ port of config.data.listen in the generated configuration, else 8080. {{- $data := .Values.config.data | default dict -}} {{- $listen := get $data "listen" | default "" | toString -}} {{- if .Values.service.containerPort -}} -{{- .Values.service.containerPort -}} +{{- int .Values.service.containerPort -}} {{- else if and (not .Values.config.existingConfigMap) (regexMatch ":[0-9]+$" $listen) -}} -{{- regexFind "[0-9]+$" $listen -}} +{{- regexFind "[1-9][0-9]*$" $listen | int -}} {{- else -}} 8080 {{- end -}} diff --git a/deploy/charts/proxy/templates/deployment.yaml b/deploy/charts/proxy/templates/deployment.yaml index 3f56ee5d..0fad83d0 100644 --- a/deploy/charts/proxy/templates/deployment.yaml +++ b/deploy/charts/proxy/templates/deployment.yaml @@ -33,10 +33,10 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} spec: - serviceAccountName: {{ include "proxy.serviceAccountName" . }} + serviceAccountName: {{ include "proxy.serviceAccountName" . | quote }} automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }} {{- with .Values.priorityClassName }} - priorityClassName: {{ . }} + priorityClassName: {{ . | quote }} {{- end }} terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} securityContext: diff --git a/deploy/charts/proxy/templates/service.yaml b/deploy/charts/proxy/templates/service.yaml index 8e80d151..cee77d42 100644 --- a/deploy/charts/proxy/templates/service.yaml +++ b/deploy/charts/proxy/templates/service.yaml @@ -13,16 +13,18 @@ spec: type: {{ .Values.service.type }} {{- if eq .Values.service.type "LoadBalancer" }} {{- with .Values.service.loadBalancerClass }} - loadBalancerClass: {{ . }} + loadBalancerClass: {{ . | quote }} {{- end }} {{- with .Values.service.loadBalancerSourceRanges }} loadBalancerSourceRanges: {{- toYaml . | nindent 4 }} {{- end }} {{- end }} + {{- if or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer") }} {{- with .Values.service.externalTrafficPolicy }} externalTrafficPolicy: {{ . }} {{- end }} + {{- end }} ports: - name: http port: {{ .Values.service.port }} diff --git a/deploy/charts/proxy/templates/serviceaccount.yaml b/deploy/charts/proxy/templates/serviceaccount.yaml index 7612d700..077968c7 100644 --- a/deploy/charts/proxy/templates/serviceaccount.yaml +++ b/deploy/charts/proxy/templates/serviceaccount.yaml @@ -2,7 +2,7 @@ apiVersion: v1 kind: ServiceAccount metadata: - name: {{ include "proxy.serviceAccountName" . }} + name: {{ include "proxy.serviceAccountName" . | quote }} namespace: {{ .Release.Namespace }} labels: {{- include "proxy.labels" . | nindent 4 }} diff --git a/deploy/charts/proxy/templates/tests/health.yaml b/deploy/charts/proxy/templates/tests/health.yaml index 95c616dd..c1b27157 100644 --- a/deploy/charts/proxy/templates/tests/health.yaml +++ b/deploy/charts/proxy/templates/tests/health.yaml @@ -7,7 +7,7 @@ metadata: name: {{ printf "%s-test-health" (include "proxy.fullname" . | trunc 51 | trimSuffix "-") }} namespace: {{ .Release.Namespace }} labels: - app.kubernetes.io/name: {{ include "proxy.name" . }}-test + app.kubernetes.io/name: {{ include "proxy.name" . | trunc 58 | trimSuffix "-" }}-test app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} annotations: @@ -24,6 +24,14 @@ spec: imagePullSecrets: {{- toYaml . | nindent 4 }} {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 4 }} + {{- end }} containers: - name: health image: {{ include "proxy.image" . | quote }} diff --git a/deploy/charts/proxy/values.yaml b/deploy/charts/proxy/values.yaml index c8a83b81..18ec26e9 100644 --- a/deploy/charts/proxy/values.yaml +++ b/deploy/charts/proxy/values.yaml @@ -28,8 +28,9 @@ imagePullSecrets: [] service: type: ClusterIP port: 8080 - # Empty takes the port from config.data.listen. Set it when the listen - # address comes from an existing ConfigMap or PROXY_LISTEN and isn't 8080. + # Empty takes the port from config.data.listen, or 8080 with an existing + # ConfigMap. Set it when the listen address comes from somewhere else: an + # existing ConfigMap with another port, or PROXY_LISTEN. containerPort: annotations: {} # Only for type NodePort or LoadBalancer. @@ -37,6 +38,7 @@ service: # Only for type LoadBalancer. loadBalancerClass: "" loadBalancerSourceRanges: [] + # Only for type NodePort or LoadBalancer. externalTrafficPolicy: "" # A ServiceAccount of the proxy's own, for cloud credentials bound to one: From cf4e63a64603d312e4123cfcff1553a81358fae6 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 5 Dec 2026 12:37:37 +0100 Subject: [PATCH 3/5] Leave the name label off the chart's test pod A long nameOverride ending in -test made the shortened label equal the proxy's own, so the Service selected the test pod. Without the label no selector of the chart can match it. --- deploy/charts/proxy/templates/tests/health.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deploy/charts/proxy/templates/tests/health.yaml b/deploy/charts/proxy/templates/tests/health.yaml index c1b27157..a7372edd 100644 --- a/deploy/charts/proxy/templates/tests/health.yaml +++ b/deploy/charts/proxy/templates/tests/health.yaml @@ -1,13 +1,13 @@ # helm test: asks the proxy for /health through its Service, with the proxy's -# own image, which has wget. The pod doesn't carry the selector labels, or the -# Service would count it as a backend. +# own image, which has wget. The pod has no app.kubernetes.io/name label, so no +# selector of the chart matches it and the Service doesn't count it as a backend. apiVersion: v1 kind: Pod metadata: name: {{ printf "%s-test-health" (include "proxy.fullname" . | trunc 51 | trimSuffix "-") }} namespace: {{ .Release.Namespace }} labels: - app.kubernetes.io/name: {{ include "proxy.name" . | trunc 58 | trimSuffix "-" }}-test + app.kubernetes.io/component: test app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} annotations: From 8e211622073c9c728d6d50432bdd585b4f8ccfc1 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 5 Dec 2026 18:31:21 +0000 Subject: [PATCH 4/5] Add a NetworkPolicy and keep secrets out of the chart's ConfigMap /ui, /api/cache, /api/mirror and /_internal/scan-fetch have no authentication of their own, and the docs ask for them to be restricted at the network layer. networkPolicy.enabled admits only the peers in networkPolicy.from, and the helm test pod, to the proxy's port, with optional egress rules. It is off by default and refuses to render without a peer. A token, password, header_value or signing_key in config.data, or a database.url with a password, would land in the ConfigMap in plain text. The chart now refuses that unless the value is a ${VAR} reference, which the proxy expands, or config.allowSecretsInConfigMap is set. CI renders both and checks the refusals. --- .github/workflows/ci.yml | 16 ++++++ deploy/charts/proxy/README.md | 55 ++++++++++++++++++- deploy/charts/proxy/templates/_helpers.tpl | 34 ++++++++++++ .../charts/proxy/templates/networkpolicy.yaml | 36 ++++++++++++ deploy/charts/proxy/values.schema.json | 24 ++++++++ deploy/charts/proxy/values.yaml | 38 +++++++++++++ 6 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 deploy/charts/proxy/templates/networkpolicy.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50a72fa5..9d2319ee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -168,10 +168,16 @@ jobs: --set revisionHistoryLimit=3 \ --set 'extraContainers[0].name=sidecar' \ --set 'extraContainers[0].image=busybox:1.37' \ + --set networkPolicy.enabled=true \ + --set 'networkPolicy.from[0].namespaceSelector.matchLabels.kubernetes\.io/metadata\.name=ingress-nginx' \ + --set 'networkPolicy.egress[0].ports[0].protocol=UDP' \ + --set 'networkPolicy.egress[0].ports[0].port=53' \ + --set-string 'config.data.upstream.auth.npm\.pkg\.github\.com.token=${GITHUB_TOKEN}' \ >build-chart-full.yaml grep -F 'kind: PodDisruptionBudget' build-chart-full.yaml >/dev/null grep -F 'kind: HTTPRoute' build-chart-full.yaml >/dev/null grep -F 'serviceAccountName: "proxy"' build-chart-full.yaml >/dev/null + grep -F 'kind: NetworkPolicy' build-chart-full.yaml >/dev/null - name: Check the values the chart derives and refuses run: | @@ -196,6 +202,16 @@ jobs: echo "a PodDisruptionBudget was rendered for one replica" >&2 exit 1 fi + # Secrets stay out of the ConfigMap unless allowed. + fails helm template proxy deploy/charts/proxy \ + --set 'config.data.upstream.auth.npm.token=plain' + fails helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=postgres://proxy:secret@db/proxy' + helm template proxy deploy/charts/proxy \ + --set 'config.data.upstream.auth.npm.token=plain' \ + --set config.allowSecretsInConfigMap=true >/dev/null + # A NetworkPolicy without peers would shut everyone out. + fails helm template proxy deploy/charts/proxy --set networkPolicy.enabled=true # The schema rejects a misspelled key. fails helm template proxy deploy/charts/proxy --set persistance.enabled=false fails helm template proxy deploy/charts/proxy --set httpRoute.enabled=true diff --git a/deploy/charts/proxy/README.md b/deploy/charts/proxy/README.md index b2cf6426..38886220 100644 --- a/deploy/charts/proxy/README.md +++ b/deploy/charts/proxy/README.md @@ -2,7 +2,7 @@ A caching proxy for package registries. This chart runs it as a Deployment with a Service, and optionally an Ingress or HTTPRoute, a ServiceMonitor, a -ServiceAccount and a PodDisruptionBudget. +ServiceAccount, a PodDisruptionBudget and a NetworkPolicy. ```bash helm install proxy oci://ghcr.io/git-pkgs/charts/proxy \ @@ -36,6 +36,32 @@ extraEnv: key: url ``` +Secrets in `config.data` would be stored in the ConfigMap in plain text, so +the chart refuses to render a `token`, `password`, `header_value` or +`signing_key` that isn't a `${VAR}` reference, or a `database.url` with a +password. The proxy expands `${VAR}` in those keys, so the value can come from +a Secret: + +```yaml +config: + data: + upstream: + auth: + "https://npm.pkg.github.com": + type: bearer + token: "${GITHUB_TOKEN}" + +extraEnv: + - name: GITHUB_TOKEN + valueFrom: + secretKeyRef: + name: proxy-upstream + key: github-token +``` + +`config.allowSecretsInConfigMap: true` turns the check off. Other keys that +can hold credentials, such as scanner `headers`, aren't checked. + Set `config.existingConfigMap` to use a ConfigMap of your own instead. The container port follows `config.data.listen`. Set `service.containerPort` @@ -73,6 +99,33 @@ uploads can be large, and most controllers cap the request body and time out long responses by default; `values.yaml` shows the annotations for ingress-nginx, and `httpRoute.timeouts` sets the route's. +## Limiting who reaches the proxy + +`/ui`, `/api/cache`, `/api/mirror` and `/_internal/scan-fetch` have no +authentication of their own. `networkPolicy.enabled` adds a NetworkPolicy that +admits only the peers in `networkPolicy.from` to the proxy's port, and the pod +of `helm test`: + +```yaml +networkPolicy: + enabled: true + from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: ingress-nginx + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: ci +``` + +List the ingress controller or gateway, the clients that pull packages, and +Prometheus if it scrapes `/metrics`. `networkPolicy.egress` restricts outgoing +traffic as well; once set, everything it doesn't list is denied, DNS included. + +A NetworkPolicy sees addresses and ports, not paths, and the cluster's network +plugin has to enforce it. To keep `/ui` or the APIs from clients that may pull +packages, restrict those paths at the ingress or gateway too. + ## Checking an install ```bash diff --git a/deploy/charts/proxy/templates/_helpers.tpl b/deploy/charts/proxy/templates/_helpers.tpl index 6011b0d0..2a0926da 100644 --- a/deploy/charts/proxy/templates/_helpers.tpl +++ b/deploy/charts/proxy/templates/_helpers.tpl @@ -93,6 +93,29 @@ true {{- end -}} {{- end }} +{{/* +proxy.secretPaths lists, space-separated, the paths in config.data whose key +names a secret (token, password, header_value, signing_key) and whose value +is set without a ${VAR} reference. Called with a dict of path and value. +*/}} +{{- define "proxy.secretPaths" -}} +{{- $path := .path -}} +{{- if kindIs "map" .value -}} +{{- range $key, $value := .value -}} +{{- $at := ternary (toString $key) (printf "%s.%s" $path (toString $key)) (eq $path "") -}} +{{- if or (kindIs "map" $value) (kindIs "slice" $value) -}} +{{- include "proxy.secretPaths" (dict "path" $at "value" $value) -}} +{{- else if and (has (lower (toString $key)) (list "token" "password" "header_value" "signing_key")) (not (kindIs "invalid" $value)) (ne (toString $value) "") (not (contains "${" (toString $value))) -}} +{{- printf "%s " $at -}} +{{- end -}} +{{- end -}} +{{- else if kindIs "slice" .value -}} +{{- range $index, $value := .value -}} +{{- include "proxy.secretPaths" (dict "path" (printf "%s[%d]" $path $index) "value" $value) -}} +{{- end -}} +{{- end -}} +{{- end }} + {{/* Fail on values that can't work, before anything is installed. */}} {{- define "proxy.validate" -}} {{- if and (gt (int .Values.replicaCount) 1) (include "proxy.sqliteForCertain" .) -}} @@ -101,4 +124,15 @@ true {{- if and .Values.ingress.enabled .Values.httpRoute.enabled -}} {{- fail "ingress.enabled and httpRoute.enabled are both set; pick one" -}} {{- end -}} +{{- if and (not .Values.config.existingConfigMap) (not .Values.config.allowSecretsInConfigMap) -}} +{{- $data := .Values.config.data | default dict -}} +{{- $secrets := include "proxy.secretPaths" (dict "path" "" "value" $data) | trim | splitList " " | compact -}} +{{- $database := get $data "database" | default dict -}} +{{- if and (kindIs "map" $database) (regexMatch "://[^/@]*:[^/@]+@" (get $database "url" | default "" | toString)) -}} +{{- $secrets = append $secrets "database.url" -}} +{{- end -}} +{{- if $secrets -}} +{{- fail (printf "config.data would put secrets in the ConfigMap in plain text: %s. Write them as ${VAR} references and set the variables through extraEnv from a Secret (the database URL as PROXY_DATABASE_URL), or set config.allowSecretsInConfigMap=true." (join ", " $secrets)) -}} +{{- end -}} +{{- end -}} {{- end }} diff --git a/deploy/charts/proxy/templates/networkpolicy.yaml b/deploy/charts/proxy/templates/networkpolicy.yaml new file mode 100644 index 00000000..49d3f0ca --- /dev/null +++ b/deploy/charts/proxy/templates/networkpolicy.yaml @@ -0,0 +1,36 @@ +{{- if .Values.networkPolicy.enabled }} +{{- if not .Values.networkPolicy.from }} +{{- fail "networkPolicy.from must be set when networkPolicy.enabled=true: with no peer, nothing but helm test reaches the proxy" }} +{{- end }} +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "proxy.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} +spec: + podSelector: + matchLabels: + {{- include "proxy.selectorLabels" . | nindent 6 }} + policyTypes: + - Ingress + {{- if .Values.networkPolicy.egress }} + - Egress + {{- end }} + ingress: + - from: + {{- toYaml .Values.networkPolicy.from | nindent 8 }} + # The pod of helm test. + - podSelector: + matchLabels: + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/component: test + ports: + - protocol: TCP + port: {{ include "proxy.containerPort" . }} + {{- with .Values.networkPolicy.egress }} + egress: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deploy/charts/proxy/values.schema.json b/deploy/charts/proxy/values.schema.json index 1a77cba6..d0bd399b 100644 --- a/deploy/charts/proxy/values.schema.json +++ b/deploy/charts/proxy/values.schema.json @@ -140,6 +140,9 @@ "config": { "type": "object", "properties": { + "allowSecretsInConfigMap": { + "type": "boolean" + }, "existingConfigMap": { "type": "string" }, @@ -361,6 +364,27 @@ }, "additionalProperties": false }, + "networkPolicy": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "from": { + "type": "array", + "items": { + "type": "object" + } + }, + "egress": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "additionalProperties": false + }, "serviceMonitor": { "type": "object", "properties": { diff --git a/deploy/charts/proxy/values.yaml b/deploy/charts/proxy/values.yaml index 18ec26e9..2f337e6b 100644 --- a/deploy/charts/proxy/values.yaml +++ b/deploy/charts/proxy/values.yaml @@ -58,6 +58,11 @@ serviceAccount: # The generated configuration is ignored when existingConfigMap is set. config: + # The chart refuses to render config.data with a token, password, + # header_value or signing_key set without a ${VAR} reference, or a + # database.url with a password: a ConfigMap is readable by anyone allowed to + # read ConfigMaps in the namespace. Set this to render them anyway. + allowSecretsInConfigMap: false existingConfigMap: "" existingConfigMapKey: config.yaml data: @@ -180,6 +185,39 @@ httpRoute: timeouts: {} # request: 600s +# A NetworkPolicy limiting who reaches the proxy, for clusters whose network +# plugin enforces them. /ui, /api/cache, /api/mirror and /_internal/scan-fetch +# have no authentication of their own, so only trusted clients should reach +# the proxy. A NetworkPolicy works on addresses and ports, not paths: to keep +# a path from some clients, restrict it at the ingress or gateway as well. +networkPolicy: + enabled: false + # Peers allowed to reach the proxy's port: the ingress controller or + # gateway, the build pods, and the Prometheus scraping /metrics. Required + # when enabled. The pod of helm test is always allowed. + from: [] + # - namespaceSelector: + # matchLabels: + # kubernetes.io/metadata.name: ingress-nginx + # - podSelector: + # matchLabels: + # app: ci-runner + # Egress rules. Empty leaves egress open; once set, everything else is + # denied, so allow DNS, the upstream registries, the database and object + # storage. + egress: [] + # - ports: + # - protocol: UDP + # port: 53 + # - protocol: TCP + # port: 53 + # - to: + # - ipBlock: + # cidr: 0.0.0.0/0 + # ports: + # - protocol: TCP + # port: 443 + # Prometheus Operator ServiceMonitor for the /metrics endpoint. Requires the # monitoring.coreos.com/v1 CRDs to be installed in the cluster. serviceMonitor: From 503bfe528e63c44401787d0bc874611ec79ce595 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 5 Dec 2026 19:57:20 +0100 Subject: [PATCH 5/5] Refuse database passwords in query strings and keyword DSNs The ConfigMap check only caught a password in the URL's userinfo, so postgres://proxy@db/proxy?password=secret and a keyword DSN like host=db user=proxy password=secret still put the password into the ConfigMap in plain text. Both forms are refused now; password-free URLs and DSNs render as before. The README also notes that other NetworkPolicies selecting the proxy pods add their allow rules to the chart's, so they can keep /ui and the APIs open. --- .github/workflows/ci.yml | 8 ++++++++ deploy/charts/proxy/README.md | 11 +++++++++-- deploy/charts/proxy/templates/_helpers.tpl | 7 ++++++- deploy/charts/proxy/values.yaml | 5 +++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9d2319ee..e1d67f5d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -207,6 +207,14 @@ jobs: --set 'config.data.upstream.auth.npm.token=plain' fails helm template proxy deploy/charts/proxy \ --set-string 'config.data.database.url=postgres://proxy:secret@db/proxy' + fails helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=postgres://proxy@db/proxy?password=secret' + fails helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=host=db user=proxy password=secret' + helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=host=db user=proxy dbname=proxy' >/dev/null + helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=postgres://proxy@db/proxy?sslmode=require' >/dev/null helm template proxy deploy/charts/proxy \ --set 'config.data.upstream.auth.npm.token=plain' \ --set config.allowSecretsInConfigMap=true >/dev/null diff --git a/deploy/charts/proxy/README.md b/deploy/charts/proxy/README.md index 38886220..002089cb 100644 --- a/deploy/charts/proxy/README.md +++ b/deploy/charts/proxy/README.md @@ -39,8 +39,9 @@ extraEnv: Secrets in `config.data` would be stored in the ConfigMap in plain text, so the chart refuses to render a `token`, `password`, `header_value` or `signing_key` that isn't a `${VAR}` reference, or a `database.url` with a -password. The proxy expands `${VAR}` in those keys, so the value can come from -a Secret: +password, whether as `user:password@`, as `?password=` or as a `password=` +keyword (set such a URL through `PROXY_DATABASE_URL` instead). The proxy +expands `${VAR}` in the other keys, so the value can come from a Secret: ```yaml config: @@ -122,6 +123,12 @@ List the ingress controller or gateway, the clients that pull packages, and Prometheus if it scrapes `/metrics`. `networkPolicy.egress` restricts outgoing traffic as well; once set, everything it doesn't list is denied, DNS included. +The chart's policy doesn't replace other NetworkPolicies that select the proxy +pods: Kubernetes adds their allow rules together, so a policy that admits all +traffic keeps `/ui`, `/api/cache`, `/api/mirror` and `/_internal/scan-fetch` +open to everyone it admits. Look for such policies in the namespace and +restrict them, for ingress and egress. + A NetworkPolicy sees addresses and ports, not paths, and the cluster's network plugin has to enforce it. To keep `/ui` or the APIs from clients that may pull packages, restrict those paths at the ingress or gateway too. diff --git a/deploy/charts/proxy/templates/_helpers.tpl b/deploy/charts/proxy/templates/_helpers.tpl index 2a0926da..326dce6a 100644 --- a/deploy/charts/proxy/templates/_helpers.tpl +++ b/deploy/charts/proxy/templates/_helpers.tpl @@ -128,7 +128,12 @@ is set without a ${VAR} reference. Called with a dict of path and value. {{- $data := .Values.config.data | default dict -}} {{- $secrets := include "proxy.secretPaths" (dict "path" "" "value" $data) | trim | splitList " " | compact -}} {{- $database := get $data "database" | default dict -}} -{{- if and (kindIs "map" $database) (regexMatch "://[^/@]*:[^/@]+@" (get $database "url" | default "" | toString)) -}} +{{- $url := "" -}} +{{- if kindIs "map" $database -}} +{{- $url = get $database "url" | default "" | toString -}} +{{- end -}} +{{- /* A password as userinfo, as a query parameter or as a keyword DSN pair. */ -}} +{{- if or (regexMatch `://[^/@]*:[^/@]+@` $url) (regexMatch `(?i)[?&]password=[^&]` $url) (regexMatch `(?i)(^|\s)password\s*=\s*[^\s]` $url) -}} {{- $secrets = append $secrets "database.url" -}} {{- end -}} {{- if $secrets -}} diff --git a/deploy/charts/proxy/values.yaml b/deploy/charts/proxy/values.yaml index 2f337e6b..024c73dd 100644 --- a/deploy/charts/proxy/values.yaml +++ b/deploy/charts/proxy/values.yaml @@ -60,8 +60,9 @@ serviceAccount: config: # The chart refuses to render config.data with a token, password, # header_value or signing_key set without a ${VAR} reference, or a - # database.url with a password: a ConfigMap is readable by anyone allowed to - # read ConfigMaps in the namespace. Set this to render them anyway. + # database.url with a password in any form (user:password@, ?password= or a + # password= keyword): a ConfigMap is readable by anyone allowed to read + # ConfigMaps in the namespace. Set this to render them anyway. allowSecretsInConfigMap: false existingConfigMap: "" existingConfigMapKey: config.yaml