Skip to content

Commit dd8f499

Browse files
committed
fix(email): bind the storage selector to the approved template
1 parent dd1ff7b commit dd8f499

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

‎scripts/diagnose-email-startup/main.go‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os/exec"
1717
"reflect"
1818
"regexp"
19+
"strings"
1920
"time"
2021
)
2122

@@ -54,8 +55,8 @@ type container struct {
5455
Command, Args []string
5556
Env, EnvFrom []json.RawMessage
5657
VolumeMounts []struct {
57-
Name, MountPath, SubPath string
58-
ReadOnly bool
58+
Name, MountPath, SubPath, SubPathExpr string
59+
ReadOnly bool
5960
}
6061
}
6162
type pod struct {
@@ -67,8 +68,12 @@ type pod struct {
6768
InitContainers []container
6869
Volumes []struct {
6970
Name string
70-
ConfigMap *struct{ Name string }
71-
EmptyDir *struct{ Medium, SizeLimit string }
71+
ConfigMap *struct {
72+
Name string
73+
Items []json.RawMessage
74+
Optional bool
75+
}
76+
EmptyDir *struct{ Medium, SizeLimit string }
7277
}
7378
}
7479
Status struct {
@@ -259,7 +264,7 @@ func bindPod(b []byte) (binding, bool, bool, error) {
259264

260265
func bindDatastoreMount(p pod, c container) (string, bool) {
261266
for _, mount := range c.VolumeMounts {
262-
if mount.MountPath != "/etc/stalwart/config.json" || mount.SubPath != "config.json" || !mount.ReadOnly {
267+
if mount.MountPath != "/etc/stalwart/config.json" || mount.SubPath != "config.json" || mount.SubPathExpr != "" || !mount.ReadOnly {
263268
continue
264269
}
265270
for _, volume := range p.Spec.Volumes {
@@ -288,9 +293,9 @@ func bindStorageSelector(p pod) (string, bool) {
288293
continue
289294
}
290295
if init.Name != "select-mail-store" || init.Image != approvedRoutedImage || mount.MountPath != "/store-selection" ||
291-
mount.SubPath != "" || len(init.Env) != 0 || len(init.EnvFrom) != 0 ||
296+
mount.SubPath != "" || mount.SubPathExpr != "" || len(init.Env) != 0 || len(init.EnvFrom) != 0 ||
292297
!reflect.DeepEqual(init.Command, []string{"/usr/bin/sh", "-c"}) || len(init.Args) != 1 ||
293-
hashBytes([]byte(init.Args[0])) != approvedStorageSelectorSHA {
298+
hashBytes([]byte(init.Args[0])) != approvedStorageSelectorSHA || !bindStorageTemplate(p, init) {
294299
return "", false
295300
}
296301
writers++
@@ -314,6 +319,33 @@ func bindStorageSelector(p pod) (string, bool) {
314319
return selector, selector != ""
315320
}
316321

322+
func bindStorageTemplate(p pod, init container) bool {
323+
const path = "/etc/private-mail-datastore"
324+
matches := 0
325+
for _, mount := range init.VolumeMounts {
326+
if strings.HasPrefix(mount.MountPath, path+"/") {
327+
return false
328+
}
329+
if mount.MountPath != path {
330+
continue
331+
}
332+
if mount.Name != "datastore" || !mount.ReadOnly || mount.SubPath != "" || mount.SubPathExpr != "" {
333+
return false
334+
}
335+
matches++
336+
}
337+
if matches != 1 {
338+
return false
339+
}
340+
for _, volume := range p.Spec.Volumes {
341+
if volume.Name == "datastore" {
342+
source := volume.ConfigMap
343+
return source != nil && source.Name == "private-mail-datastore" && len(source.Items) == 0 && !source.Optional
344+
}
345+
}
346+
return false
347+
}
348+
317349
// statusFor refuses duplicate identities, independent of Kubernetes list ordering.
318350
func statusFor(p pod, name string) (containerStatus, bool) {
319351
var result containerStatus

‎scripts/diagnose-email-startup/main_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func TestBindPodRecognizesOnlyReviewedFencedStorageProducer(t *testing.T) {
4545
name string
4646
change func(map[string]any)
4747
}{
48+
{"foreign template", func(p map[string]any) {
49+
p["spec"].(map[string]any)["volumes"].([]any)[0].(map[string]any)["configMap"].(map[string]any)["name"] = "foreign-template"
50+
}},
4851
{"changed selector", func(p map[string]any) {
4952
p["spec"].(map[string]any)["initContainers"].([]any)[0].(map[string]any)["args"] = []string{"unexpected"}
5053
}},

0 commit comments

Comments
 (0)