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 }