Adds a real :25 SMTP listener to the Go protocols service, giving F1's "Nucleo SMTP" milestone its first working sub-project: application, infrastructure, and presentation layers on top of F0's domain-only Clean Architecture. - ProcessInboundEmailUseCase resolves recipients against Postgres and persists accepted mail (blob dedup by content hash, transactional UID allocation + outbox event, never sending 250 OK before durable commit per PLAN.md section 6.1). - Postgres repositories via pgx/v5 (replacing lib/pq entirely). - Local-disk blob storage with content-hash dedup, rename-before-commit ordering so a DB row can never reference a file that isn't actually on disk. - A local-file TLS cert provider for STARTTLS, with an ephemeral self-signed fallback so a missing/misconfigured cert degrades gracefully instead of crash-looping the service (PLAN.md section 8.4). - A github.com/emersion/go-smtp Backend/Session adapting real SMTP traffic to the use case: unknown recipients get 550 5.1.1 before DATA, duplicate RCPT TO for the same address is deduplicated, TLS is hardened to the project's required PFS-only cipher/curve policy. - A dev-seed script and Makefile target for local testing. Explicitly out of scope (separate follow-up sub-projects): alias resolution, SPF/DKIM/DMARC/ARC verification, Rspamd/ClamAV, Submission ports with SASL auth, DKIM signing, outbound delivery, and the real Traefik acme.json cert watcher. Verified live end-to-end via WSL Docker: the full compose stack (db, redis, migrate, auth, storage, protocols, web) reaches healthy, and a real swaks SMTP session is accepted and durably persisted with correct sequential UIDs, while an unknown recipient is correctly rejected with 550 5.1.1 and zero rows persisted. Built via 10 sequential tasks under subagent-driven development, each independently reviewed, plus a final whole-branch review that caught and fixed a TLS-crash bug, a blob commit-ordering bug, missing TLS hardening, and missing CI integration coverage before merge. Known follow-up, not blocking: email_messages' partition only covers July 2026 and needs a partition-management job before 2026-08-01.
93 lines
3.0 KiB
Go
93 lines
3.0 KiB
Go
// Command lambdamail-protocols is the entry point for the protocols service.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
gosmtp "github.com/emersion/go-smtp"
|
|
|
|
"lambdamail/protocols/internal/application/usecase"
|
|
"lambdamail/protocols/internal/health"
|
|
"lambdamail/protocols/internal/infrastructure/diskstorage"
|
|
"lambdamail/protocols/internal/infrastructure/postgres"
|
|
tlsprovider "lambdamail/protocols/internal/infrastructure/tls"
|
|
smtppresentation "lambdamail/protocols/internal/presentation/smtp"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
|
|
pool, err := postgres.NewPool(ctx, os.Getenv("DATABASE_URL"))
|
|
if err != nil {
|
|
log.Fatalf("open database pool: %v", err)
|
|
}
|
|
defer pool.Close()
|
|
|
|
spoolDir := envOrDefault("PROTOCOLS_SPOOL_DIR", "/var/lambdamail/spool")
|
|
if err := os.MkdirAll(spoolDir, 0o755); err != nil {
|
|
log.Fatalf("create spool dir %s: %v", spoolDir, err)
|
|
}
|
|
|
|
mailboxes := postgres.NewMailboxRepository(pool)
|
|
blobs := diskstorage.NewLocalDiskBlobStorage(pool, spoolDir)
|
|
messages := postgres.NewInboundMessageRepository(pool)
|
|
useCase := usecase.NewProcessInboundEmailUseCase(mailboxes, blobs, messages)
|
|
|
|
certProvider, err := tlsprovider.NewLocalFileCertProvider(
|
|
os.Getenv("PROTOCOLS_CERT_PATH"),
|
|
os.Getenv("PROTOCOLS_KEY_PATH"),
|
|
)
|
|
if err != nil {
|
|
log.Printf("WARNING: could not load configured TLS certificate (%v) - falling back to an ephemeral self-signed certificate; this is degraded mode, not suitable for real deployments", err)
|
|
certProvider, err = tlsprovider.NewEphemeralSelfSignedCertProvider()
|
|
if err != nil {
|
|
log.Fatalf("generate fallback self-signed certificate: %v", err)
|
|
}
|
|
}
|
|
|
|
backend := smtppresentation.NewBackend(useCase)
|
|
smtpServer := gosmtp.NewServer(backend)
|
|
smtpServer.Addr = ":25"
|
|
smtpServer.Domain = envOrDefault("PRIMARY_MAIL_HOST", "mail.localhost")
|
|
smtpServer.MaxMessageBytes = 52428800 // 50 MB default; per-domain limit is a future iteration
|
|
smtpServer.MaxRecipients = 100
|
|
smtpServer.TLSConfig = &tls.Config{
|
|
GetCertificate: certProvider.GetCertificate,
|
|
MinVersion: tls.VersionTLS12,
|
|
CipherSuites: []uint16{
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
},
|
|
CurvePreferences: []tls.CurveID{tls.X25519, tls.CurveP256, tls.CurveP384},
|
|
}
|
|
|
|
go func() {
|
|
log.Printf("lambdamail-protocols SMTP listening on %s", smtpServer.Addr)
|
|
if err := smtpServer.ListenAndServe(); err != nil {
|
|
log.Fatalf("smtp serve: %v", err)
|
|
}
|
|
}()
|
|
|
|
handler := health.Handler(func() error { return pool.Ping(ctx) })
|
|
addr := ":8080"
|
|
log.Printf("lambdamail-protocols health listening on %s", addr)
|
|
if err := http.ListenAndServe(addr, handler); err != nil {
|
|
log.Fatalf("serve: %v", err)
|
|
}
|
|
}
|
|
|
|
func envOrDefault(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|