feat(statuspage): obtain tls certificates on demand for custom domains

With UPTIME_STATUSPAGE_TLS the server serves HTTPS with ACME
certificates for its hosts and for verified custom domains of pages it
may serve, shared by all instances through the API. Plain HTTP answers
ACME challenges and health checks and redirects everything else.

Closes #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 08:54:08 +01:00
1 parent 46f1f82481
commit 4d1db0943e
8 files changed
+255 -17

No files matched your search

+65 -2
View File
@@ -4,12 +4,18 @@ package main
import (
"context"
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"time"
"github.com/caddyserver/certmagic"
"github.com/google/uuid"
"go.uber.org/zap"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
"git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo"
@@ -61,7 +67,64 @@ func run(log *slog.Logger, level *slog.LevelVar) error {
r := platform.NewRouter(log)
health.Register(r)
r.StaticFS("/static", web.Static())
statuspage.New(client, catalog, log, cfg.Hosts).Mount(r)
pages := statuspage.New(client, catalog, log, cfg.Hosts)
pages.Mount(r)
return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.Listen, r), health, 5*time.Second, cfg.ShutdownTimeout)
if !cfg.TLS {
return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.Listen, r), health, 5*time.Second, cfg.ShutdownTimeout)
}
tlsConfig, challenges, err := certificates(ctx, cfg, client, pages, log)
if err != nil {
return err
}
https := platform.NewHTTPServer(cfg.TLSListen, r)
https.TLSConfig = tlsConfig
plain := platform.NewHTTPServer(cfg.Listen, challenges(redirectToHTTPS(r)))
errs := make(chan error, 2)
go func() { errs <- platform.Serve(ctx, log, plain, health, 5*time.Second, cfg.ShutdownTimeout) }()
go func() { errs <- platform.ServeTLS(ctx, log, https, health, 5*time.Second, cfg.ShutdownTimeout) }()
err = <-errs
stop()
return errors.Join(err, <-errs)
}
// certificates sets up on-demand ACME certificates stored through the API,
// shared by every instance of this status page server.
func certificates(ctx context.Context, cfg config.StatusPage, client *agentapi.Client, pages *statuspage.Server, log *slog.Logger) (*tls.Config, func(http.Handler) http.Handler, error) {
var magic *certmagic.Config
cache := certmagic.NewCache(certmagic.CacheOptions{
GetConfigForCert: func(certmagic.Certificate) (*certmagic.Config, error) { return magic, nil },
})
magic = certmagic.New(cache, certmagic.Config{
Storage: statuspage.NewCertStorage(client),
OnDemand: &certmagic.OnDemandConfig{DecisionFunc: pages.AllowCertificate},
Logger: zap.NewNop(),
})
issuer := certmagic.NewACMEIssuer(magic, certmagic.ACMEIssuer{CA: cfg.ACMECA, Email: cfg.ACMEEmail, Agreed: true})
magic.Issuers = []certmagic.Issuer{issuer}
go func() {
<-ctx.Done()
cache.Stop()
}()
tc := magic.TLSConfig()
tc.NextProtos = append([]string{"h2", "http/1.1"}, tc.NextProtos...)
log.Info("tls enabled", "ca", cfg.ACMECA, "listen", cfg.TLSListen)
return tc, issuer.HTTPChallengeHandler, nil
}
// redirectToHTTPS sends page requests to HTTPS but keeps health checks on
// plain HTTP for load balancers and orchestrators.
func redirectToHTTPS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/healthz" || r.URL.Path == "/readyz" {
next.ServeHTTP(w, r)
return
}
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
http.Redirect(w, r, "https://"+host+r.URL.RequestURI(), http.StatusMovedPermanently)
})
}