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:
1 parent
46f1f82481
commit
4d1db0943e
8 files changed
+255
-17
No files matched your search
@@ -143,16 +143,7 @@ func TestStatusPages(t *testing.T) {
|
||||
if _, err := platform.Page(context.Background(), domain, ""); !errors.Is(err, agentapi.ErrNotFound) {
|
||||
t.Errorf("Page(unverified domain) error = %v, want not found", err)
|
||||
}
|
||||
saved := *api.LookupTXT
|
||||
t.Cleanup(func() { *api.LookupTXT = saved })
|
||||
*api.LookupTXT = func(_ context.Context, name string) ([]string, error) {
|
||||
if name == record["name"] {
|
||||
return []string{"something else", record["value"].(string)}, nil
|
||||
}
|
||||
return nil, errors.New("no such record")
|
||||
}
|
||||
st, body = owner.do("POST", "/api/v1/org/statuspages/"+pageID+"/domain/verify", nil)
|
||||
expect(t, "verify domain", st, body, http.StatusNoContent, "")
|
||||
verifyDomain(t, owner, pageID, record)
|
||||
if snap, err := platform.Page(context.Background(), domain, ""); err != nil || snap.Title != "Acme Status" {
|
||||
t.Errorf("Page(verified domain) = %v, %v", snap.Title, err)
|
||||
}
|
||||
@@ -203,3 +194,18 @@ func editorEmail(b *browser) string {
|
||||
_, me := b.do("GET", "/api/v1/me", nil)
|
||||
return me["email"].(string)
|
||||
}
|
||||
|
||||
// verifyDomain answers the TXT lookup with record and verifies the domain.
|
||||
func verifyDomain(t *testing.T, owner *browser, pageID string, record map[string]any) {
|
||||
t.Helper()
|
||||
saved := *api.LookupTXT
|
||||
t.Cleanup(func() { *api.LookupTXT = saved })
|
||||
*api.LookupTXT = func(_ context.Context, name string) ([]string, error) {
|
||||
if name == record["name"] {
|
||||
return []string{"something else", record["value"].(string)}, nil
|
||||
}
|
||||
return nil, errors.New("no such record")
|
||||
}
|
||||
st, body := owner.do("POST", "/api/v1/org/statuspages/"+pageID+"/domain/verify", nil)
|
||||
expect(t, "verify domain", st, body, http.StatusNoContent, "")
|
||||
}
|
||||
@@ -147,6 +147,14 @@ type StatusPage struct {
|
||||
// Hosts are the host names that serve pages by slug, e.g.
|
||||
// status.example.com. Any other host is treated as a custom domain.
|
||||
Hosts []string `env:"UPTIME_STATUSPAGE_HOSTS" envSeparator:"," envDefault:"localhost"`
|
||||
// TLS obtains certificates via ACME for the hosts and verified custom
|
||||
// domains and serves HTTPS on TLSListen; Listen then answers ACME HTTP
|
||||
// challenges and redirects to HTTPS.
|
||||
TLS bool `env:"UPTIME_STATUSPAGE_TLS"`
|
||||
TLSListen string `env:"UPTIME_STATUSPAGE_TLS_LISTEN" envDefault:":443"`
|
||||
ACMEEmail string `env:"UPTIME_ACME_EMAIL"`
|
||||
// ACMECA defaults to Let's Encrypt; use its staging URL for testing.
|
||||
ACMECA string `env:"UPTIME_ACME_CA" envDefault:"https://acme-v02.api.letsencrypt.org/directory"`
|
||||
}
|
||||
|
||||
// Load parses the environment into a T, applying envDefault tags.
|
||||
|
||||
@@ -11,10 +11,19 @@ import (
|
||||
// Serve blocks until ctx is cancelled, then marks the instance as draining,
|
||||
// waits drainDelay for load balancers to notice and shuts down gracefully.
|
||||
func Serve(ctx context.Context, log *slog.Logger, srv *http.Server, health *Health, drainDelay, timeout time.Duration) error {
|
||||
return serve(ctx, log, srv, health, drainDelay, timeout, srv.ListenAndServe)
|
||||
}
|
||||
|
||||
// ServeTLS is Serve for a server whose TLSConfig provides certificates.
|
||||
func ServeTLS(ctx context.Context, log *slog.Logger, srv *http.Server, health *Health, drainDelay, timeout time.Duration) error {
|
||||
return serve(ctx, log, srv, health, drainDelay, timeout, func() error { return srv.ListenAndServeTLS("", "") })
|
||||
}
|
||||
|
||||
func serve(ctx context.Context, log *slog.Logger, srv *http.Server, health *Health, drainDelay, timeout time.Duration, listen func() error) error {
|
||||
errc := make(chan error, 1)
|
||||
go func() {
|
||||
log.Info("http listening", "addr", srv.Addr)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Info("http listening", "addr", srv.Addr, "tls", srv.TLSConfig != nil)
|
||||
if err := listen(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
errc <- err
|
||||
}
|
||||
close(errc)
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package statuspage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
)
|
||||
|
||||
// CertStorage keeps certmagic's certificates and locks in the API, so every
|
||||
// instance of a status page server shares them without a database.
|
||||
type CertStorage struct {
|
||||
c *agentapi.Client
|
||||
}
|
||||
|
||||
var _ certmagic.Storage = CertStorage{}
|
||||
|
||||
// NewCertStorage returns storage backed by c.
|
||||
func NewCertStorage(c *agentapi.Client) CertStorage { return CertStorage{c: c} }
|
||||
|
||||
func notExist(err error) error {
|
||||
if errors.Is(err, agentapi.ErrNotFound) {
|
||||
return fs.ErrNotExist
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Store implements certmagic.Storage.
|
||||
func (s CertStorage) Store(ctx context.Context, key string, value []byte) error {
|
||||
return s.c.StoragePut(ctx, key, value)
|
||||
}
|
||||
|
||||
// Load implements certmagic.Storage.
|
||||
func (s CertStorage) Load(ctx context.Context, key string) ([]byte, error) {
|
||||
v, err := s.c.StorageGet(ctx, key)
|
||||
return v, notExist(err)
|
||||
}
|
||||
|
||||
// Delete implements certmagic.Storage.
|
||||
func (s CertStorage) Delete(ctx context.Context, key string) error {
|
||||
return notExist(s.c.StorageDelete(ctx, key))
|
||||
}
|
||||
|
||||
// Exists implements certmagic.Storage.
|
||||
func (s CertStorage) Exists(ctx context.Context, key string) bool {
|
||||
_, err := s.c.StorageStat(ctx, key)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// List implements certmagic.Storage.
|
||||
func (s CertStorage) List(ctx context.Context, path string, recursive bool) ([]string, error) {
|
||||
keys, err := s.c.StorageList(ctx, path, recursive)
|
||||
if err == nil && len(keys) == 0 {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
return keys, err
|
||||
}
|
||||
|
||||
// Stat implements certmagic.Storage.
|
||||
func (s CertStorage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
|
||||
info, err := s.c.StorageStat(ctx, key)
|
||||
if err != nil {
|
||||
return certmagic.KeyInfo{}, notExist(err)
|
||||
}
|
||||
return certmagic.KeyInfo{Key: info.Key, Modified: info.Modified, Size: info.Size, IsTerminal: info.IsTerminal}, nil
|
||||
}
|
||||
|
||||
// Lock implements certmagic.Locker. It blocks until the lock is free or ctx
|
||||
// ends; locks expire on the API side if their holder dies.
|
||||
func (s CertStorage) Lock(ctx context.Context, name string) error {
|
||||
wait := 200 * time.Millisecond
|
||||
for {
|
||||
err := s.c.StorageLock(ctx, name)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !errors.Is(err, agentapi.ErrLocked) {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(wait):
|
||||
}
|
||||
wait = min(wait*2, 5*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// Unlock implements certmagic.Locker.
|
||||
func (s CertStorage) Unlock(ctx context.Context, name string) error {
|
||||
return s.c.StorageUnlock(ctx, name)
|
||||
}
|
||||
|
||||
// AllowCertificate decides whether to obtain a certificate for name on
|
||||
// demand: only for the default hosts and for verified custom domains of
|
||||
// pages this server may serve. Anything else would let strangers make us
|
||||
// request certificates for arbitrary names.
|
||||
func (s *Server) AllowCertificate(ctx context.Context, name string) error {
|
||||
if s.ownHosts[hostOnly(name)] {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.src.Page(ctx, hostOnly(name), ""); err != nil {
|
||||
return fmt.Errorf("no status page for %s: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user