Hook Jobs under a deny-all on a first install too #34

Merged
jcoffey-dev merged 1 commits from fix/hook-network-policies into main 2026-11-20 05:37:55 +00:00
3 changed files with 95 additions and 2 deletions

No files matched your search

+4 -2
View File
@@ -114,8 +114,10 @@ runs, pinned by digest.
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.
for remote images); the console reaches nothing. The seed and upgrade
Jobs, which run before the release's own policies exist on a first
install, are covered by copies of the two they need, run as hooks first.
Every pod passes Pod Security "restricted", tested in enforce mode.
- Releases (K-C13): a `v*` tag releases the chart from `release/suite.yaml`,
with the server, webmail and console images pinned by digest once their
signatures check out; the chart pushed to GHCR and the Gitea registry and
@@ -19,6 +19,12 @@ all, in and out; the policies after it open only what each one needs:
and calendars (networkPolicy.webmailInternet)
console 8080 from networkPolicy.httpFrom nothing (the browser calls the server)
The seed and upgrade Jobs are hooks, and a hook runs before the release's
own objects exist: on a first install, before any of these policies. So the
deny-all and the jobs' policy are rendered twice, the second time as hooks
that run first (weight -20) and go once the hooks have succeeded, by which
point the release's own copies are there.
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.
@@ -166,4 +172,40 @@ spec:
ingress:
{{- include "inbuxa.networkPolicy.frontEndIngress" $root | nindent 4 }}
{{- end }}
{{- /* The two the hook Jobs need, as hooks; see the top of this file */}}
{{- $hook := dict "helm.sh/hook" "pre-install,pre-upgrade,pre-rollback" "helm.sh/hook-weight" "-20" "helm.sh/hook-delete-policy" "before-hook-creation,hook-succeeded" }}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ include "inbuxa.fullname" $root }}-hooks-deny-all
namespace: {{ $root.Release.Namespace }}
labels:
{{- include "inbuxa.labels" $root | nindent 4 }}
annotations:
{{- toYaml $hook | 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 }}-hooks-jobs
namespace: {{ $root.Release.Namespace }}
labels:
{{- include "inbuxa.labels" $root | nindent 4 }}
annotations:
{{- toYaml $hook | 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 }}
{{- end }}
@@ -147,3 +147,52 @@ tests:
- {port: 10465, protocol: TCP}
- {port: 10993, protocol: TCP}
- {port: 14190, protocol: TCP}
# A first install runs the seed before the release's own objects exist
- it: covers the hook Jobs with a deny-all that is itself a hook, run first
documentSelector:
path: metadata.name
value: mail-inbuxa-hooks-deny-all
asserts:
- equal:
path: metadata.annotations
value:
helm.sh/hook: pre-install,pre-upgrade,pre-rollback
helm.sh/hook-weight: "-20"
helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
- 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.podSelector.matchExpressions
- notExists:
path: spec.ingress
- notExists:
path: spec.egress
- it: lets the hook Jobs out as the server, as a hook too
documentSelector:
path: metadata.name
value: mail-inbuxa-hooks-jobs
asserts:
- equal:
path: metadata.annotations["helm.sh/hook-weight"]
value: "-20"
- equal:
path: spec.podSelector.matchExpressions
value:
- {key: app.kubernetes.io/component, operator: In, values: [seed, upgrade]}
- notExists:
path: spec.ingress
- isNotEmpty:
path: spec.egress
- it: makes no hook policies when policies are off
set:
networkPolicy.enabled: false
asserts:
- hasDocuments:
count: 0