From 2ac8fa8527abf5467e62ccbbb75bc481da431eae Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 18:03:18 -0700 Subject: [PATCH] Security defaults: NetworkPolicies, and the restricted map networkPolicy, on by default: every pod of the release starts from deny all, in and out. The server takes mail on its listeners from anywhere and connects anywhere; httpFrom narrows who reaches the HTTP ports, and serverEgress narrows the server's and the Jobs' egress to DNS plus those rules. The webmail reaches DNS, the server's http port and, for remote images and calendars, TCP 80 and 443. The console reaches nothing. README: the security defaults and the NetworkPolicy map. --- CHANGELOG.md | 7 + README.md | 22 +++ charts/inbuxa/ci/everything-values.yaml | 9 ++ charts/inbuxa/templates/_helpers.tpl | 28 ++++ charts/inbuxa/templates/networkpolicy.yaml | 156 ++++++++++++++++++++ charts/inbuxa/tests/networkpolicy_test.yaml | 128 ++++++++++++++++ charts/inbuxa/values.schema.json | 24 +++ charts/inbuxa/values.yaml | 20 +++ 8 files changed, 394 insertions(+) create mode 100644 charts/inbuxa/templates/networkpolicy.yaml create mode 100644 charts/inbuxa/tests/networkpolicy_test.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c6810c..a45e917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,3 +45,10 @@ runs, pinned by digest. clients. - `routes`: the server's, the webmail's and the console's public addresses, each by host name, as Gateway API HTTPRoutes or one Ingress. +- `networkPolicy`, on by default: every pod starts from deny all, in and + out, and gets only what it needs. The server takes mail on its listeners + from anywhere and connects anywhere; `networkPolicy.httpFrom` and + `networkPolicy.serverEgress` narrow its HTTP port and its egress. The + webmail reaches DNS, the server and the web (`networkPolicy.webmailInternet` + for remote images); the console reaches nothing. Every pod passes Pod + Security "restricted", tested in enforce mode. diff --git a/README.md b/README.md index ac14c38..f2adc55 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,28 @@ doesn't install anything yet. Release candidates start with `27.1.0-rc.1`. - `tools/check.sh`: every check CI runs; see [CONTRIBUTING.md](CONTRIBUTING.md) - [CHANGELOG.md](CHANGELOG.md) +## Security defaults + +Every pod passes Pod Security "restricted" in enforce mode: non-root, every +capability dropped, no privilege escalation, a read-only root filesystem, +the RuntimeDefault seccomp profile. No pod gets a service-account token except +the upgrade Job, which scales the server down for a layout change through a +Role that only it holds. Secrets reach the pods as mounted files, never as +values. One is still an environment variable until the chart moves to server +27.1-rc.32: the webmail's OAuth client secret, on the server's side. + +NetworkPolicies are on by default (`networkPolicy`) and need a CNI that +enforces them. Every pod starts from deny all, in and out: + +| Pods | In | Out | +|---|---|---| +| server | its mail listeners and `https` from anywhere; `http` from `networkPolicy.httpFrom` and the webmail (anywhere when `httpFrom` is empty) | anywhere, or DNS plus `networkPolicy.serverEgress` | +| seed and upgrade Jobs | nothing | as the server | +| webmail | 8080 from `networkPolicy.httpFrom` (anywhere when empty) | DNS, the server's `http` port, and TCP 80 and 443 anywhere for remote images and calendars (`networkPolicy.webmailInternet`) | +| console | 8080 from `networkPolicy.httpFrom` (anywhere when empty) | nothing: the browser calls the server | + +The kubelet's probes come from the node, which NetworkPolicy always allows. + Installs on real clusters, the mail tests and the upgrade tests are in [inbuxa-k8s-lab](https://git.coffeylabs.org/inbuxa/inbuxa-k8s-lab). The guide is at [docs.inbuxa.org](https://docs.inbuxa.org/). diff --git a/charts/inbuxa/ci/everything-values.yaml b/charts/inbuxa/ci/everything-values.yaml index b267b1f..3054075 100644 --- a/charts/inbuxa/ci/everything-values.yaml +++ b/charts/inbuxa/ci/everything-values.yaml @@ -87,3 +87,12 @@ routes: enabled: true parentRefs: [{name: public, namespace: gateway, sectionName: https}] annotations: {example.org/route: public} +networkPolicy: + httpFrom: + - namespaceSelector: + matchLabels: {kubernetes.io/metadata.name: gateway} + serverEgress: + - to: + - namespaceSelector: + matchLabels: {kubernetes.io/metadata.name: data} + webmailInternet: false diff --git a/charts/inbuxa/templates/_helpers.tpl b/charts/inbuxa/templates/_helpers.tpl index 0e491b4..2eb007d 100644 --- a/charts/inbuxa/templates/_helpers.tpl +++ b/charts/inbuxa/templates/_helpers.tpl @@ -105,3 +105,31 @@ Call with (dict "url" .Values.webmail.publicUrl "name" "webmail"). {{- fail (printf "%s.publicUrl: %q should be https://host, with no path" .name .url) -}} {{- end -}} {{- end -}} + +{{/* The server's egress rules (and the Jobs'): anywhere, or DNS plus networkPolicy.serverEgress. */}} +{{- define "inbuxa.networkPolicy.serverEgress" -}} +{{- if .Values.networkPolicy.serverEgress -}} +{{ include "inbuxa.networkPolicy.dns" . }} +{{ toYaml .Values.networkPolicy.serverEgress }} +{{- else -}} +# A mail server delivers to, and looks up, anywhere on the internet +- {} +{{- end -}} +{{- end -}} + +{{/* DNS, to whichever resolver the cluster uses. */}} +{{- define "inbuxa.networkPolicy.dns" -}} +- ports: + - {port: 53, protocol: UDP} + - {port: 53, protocol: TCP} +{{- end -}} + +{{/* The webmail's and the console's 8080: from networkPolicy.httpFrom, or anywhere. */}} +{{- define "inbuxa.networkPolicy.frontEndIngress" -}} +- ports: + - {port: 8080, protocol: TCP} + {{- with .Values.networkPolicy.httpFrom }} + from: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end -}} diff --git a/charts/inbuxa/templates/networkpolicy.yaml b/charts/inbuxa/templates/networkpolicy.yaml new file mode 100644 index 0000000..d2a91cc --- /dev/null +++ b/charts/inbuxa/templates/networkpolicy.yaml @@ -0,0 +1,156 @@ +{{/* +SPDX-FileCopyrightText: 2026 Coffey Labs LLC +SPDX-License-Identifier: Apache-2.0 + +NetworkPolicies, on by default. Every pod the chart makes starts from deny +all, in and out; the policies after it open only what each one needs: + + pods in out + server its mail listeners and https from anywhere (networkPolicy.serverEgress + anywhere; http from the webmail narrows it) + and from networkPolicy.httpFrom + seed, upgrade nothing as the server + webmail 8080 from networkPolicy.httpFrom DNS; the server's http port; TCP 80 + and 443 anywhere, for remote images + and calendars (networkPolicy.webmailInternet) + console 8080 from networkPolicy.httpFrom nothing (the browser calls the server) + +httpFrom empty means from anywhere: the ingress controller or gateway that +routes to these ports is in a namespace the chart can't know. The kubelet's +probes come from the node, which NetworkPolicy always allows. +*/}} +{{- $root := . }} +{{- $np := .Values.networkPolicy }} +{{- if $np.enabled }} +{{- $server := .Values.server }} +{{- $http := int $server.listeners.http }} +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "inbuxa.fullname" $root }}-deny-all + namespace: {{ $root.Release.Namespace }} + labels: + {{- include "inbuxa.labels" $root | nindent 4 }} +spec: + podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 6 }} + policyTypes: [Ingress, Egress] +--- +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "inbuxa.fullname" $root }}-server + namespace: {{ $root.Release.Namespace }} + labels: + {{- include "inbuxa.labels" $root | nindent 4 }} + app.kubernetes.io/component: server +spec: + podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 6 }} + app.kubernetes.io/component: server + policyTypes: [Ingress, Egress] + ingress: + {{- $public := list }} + {{- range $name := list "smtp" "submissions" "imaps" "pop3s" "sieve" "https" }} + {{- $port := int (index $server.listeners $name) }} + {{- if $port }}{{ $public = append $public $port }}{{ end }} + {{- end }} + {{- if $public }} + # Mail and the server's own TLS listener: clients come from anywhere + - ports: + {{- range $public }} + - {port: {{ . }}, protocol: TCP} + {{- end }} + {{- end }} + {{- if $http }} + - ports: + - {port: {{ $http }}, protocol: TCP} + {{- if $np.httpFrom }} + from: + {{- toYaml $np.httpFrom | nindent 8 }} + {{- if $root.Values.webmail.enabled }} + - podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 14 }} + app.kubernetes.io/component: webmail + {{- end }} + {{- end }} + {{- end }} + egress: + {{- include "inbuxa.networkPolicy.serverEgress" $root | nindent 4 }} +--- +# The seed and upgrade Jobs reach the data store as the server does; the +# upgrade Job reaches the Kubernetes API too. Nothing connects to them. +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "inbuxa.fullname" $root }}-jobs + namespace: {{ $root.Release.Namespace }} + labels: + {{- include "inbuxa.labels" $root | nindent 4 }} +spec: + podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 6 }} + matchExpressions: + - {key: app.kubernetes.io/component, operator: In, values: [seed, upgrade]} + policyTypes: [Ingress, Egress] + egress: + {{- include "inbuxa.networkPolicy.serverEgress" $root | nindent 4 }} +{{- if .Values.webmail.enabled }} +--- +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "inbuxa.fullname" $root }}-webmail + namespace: {{ $root.Release.Namespace }} + labels: + {{- include "inbuxa.labels" $root | nindent 4 }} + app.kubernetes.io/component: webmail +spec: + podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 6 }} + app.kubernetes.io/component: webmail + policyTypes: [Ingress, Egress] + ingress: + {{- include "inbuxa.networkPolicy.frontEndIngress" $root | nindent 4 }} + egress: + {{- include "inbuxa.networkPolicy.dns" $root | nindent 4 }} + - ports: + - {port: {{ $http }}, protocol: TCP} + to: + - podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 14 }} + app.kubernetes.io/component: server + {{- if $np.webmailInternet }} + # Remote images (IMAGE_PROXY) and subscribed calendars + - ports: + - {port: 80, protocol: TCP} + - {port: 443, protocol: TCP} + {{- end }} +{{- end }} +{{- if .Values.console.enabled }} +--- +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "inbuxa.fullname" $root }}-console + namespace: {{ $root.Release.Namespace }} + labels: + {{- include "inbuxa.labels" $root | nindent 4 }} + app.kubernetes.io/component: console +spec: + podSelector: + matchLabels: + {{- include "inbuxa.selectorLabels" $root | nindent 6 }} + app.kubernetes.io/component: console + # No egress rule: nginx serves files and never connects out + policyTypes: [Ingress, Egress] + ingress: + {{- include "inbuxa.networkPolicy.frontEndIngress" $root | nindent 4 }} +{{- end }} +{{- end }} diff --git a/charts/inbuxa/tests/networkpolicy_test.yaml b/charts/inbuxa/tests/networkpolicy_test.yaml new file mode 100644 index 0000000..73a104c --- /dev/null +++ b/charts/inbuxa/tests/networkpolicy_test.yaml @@ -0,0 +1,128 @@ +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: Apache-2.0 +suite: network policies +templates: + - networkpolicy.yaml +release: + name: mail + namespace: inbuxa +values: + - ../ci/defaults-values.yaml +tests: + - it: denies everything to every pod of the release first + documentSelector: + path: metadata.name + value: mail-inbuxa-deny-all + asserts: + - equal: + path: spec.podSelector.matchLabels + value: {app.kubernetes.io/name: inbuxa, app.kubernetes.io/instance: mail} + - equal: + path: spec.policyTypes + value: [Ingress, Egress] + - notExists: + path: spec.ingress + - notExists: + path: spec.egress + + - it: opens the server's listeners to anywhere, and its egress + documentSelector: + path: metadata.name + value: mail-inbuxa-server + asserts: + - equal: + path: spec.ingress[0].ports + value: + - {port: 25, protocol: TCP} + - {port: 465, protocol: TCP} + - {port: 993, protocol: TCP} + - {port: 4190, protocol: TCP} + - {port: 443, protocol: TCP} + - equal: + path: spec.ingress[1] + value: {ports: [{port: 8080, protocol: TCP}]} + - equal: + path: spec.egress + value: [{}] + + - it: lets only httpFrom and the webmail reach the server's http port when httpFrom is set + set: + webmail.enabled: true + webmail.publicUrl: https://webmail.example.org + webmail.image.tag: 27.1-rc.7 + server.publicUrl: https://mail.example.org + networkPolicy.httpFrom: + - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: ingress}} + documentSelector: + path: metadata.name + value: mail-inbuxa-server + asserts: + - equal: + path: spec.ingress[1].from + value: + - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: ingress}} + - podSelector: + matchLabels: + app.kubernetes.io/name: inbuxa + app.kubernetes.io/instance: mail + app.kubernetes.io/component: webmail + + - it: narrows the server and the Jobs to DNS and serverEgress + set: + networkPolicy.serverEgress: + - to: [{namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: data}}}] + asserts: + - equal: + path: spec.egress + value: + - ports: [{port: 53, protocol: UDP}, {port: 53, protocol: TCP}] + - to: [{namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: data}}}] + documentSelector: + path: metadata.name + value: mail-inbuxa-jobs + + - it: lets the webmail reach DNS, the server and the web, and the console nothing + set: + server.publicUrl: https://mail.example.org + webmail.enabled: true + webmail.publicUrl: https://webmail.example.org + webmail.image.tag: 27.1-rc.7 + console.enabled: true + console.publicUrl: https://admin.example.org + console.image.tag: 27.1-rc.21 + asserts: + - equal: + path: spec.egress[1].ports + value: [{port: 8080, protocol: TCP}] + documentSelector: {path: metadata.name, value: mail-inbuxa-webmail} + - equal: + path: spec.egress[2].ports + value: [{port: 80, protocol: TCP}, {port: 443, protocol: TCP}] + documentSelector: {path: metadata.name, value: mail-inbuxa-webmail} + - equal: + path: spec.policyTypes + value: [Ingress, Egress] + documentSelector: {path: metadata.name, value: mail-inbuxa-console} + - notExists: + path: spec.egress + documentSelector: {path: metadata.name, value: mail-inbuxa-console} + + - it: keeps the webmail off the web with webmailInternet off + set: + server.publicUrl: https://mail.example.org + webmail.enabled: true + webmail.publicUrl: https://webmail.example.org + webmail.image.tag: 27.1-rc.7 + networkPolicy.webmailInternet: false + documentSelector: {path: metadata.name, value: mail-inbuxa-webmail} + asserts: + - lengthEqual: + path: spec.egress + count: 2 + + - it: can be turned off + set: + networkPolicy.enabled: false + asserts: + - hasDocuments: + count: 0 diff --git a/charts/inbuxa/values.schema.json b/charts/inbuxa/values.schema.json index b047775..b6fab13 100644 --- a/charts/inbuxa/values.schema.json +++ b/charts/inbuxa/values.schema.json @@ -645,6 +645,30 @@ } } }, + "networkPolicy": { + "type": "object", + "additionalProperties": false, + "properties": { + "enabled": { + "type": "boolean" + }, + "httpFrom": { + "type": "array", + "items": { + "type": "object" + } + }, + "serverEgress": { + "type": "array", + "items": { + "type": "object" + } + }, + "webmailInternet": { + "type": "boolean" + } + } + }, "routes": { "type": "object", "additionalProperties": false, diff --git a/charts/inbuxa/values.yaml b/charts/inbuxa/values.yaml index f019501..497dadf 100644 --- a/charts/inbuxa/values.yaml +++ b/charts/inbuxa/values.yaml @@ -267,6 +267,26 @@ console: affinity: {} podAnnotations: {} +# NetworkPolicies: every pod the chart makes denies all traffic in and out +# except what it needs (templates/networkpolicy.yaml has the map). They need +# a CNI that enforces them. +networkPolicy: + enabled: true + # Who may reach the HTTP ports of the server, the webmail and the console: + # your ingress controller or gateway, as NetworkPolicy peers, e.g. + # - namespaceSelector: + # matchLabels: {kubernetes.io/metadata.name: ingress-nginx} + # Empty: from anywhere. The webmail always reaches the server. + httpFrom: [] + # Where the server and its Jobs may connect, as NetworkPolicy egress rules, + # besides DNS. Empty: anywhere, which a mail server needs to deliver mail + # unless it hands everything to a relay. Narrow it to your data store, + # relay and the Kubernetes API (for the upgrade Job) if you can. + serverEgress: [] + # The webmail fetches remote images in mail and subscribed calendars over + # HTTP and HTTPS. Off: only the server. + webmailInternet: true + # Routes from outside to the server's publicUrl, the webmail's and the # console's, each by its host name. Gateway API where the cluster has it; # an Ingress where it doesn't. TLS is the Gateway's or the Ingress's. -- 2.54.0