Split frontend/backend images (scratch backend), Helm chart, multi-DB config, spamd sidecar

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonas Leder 2026-11-16 11:31:37 +01:00
commit 4a505c2226
32 changed files with 1830 additions and 86 deletions

5
.dockerignore Normal file
View file

@ -0,0 +1,5 @@
**/node_modules
frontend/dist
.git
*.db
.env

View file

@ -3,9 +3,17 @@ JMAP_URL=https://mail.example.org # Stalwart base URL
JMAP_USER=catchall@mail-test.example.org
JMAP_PASSWORD=change-me
# JMAP_TOKEN= # alternative: bearer token instead of user/password
# SPAMD_ADDR= # empty = use the bundled spamd (Docker image); set to use an external one
DB_DSN=mailtester.db
# SPAMD_ADDR=127.0.0.1:783 # empty = SpamAssassin check skipped
# Database: sqlite (default), mysql or postgres
DB_DRIVER=sqlite
DB_NAME=mailtester.db # sqlite: file path; mysql/postgres: database name
# DB_HOST=localhost
# DB_PORT=3306 # default 3306 (mysql) / 5432 (postgres)
# DB_USER=mailtester
# DB_PASSWORD=
# DB_SSLMODE=prefer # postgres only
LISTEN=:8080
FRONTEND_DIR=./static
# TRUSTED_PROXIES=10.0.0.0/8 # set when behind a reverse proxy, else rate limit sees the proxy IP
# LOG_LEVEL=INFO

View file

@ -1,23 +0,0 @@
FROM node:24-alpine AS web
WORKDIR /w
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ .
RUN npm run build
FROM golang:1.27-alpine AS api
WORKDIR /b
COPY backend/ .
RUN CGO_ENABLED=0 go build -o /mailtester .
FROM alpine:3.22
# spamassassin is bundled; the app starts spamd itself when SPAMD_ADDR is unset
RUN apk add --no-cache spamassassin ca-certificates && sa-update || true
RUN adduser -D -h /app app && mkdir /data && chown app /data
USER app
WORKDIR /app
COPY --from=api /mailtester .
COPY --from=web /w/dist ./static
ENV DB_DSN=/data/mailtester.db HOME=/tmp
EXPOSE 8080
ENTRYPOINT ["/app/mailtester"]

15
Dockerfile.backend Normal file
View file

@ -0,0 +1,15 @@
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.10.0 AS xx
FROM --platform=$BUILDPLATFORM golang:1.27.3-alpine AS build
ARG TARGETPLATFORM
ARG GOPROXY
COPY --from=xx / /
WORKDIR /app
COPY backend/go.mod backend/go.sum ./
RUN go mod download
COPY ./backend/ ./
RUN CGO_ENABLED=0 GOOS=linux xx-go build -ldflags "-w -s" -o /mailtester .
FROM jonasled.dev/infra/images/scratch-rootless:latest@sha256:9e291c85277e40987bdbc84af228b4b40db5c24e1634d047465e2198ed9302e8
ENV GIN_MODE=release
COPY --from=build /mailtester /mailtester
ENTRYPOINT ["/mailtester"]

12
Dockerfile.frontend Normal file
View file

@ -0,0 +1,12 @@
FROM --platform=$BUILDPLATFORM node:24.22-alpine AS build
WORKDIR /app
COPY ./frontend/package*.json ./
RUN npm ci
COPY ./frontend ./
RUN npm run build
FROM cgr.dev/chainguard/nginx:latest
ARG NGINX_CONF=nginx.k8s.conf
WORKDIR /usr/share/nginx/html
COPY --from=build /app/dist/ .
COPY container/${NGINX_CONF} /etc/nginx/conf.d/nginx.default.conf

9
Dockerfile.spamassassin Normal file
View file

@ -0,0 +1,9 @@
# spamd sidecar for the backend (the backend image is scratch and cannot run spamd)
FROM alpine:3.22
RUN apk add --no-cache spamassassin && sa-update || true
RUN adduser -D -u 1000 spamd
USER 1000
ENV HOME=/tmp
EXPOSE 783
# -A: allow all clients; only reachable inside the pod / compose network
ENTRYPOINT ["spamd", "-i", "0.0.0.0", "-A", "0.0.0.0/0", "-p", "783", "-x", "--max-children", "3", "-s", "stderr"]

View file

@ -7,19 +7,25 @@ send a mail to it, get a score out of 10.
7 DNS blacklists, TLS in transit, message format (Message-ID, Date, MIME, text alternative).
Weights live in [backend/checks.go](backend/checks.go); skipped checks (e.g. no spamd) don't count.
SpamAssassin is bundled in the Docker image (started by the app, no extra service).
SpamAssassin runs as a sidecar container next to the backend (the backend image is `scratch`).
**Stack:** Go (Gin, Gorm + gormigrate, swaggo, godotenv), React + Vite, Stalwart via JMAP.
## Run
```sh
cp .env.example .env # edit
docker compose up --build
# edit the environment block in compose.yaml, then:
docker compose up --build # frontend :8080, backend, spamassassin
```
Kubernetes: `helm install mail-tester ./helm -f my-values.yaml` (frontend and backend are separate deployments,
SpamAssassin is a sidecar of the backend; backend is deliberately a single replica).
Database: `DB_DRIVER=sqlite|mysql|postgres` with `DB_HOST`, `DB_PORT`, `DB_USER`, `DB_PASSWORD`, `DB_NAME`
(`DB_SSLMODE` for postgres; for sqlite `DB_NAME` is the file path). See `.env.example`.
Then set up Stalwart: [docs/stalwart.md](docs/stalwart.md).
Dev: `cd backend && go run .` and `cd frontend && npm i && npm run dev` (proxies `/api` to :8080).
Dev: `cd backend && go run .` (reads `.env`) and `cd frontend && npm i && npm run dev` (proxies `/api` to :8080).
## API

View file

@ -14,11 +14,14 @@ require (
github.com/swaggo/swag v1.16.6
golang.org/x/net v0.62.0
golang.org/x/time v0.16.0
gorm.io/driver/mysql v1.6.0
gorm.io/driver/postgres v1.6.3
gorm.io/gorm v1.31.2
jonasled.dev/jonasled/go-libs v0.0.4
)
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
@ -37,9 +40,14 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.30.1 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.10.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect

View file

@ -1,5 +1,7 @@
blitiri.com.ar/go/spf v1.6.0 h1:TK91HOya1R2J5b+x+NZfdYTqDqbr+Q+hil5gy8WzLDQ=
blitiri.com.ar/go/spf v1.6.0/go.mod h1:x9HYT28jEB65YMJOIVWSx0p88YCJ2h1N0fDFEhhWFBc=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
@ -54,6 +56,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w=
github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
@ -65,6 +69,14 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@ -120,6 +132,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
@ -207,6 +220,10 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
gorm.io/driver/postgres v1.6.3 h1:bAn6O2pUa8LtpWEvL5NFU4+52Tfx8Ut7IVaIacCLcI0=
gorm.io/driver/postgres v1.6.3/go.mod h1:0c4fQA44XhOklXDkgtuKqysHCycTa5i9e3EIpDGCwXk=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo=

View file

@ -7,8 +7,6 @@ package main
import (
"golang.org/x/time/rate"
"net/http"
"os"
"os/exec"
_ "github.com/joho/godotenv/autoload"
swaggerFiles "github.com/swaggo/files"
@ -39,37 +37,15 @@ func main() {
gin.SetMode(gin.ReleaseMode)
}
db, err := openDB(config.GetOrDefault("DB_DSN", "mailtester.db"))
db, err := openDB()
if err != nil {
log.Log.Fatal("database: ", err)
}
go runWorker(db, NewJMAP(jmapURL, user, pass, token), spamdAddr())
go runWorker(db, NewJMAP(jmapURL, user, pass, token), config.GetOrDefault("SPAMD_ADDR", ""))
s := &server{db: db, domain: domain, limits: map[string]*rate.Limiter{}}
addr := config.GetOrDefault("LISTEN", ":8080")
log.Log.Info("listening on ", addr)
srv := &http.Server{Addr: addr, Handler: s.routes(config.GetOrDefault("FRONTEND_DIR", "./static"))}
srv := &http.Server{Addr: addr, Handler: s.routes()}
log.Log.Fatal(srv.ListenAndServe())
}
// spamdAddr returns SPAMD_ADDR, or starts the bundled spamd (if installed) and returns its address.
// shortcut: child is not supervised or restarted; fine in a container, add a restart loop if spamd crashes.
func spamdAddr() string {
if a := config.GetOrDefault("SPAMD_ADDR", ""); a != "" {
return a
}
bin, err := exec.LookPath("spamd")
if err != nil {
log.Log.Warn("no spamd found and SPAMD_ADDR unset: SpamAssassin check disabled")
return ""
}
cmd := exec.Command(bin, "-i", "127.0.0.1", "-p", "783", "-x", "--max-children", "3", "-s", "stderr")
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Start(); err != nil {
log.Log.Error("start spamd: ", err)
return ""
}
log.Log.Info("started bundled spamd, pid ", cmd.Process.Pid)
go func() { log.Log.Error("spamd exited: ", cmd.Wait()) }()
return "127.0.0.1:783"
}

View file

@ -19,7 +19,7 @@ func migrate(db *gorm.DB) error {
Score float64
Subject string
Sender string
Checks string
Checks string `gorm:"size:16777216"` // mysql: mediumtext instead of 64KB text
CreatedAt time.Time
ExpiresAt time.Time
}

View file

@ -3,7 +3,6 @@ package main
import (
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
@ -64,7 +63,7 @@ func (s *server) allow(ip string) bool {
return l.Allow()
}
func (s *server) routes(frontendDir string) *gin.Engine {
func (s *server) routes() *gin.Engine {
r := gin.New()
r.Use(gin.Recovery(), securityHeaders)
if p := os.Getenv("TRUSTED_PROXIES"); p != "" {
@ -78,20 +77,7 @@ func (s *server) routes(frontendDir string) *gin.Engine {
api.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) })
docs(r)
index := filepath.Join(frontendDir, "index.html")
r.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.JSON(404, ErrorResponse{"not found"})
return
}
// Clean on a rooted path blocks ../ traversal
f := filepath.Join(frontendDir, filepath.Clean("/"+c.Request.URL.Path))
if st, err := os.Stat(f); err == nil && !st.IsDir() {
c.File(f)
return
}
c.File(index)
})
r.NoRoute(func(c *gin.Context) { c.JSON(404, ErrorResponse{"not found"}) })
return r
}

View file

@ -1,11 +1,16 @@
package main
import (
"fmt"
"net/url"
"time"
"github.com/glebarez/sqlite"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"jonasled.dev/jonasled/go-libs/config"
"jonasled.dev/jonasled/go-libs/log"
)
@ -14,8 +19,26 @@ const (
retention = 24 * time.Hour // how long reports and raw mail are kept
)
func openDB(dsn string) (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: gormlogger.Default.LogMode(gormlogger.Warn)})
func openDB() (*gorm.DB, error) {
var d gorm.Dialector
user, pass := config.GetOrDefault("DB_USER", ""), config.GetOrDefault("DB_PASSWORD", "")
host, name := config.GetOrDefault("DB_HOST", "localhost"), config.GetOrDefault("DB_NAME", "mailtester")
switch drv := config.GetOrDefault("DB_DRIVER", "sqlite"); drv {
case "sqlite":
d = sqlite.Open(config.GetOrDefault("DB_NAME", "mailtester.db") + "?_pragma=busy_timeout(5000)")
case "mysql":
d = mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
user, pass, host, config.GetOrDefault("DB_PORT", "3306"), name))
case "postgres":
d = postgres.Open((&url.URL{
Scheme: "postgres", User: url.UserPassword(user, pass),
Host: host + ":" + config.GetOrDefault("DB_PORT", "5432"), Path: name,
RawQuery: "sslmode=" + url.QueryEscape(config.GetOrDefault("DB_SSLMODE", "prefer")),
}).String())
default:
return nil, fmt.Errorf("unknown DB_DRIVER %q (sqlite, mysql, postgres)", drv)
}
db, err := gorm.Open(d, &gorm.Config{Logger: gormlogger.Default.LogMode(gormlogger.Warn)})
if err != nil {
return nil, err
}

63
compose.yaml Normal file
View file

@ -0,0 +1,63 @@
x-backend-environment: &backend-environment
MAIL_DOMAIN: mail-test.example.org
JMAP_URL: https://mail.example.org
JMAP_USER: catchall@mail-test.example.org
JMAP_PASSWORD: change-me
SPAMD_ADDR: spamassassin:783
# SQLite (default)
DB_DRIVER: sqlite
DB_NAME: /data/mailtester.db
# MySQL / MariaDB
# DB_DRIVER: mysql
# DB_HOST: 1.2.3.4
# DB_PORT: 3306
# DB_USER: mailtester
# DB_PASSWORD: mysecurepassword
# DB_NAME: mailtester
# PostgreSQL
# DB_DRIVER: postgres
# DB_HOST: 1.2.3.4
# DB_PORT: 5432
# DB_USER: mailtester
# DB_PASSWORD: mysecurepassword
# DB_NAME: mailtester
# DB_SSLMODE: prefer
services:
frontend:
image: jonasled.dev/jonasled/mail-tester/frontend:latest
restart: always
build:
dockerfile: ./Dockerfile.frontend
context: .
args:
NGINX_CONF: nginx.conf
ports:
- 127.0.0.1:8080:8080
backend:
image: jonasled.dev/jonasled/mail-tester/backend:latest
build:
dockerfile: ./Dockerfile.backend
context: .
user: "1000:1000"
restart: always
volumes:
- mailtester:/data # only required for SQLite
environment:
<<: *backend-environment
depends_on:
- spamassassin
spamassassin:
image: jonasled.dev/jonasled/mail-tester/spamassassin:latest
restart: always
build:
dockerfile: ./Dockerfile.spamassassin
context: .
volumes:
mailtester:

32
container/nginx.conf Normal file
View file

@ -0,0 +1,32 @@
server {
listen 8080;
server_name localhost;
add_header Content-Security-Policy "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; manifest-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" always;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "no-referrer" always;
add_header Permissions-Policy "accelerometer=(), autoplay=(), camera=(), display-capture=(), geolocation=(), gyroscope=(), microphone=(), midi=(), payment=(), usb=()" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Embedder-Policy "require-corp" always;
add_header Cross-Origin-Resource-Policy "same-origin" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
# compose: no ingress, so proxy the API to the backend (Swagger UI keeps its own relaxed CSP)
location /api/ {
proxy_pass http://backend:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# an add_header here stops the server-level headers being inherited, so the backend's own (incl. Swagger CSP) pass through
add_header X-Api-Proxy "mail-tester" always;
}
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
}

21
container/nginx.k8s.conf Normal file
View file

@ -0,0 +1,21 @@
server {
listen 8080;
server_name localhost;
add_header Content-Security-Policy "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; manifest-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" always;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "no-referrer" always;
add_header Permissions-Policy "accelerometer=(), autoplay=(), camera=(), display-capture=(), geolocation=(), gyroscope=(), microphone=(), midi=(), payment=(), usb=()" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Embedder-Policy "require-corp" always;
add_header Cross-Origin-Resource-Policy "same-origin" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
}

View file

@ -1,8 +0,0 @@
services:
mail-tester:
build: .
env_file: .env
ports: ["8080:8080"]
volumes: ["data:/data"]
volumes:
data:

View file

@ -48,8 +48,8 @@ As a safety net you can additionally set an auto-expunge / retention of 1 day in
## 4. SpamAssassin
The Docker image bundles SpamAssassin: the app starts `spamd` on 127.0.0.1:783 itself, nothing to configure.
Set `SPAMD_ADDR` only to use an external spamd. Stalwart's own spam filter is not used.
`compose.yaml` and the Helm chart run a `spamd` sidecar (`Dockerfile.spamassassin`) and set `SPAMD_ADDR` for you.
Without `SPAMD_ADDR` the check is skipped. Stalwart's own spam filter is not used.
## 5. DNS resolver for blacklists

1092
frontend/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,2 @@
allowBuilds:
esbuild: set this to true or false

7
helm/.helmignore Normal file
View file

@ -0,0 +1,7 @@
.DS_Store
.git/
*.swp
*.bak
*.tmp
.idea/
.vscode/

6
helm/Chart.yaml Normal file
View file

@ -0,0 +1,6 @@
apiVersion: v2
name: mail-tester
description: Open source mail deliverability tester
type: application
version: 1.0.0
appVersion: "1.0.0"

113
helm/templates/_helpers.tpl Normal file
View file

@ -0,0 +1,113 @@
{{- define "mail-tester.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- define "mail-tester.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
{{- define "mail-tester.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- define "mail-tester.labels" -}}
helm.sh/chart: {{ include "mail-tester.chart" . }}
{{ include "mail-tester.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{- define "mail-tester.selectorLabels" -}}
app.kubernetes.io/name: {{ include "mail-tester.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{- define "mail-tester.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "mail-tester.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
{{- define "mail-tester.sqlite" -}}
{{- eq .Values.db.driver "sqlite" -}}
{{- end }}
{{- define "mail-tester.dbSecretName" -}}
{{- default (printf "%s-db" (include "mail-tester.fullname" .)) .Values.db.existingSecret -}}
{{- end }}
{{- define "mail-tester.jmapSecretName" -}}
{{- default (printf "%s-jmap" (include "mail-tester.fullname" .)) .Values.jmap.existingSecret -}}
{{- end }}
{{/* Environment of the backend container: mail, JMAP, SpamAssassin and the split DB_* settings. */}}
{{- define "mail-tester.backendEnv" -}}
- name: MAIL_DOMAIN
value: {{ required "mailDomain is required" .Values.mailDomain | quote }}
- name: JMAP_URL
value: {{ required "jmap.url is required" .Values.jmap.url | quote }}
{{- if .Values.jmap.useToken }}
- name: JMAP_TOKEN
valueFrom:
secretKeyRef:
name: {{ include "mail-tester.jmapSecretName" . }}
key: token
{{- else }}
- name: JMAP_USER
value: {{ required "jmap.user is required" .Values.jmap.user | quote }}
- name: JMAP_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "mail-tester.jmapSecretName" . }}
key: password
{{- end }}
{{- if .Values.spamassassin.enabled }}
- name: SPAMD_ADDR
value: "127.0.0.1:783"
{{- end }}
- name: LOG_LEVEL
value: {{ .Values.logLevel | quote }}
{{- if .Values.trustedProxies }}
- name: TRUSTED_PROXIES
value: {{ .Values.trustedProxies | quote }}
{{- end }}
- name: DB_DRIVER
value: {{ .Values.db.driver | quote }}
{{- if eq .Values.db.driver "sqlite" }}
- name: DB_NAME
value: "/data/{{ .Values.db.name }}.db"
{{- else }}
- name: DB_NAME
value: {{ .Values.db.name | quote }}
- name: DB_HOST
value: {{ required "db.host is required for mysql/postgres" .Values.db.host | quote }}
{{- with .Values.db.port }}
- name: DB_PORT
value: {{ . | quote }}
{{- end }}
- name: DB_USER
value: {{ required "db.user is required for mysql/postgres" .Values.db.user | quote }}
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "mail-tester.dbSecretName" . }}
key: {{ .Values.db.existingSecretKey }}
{{- if eq .Values.db.driver "postgres" }}
- name: DB_SSLMODE
value: {{ .Values.db.sslmode | quote }}
{{- end }}
{{- end }}
{{- end }}

View file

@ -0,0 +1,95 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "mail-tester.fullname" . }}-backend
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
spec:
replicas: 1
strategy:
type: Recreate # sqlite volume is ReadWriteOnce; two pollers would double-process mail
selector:
matchLabels:
{{- include "mail-tester.selectorLabels" . | nindent 6 }}
mail-tester/service: backend
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
mail-tester/service: backend
{{- include "mail-tester.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "mail-tester.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: backend
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repositoryBackend }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /api/v1/health
port: http
readinessProbe:
httpGet:
path: /api/v1/health
port: http
env:
{{- include "mail-tester.backendEnv" . | nindent 12 }}
{{- if eq (include "mail-tester.sqlite" .) "true" }}
volumeMounts:
- name: data
mountPath: /data
{{- end }}
resources:
{{- toYaml (default .Values.resources .Values.resourcesBackend) | nindent 12 }}
{{- if .Values.spamassassin.enabled }}
- name: spamassassin
image: "{{ .Values.image.repositorySpamassassin }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
ports:
- name: spamd
containerPort: 783
readinessProbe:
tcpSocket:
port: spamd
resources:
{{- toYaml .Values.spamassassin.resources | nindent 12 }}
{{- end }}
{{- if eq (include "mail-tester.sqlite" .) "true" }}
volumes:
- name: data
{{- if .Values.persistence.enabled }}
persistentVolumeClaim:
claimName: {{ default (printf "%s-data" (include "mail-tester.fullname" .)) .Values.persistence.existingClaim }}
{{- else }}
emptyDir: {}
{{- end }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}

View file

@ -0,0 +1,57 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "mail-tester.fullname" . }}-frontend
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCountFrontend }}
selector:
matchLabels:
{{- include "mail-tester.selectorLabels" . | nindent 6 }}
mail-tester/service: frontend
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
mail-tester/service: frontend
{{- include "mail-tester.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "mail-tester.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: frontend
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repositoryFrontend }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}

View file

@ -0,0 +1,50 @@
{{- if .Values.ingress.enabled }}
{{- $fullName := include "mail-tester.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- with .Values.ingress.className }}
ingressClassName: {{ . }}
{{- end }}
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . | quote }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
pathType: {{ default "Prefix" .pathType }}
backend:
service:
name: {{ $fullName }}-frontend
port:
number: {{ $svcPort }}
- path: {{ .path }}api
pathType: {{ default "Prefix" .pathType }}
backend:
service:
name: {{ $fullName }}-backend
port:
number: {{ $svcPort }}
{{- end }}
{{- end }}
{{- end }}

16
helm/templates/pvc.yaml Normal file
View file

@ -0,0 +1,16 @@
{{- if and (eq .Values.db.driver "sqlite") .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ include "mail-tester.fullname" . }}-data
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
spec:
accessModes: [ReadWriteOnce]
{{- with .Values.persistence.storageClass }}
storageClassName: {{ . | quote }}
{{- end }}
resources:
requests:
storage: {{ .Values.persistence.size }}
{{- end }}

View file

@ -0,0 +1,27 @@
{{- if and (not .Values.jmap.existingSecret) }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "mail-tester.jmapSecretName" . }}
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
type: Opaque
stringData:
{{- if .Values.jmap.useToken }}
token: {{ .Values.jmap.password | quote }}
{{- else }}
password: {{ .Values.jmap.password | quote }}
{{- end }}
{{- end }}
{{- if and (ne .Values.db.driver "sqlite") (not .Values.db.existingSecret) }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "mail-tester.dbSecretName" . }}
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
type: Opaque
stringData:
{{ .Values.db.existingSecretKey }}: {{ .Values.db.password | quote }}
{{- end }}

View file

@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "mail-tester.fullname" . }}-backend
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "mail-tester.selectorLabels" . | nindent 4 }}
mail-tester/service: backend

View file

@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "mail-tester.fullname" . }}-frontend
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "mail-tester.selectorLabels" . | nindent 4 }}
mail-tester/service: frontend

View file

@ -0,0 +1,12 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "mail-tester.serviceAccountName" . }}
labels:
{{- include "mail-tester.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}

85
helm/values.yaml Normal file
View file

@ -0,0 +1,85 @@
image:
repositoryBackend: jonasled.dev/jonasled/mail-tester/backend
repositoryFrontend: jonasled.dev/jonasled/mail-tester/frontend
repositorySpamassassin: jonasled.dev/jonasled/mail-tester/spamassassin
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: ""
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
create: true
annotations: {}
name: ""
podAnnotations: {}
podSecurityContext: {}
securityContext: {}
# The backend polls Stalwart, so it must run as a single replica
# (several replicas would analyse the same mails twice).
replicaCountFrontend: 1
service:
type: ClusterIP
port: 80
ingress:
enabled: false
className: ""
annotations: {}
hosts:
- host: mail-tester.example.org
paths:
- path: /
pathType: Prefix
tls: []
# Domain of the generated test addresses (your Stalwart domain)
mailDomain: mail-test.example.org
jmap:
url: https://mail.example.org
user: ""
password: ""
# Or an existing Secret with keys "password" (or "token" when useToken is set)
existingSecret: ""
useToken: false
# SpamAssassin runs as a sidecar container in the backend pod (the backend image is scratch).
spamassassin:
enabled: true
resources: {}
# sqlite, mysql or postgres
db:
driver: sqlite
# sqlite: file inside the persistence volume; mysql/postgres: database name
name: mailtester
host: ""
port: "" # default 3306 (mysql) / 5432 (postgres)
user: ""
sslmode: prefer # postgres only
password: ""
# Or an existing Secret holding the password
existingSecret: ""
existingSecretKey: password
# Only used with db.driver=sqlite
persistence:
enabled: true
size: 1Gi
storageClass: ""
existingClaim: ""
trustedProxies: "" # e.g. the ingress controller CIDR, needed for correct per-IP rate limiting
logLevel: INFO
resources: {}
resourcesBackend: {}
nodeSelector: {}
tolerations: []
affinity: {}