diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c35abc..bee425d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,12 @@ runs, pinned by digest. isn't one of the expected addresses. It never fails an install. Its token may create events and nothing else. `podLabels` on each server group, for an egress gateway that selects the mta pods. +- Mail in through a load balancer that sends PROXY protocol v2 + (`server.services.mail.proxyProtocol`, off by default): the mail Service + sends each port to a second listener in the pods that takes the client's + address from the PROXY header, believed only from `trustedNetworks`, the + load balancer's own addresses. It needs `externalTrafficPolicy: Local`, and + server 27.1-rc.32. - `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 diff --git a/charts/inbuxa/templates/_helpers.tpl b/charts/inbuxa/templates/_helpers.tpl index 2eb007d..c55425c 100644 --- a/charts/inbuxa/templates/_helpers.tpl +++ b/charts/inbuxa/templates/_helpers.tpl @@ -72,6 +72,27 @@ inbuxa.org/group: {{ .group }} {{- join "," $items -}} {{- end -}} +{{/* +K-C6: the mail listeners that get a PROXY copy, as INBUXA_MAIL_PROXY_LISTENERS +takes them ("smtp=10025,imaps=10993"): those the pods listen on and the mail +Service sends to. Empty when PROXY protocol is off. +*/}} +{{- define "inbuxa.server.proxyListeners" -}} +{{- $mail := .Values.server.services.mail -}} +{{- $items := list -}} +{{- if and $mail.enabled $mail.proxyProtocol.enabled -}} +{{- range $name := list "smtp" "submissions" "imaps" "pop3s" "sieve" -}} +{{- $target := int (index $.Values.server.listeners $name) -}} +{{- $port := int (index $mail.ports $name) -}} +{{- $proxy := int (index $mail.proxyProtocol.ports $name) -}} +{{- if and $target $port $proxy -}} +{{- $items = append $items (printf "%s=%d" $name $proxy) -}} +{{- end -}} +{{- end -}} +{{- end -}} +{{- join "," $items -}} +{{- end -}} + {{/* A front end's image, by digest when one is set. Its tag has no default: the webmail and the console are versioned apart from the server. Call with diff --git a/charts/inbuxa/templates/networkpolicy.yaml b/charts/inbuxa/templates/networkpolicy.yaml index d910af9..9653027 100644 --- a/charts/inbuxa/templates/networkpolicy.yaml +++ b/charts/inbuxa/templates/networkpolicy.yaml @@ -58,6 +58,9 @@ spec: {{- $port := int (index $server.listeners $name) }} {{- if $port }}{{ $public = append $public $port }}{{ end }} {{- end }} + {{- range $item := splitList "," (include "inbuxa.server.proxyListeners" $root) }} + {{- if $item }}{{ $public = append $public (int (index (splitList "=" $item) 1)) }}{{ end }} + {{- end }} {{- if $public }} # Mail and the server's own TLS listener: clients come from anywhere - ports: diff --git a/charts/inbuxa/templates/server/deployment.yaml b/charts/inbuxa/templates/server/deployment.yaml index 19b84af..b78eb9b 100644 --- a/charts/inbuxa/templates/server/deployment.yaml +++ b/charts/inbuxa/templates/server/deployment.yaml @@ -12,6 +12,10 @@ are interchangeable: Deployments, not StatefulSets. {{- $g := index $server.groups $group }} {{- if $g.enabled }} {{- $labels := include "inbuxa.server.selectorLabels" (dict "root" $root "group" $group) }} +{{- $proxyListeners := "" }} +{{- if has $group $server.services.mail.groups }} +{{- $proxyListeners = include "inbuxa.server.proxyListeners" $root }} +{{- end }} --- apiVersion: apps/v1 kind: Deployment @@ -151,6 +155,14 @@ spec: - name: INBUXA_HTTP_TRUSTED_PROXIES value: {{ join "," . | quote }} {{- end }} + {{- if $proxyListeners }} + # PROXY protocol copies of the mail listeners, believed from + # the load balancer only + - name: INBUXA_MAIL_PROXY_LISTENERS + value: {{ $proxyListeners | quote }} + - name: INBUXA_MAIL_PROXY_TRUSTED_NETWORKS + value: {{ join "," $server.services.mail.proxyProtocol.trustedNetworks | quote }} + {{- end }} {{- /* The front ends, registered as OAuth clients on every start */}} {{- with $root.Values.webmail }} {{- if .enabled }} @@ -177,6 +189,14 @@ spec: protocol: TCP {{- end }} {{- end }} + {{- range $item := splitList "," $proxyListeners }} + {{- if $item }} + {{- $kv := splitList "=" $item }} + - name: p-{{ index $kv 0 }} + containerPort: {{ index $kv 1 }} + protocol: TCP + {{- end }} + {{- end }} startupProbe: httpGet: path: /healthz/live diff --git a/charts/inbuxa/templates/server/service-mail.yaml b/charts/inbuxa/templates/server/service-mail.yaml index 1b1ed43..c21f5d9 100644 --- a/charts/inbuxa/templates/server/service-mail.yaml +++ b/charts/inbuxa/templates/server/service-mail.yaml @@ -15,6 +15,15 @@ address. {{- fail (printf "server.services.mail.groups names %q, which is not enabled" $group) }} {{- end }} {{- end }} +{{- $proxy := $svc.proxyProtocol }} +{{- if $proxy.enabled }} +{{- if not $proxy.trustedNetworks }} +{{- fail "server.services.mail.proxyProtocol.trustedNetworks is required: the load balancer's addresses, the only ones whose PROXY headers are believed" }} +{{- end }} +{{- if and (ne $svc.type "ClusterIP") (ne $svc.externalTrafficPolicy "Local") }} +{{- fail "server.services.mail.proxyProtocol needs externalTrafficPolicy Local: with Cluster, connections arrive from the nodes, and trusting them lets anyone claim any address" }} +{{- end }} +{{- end }} apiVersion: v1 kind: Service metadata: @@ -49,7 +58,12 @@ spec: {{- if and $target $port }} - name: {{ $name }} port: {{ $port }} + {{- if and $proxy.enabled (int (index $proxy.ports $name)) }} + # The pods' PROXY copy of the listener (K-C6) + targetPort: p-{{ $name }} + {{- else }} targetPort: {{ $name }} + {{- end }} protocol: TCP {{- end }} {{- end }} diff --git a/charts/inbuxa/tests/networkpolicy_test.yaml b/charts/inbuxa/tests/networkpolicy_test.yaml index 73a104c..efaaae5 100644 --- a/charts/inbuxa/tests/networkpolicy_test.yaml +++ b/charts/inbuxa/tests/networkpolicy_test.yaml @@ -126,3 +126,24 @@ tests: asserts: - hasDocuments: count: 0 + + - it: opens the PROXY copies of the mail listeners too + set: + server.services.mail.proxyProtocol.enabled: true + server.services.mail.proxyProtocol.trustedNetworks: [10.0.0.10] + 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} + - {port: 10025, protocol: TCP} + - {port: 10465, protocol: TCP} + - {port: 10993, protocol: TCP} + - {port: 14190, protocol: TCP} diff --git a/charts/inbuxa/tests/server_test.yaml b/charts/inbuxa/tests/server_test.yaml index d32eef4..5aea33e 100644 --- a/charts/inbuxa/tests/server_test.yaml +++ b/charts/inbuxa/tests/server_test.yaml @@ -256,3 +256,56 @@ tests: - contains: path: spec.template.spec.containers[0].env content: {name: INBUXA_HTTP_TRUSTED_PROXIES, value: "100.64.0.0/10,10.0.0.0/8"} + + - it: sends mail to PROXY copies of the listeners, believed from the load balancer only + set: + server.services.mail.proxyProtocol.enabled: true + server.services.mail.proxyProtocol.trustedNetworks: [10.0.0.10, 10.0.0.11] + asserts: + - equal: + path: spec.ports + value: + - {name: smtp, port: 25, targetPort: p-smtp, protocol: TCP} + - {name: submissions, port: 465, targetPort: p-submissions, protocol: TCP} + - {name: imaps, port: 993, targetPort: p-imaps, protocol: TCP} + - {name: sieve, port: 4190, targetPort: p-sieve, protocol: TCP} + template: server/service-mail.yaml + - contains: + path: spec.template.spec.containers[0].env + content: {name: INBUXA_MAIL_PROXY_LISTENERS, value: "smtp=10025,submissions=10465,imaps=10993,sieve=14190"} + template: server/deployment.yaml + documentIndex: 0 + - contains: + path: spec.template.spec.containers[0].env + content: {name: INBUXA_MAIL_PROXY_TRUSTED_NETWORKS, value: "10.0.0.10,10.0.0.11"} + template: server/deployment.yaml + documentIndex: 1 + - contains: + path: spec.template.spec.containers[0].ports + content: {name: p-submissions, containerPort: 10465, protocol: TCP} + template: server/deployment.yaml + documentIndex: 0 + # core takes no mail from outside + - notContains: + path: spec.template.spec.containers[0].env + content: {name: INBUXA_MAIL_PROXY_LISTENERS, value: "smtp=10025,submissions=10465,imaps=10993,sieve=14190"} + template: server/deployment.yaml + documentIndex: 2 + + - it: refuses PROXY protocol without the load balancer's addresses + set: + server.services.mail.proxyProtocol.enabled: true + template: server/service-mail.yaml + asserts: + - failedTemplate: + errorPattern: trustedNetworks is required + + - it: refuses PROXY protocol behind the Cluster traffic policy + set: + server.services.mail.proxyProtocol.enabled: true + server.services.mail.proxyProtocol.trustedNetworks: [10.0.0.10] + server.services.mail.externalTrafficPolicy: Cluster + template: server/service-mail.yaml + asserts: + - failedTemplate: + errorPattern: needs externalTrafficPolicy Local diff --git a/charts/inbuxa/values.schema.json b/charts/inbuxa/values.schema.json index 748855b..728f833 100644 --- a/charts/inbuxa/values.schema.json +++ b/charts/inbuxa/values.schema.json @@ -471,6 +471,44 @@ "$ref": "#/definitions/port" } } + }, + "proxyProtocol": { + "type": "object", + "additionalProperties": false, + "properties": { + "enabled": { + "type": "boolean" + }, + "trustedNetworks": { + "description": "The load balancer's addresses, as it connects to the pods: addresses or networks of /8 or narrower.", + "type": "array", + "items": { + "type": "string", + "minLength": 1 + } + }, + "ports": { + "type": "object", + "additionalProperties": false, + "properties": { + "smtp": { + "$ref": "#/definitions/listenerPort" + }, + "submissions": { + "$ref": "#/definitions/listenerPort" + }, + "imaps": { + "$ref": "#/definitions/listenerPort" + }, + "pop3s": { + "$ref": "#/definitions/listenerPort" + }, + "sieve": { + "$ref": "#/definitions/listenerPort" + } + } + } + } } } } diff --git a/charts/inbuxa/values.yaml b/charts/inbuxa/values.yaml index d407c2f..25706f1 100644 --- a/charts/inbuxa/values.yaml +++ b/charts/inbuxa/values.yaml @@ -214,6 +214,25 @@ server: imaps: 993 pop3s: 995 sieve: 4190 + # PROXY protocol v2 from the load balancer, instead of keeping the + # client's address with externalTrafficPolicy Local alone. The pods get + # a second listener for each mail port (these ports in the pod) that + # takes the client's address from the PROXY header, believed only from + # trustedNetworks: the load balancer's own addresses, as it connects to + # the pods. Keep externalTrafficPolicy Local: with Cluster, connections + # arrive from the nodes, and trusting nodes lets anyone who reaches a + # port claim any address. The load balancer listens on the standard + # ports (25, 465, ...): the server tells mail from the internet by port + # 25, which it reads from the PROXY header. Needs server 27.1-rc.32. + proxyProtocol: + enabled: false + trustedNetworks: [] + ports: + smtp: 10025 + submissions: 10465 + imaps: 10993 + pop3s: 10995 + sieve: 14190 # The webmail, signing people in through the server's own page (OAuth). Off # by default: it needs its own address, and the server's publicUrl.