diff --git a/charts/netbird/docs/configuration.md b/charts/netbird/docs/configuration.md index ac864d1..ce4deb3 100644 --- a/charts/netbird/docs/configuration.md +++ b/charts/netbird/docs/configuration.md @@ -74,7 +74,7 @@ Each component renders `repository:tag`. Set `image.digest` (`sha256:...`) to re Each component has a `rootless` value, `false` by default. Setting it to `true` applies the `restricted` Pod Security Standard to that component: `runAsNonRoot`, the `RuntimeDefault` seccomp profile, no privilege escalation, and no capabilities. The container also moves to an unprivileged port (dashboard and management `8080`, signal `10000`; relay always uses `33080`). The Service ports do not change. -The backend images default to root, so rootless management, signal and relay run as `65532:65532` with `fsGroup: 65532`. A rootless dashboard runs the `-rootless` variant of its tag (`v2.96.0` renders `v2.96.0-rootless`; published from `v2.95.0`), which runs as `10001:0` and listens on `8080`. A tag that already ends in `-rootless` is used as is, and `image.digest` is used as given, so it must be a rootless image's digest. [`examples/rootless`](../examples/rootless) enables every component: +The backend images default to root, so rootless management, signal and relay run as `65532:65532` with `fsGroup: 65532`. Their root filesystem is read-only (`readOnlyRootFilesystem: true`): management writes only to its data directory and to an `emptyDir` on `/tmp` (the geolocation download), and signal and relay write nothing unless their log file or Let's Encrypt data directory is set to a path, which then needs a volume through `volumes` and `volumeMounts`. The dashboard image rewrites its nginx config and html at start-up, so its root filesystem stays writable until the image changes. A rootless dashboard runs the `-rootless` variant of its tag (`v2.96.0` renders `v2.96.0-rootless`; published from `v2.95.0`), which runs as `10001:0` and listens on `8080`. A tag that already ends in `-rootless` is used as is, and `image.digest` is used as given, so it must be a rootless image's digest. [`examples/rootless`](../examples/rootless) enables every component: ```yaml dashboard: diff --git a/charts/netbird/templates/_helpers.tpl b/charts/netbird/templates/_helpers.tpl index d9a5b92..51e5f97 100644 --- a/charts/netbird/templates/_helpers.tpl +++ b/charts/netbird/templates/_helpers.tpl @@ -43,10 +43,14 @@ {{- end -}} {{- end -}} -{{/* Restricted Pod Security Standard defaults for rootless components. Context: kind (pod, backendPod or container). */}} +{{/* Restricted Pod Security Standard defaults for rootless components. Context: kind (pod, backendPod, container or backendContainer). */}} {{- define "netbird.rootlessSecurityContext" -}} -{{- if eq .kind "container" -}} +{{- if has .kind (list "container" "backendContainer") -}} allowPrivilegeEscalation: false +{{- if eq .kind "backendContainer" }} +{{- /* Not the dashboard: its image rewrites the nginx config and html at start-up. */}} +readOnlyRootFilesystem: true +{{- end }} capabilities: drop: - ALL @@ -67,7 +71,7 @@ seccompProfile: Security context: the rootless defaults when rootless is set, then each key of securityContext replaces its default (null removes it). Pod contexts lose runAsUser, runAsGroup and fsGroup on OpenShift. -Context: root, rootless, kind (pod, backendPod or container), securityContext. +Context: root, rootless, kind (pod, backendPod, container or backendContainer), securityContext. */}} {{- define "netbird.securityContext" -}} {{- $context := dict -}} @@ -81,7 +85,7 @@ Context: root, rootless, kind (pod, backendPod or container), securityContext. {{- $_ := set $context $key $value -}} {{- end -}} {{- end -}} -{{- if ne .kind "container" -}} +{{- if not (has .kind (list "container" "backendContainer")) -}} {{- $mode := .root.Values.global.compatibility.openshift.adaptSecurityContext -}} {{- if not (has $mode (list "auto" "force" "disabled")) -}} {{- fail (printf "global.compatibility.openshift.adaptSecurityContext must be auto, force or disabled, got %q" $mode) -}} diff --git a/charts/netbird/templates/split/management/deployment.yaml b/charts/netbird/templates/split/management/deployment.yaml index dd19cd1..3edc296 100644 --- a/charts/netbird/templates/split/management/deployment.yaml +++ b/charts/netbird/templates/split/management/deployment.yaml @@ -80,7 +80,7 @@ spec: {{- toYaml . | nindent 12 }} {{- end }} securityContext: - {{- include "netbird.securityContext" (dict "root" . "rootless" $values.rootless "kind" "container" "securityContext" $values.container.securityContext) | nindent 12 }} + {{- include "netbird.securityContext" (dict "root" . "rootless" $values.rootless "kind" "backendContainer" "securityContext" $values.container.securityContext) | nindent 12 }} {{- if or (not $values.overrideConfig) $values.env $values.envRaw $values.envFromSecret }} env: {{- if not $values.overrideConfig }} @@ -125,6 +125,10 @@ spec: subPath: management.json - name: data mountPath: {{ $values.persistence.mountPath }} + {{- if $values.rootless }} + - name: tmp + mountPath: /tmp + {{- end }} {{- with $values.volumeMounts }} {{- toYaml . | nindent 12 }} {{- end }} @@ -140,6 +144,11 @@ spec: {{- /* The image's data directory is root-owned; give the non-root user a writable one. */}} emptyDir: {} {{- end }} + {{- if $values.rootless }} + {{- /* The root filesystem is read-only; the geolocation download goes to /tmp. */}} + - name: tmp + emptyDir: {} + {{- end }} {{- with $values.volumes }} {{- toYaml . | nindent 8 }} {{- end }} diff --git a/charts/netbird/templates/split/relay/deployment.yaml b/charts/netbird/templates/split/relay/deployment.yaml index 373a546..fc4e93a 100644 --- a/charts/netbird/templates/split/relay/deployment.yaml +++ b/charts/netbird/templates/split/relay/deployment.yaml @@ -78,7 +78,7 @@ spec: {{- toYaml . | nindent 12 }} {{- end }} securityContext: - {{- include "netbird.securityContext" (dict "root" . "rootless" $values.rootless "kind" "container" "securityContext" $values.container.securityContext) | nindent 12 }} + {{- include "netbird.securityContext" (dict "root" . "rootless" $values.rootless "kind" "backendContainer" "securityContext" $values.container.securityContext) | nindent 12 }} env: - name: NB_AUTH_SECRET valueFrom: diff --git a/charts/netbird/templates/split/signal/deployment.yaml b/charts/netbird/templates/split/signal/deployment.yaml index e72b3a5..fd2a08d 100644 --- a/charts/netbird/templates/split/signal/deployment.yaml +++ b/charts/netbird/templates/split/signal/deployment.yaml @@ -66,7 +66,7 @@ spec: {{- toYaml . | nindent 12 }} {{- end }} securityContext: - {{- include "netbird.securityContext" (dict "root" . "rootless" $values.rootless "kind" "container" "securityContext" $values.container.securityContext) | nindent 12 }} + {{- include "netbird.securityContext" (dict "root" . "rootless" $values.rootless "kind" "backendContainer" "securityContext" $values.container.securityContext) | nindent 12 }} {{- if or $values.config.pprofAddress $values.env $values.envRaw $values.envFromSecret }} env: {{- with $values.config.pprofAddress }} diff --git a/charts/netbird/tests/render.sh b/charts/netbird/tests/render.sh index bfcbee1..6383401 100755 --- a/charts/netbird/tests/render.sh +++ b/charts/netbird/tests/render.sh @@ -129,6 +129,7 @@ lacks "nodePort" "$out" # 20. rootless is off by default: no security context, port 80. out="$(render "${required[@]}")" lacks "runAsNonRoot" "$out" +lacks "readOnlyRootFilesystem" "$out"; lacks "mountPath: /tmp" "$out" test "$(grep -c "containerPort: 80$" <<<"$out")" = 3 || { echo "FAIL: want port 80 on dashboard, management and signal"; exit 1; } has 'image: "netbirdio/dashboard:v2.96.0"' "$out" @@ -138,6 +139,8 @@ out="$(render "${required[@]}" "${rootless[@]}")" test "$(grep -c "runAsNonRoot: true" <<<"$out")" = 4 || { echo "FAIL: want runAsNonRoot on 4 pods"; exit 1; } test "$(grep -c "allowPrivilegeEscalation: false" <<<"$out")" = 4 || { echo "FAIL: want allowPrivilegeEscalation false on 4 containers"; exit 1; } test "$(grep -c "runAsUser: 65532" <<<"$out")" = 3 || { echo "FAIL: want runAsUser on 3 backend pods"; exit 1; } +test "$(grep -c "readOnlyRootFilesystem: true" <<<"$out")" = 3 || { echo "FAIL: want a read-only root filesystem on 3 backend containers"; exit 1; } +test "$(grep -c "mountPath: /tmp$" <<<"$out")" = 1 || { echo "FAIL: want /tmp on management only"; exit 1; } test "$(grep -c "containerPort: 8080$" <<<"$out")" = 2 || { echo "FAIL: want port 8080 on dashboard and management"; exit 1; } has "containerPort: 10000" "$out" has 'image: "netbirdio/dashboard:v2.96.0-rootless"' "$out" @@ -158,6 +161,8 @@ out="$(render "${required[@]}" --set backend.split.signal.rootless=true \ --set backend.split.signal.container.port=9999 --show-only templates/split/signal/deployment.yaml)" has "runAsUser: 1000" "$out"; has "runAsGroup: 65532" "$out"; lacks "fsGroup" "$out" has "containerPort: 9999" "$out"; has "--port=9999" "$out" +out="$(render "${required[@]}" --set backend.split.management.rootless=true --set backend.split.management.container.securityContext.readOnlyRootFilesystem=null --show-only templates/split/management/deployment.yaml)" +lacks "readOnlyRootFilesystem" "$out"; has "allowPrivilegeEscalation: false" "$out" # 20d. A rootless dashboard runs the -rootless tag variant; an explicit variant or a digest is kept. out="$(render "${required[@]}" --set dashboard.rootless=true --set dashboard.image.tag=v2.96.0-rootless --show-only templates/dashboard/deployment.yaml)" diff --git a/charts/netbird/values.yaml b/charts/netbird/values.yaml index 3589c11..8e24a29 100644 --- a/charts/netbird/values.yaml +++ b/charts/netbird/values.yaml @@ -21,7 +21,8 @@ dashboard: replicaCount: 1 # Restricted Pod Security Standard defaults, merged under pod and container # securityContext, container port 8080, and the image tag's -rootless variant - # (image.digest must already be one). See examples/rootless. + # (image.digest must already be one). The root filesystem stays writable: the + # image rewrites its nginx config and html at start-up. See examples/rootless. rootless: false image: @@ -124,7 +125,8 @@ backend: namespace: "" replicaCount: 1 # Restricted Pod Security Standard defaults, merged under pod and container - # securityContext: non-root user 65532 with fsGroup, and container port 8080. + # securityContext: non-root user 65532 with fsGroup, a read-only root + # filesystem with an emptyDir on /tmp, and container port 8080. # See examples/rootless. rootless: false @@ -339,8 +341,8 @@ backend: namespace: "" replicaCount: 1 # Restricted Pod Security Standard defaults, merged under pod and container - # securityContext: non-root user 65532 with fsGroup, and container port 10000. - # See examples/rootless. + # securityContext: non-root user 65532 with fsGroup, a read-only root + # filesystem, and container port 10000. See examples/rootless. rootless: false image: @@ -439,8 +441,8 @@ backend: namespace: "" replicaCount: 1 # Restricted Pod Security Standard defaults, merged under pod and container - # securityContext: non-root user 65532 with fsGroup. - # See examples/rootless. + # securityContext: non-root user 65532 with fsGroup and a read-only root + # filesystem. See examples/rootless. rootless: false image: