carlos / platform Public

mailer: Task 3 review fixes (Fable P2-1, two P3s)

P2-1: TestMailerServeIsGivenTheEdgePeerRule, a source guard: every
mailer.Config outside internal/mailer sets PeerOK to exactly
instancectl.EdgeMainPIDPeer(), and there is one, in mailer_cli.go. An
admit-all rule there passed every test before; it fails this one. A
behavioural test cannot build the production rule without a carlos user
(EdgeMainPIDPeer exits without one), so the guard is on the source.

P3: TestEdgePeerRuleForHasNoProductionCaller: the test seam has no
caller outside tests.

P3: carlos-mailer.service unsets every credential a host.env may carry
(the secrets and release keys, the tunnel, service, sidecar and DNS
tokens, the alert webhook, admin and instance keys, AWS keys and
profiles), pinned by TestMailerUnitShape. A true allowlist cannot be
expressed over EnvironmentFile=, so it goes on the follow-up list.

Census: the unit's claim updated, overlay re-proposed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a1e9de6405ca9547e99005cc1326f23b720170d2 parent f1db2e2
7 files changed, +141 −9
  • docs/superpowers/notes/census-claims-platform-mail-submission.tsv +1 −1
  • docs/superpowers/notes/census-proposal-platform-mail-submission.md +1 −1
  • internal/host/units/carlos-mailer.service +6 −2
  • internal/host/units_isolation_test.go +18 −1
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −1
  • internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv +3 −3
  • mailer_guard_test.go +111 −0

CI cancelled — superseded by a newer push — run details

