Skip to content

Commit da2bbbe

Browse files
authored
fix(email): keep mailbox storage selection stable across rollback (#6694)
* fix(email): keep mailbox storage selection stable across rollback * fix(email): express the storage route as guarded patch operations
1 parent 715ed42 commit da2bbbe

9 files changed

Lines changed: 280 additions & 99 deletions

File tree

‎k8s/providers/hetzner/apps/private-mail/kustomization.yaml‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ patches:
2626
- path: patches/disable-admin-credential-seeding.yaml
2727
- path: patches/disable-maintainer-credential-seeding.yaml
2828
- path: patches/disable-agent-credential-seeding.yaml
29-
# Serve from the encrypted claim after a verified one-time copy of the
30-
# original store, which stays mounted read-only as the rollback.
29+
# Both encrypted claims use the same serving route across subsequent rollbacks.
3130
- path: patches/serve-from-encrypted-storage.yaml
3231
# The server now consumes the encrypted claim, so deployment must wait for it
3332
# to bind instead of treating it as prepared storage.
@@ -39,13 +38,28 @@ patches:
3938
patch: |-
4039
- op: remove
4140
path: /metadata/labels/platform.devantler.tech~1storage-stage
41+
- target:
42+
version: v1
43+
kind: PersistentVolumeClaim
44+
name: email-data-encrypted-v2
45+
namespace: private-mail
46+
patch: |-
47+
- op: remove
48+
path: /metadata/labels/platform.devantler.tech~1storage-stage
49+
- target:
50+
group: apps
51+
version: v1
52+
kind: StatefulSet
53+
name: private-mail
54+
namespace: private-mail
55+
path: patches/route-serving-storage.yaml
4256
- target:
4357
group: apps
4458
version: v1
4559
kind: StatefulSet
4660
name: private-mail
4761
namespace: private-mail
48-
path: patches/migrate-to-encrypted-storage.yaml
62+
path: patches/select-mail-store.yaml
4963
# The mail listeners trust PROXY headers from their own load balancer only.
5064
- target:
5165
group: apps
@@ -78,7 +92,7 @@ patches:
7892
name: private-mail
7993
namespace: private-mail
8094
path: patches/certify-mail-hostname.yaml
81-
# The copy step uses the same signed setup image as the server. A parent image
95+
# The selector uses the same signed setup image as the server. A parent image
8296
# transformer does not pin a container added here.
8397
replacements:
8498
- source:
@@ -92,4 +106,4 @@ replacements:
92106
name: private-mail
93107
namespace: private-mail
94108
fieldPaths:
95-
- spec.template.spec.initContainers.[name=migrate-storage].image
109+
- spec.template.spec.initContainers.[name=select-mail-store].image

‎k8s/providers/hetzner/apps/private-mail/patches/migrate-to-encrypted-storage.yaml‎

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Keep the selected config mount, and both encrypted stores, on setup and serving.
2+
- op: test
3+
path: /spec/template/spec/initContainers/0/name
4+
value: configure
5+
- op: test
6+
path: /spec/template/spec/containers/1/name
7+
value: private-mail
8+
- op: move
9+
from: /spec/template/spec/containers/1
10+
path: /spec/template/spec/containers/0
11+
- op: test
12+
path: /spec/template/spec/initContainers/0/volumeMounts/0/name
13+
value: datastore
14+
- op: test
15+
path: /spec/template/spec/containers/0/volumeMounts/0/name
16+
value: datastore
17+
- op: replace
18+
path: /spec/template/spec/initContainers/0/volumeMounts/0/name
19+
value: serving-storage
20+
- op: replace
21+
path: /spec/template/spec/containers/0/volumeMounts/0/name
22+
value: serving-storage
23+
- op: add
24+
path: /spec/template/spec/initContainers/0/volumeMounts/1
25+
value:
26+
name: v2-data
27+
mountPath: /var/lib/stalwart-v2
28+
- op: add
29+
path: /spec/template/spec/containers/0/volumeMounts/1
30+
value:
31+
name: v2-data
32+
mountPath: /var/lib/stalwart-v2
33+
- op: test
34+
path: /spec/template/spec/volumes/1/name
35+
value: legacy-data
36+
- op: remove
37+
path: /spec/template/spec/volumes/1
38+
- op: add
39+
path: /spec/template/spec/volumes/0
40+
value:
41+
name: serving-storage
42+
emptyDir:
43+
medium: Memory
44+
sizeLimit: 1Mi
45+
- op: add
46+
path: /spec/template/spec/volumes/1
47+
value:
48+
name: v2-data
49+
persistentVolumeClaim:
50+
claimName: email-data-encrypted-v2
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Rollbacks must select the same store that most recently accepted mail.
2+
- op: add
3+
path: /spec/template/spec/initContainers/0
4+
value:
5+
name: select-mail-store
6+
image: ghcr.io/devantler-tech/platform-private-mail-bootstrap
7+
imagePullPolicy: IfNotPresent
8+
command: ["/usr/bin/sh", "-c"]
9+
args:
10+
- |
11+
set -eu
12+
old=/var/lib/stalwart
13+
next=/var/lib/stalwart-v2
14+
template=/etc/private-mail-datastore/config.json
15+
route=/store-selection/config.json
16+
fence="$old/.private-mail-v1-fenced"
17+
receipt="$next/.private-mail-v2-copy-complete"
18+
refuse() { echo "Email storage route refused: $1." >&2; exit 1; }
19+
exists() { [ -e "$1" ] || [ -L "$1" ]; }
20+
valid_receipt() {
21+
[ -f "$1" ] && [ ! -L "$1" ] && [ "$(wc -l <"$1")" -eq 1 ] &&
22+
grep -Exq 'private-mail-longhorn-v2 [0-9a-f]{64}' "$1"
23+
}
24+
selected="$old"
25+
if exists "$receipt"; then
26+
valid_receipt "$receipt" || refuse "unverified V2 completion receipt"
27+
[ -d "$next/data" ] && [ ! -L "$next/data" ] || refuse "the V2 store is missing"
28+
if exists "$fence"; then
29+
valid_receipt "$fence" && cmp -s "$fence" "$receipt" || refuse "the V1 fence and V2 receipt disagree"
30+
else
31+
if exists "$fence.tmp"; then
32+
valid_receipt "$fence.tmp" && cmp -s "$fence.tmp" "$receipt" || refuse "incomplete V1 fence"
33+
else
34+
(set -C; cat "$receipt" >"$fence.tmp")
35+
fi
36+
sync
37+
mv -- "$fence.tmp" "$fence"
38+
sync
39+
fi
40+
selected="$next"
41+
elif exists "$fence" || exists "$fence.tmp"; then
42+
refuse "V1 was fenced and its V2 completion receipt is missing"
43+
fi
44+
[ -d "$selected/data" ] && [ ! -L "$selected/data" ] || refuse "the selected store is missing"
45+
[ "$(grep -Fc "\"$old/data\"" "$template")" -eq 1 ] || refuse "the datastore configuration path is unverified"
46+
tmp=$(mktemp /store-selection/.config.XXXXXX)
47+
trap 'rm -f -- "$tmp"' EXIT
48+
sed "s#\"$old/data\"#\"$selected/data\"#" "$template" >"$tmp"
49+
[ "$(grep -Fc "\"$selected/data\"" "$tmp")" -eq 1 ] || refuse "the selected datastore configuration is unverified"
50+
mv -- "$tmp" "$route"
51+
if [ "$selected" = "$next" ]; then
52+
echo "Email storage route selected Longhorn V2."
53+
else
54+
echo "Email storage route selected the original encrypted store."
55+
fi
56+
resources:
57+
requests:
58+
cpu: 10m
59+
memory: 32Mi
60+
limits:
61+
cpu: 100m
62+
memory: 64Mi
63+
securityContext:
64+
runAsNonRoot: true
65+
allowPrivilegeEscalation: false
66+
readOnlyRootFilesystem: true
67+
capabilities:
68+
drop: [ALL]
69+
seccompProfile:
70+
type: RuntimeDefault
71+
seLinuxOptions: {}
72+
volumeMounts:
73+
- name: serving-storage
74+
mountPath: /store-selection
75+
- name: datastore
76+
mountPath: /etc/private-mail-datastore
77+
readOnly: true
78+
- name: data
79+
mountPath: /var/lib/stalwart
80+
- name: v2-data
81+
mountPath: /var/lib/stalwart-v2
82+
readOnly: true

‎k8s/providers/hetzner/apps/private-mail/patches/trust-mail-load-balancer.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The mail listeners trust PROXY headers from the mail load balancer only
22
# (#5481). The address must equal the private-ipv4 annotation on the
3-
# private-mail-public Service. The setup runs after the one-time storage copy.
3+
# private-mail-public Service. The setup runs after storage selection.
44
- op: test
55
path: /spec/template/spec/initContainers/1/name
66
value: configure

‎scripts/tests/private-mail-preparation/encryption_test.go‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,18 @@ func TestProductionServesTheMailStoreFromEncryptedStorage(t *testing.T) {
114114
if mapping(t, object(t, production, "StorageClass", "email-encrypted-v2")["parameters"])["encrypted"] != "true" {
115115
t.Fatal("the V2 migration destination must encrypt at rest")
116116
}
117-
prepared := object(t, production, "PersistentVolumeClaim", "email-data-encrypted-v2")
118-
metadata := mapping(t, prepared["metadata"])
119-
spec := mapping(t, prepared["spec"])
117+
destination := object(t, production, "PersistentVolumeClaim", "email-data-encrypted-v2")
118+
metadata := mapping(t, destination["metadata"])
119+
spec := mapping(t, destination["spec"])
120+
labels, _ := metadata["labels"].(map[string]any)
120121
if metadata["namespace"] != "private-mail" ||
121122
mapping(t, metadata["annotations"])["kustomize.toolkit.fluxcd.io/prune"] != "disabled" ||
122-
mapping(t, metadata["labels"])["platform.devantler.tech/storage-stage"] != "prepared" ||
123+
labels["platform.devantler.tech/storage-stage"] != nil ||
123124
spec["storageClassName"] != "email-encrypted-v2" || spec["volumeMode"] != "Filesystem" ||
124125
!reflect.DeepEqual(spec["accessModes"], []any{"ReadWriteOnce"}) ||
125126
mapping(t, mapping(t, spec["resources"])["requests"])["storage"] != "2Gi" ||
126127
spec["volumeName"] != nil || spec["dataSource"] != nil || spec["dataSourceRef"] != nil {
127-
t.Fatal("the V2 destination must be a distinct, retained, unbound 2 GiB filesystem claim")
128+
t.Fatal("the V2 destination must be a distinct, retained 2 GiB filesystem claim that waits for binding")
128129
}
129130
pod := mapping(t, mapping(t, mapping(t, object(t, production, "StatefulSet", "private-mail")["spec"])["template"])["spec"])
130131
volumes := map[string]map[string]any{}
@@ -135,8 +136,8 @@ func TestProductionServesTheMailStoreFromEncryptedStorage(t *testing.T) {
135136
}
136137
}
137138
if len(volumes) != 2 || volumes["data"]["claimName"] != "email-data-encrypted" || volumes["data"]["readOnly"] == true ||
138-
volumes["legacy-data"]["claimName"] != "private-mail-data" || volumes["legacy-data"]["readOnly"] != true {
139-
t.Fatal("the server must store data on the encrypted claim and see the original only read-only")
139+
volumes["v2-data"]["claimName"] != "email-data-encrypted-v2" || volumes["v2-data"]["readOnly"] == true {
140+
t.Fatal("both encrypted claims must be available to the rollback-safe selector")
140141
}
141142
server := ""
142143
for _, item := range sequence(t, pod["containers"]) {
@@ -145,30 +146,40 @@ func TestProductionServesTheMailStoreFromEncryptedStorage(t *testing.T) {
145146
server = stringField(t, container, "image")
146147
}
147148
}
148-
copySteps, sourceMounts := 0, 0
149+
expected := map[string]bool{"select-mail-store": true, "configure": true, "private-mail": true}
149150
for index, group := range []string{"initContainers", "containers"} {
150151
for position, item := range sequence(t, pod[group]) {
151152
container := mapping(t, item)
152153
name := stringField(t, container, "name")
154+
if !expected[name] {
155+
continue
156+
}
157+
delete(expected, name)
158+
mounts := map[string]map[string]any{}
153159
for _, mountItem := range sequence(t, container["volumeMounts"]) {
154160
mount := mapping(t, mountItem)
155-
if mount["name"] != "legacy-data" {
156-
continue
157-
}
158-
if index != 0 || position != 0 || name != "migrate-storage" || mount["readOnly"] != true {
159-
t.Fatal("only the first initializer, the copy step, may mount the original claim, and only read-only")
161+
mounts[stringField(t, mount, "name")] = mount
162+
if mount["name"] == "legacy-data" {
163+
t.Fatal("the retired plaintext claim must not mount in the rollback artifact")
160164
}
161-
sourceMounts++
162165
}
163-
if name == "migrate-storage" {
164-
copySteps++
165-
if index != 0 || position != 0 || container["image"] != server {
166-
t.Fatal("the copy must run first, before setup, with the server's signed image")
167-
}
166+
selecting := name == "select-mail-store"
167+
routeMount := "/etc/stalwart/config.json"
168+
if selecting {
169+
routeMount = "/store-selection"
170+
}
171+
if mounts["serving-storage"]["mountPath"] != routeMount || (mounts["serving-storage"]["readOnly"] == true) == selecting ||
172+
!selecting && mounts["serving-storage"]["subPath"] != "config.json" ||
173+
mounts["data"]["mountPath"] != "/var/lib/stalwart" || mounts["v2-data"]["mountPath"] != "/var/lib/stalwart-v2" ||
174+
(mounts["v2-data"]["readOnly"] == true) != selecting {
175+
t.Fatalf("%s does not use the shared fenced storage route", name)
176+
}
177+
if selecting && (index != 0 || position != 0 || container["image"] != server) {
178+
t.Fatal("the selector must run before setup with the server's signed image")
168179
}
169180
}
170181
}
171-
if copySteps != 1 || sourceMounts != 1 {
172-
t.Fatal("exactly one copy step must mount the original claim exactly once")
182+
if len(expected) != 0 {
183+
t.Fatalf("missing storage consumers: %v", expected)
173184
}
174185
}

‎scripts/tests/private-mail-preparation/production_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func TestProductionAdministrationUsesEncryptedSettings(t *testing.T) {
1111
sts := object(t, objects, "StatefulSet", "private-mail")
1212
pod := mapping(t, mapping(t, mapping(t, sts["spec"])["template"])["spec"])
1313
initializers := sequence(t, pod["initContainers"])
14-
if len(initializers) != 2 || mapping(t, initializers[0])["name"] != "migrate-storage" || mapping(t, initializers[1])["name"] != "configure" || len(sequence(t, pod["containers"])) != 2 {
15-
t.Fatal("production must copy its store, then initialize, before serving administration through the client proxy")
14+
if len(initializers) != 2 || mapping(t, initializers[0])["name"] != "select-mail-store" || mapping(t, initializers[1])["name"] != "configure" || len(sequence(t, pod["containers"])) != 2 {
15+
t.Fatal("production must select its store, then initialize, before serving administration through the client proxy")
1616
}
1717
secret := object(t, objects, "Secret", "private-mail-settings")
1818
if mapping(t, secret["metadata"])["namespace"] != "private-mail" || secret["data"] != nil {

‎scripts/tests/private-mail-preparation/storage_health_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ func TestEncryptedMailStorageHealthAcrossServing(t *testing.T) {
139139
wantFailed bool
140140
}{
141141
{name: "prepared encrypted storage", phase: "Pending", want: true},
142-
{name: "prepared encrypted V2 claim", phase: "Pending", path: "k8s/providers/hetzner/apps", claim: "email-data-encrypted-v2", want: true},
142+
{name: "prepared encrypted V2 claim", phase: "Pending", claim: "email-data-encrypted-v2", want: true},
143+
{name: "production mounts the V2 claim and waits for binding", phase: "Pending", path: "k8s/providers/hetzner/apps", claim: "email-data-encrypted-v2"},
144+
{name: "production V2 claim is bound", phase: "Bound", path: "k8s/providers/hetzner/apps", claim: "email-data-encrypted-v2", want: true},
143145
{name: "encrypted V2 with the older class", phase: "Pending", claim: "email-data-encrypted-v2",
144146
mutate: func(_, s map[string]any) { s["storageClassName"] = "email-encrypted" }},
145147
{name: "encrypted V1 with the V2 class", phase: "Pending",

0 commit comments

Comments
 (0)