Fichas: mail through rastrillo's FromCarlos, ready for CARLOS platform mail submission (SMTP unchanged until switched)
CARLOS platform mail submission, Task 9. Fable APPROVED. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from mail/from-carlos into main.
8 files changed,
+203
−13
Makefile+3 −1README.md+14 −0cmd/fichas/main.go+8 −3go.mod+1 −1go.sum+2 −2internal/fichas/config.go+11 −6internal/fichas/mailer.go+23 −0internal/fichas/mailer_test.go+141 −0
CI passed — run details
CI log
=== vet === go vet ./... go: downloading amadan.net/rastrillo/rastrillo v0.35.1-0.20261116213651-96812beacacc go vet -tags browser ./internal/fichastest/ === fmt === === test === go test ./... ? amadan.net/oficina/fichas/cmd/fichas [no test files] ok amadan.net/oficina/fichas/internal/fichas 1.957s ? amadan.net/oficina/fichas/internal/fichas/icons [no test files] ok amadan.net/oficina/fichas/internal/fichas/seal (cached) ok amadan.net/oficina/fichas/internal/fichastest 1.213s ? amadan.net/oficina/fichas/scripts/mockups [no test files] ? amadan.net/oficina/fichas/scripts/preview [no test files] ? amadan.net/oficina/fichas/scripts/writeestilo [no test files] === migration-check === go tool rastrillo migration check rastrillo migration check: models and migrations agree
diff --git a/Makefile b/Makefile| index 44cd462..6e2dbd9 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -54,7 +54,9 @@ release: version-check |
| @echo |
| @echo "built $(RELEASE_BIN) reporting version $(VERSION)" |
| @echo "ship it with:" |
| - @echo " carlos ship -app $(APP) -target $(RELEASE_GOOS)-$(RELEASE_GOARCH) $(RELEASE_BIN)" |
| + @echo " carlos ship -app $(APP) -mail submit -target $(RELEASE_GOOS)-$(RELEASE_GOARCH) $(RELEASE_BIN)" |
| + @echo "then promote it (carlos deploy has no -mail flag yet; see README):" |
| + @echo " carlos promote -app $(APP) <version>" |
| # version-check refuses rather than letting release ship a binary whose |
| # version is untrue. Both refusals are about the same thing: /api/version |
diff --git a/README.md b/README.md| index afceb40..8de6736 100644 |
| --- a/README.md |
| +++ b/README.md |
| @@ -109,6 +109,20 @@ carlos deploy --app fichas --account bds --channel edge --timeout 5m \ |
| releases/fichas-linux-arm64 |
| ``` |
| +**Mail submission.** Ship every build with `--mail submit`: it declares |
| +that the build sends through CARLOS mail submission (`mail.FromCarlos`, |
| +`internal/fichas/mailer.go`). Until `carlos email submission on --app |
| +fichas` runs, fichas keeps sending over `CARLOS_SMTP_*`; after it the |
| +agent sets `CARLOS_MAIL=submit`, fichas holds no SMTP credential, and |
| +every instance must run a build shipped with the flag. `carlos deploy` |
| +has no `--mail` flag yet (carlos main 9eec0236), so until it does ship |
| +and promote instead: |
| + |
| +```sh |
| +carlos ship --app fichas --account bds --mail submit releases/fichas-linux-arm64 |
| +carlos promote --app fichas --account bds <version> edge |
| +``` |
| + |
| ### Sign-in |
| Magic link through rastrillo's `auth`, with the owners list as the |
diff --git a/cmd/fichas/main.go b/cmd/fichas/main.go| index 47e3806..750a9ec 100644 |
| --- a/cmd/fichas/main.go |
| +++ b/cmd/fichas/main.go |
| @@ -8,7 +8,6 @@ import ( |
| "amadan.net/rastrillo/rastrillo" |
| "amadan.net/rastrillo/rastrillo/db" |
| - "amadan.net/rastrillo/rastrillo/mail" |
| fichas "amadan.net/oficina/fichas/internal/fichas" |
| ) |
| @@ -62,11 +61,17 @@ func main() { |
| // credential — into the process log; LoadConfig refuses to boot in |
| // that state unless FICHAS_ALLOW_LOGGED_MAIL says so out loud. But |
| // refusing was only half of memoria's fix: nothing was building a |
| - // real sender either. "CARLOS" is the env prefix the suite uses. |
| + // real sender either. fichas.Mailer: CARLOS mail submission or SMTP |
| + // under the suite's CARLOS prefix, through mail.FromCarlos. |
| + mailer, err := fichas.Mailer(cfg, logger) |
| + if err != nil { |
| + logger.Error("configure mail", "err", err) |
| + os.Exit(1) |
| + } |
| au, err := fichas.NewAuth(d.Writer(), fichas.AuthEnv{ |
| Origin: cfg.Origin, |
| InstanceKey: cfg.InstanceKey, |
| - Mailer: mail.FromEnv("CARLOS", logger), |
| + Mailer: mailer, |
| }, logger, app.Admit) |
| if err != nil { |
| logger.Error("wire sign-in", "err", err) |
diff --git a/go.mod b/go.mod| index d6cc7a2..0d6162e 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -7,7 +7,7 @@ toolchain go1.26.11 |
| require ( |
| amadan.net/oficina/estilo.git v0.0.0-20261111234914-39792ae278b0 |
| amadan.net/oficina/suite v0.0.0-20261102002704-91affa14eae4 |
| - amadan.net/rastrillo/rastrillo v0.35.1-0.20261111230602-f908c5fa31f1 |
| + amadan.net/rastrillo/rastrillo v0.35.1-0.20261116213651-96812beacacc |
| github.com/chromedp/cdproto v0.158.6 |
| github.com/chromedp/chromedp v0.21.0 |
| github.com/go-chi/chi/v5 v5.3.2 |
diff --git a/go.sum b/go.sum| index a04a003..d00f93f 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -4,8 +4,8 @@ amadan.net/oficina/suite v0.0.0-20261102002704-91affa14eae4 h1:BP+KHZTSgL7HDCz9I |
| amadan.net/oficina/suite v0.0.0-20261102002704-91affa14eae4/go.mod h1:Xgw0N+DBiDzkhQHm/nIhgumfaiMElDg/KIFD1OxTm5w= |
| amadan.net/rastrillo/fiesta v1.3.0 h1:7SsHw4aeWPLStR3RHFEEtX4CwqKu+Ig3PgNX8FvD8WM= |
| amadan.net/rastrillo/fiesta v1.3.0/go.mod h1:LpO391W1dIRUxOIKklHm+bGyWXtlFyBRRPQCysb9LjY= |
| -amadan.net/rastrillo/rastrillo v0.35.1-0.20261111230602-f908c5fa31f1 h1:tAGrlcgRBwpuJOvowtu7JKZ/QKjwDZIEtQs+EX9ggMI= |
| -amadan.net/rastrillo/rastrillo v0.35.1-0.20261111230602-f908c5fa31f1/go.mod h1:y45ucMD+Al4tN47mmnm7h6pGDw7N20k0CMg6IwB9vfc= |
| +amadan.net/rastrillo/rastrillo v0.35.1-0.20261116213651-96812beacacc h1:QakboVkj9nqA6ZaKXeDVXWLPobQSVvd80ic521aA8/U= |
| +amadan.net/rastrillo/rastrillo v0.35.1-0.20261116213651-96812beacacc/go.mod h1:y45ucMD+Al4tN47mmnm7h6pGDw7N20k0CMg6IwB9vfc= |
| amadan.net/rastrillo/rastrillo/money v0.1.0 h1:MxU4oqNrW8abxRTkgZBv24OYNpFnvonajNqeLIzmmaM= |
| amadan.net/rastrillo/rastrillo/money v0.1.0/go.mod h1:cmgkjBEIQh1pFSYvQk0UU6oPYS8kSDypdT9+t4Flx0U= |
| github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= |
diff --git a/internal/fichas/config.go b/internal/fichas/config.go| index df39fbe..eb3b4b3 100644 |
| --- a/internal/fichas/config.go |
| +++ b/internal/fichas/config.go |
| @@ -35,9 +35,9 @@ type Config struct { |
| // admitted set; in the SUITE shape it answers the narrower question |
| // of who operates the instance (ruling 5, memoria §2). |
| Owners []string |
| - // MailConfigured reports whether real SMTP is wired. False means |
| - // rastrillo substitutes mail.Logged, which writes sign-in links into |
| - // the process log. |
| + // MailConfigured reports whether real mail is wired: CARLOS mail |
| + // submission or SMTP. False means mail.Logged, which writes sign-in |
| + // links into the process log. |
| MailConfigured bool |
| // Shape is "lone" or "suite" — ruling 5's one switch. Suite is the |
| // intended default; see ErrSuiteNotBuilt. |
| @@ -124,11 +124,16 @@ func LoadConfig(getenv func(string) string) (Config, error) { |
| // past. memoria refuses to start in this state and this app claimed |
| // to without doing it. |
| // |
| - // The prefix is the suite's, matching what main hands mail.FromEnv. |
| - c.MailConfigured = getenv("CARLOS_SMTP_HOST") != "" && getenv("CARLOS_SMTP_FROM") != "" |
| + // Mail is either channel rastrillo's mail.FromCarlos takes (Mailer, |
| + // mailer.go): CARLOS mail submission, which the agent turns on with |
| + // CARLOS_MAIL=submit beside its control socket, or SMTP under the |
| + // suite's prefix, CARLOS_SMTP_*. |
| + submit := getenv("CARLOS_MAIL") == "submit" |
| + smtp := getenv("CARLOS_SMTP_HOST") != "" && getenv("CARLOS_SMTP_FROM") != "" |
| + c.MailConfigured = submit || smtp |
| if !c.MailConfigured && !c.AllowLoggedMail { |
| return Config{}, fmt.Errorf( |
| - "%w: CARLOS_SMTP_HOST and CARLOS_SMTP_FROM — without them every sign-in link is written "+ |
| + "%w: CARLOS_MAIL=submit, or CARLOS_SMTP_HOST and CARLOS_SMTP_FROM — without one every sign-in link is written "+ |
| "to the log instead of sent, which is a live credential in storage we cannot retract; "+ |
| "set FICHAS_ALLOW_LOGGED_MAIL=1 to accept that on a loopback origin", ErrMissing) |
| } |
diff --git a/internal/fichas/mailer.go b/internal/fichas/mailer.go| new file mode 100644 |
| index 0000000..4b47b1f |
| --- /dev/null |
| +++ b/internal/fichas/mailer.go |
| @@ -0,0 +1,23 @@ |
| +package fichas |
| + |
| +import ( |
| + "log/slog" |
| + |
| + "amadan.net/rastrillo/rastrillo/mail" |
| +) |
| + |
| +// Mailer builds the sign-in sender. Call it only where fichas serves: |
| +// a CARLOS seed or one-shot runs with no control socket, and the agent |
| +// strips CARLOS_MAIL from it, so mail.FromCarlos there would refuse once |
| +// the app is switched to submission. |
| +// |
| +// mail.FromCarlos picks submission when CARLOS_MAIL=submit, else SMTP, |
| +// and refuses with neither, or with submission half set up. Logged mail |
| +// is only what LoadConfig admitted: FICHAS_ALLOW_LOGGED_MAIL on loopback |
| +// with no channel at all. |
| +func Mailer(cfg Config, logger *slog.Logger) (mail.Sender, error) { |
| + if !cfg.MailConfigured { |
| + return mail.FromEnv("CARLOS", logger), nil |
| + } |
| + return mail.FromCarlos("CARLOS", logger) |
| +} |
diff --git a/internal/fichas/mailer_test.go b/internal/fichas/mailer_test.go| new file mode 100644 |
| index 0000000..b47f3c2 |
| --- /dev/null |
| +++ b/internal/fichas/mailer_test.go |
| @@ -0,0 +1,141 @@ |
| +package fichas |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "log/slog" |
| + "net" |
| + "net/http" |
| + "os" |
| + "path/filepath" |
| + "testing" |
| + |
| + "amadan.net/rastrillo/rastrillo/mail" |
| +) |
| + |
| +// mailEnv is every variable the boot's mail choice reads, so each case |
| +// starts from none of them whatever the test process inherited. |
| +var mailEnv = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET", |
| + "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CARLOS_SMTP_HOST", |
| + "CARLOS_SMTP_PORT", "CARLOS_SMTP_FROM", "CARLOS_SMTP_USER", |
| + "CARLOS_SMTP_PASS"} |
| + |
| +// boot is main's mail path: LoadConfig, then Mailer, against the |
| +// process environment, which is what mail.FromCarlos reads. |
| +func boot(t *testing.T, env map[string]string) (mail.Sender, error) { |
| + t.Helper() |
| + for _, key := range mailEnv { |
| + t.Setenv(key, "") |
| + } |
| + for key, value := range env { |
| + t.Setenv(key, value) |
| + } |
| + config, err := LoadConfig(os.Getenv) |
| + if err != nil { |
| + return nil, err |
| + } |
| + return Mailer(config, slog.New(slog.DiscardHandler)) |
| +} |
| + |
| +// loopback is a local instance that asked for logged mail. |
| +func loopback() map[string]string { |
| + return map[string]string{ |
| + "FICHAS_DEK": goodDEK(), "FICHAS_INSTANCE_KEY": "k", |
| + "FICHAS_OWNERS": "ana@example.com", |
| + "FICHAS_ORIGIN": "http://127.0.0.1:8824", |
| + "FICHAS_ALLOW_LOGGED_MAIL": "1", |
| + } |
| +} |
| + |
| +// public is a deployed instance: no logged mail, a public origin. |
| +func public(env map[string]string) map[string]string { |
| + full := loopback() |
| + full["FICHAS_ALLOW_LOGGED_MAIL"] = "" |
| + full["FICHAS_ORIGIN"] = "https://paulca.fichas.oficina.works" |
| + for key, value := range env { |
| + full[key] = value |
| + } |
| + return full |
| +} |
| + |
| +// mailVerb is the agent's control socket, answering POST /v1/mail. |
| +func mailVerb(t *testing.T) (string, chan string) { |
| + t.Helper() |
| + // A short directory: a unix socket path has a 104-byte limit. |
| + dir, err := os.MkdirTemp("/tmp", "fichas-mail") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.RemoveAll(dir) }) |
| + socket := filepath.Join(dir, "control.sock") |
| + listener, err := net.Listen("unix", socket) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + paths := make(chan string, 1) |
| + server := &http.Server{Handler: http.HandlerFunc( |
| + func(writer http.ResponseWriter, request *http.Request) { |
| + paths <- request.Method + " " + request.URL.Path |
| + writer.Write([]byte(`{"message_id":"m-1"}`)) |
| + })} |
| + go server.Serve(listener) |
| + t.Cleanup(func() { server.Close() }) |
| + return socket, paths |
| +} |
| + |
| +func TestBootsOnMailSubmission(t *testing.T) { |
| + socket, paths := mailVerb(t) |
| + sender, err := boot(t, public(map[string]string{ |
| + "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, |
| + "CARLOS_ADMIN_TOKEN": "token", |
| + "CARLOS_MAIL_FROM": "noreply@fichas.oficina.works"})) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + err = sender.Send(context.Background(), "ana@example.com", |
| + "Sign in", "link") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := <-paths; got != "POST /v1/mail" { |
| + t.Fatalf("the sign-in went to %s, not the mail verb", got) |
| + } |
| +} |
| + |
| +func TestBootsOnSMTPAlone(t *testing.T) { |
| + sender, err := boot(t, public(map[string]string{ |
| + "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", |
| + "CARLOS_SMTP_FROM": "noreply@fichas.oficina.works"})) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !mail.IsConfigured(sender) { |
| + t.Fatal("SMTP is configured but the sender only logs") |
| + } |
| +} |
| + |
| +func TestRefusesWithNeitherChannel(t *testing.T) { |
| + _, err := boot(t, public(nil)) |
| + if !errors.Is(err, ErrMissing) { |
| + t.Fatalf("booted with no mail: %v", err) |
| + } |
| +} |
| + |
| +func TestRefusesHalfASubmission(t *testing.T) { |
| + // CARLOS_MAIL=submit with no socket: mail.FromCarlos says why. |
| + _, err := boot(t, public(map[string]string{"CARLOS_MAIL": "submit", |
| + "CARLOS_MAIL_FROM": "noreply@fichas.oficina.works"})) |
| + if err == nil { |
| + t.Fatal("booted with submission and no control socket") |
| + } |
| +} |
| + |
| +func TestLoggedMailStaysLoopbackOnly(t *testing.T) { |
| + sender, err := boot(t, loopback()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if mail.IsConfigured(sender) { |
| + t.Fatal("logged mail on loopback built a real sender") |
| + } |
| +} |