feat(statuspage): keep serving status pages while the api is down
Snapshots are persisted to UPTIME_STATUSPAGE_CACHE_DIR and loaded at start, so pages survive restarts during an outage. Cached pages are answered without waiting for the API, refreshed in the background and served for as long as the API stays unreachable, with a notice once they are older than five minutes. Only an explicit not found from the API removes a page. Readiness no longer depends on the API when pages are cached, the server starts without a reachable API, and certificates are mirrored locally so HTTPS keeps working after a restart. Closes #56 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
20664dd5dd
commit
6e2f8d0626
12 files changed
+660
-214
No files matched your search
@@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
@@ -13,15 +15,43 @@ import (
|
||||
)
|
||||
|
||||
// CertStorage keeps certmagic's certificates and locks in the API, so every
|
||||
// instance of a status page server shares them without a database.
|
||||
// instance of a status page server shares them without a database. Values
|
||||
// are mirrored to a local directory and read from there when the API is
|
||||
// unreachable, so HTTPS keeps working after a restart during an outage.
|
||||
type CertStorage struct {
|
||||
c *agentapi.Client
|
||||
c *agentapi.Client
|
||||
dir string
|
||||
}
|
||||
|
||||
var _ certmagic.Storage = CertStorage{}
|
||||
|
||||
// NewCertStorage returns storage backed by c.
|
||||
func NewCertStorage(c *agentapi.Client) CertStorage { return CertStorage{c: c} }
|
||||
// NewCertStorage returns storage backed by c, mirrored to dir unless dir is
|
||||
// empty.
|
||||
func NewCertStorage(c *agentapi.Client, dir string) CertStorage { return CertStorage{c: c, dir: dir} }
|
||||
|
||||
// local maps a storage key to a file below dir. Keys come from certmagic,
|
||||
// but are cleaned anyway so they cannot point outside dir.
|
||||
func (s CertStorage) local(key string) string {
|
||||
if s.dir == "" {
|
||||
return ""
|
||||
}
|
||||
clean := filepath.Clean("/" + key)
|
||||
return filepath.Join(s.dir, filepath.FromSlash(clean))
|
||||
}
|
||||
|
||||
func (s CertStorage) mirror(key string, value []byte) {
|
||||
if p := s.local(key); p != "" {
|
||||
if os.MkdirAll(filepath.Dir(p), 0o700) == nil {
|
||||
_ = os.WriteFile(p, value, 0o600)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// unreachable reports errors that mean the API could not answer, as
|
||||
// opposed to answers such as "not found".
|
||||
func unreachable(err error) bool {
|
||||
return err != nil && !errors.Is(err, agentapi.ErrNotFound) && !errors.Is(err, agentapi.ErrLocked)
|
||||
}
|
||||
|
||||
func notExist(err error) error {
|
||||
if errors.Is(err, agentapi.ErrNotFound) {
|
||||
@@ -32,23 +62,43 @@ func notExist(err error) error {
|
||||
|
||||
// Store implements certmagic.Storage.
|
||||
func (s CertStorage) Store(ctx context.Context, key string, value []byte) error {
|
||||
return s.c.StoragePut(ctx, key, value)
|
||||
if err := s.c.StoragePut(ctx, key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
s.mirror(key, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
switch {
|
||||
case err == nil:
|
||||
s.mirror(key, v)
|
||||
return v, nil
|
||||
case unreachable(err) && s.local(key) != "":
|
||||
if local, lerr := os.ReadFile(s.local(key)); lerr == nil {
|
||||
return local, nil
|
||||
}
|
||||
}
|
||||
return nil, notExist(err)
|
||||
}
|
||||
|
||||
// Delete implements certmagic.Storage.
|
||||
func (s CertStorage) Delete(ctx context.Context, key string) error {
|
||||
if p := s.local(key); p != "" {
|
||||
_ = os.RemoveAll(p)
|
||||
}
|
||||
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)
|
||||
if unreachable(err) && s.local(key) != "" {
|
||||
_, lerr := os.Stat(s.local(key))
|
||||
return lerr == nil
|
||||
}
|
||||
return err == nil
|
||||
}
|
||||
|
||||
@@ -64,6 +114,11 @@ func (s CertStorage) List(ctx context.Context, path string, recursive bool) ([]s
|
||||
// Stat implements certmagic.Storage.
|
||||
func (s CertStorage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
|
||||
info, err := s.c.StorageStat(ctx, key)
|
||||
if unreachable(err) && s.local(key) != "" {
|
||||
if fi, lerr := os.Stat(s.local(key)); lerr == nil {
|
||||
return certmagic.KeyInfo{Key: key, Modified: fi.ModTime(), Size: fi.Size(), IsTerminal: !fi.IsDir()}, nil
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return certmagic.KeyInfo{}, notExist(err)
|
||||
}
|
||||
@@ -100,12 +155,16 @@ func (s CertStorage) Unlock(ctx context.Context, name string) error {
|
||||
// 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.
|
||||
//
|
||||
// Names with a cached page are allowed without asking the API, so existing
|
||||
// certificates can be loaded after a restart while the API is down.
|
||||
func (s *Server) AllowCertificate(ctx context.Context, name string) error {
|
||||
if s.ownHosts[hostOnly(name)] {
|
||||
host := hostOnly(name)
|
||||
if s.ownHosts[host] {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.src.Page(ctx, hostOnly(name), ""); err != nil {
|
||||
return fmt.Errorf("no status page for %s: %w", name, err)
|
||||
if _, ok := s.lookup(ctx, "host:"+host, host, ""); !ok {
|
||||
return fmt.Errorf("no status page for %s", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user