diff --git a/docs/superpowers/notes/census-claims-platform-mail-submission.tsv b/docs/superpowers/notes/census-claims-platform-mail-submission.tsv
index da38ca9fa..9dbd1b252 100644
--- a/docs/superpowers/notes/census-claims-platform-mail-submission.tsv
+++ b/docs/superpowers/notes/census-claims-platform-mail-submission.tsv
@@ -27,7 +27,7 @@ internal/host/units/carlos-instance-tty@.service:external Task 3: InaccessiblePa
internal/host/units/carlos-instance@.service:external Task 3: InaccessiblePaths= gains -/run/carlos-mailer -/etc/carlos-mailer (optional). Only hides more; no read added.
internal/host/units/carlos-mailer-arm.path:external Task 3, new: [Path] PathExists=/etc/carlos-mailer/keys, Unit=carlos-mailer.socket, WantedBy=multi-user.target; enabled and armed by bootstrap like every .path. PID 1 watches a path in a root-owned 0750 root:carlos-mailer directory only root (the operator) writes; it starts a socket unit and reads nothing.
internal/host/units/carlos-mailer.socket:external Task 3, new: ConditionPathExists=/etc/carlos-mailer/keys, ListenStream=/run/carlos-mailer/mailer.sock, SocketUser=carlos, SocketGroup=carlos, SocketMode=0600, Accept=no; not enabled, started only by the arming path. PID 1 binds a socket in a root-owned directory, as carlos-instance-control.socket does in /run/carlos-control. No read added.
-internal/host/units/carlos-mailer.service:external Task 3, new: runs /opt/carlos/carlos mailer serve as the static user carlos-mailer (never root), socket-activated, ConditionPathExists=/etc/carlos-mailer/keys, EnvironmentFile=-/etc/carlos/host.env (read by PID 1, as every carlos service's) with UnsetEnvironment=CARLOS_SECRETS_KEY, AWS_EC2_METADATA_DISABLED=true, IPAddressDeny=169.254.169.254/32 fd00:ec2::254/128, StateDirectory=carlos-mailer 0700, and the relay's hardening block with no ReadWritePaths. No root process reads anything new; the mailer reads its own key file and state.
+internal/host/units/carlos-mailer.service:external Task 3, new: runs /opt/carlos/carlos mailer serve as the static user carlos-mailer (never root), socket-activated, ConditionPathExists=/etc/carlos-mailer/keys, EnvironmentFile=-/etc/carlos/host.env (read by PID 1, as every carlos service's) with UnsetEnvironment= of every credential a host.env may carry (CARLOS_SECRETS_KEY, CARLOS_RELEASE_KEY, the tunnel, service, sidecar and DNS tokens, the alert webhook, CARLOS_ADMIN_TOKEN, CARLOS_INSTANCE_KEY, AWS keys and profiles; review round 1 Task 3 P3), AWS_EC2_METADATA_DISABLED=true, IPAddressDeny=169.254.169.254/32 fd00:ec2::254/128, StateDirectory=carlos-mailer 0700, and the relay's hardening block with no ReadWritePaths. No root process reads anything new; the mailer reads its own key file and state.
internal/status/snapshot.go:CollectBox Task 3: copies Probes.MailerArmed into BoxMetrics.MailerArmed (json mailer_armed, omitempty; private, like Layout) and counts it in the condition that builds Metrics; the same 28 sites, no read added.
#
# Task 4 (the verb at both bind sites, CARLOS_MAIL agent-owned).
diff --git a/docs/superpowers/notes/census-proposal-platform-mail-submission.md b/docs/superpowers/notes/census-proposal-platform-mail-submission.md
index f59eb0586..e1c7b2b73 100644
--- a/docs/superpowers/notes/census-proposal-platform-mail-submission.md
+++ b/docs/superpowers/notes/census-proposal-platform-mail-submission.md
@@ -72,6 +72,6 @@ New dispositions, which census-propose wrote and the reviewer checks against the
- new `internal/mailsubmit/forward.go:SocketForwarder.Send`: reads +file:ReadAll; written legacy, accepted-legacy, frozen
- new `internal/mailsubmit/verb.go:Mount`: reads +registry:App, +registry:Host, +whole:Route; written legacy, accepted-legacy, frozen
-Seal: `07b748a749084fdaf61c5087a6edfca8a79273962822ce551dd4d42b0d20c063` → `e8d4820f40e9b6459ddcba3f3efb9926b742ccd5cce8b33fa713551152265d51`.
+Seal: `07b748a749084fdaf61c5087a6edfca8a79273962822ce551dd4d42b0d20c063` → `4d48de4cb1d474a299bab67233af408adc5f9e0b32e2beeb0ac1500edd2d3353`.
Overlay: `internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/host/units/carlos-mailer.service b/internal/host/units/carlos-mailer.service
index 369aa2ff0..8658de588 100644
--- a/internal/host/units/carlos-mailer.service
+++ b/internal/host/units/carlos-mailer.service
@@ -12,7 +12,11 @@
# every client from the static key (rootaws.LoadStaticConfig).
#
# host.env is read by PID 1 (EnvironmentFile), so this user needs no read
-# access to it; the secrets key it may hold is unset before exec.
+# access to it. Every credential a box's host.env may carry is unset before
+# exec: the mailer reads only the bucket, the two regions, the placement
+# and its state directory, and its one key comes from its own file. (An
+# allowlist would be stronger; systemd cannot express one over an
+# EnvironmentFile, so it is a follow-up.)
#
# Socket-activated by carlos-mailer.socket, which carlos-mailer-arm.path
# starts once the key exists. Not enabled on its own.
@@ -25,7 +29,7 @@ ConditionPathExists=/etc/carlos-mailer/keys
[Service]
EnvironmentFile=-/etc/carlos/host.env
-UnsetEnvironment=CARLOS_SECRETS_KEY
+UnsetEnvironment=CARLOS_SECRETS_KEY CARLOS_RELEASE_KEY CARLOS_TUNNEL_TOKEN CARLOS_SERVICE_TOKEN CARLOS_SIDECAR_TOKEN CARLOS_DNSIMPLE_TOKEN CARLOS_ALERT_WEBHOOK CARLOS_ADMIN_TOKEN CARLOS_INSTANCE_KEY AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILE AWS_DEFAULT_PROFILE
Environment=AWS_EC2_METADATA_DISABLED=true
ExecStart=/opt/carlos/carlos mailer serve
Restart=always
diff --git a/internal/host/units_isolation_test.go b/internal/host/units_isolation_test.go
index 72f8d8c13..3d12a5581 100644
--- a/internal/host/units_isolation_test.go
+++ b/internal/host/units_isolation_test.go
@@ -733,7 +733,6 @@ func TestMailerUnitShape(t *testing.T) {
"Requires=carlos-mailer.socket",
"ConditionPathExists=/etc/carlos-mailer/keys",
"EnvironmentFile=-/etc/carlos/host.env",
- "UnsetEnvironment=CARLOS_SECRETS_KEY",
"Environment=AWS_EC2_METADATA_DISABLED=true",
"ExecStart=/opt/carlos/carlos mailer serve",
"Restart=always",
@@ -760,6 +759,24 @@ func TestMailerUnitShape(t *testing.T) {
t.Errorf("carlos-mailer.service lacks %q", want)
}
}
+ // Every credential a host.env may carry is unset before exec (review
+ // round 1, Task 3 P3): the secrets key, the box's tokens, a webhook, and
+ // any AWS key or profile, which the static-key clients must never see.
+ unset := map[string]bool{}
+ for _, d := range svc {
+ if v, ok := strings.CutPrefix(d, "UnsetEnvironment="); ok {
+ for _, n := range strings.Fields(v) {
+ unset[n] = true
+ }
+ }
+ }
+ for _, n := range []string{"CARLOS_SECRETS_KEY", "CARLOS_RELEASE_KEY", "CARLOS_TUNNEL_TOKEN", "CARLOS_SERVICE_TOKEN", "CARLOS_SIDECAR_TOKEN",
+ "CARLOS_DNSIMPLE_TOKEN", "CARLOS_ALERT_WEBHOOK", "CARLOS_ADMIN_TOKEN", "CARLOS_INSTANCE_KEY",
+ "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_PROFILE", "AWS_DEFAULT_PROFILE"} {
+ if !unset[n] {
+ t.Errorf("carlos-mailer.service does not unset %s", n)
+ }
+ }
for _, d := range svc {
switch {
case strings.HasPrefix(d, "ReadWritePaths="), strings.HasPrefix(d, "BindPaths="):
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index f29697b52..57f8c9e3f 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -204,4 +204,4 @@ plan14-root-reads-static-site-geo-962.tsv 5e8a72175700a3e60c1e283b133c1ee25e36f2
plan14-root-reads-member-backups-drill-nodb.tsv 07b748a749084fdaf61c5087a6edfca8a79273962822ce551dd4d42b0d20c063
plan14-root-reads-spread-every-schedule-ticks.tsv 07b748a749084fdaf61c5087a6edfca8a79273962822ce551dd4d42b0d20c063
plan14-root-reads-member-backups-2b.tsv 07b748a749084fdaf61c5087a6edfca8a79273962822ce551dd4d42b0d20c063
-plan14-root-reads-platform-mail-submission.tsv e8d4820f40e9b6459ddcba3f3efb9926b742ccd5cce8b33fa713551152265d51
+plan14-root-reads-platform-mail-submission.tsv 4d48de4cb1d474a299bab67233af408adc5f9e0b32e2beeb0ac1500edd2d3353
diff --git a/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv b/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv
index ed8fda781..c9d78011a 100644
--- a/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv
+++ b/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv
@@ -21,7 +21,7 @@
@legacy bce32b49d764738f9b80e81a4b6473b446149bb1e62062886297d1431cc4cc4e internal/activator/backend_exec.go ExecBackend.mailSubmits legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 4, new: the CARLOS_MAIL rule. Reads rt.App, rt.Host, rt.Provisioned and RouteAccount(rt) from the agent's own registry row, and asks b.MailSwitch (the agent's cache of the console-written app sender record, internal/host/mailswitch.go: one bounded Get of apps/<acct>/<app>/email/senders/app.json through the agent's deploy store). Logs once per host at error level for an unprovisioned route of a switched app. Agent, not root; no file read. registry:App@213,registry:App@526,registry:Host@317,registry:Host@511,registry:Provisioned@246,whole:Route@185 cfd7494b38dc64c2710233dbc2da850eb7b89fc9aecfa648cd7affd5e9a354d0
@legacy d6700cb69f36e6e7cb8fdf4f4b747269ba9bf6545143e4dd943acedc018bc4b1 internal/host/mailerreport.go mailerArmed legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: the agent's MailerArmed probe. Stats the constant socket path and dials it for GET /v1/health with a 2 s timeout; true only on a 200. Runs in the agent as carlos, never as root; the path is a constant, in a root-owned 0750 root:carlos directory nothing a tenant or an app can write. Answers a bool into the private status block. file:Stat@72 6fd83d5c27429d06864d02a141f7036c2f3e1e8e89366ad6d2a232d417420052
@legacy 85d678c6a608ff70b7a60141d0efde911544f4b7aa9a7d9508f0e13cf2b7e724 internal/host/units/carlos-mailer-arm.path external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: [Path] PathExists=/etc/carlos-mailer/keys, Unit=carlos-mailer.socket, WantedBy=multi-user.target; enabled and armed by bootstrap like every .path. PID 1 watches a path in a root-owned 0750 root:carlos-mailer directory only root (the operator) writes; it starts a socket unit and reads nothing. file:external@0 6b28c6282ee930560f859f20c752c65f0e550412b9c6ffb1fc10c292a3941a2a
-@legacy f99b76edb6264d669d69dee1931907a85d99256a5f891eda8be179f49c658492 internal/host/units/carlos-mailer.service external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: runs /opt/carlos/carlos mailer serve as the static user carlos-mailer (never root), socket-activated, ConditionPathExists=/etc/carlos-mailer/keys, EnvironmentFile=-/etc/carlos/host.env (read by PID 1, as every carlos service's) with UnsetEnvironment=CARLOS_SECRETS_KEY, AWS_EC2_METADATA_DISABLED=true, IPAddressDeny=169.254.169.254/32 fd00:ec2::254/128, StateDirectory=carlos-mailer 0700, and the relay's hardening block with no ReadWritePaths. No root process reads anything new; the mailer reads its own key file and state. file:external@0 3f9bd1c2d6e1a4d5d1c72d0c24b000e122cc95290cca8b0d2e62d86576e2dd5f
+@legacy 2e9ea18fa4d1aa973bb5394fcc21385c5883dc605f5f6bb7aec8c7d7c2976ac1 internal/host/units/carlos-mailer.service external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: runs /opt/carlos/carlos mailer serve as the static user carlos-mailer (never root), socket-activated, ConditionPathExists=/etc/carlos-mailer/keys, EnvironmentFile=-/etc/carlos/host.env (read by PID 1, as every carlos service's) with UnsetEnvironment= of every credential a host.env may carry (CARLOS_SECRETS_KEY, CARLOS_RELEASE_KEY, the tunnel, service, sidecar and DNS tokens, the alert webhook, CARLOS_ADMIN_TOKEN, CARLOS_INSTANCE_KEY, AWS keys and profiles; review round 1 Task 3 P3), AWS_EC2_METADATA_DISABLED=true, IPAddressDeny=169.254.169.254/32 fd00:ec2::254/128, StateDirectory=carlos-mailer 0700, and the relay's hardening block with no ReadWritePaths. No root process reads anything new; the mailer reads its own key file and state. file:external@0 05014587b16352f1a0ecddc63ce2ea95732cadf7b284f3c8c7bb2b82b95ec922
@legacy 486f70ac08ac5f675d91dc3b7bddf949c908db227e4596af5706df846c4fb32f internal/host/units/carlos-mailer.socket external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: ConditionPathExists=/etc/carlos-mailer/keys, ListenStream=/run/carlos-mailer/mailer.sock, SocketUser=carlos, SocketGroup=carlos, SocketMode=0600, Accept=no; not enabled, started only by the arming path. PID 1 binds a socket in a root-owned directory, as carlos-instance-control.socket does in /run/carlos-control. No read added. file:external@0 275aae364c39e89bdf3d76d7e66aea9cf65b398ac0c16c087db353cfd5140c34
@legacy 6124a227150c2055499a081f370c0d3211decfc51a16ff34a0449493b2a94cb3 internal/mailer/counter.go Counter.loadLocked legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 5, new: reads the day's counts file from the mailer's own state directory; a body that does not decode is renamed aside, logged loudly, and the day restarts at zero (the per-box bound §4.1 (a) accepts). carlos-mailer only. file:ReadFile@142 a317a61e58c996133e1ae96045932c927052cb5f44720fb90193ae45abbe2d1b
@legacy 66171fc9229872bbf17d3f26e75389dbacb7171bbd7ff08f5c820b60140e2ebf internal/mailer/counter.go Counter.writeLocked legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 5, new: writes the day's counts durably (temp file in the same directory, fsync, rename, directory fsync) before a send. carlos-mailer only, in its own state directory. file:CreateTemp@118,file:Open@489 40cb38a8252e6c90bd21efdfafc7bbcb9b66b04a10bf9c2154f5796e1bff4820
@@ -56,7 +56,7 @@ internal/activator/backend_exec.go ExecBackend.controlMount legacy accepted-lega
internal/activator/backend_exec.go ExecBackend.mailSubmits legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 4, new: the CARLOS_MAIL rule. Reads rt.App, rt.Host, rt.Provisioned and RouteAccount(rt) from the agent's own registry row, and asks b.MailSwitch (the agent's cache of the console-written app sender record, internal/host/mailswitch.go: one bounded Get of apps/<acct>/<app>/email/senders/app.json through the agent's deploy store). Logs once per host at error level for an unprovisioned route of a switched app. Agent, not root; no file read. registry:App@213,registry:App@526,registry:Host@317,registry:Host@511,registry:Provisioned@246,whole:Route@185 cfd7494b38dc64c2710233dbc2da850eb7b89fc9aecfa648cd7affd5e9a354d0
internal/host/mailerreport.go mailerArmed legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: the agent's MailerArmed probe. Stats the constant socket path and dials it for GET /v1/health with a 2 s timeout; true only on a 200. Runs in the agent as carlos, never as root; the path is a constant, in a root-owned 0750 root:carlos directory nothing a tenant or an app can write. Answers a bool into the private status block. file:Stat@72 6fd83d5c27429d06864d02a141f7036c2f3e1e8e89366ad6d2a232d417420052
internal/host/units/carlos-mailer-arm.path external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: [Path] PathExists=/etc/carlos-mailer/keys, Unit=carlos-mailer.socket, WantedBy=multi-user.target; enabled and armed by bootstrap like every .path. PID 1 watches a path in a root-owned 0750 root:carlos-mailer directory only root (the operator) writes; it starts a socket unit and reads nothing. file:external@0 6b28c6282ee930560f859f20c752c65f0e550412b9c6ffb1fc10c292a3941a2a
-internal/host/units/carlos-mailer.service external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: runs /opt/carlos/carlos mailer serve as the static user carlos-mailer (never root), socket-activated, ConditionPathExists=/etc/carlos-mailer/keys, EnvironmentFile=-/etc/carlos/host.env (read by PID 1, as every carlos service's) with UnsetEnvironment=CARLOS_SECRETS_KEY, AWS_EC2_METADATA_DISABLED=true, IPAddressDeny=169.254.169.254/32 fd00:ec2::254/128, StateDirectory=carlos-mailer 0700, and the relay's hardening block with no ReadWritePaths. No root process reads anything new; the mailer reads its own key file and state. file:external@0 3f9bd1c2d6e1a4d5d1c72d0c24b000e122cc95290cca8b0d2e62d86576e2dd5f
+internal/host/units/carlos-mailer.service external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: runs /opt/carlos/carlos mailer serve as the static user carlos-mailer (never root), socket-activated, ConditionPathExists=/etc/carlos-mailer/keys, EnvironmentFile=-/etc/carlos/host.env (read by PID 1, as every carlos service's) with UnsetEnvironment= of every credential a host.env may carry (CARLOS_SECRETS_KEY, CARLOS_RELEASE_KEY, the tunnel, service, sidecar and DNS tokens, the alert webhook, CARLOS_ADMIN_TOKEN, CARLOS_INSTANCE_KEY, AWS keys and profiles; review round 1 Task 3 P3), AWS_EC2_METADATA_DISABLED=true, IPAddressDeny=169.254.169.254/32 fd00:ec2::254/128, StateDirectory=carlos-mailer 0700, and the relay's hardening block with no ReadWritePaths. No root process reads anything new; the mailer reads its own key file and state. file:external@0 05014587b16352f1a0ecddc63ce2ea95732cadf7b284f3c8c7bb2b82b95ec922
internal/host/units/carlos-mailer.socket external legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 3, new: ConditionPathExists=/etc/carlos-mailer/keys, ListenStream=/run/carlos-mailer/mailer.sock, SocketUser=carlos, SocketGroup=carlos, SocketMode=0600, Accept=no; not enabled, started only by the arming path. PID 1 binds a socket in a root-owned directory, as carlos-instance-control.socket does in /run/carlos-control. No read added. file:external@0 275aae364c39e89bdf3d76d7e66aea9cf65b398ac0c16c087db353cfd5140c34
internal/mailer/counter.go Counter.loadLocked legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 5, new: reads the day's counts file from the mailer's own state directory; a body that does not decode is renamed aside, logged loudly, and the day restarts at zero (the per-box bound §4.1 (a) accepts). carlos-mailer only. file:ReadFile@142 a317a61e58c996133e1ae96045932c927052cb5f44720fb90193ae45abbe2d1b
internal/mailer/counter.go Counter.writeLocked legacy accepted-legacy accepted-legacy;rev3=census-ruling-platform-mail-submission;basis=Task 5, new: writes the day's counts durably (temp file in the same directory, fsync, rename, directory fsync) before a send. carlos-mailer only, in its own state directory. file:CreateTemp@118,file:Open@489 40cb38a8252e6c90bd21efdfafc7bbcb9b66b04a10bf9c2154f5796e1bff4820
@@ -72,7 +72,7 @@ internal/activator/prepare.go ExecBackend.listenControl legacy accepted-legacy a
@coverage internal/activator 2fe6860f1b7ef4f5644e17f59fe3cf75759ec2ffe597994c1478edd0512cbc38
@coverage internal/host c0ad562284905411f38c76f5d8aefe76eada13927a4629884e46bff52ccf0b30
@coverage internal/host/demo_units 3a34238356068c6176879f3ee189c81af8e08bfcc0246c5feba80d7169f7af6a
-@coverage internal/host/units 460478599582ed846a1b6c2d5273c8b2460dc40dcb2e82807a4d404d64c7a330
+@coverage internal/host/units 691c24cbe981fb8961915598f2a8585d1ccf8368a8eb3b6a58f8c7a2834fe886
@coverage internal/hostenv 6c96c4abe8548fe3cf12d3a220de3e2917c3edfa5320b1662ea76bdf364d24b5
@coverage internal/instancectl 50d4d80f1f1a86bbed67676f4e439eb79a08bb546ea85ff01a3b433925b9d984
@coverage internal/mailer 985199bf5324887938514750668eaa8eb099b1b3be0734c7de77c7fddd4230b1
diff --git a/mailer_guard_test.go b/mailer_guard_test.go
new file mode 100644
index 000000000..4b043b352
--- /dev/null
+++ b/mailer_guard_test.go
@@ -0,0 +1,111 @@
+package main
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "io/fs"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// moduleGoFiles parses every non-test .go file of the module, testdata,
+// vendored and hidden trees aside.
+func moduleGoFiles(t *testing.T) map[string]*ast.File {
+ t.Helper()
+ fset := token.NewFileSet()
+ out := map[string]*ast.File{}
+ err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+ if d.IsDir() {
+ if path != "." && (strings.HasPrefix(d.Name(), ".") || d.Name() == "testdata" || d.Name() == "vendor") {
+ return filepath.SkipDir
+ }
+ return nil
+ }
+ if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
+ return nil
+ }
+ f, err := parser.ParseFile(fset, path, nil, 0)
+ if err != nil {
+ return err
+ }
+ out[filepath.ToSlash(path)] = f
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ return out
+}
+
+// isSel reports whether e is pkg.name.
+func isSel(e ast.Expr, pkg, name string) bool {
+ s, ok := e.(*ast.SelectorExpr)
+ if !ok {
+ return false
+ }
+ id, ok := s.X.(*ast.Ident)
+ return ok && id.Name == pkg && s.Sel.Name == name
+}
+
+// The mailer's peer rule (design decision 3) is decided in one place: every
+// mailer.Config built outside internal/mailer's own tests sets PeerOK to
+// exactly instancectl.EdgeMainPIDPeer(). The rule itself is tested in
+// internal/mailer and internal/instancectl; this pins that the production
+// server is given it (Task 3 review P2-1: an admit-all rule there passed
+// every other test). It needs no carlos user, which EdgeMainPIDPeer's
+// construction does.
+func TestMailerServeIsGivenTheEdgePeerRule(t *testing.T) {
+ files := moduleGoFiles(t)
+ configs := 0
+ for path, f := range files {
+ if strings.HasPrefix(path, "internal/mailer/") {
+ continue
+ }
+ ast.Inspect(f, func(n ast.Node) bool {
+ lit, ok := n.(*ast.CompositeLit)
+ if !ok || !isSel(lit.Type, "mailer", "Config") {
+ return true
+ }
+ configs++
+ var peer ast.Expr
+ for _, el := range lit.Elts {
+ if kv, ok := el.(*ast.KeyValueExpr); ok {
+ if k, ok := kv.Key.(*ast.Ident); ok && k.Name == "PeerOK" {
+ peer = kv.Value
+ }
+ }
+ }
+ call, ok := peer.(*ast.CallExpr)
+ if !ok || !isSel(call.Fun, "instancectl", "EdgeMainPIDPeer") || len(call.Args) != 0 {
+ t.Errorf("%s: a mailer.Config whose PeerOK is not instancectl.EdgeMainPIDPeer()", path)
+ }
+ return true
+ })
+ }
+ if configs != 1 {
+ t.Fatalf("found %d mailer.Config literals outside internal/mailer, want exactly the one in mailer_cli.go", configs)
+ }
+}
+
+// EdgePeerRuleFor is a test seam (Task 3 review P3): no production code may
+// call it, since it takes the uid and the main pid from its caller.
+func TestEdgePeerRuleForHasNoProductionCaller(t *testing.T) {
+ for path, f := range moduleGoFiles(t) {
+ ast.Inspect(f, func(n ast.Node) bool {
+ if call, ok := n.(*ast.CallExpr); ok {
+ if isSel(call.Fun, "instancectl", "EdgePeerRuleFor") {
+ t.Errorf("%s calls instancectl.EdgePeerRuleFor", path)
+ }
+ if id, ok := call.Fun.(*ast.Ident); ok && id.Name == "EdgePeerRuleFor" {
+ t.Errorf("%s calls EdgePeerRuleFor", path)
+ }
+ }
+ return true
+ })
+ }
+}