feat: guard certificate orders for custom domains and explain the setup
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 8m21s
ci / lint (pull_request) Successful in 11m0s
ci / build (pull_request) Canceled after 8m49s
ci / image (pull_request) Canceled after 0s

Status page servers order certificates on demand. Verified domains were
already the only ones they would order for, but nothing bounded how many:
an organization switching domains, or many organizations at once, could use
up the CA's rate limits that all servers sharing an ACME account depend on,
and every handshake or request for a made-up name was passed on to the API.

- Before a new order (not a renewal) the server asks the API
  (POST /agent/v1/certificates). It allows verified domains of pages that
  server serves, within limits per domain and day, per organization and
  day, and per ACME account owner and hour (UPTIME_CERTS_*), counted
  across all instances.
- An organization can set a new custom domain at most 10 times a day.
- The server remembers names without a page for a minute and passes at
  most 20 unknown names a second on to the API; pages the sync cached are
  not affected. Only real pages are recorded as visited.
- The page editor explains the three steps: the TXT record, the CNAME
  record with the platform's status page host as its target (or the
  organization's own servers), apex domains, and when HTTPS follows.

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-25 22:39:01 +01:00
1 parent 7509b8c429
commit db36d918fe
18 files changed
+787 -161

No files matched your search

+55 -3
View File
@@ -45,6 +45,15 @@ const (
// staleNoticeAfter is when visitors are told the state may be outdated;
// a page nobody visited is up to syncInterval old.
staleNoticeAfter = 2 * syncInterval
// missFor is how long a host or slug without a page is answered
// without asking the API again.
missFor = time.Minute
maxMissing = 10_000
// unknownPerSecond bounds the lookups of hosts and slugs that are
// neither cached nor known to be missing, so a flood of made-up names
// is not passed on to the API. Every page the API lists is cached by
// the sync, so known pages are not affected.
unknownPerSecond = 20
)
// Source fetches snapshots; *agentapi.Client implements it.
@@ -70,8 +79,14 @@ type Server struct {
mu sync.Mutex
cache map[string]entry
refreshing map[string]bool
// visited holds when each page was last visited.
// visited holds when each cached page was last visited.
visited map[string]time.Time
// missing holds when a key was last found to have no page.
missing map[string]time.Time
// unknownSecond and unknownCount count lookups passed on to the API in
// the current second.
unknownSecond int64
unknownCount int
// background tracks refreshes started by visits.
background sync.WaitGroup
}
@@ -104,6 +119,7 @@ func New(src Source, catalog *i18n.Catalog, log *slog.Logger, o Options) (*Serve
s := &Server{
src: src, catalog: catalog, log: log, ownHosts: map[string]bool{}, dir: o.CacheDir,
now: time.Now, cache: map[string]entry{}, refreshing: map[string]bool{}, visited: map[string]time.Time{},
missing: map[string]time.Time{},
}
for _, h := range o.Hosts {
s.ownHosts[strings.ToLower(h)] = true
@@ -167,6 +183,7 @@ func (s *Server) persist(e entry) {
func (s *Server) forget(key string) {
s.mu.Lock()
delete(s.cache, key)
delete(s.visited, key)
s.mu.Unlock()
if s.dir != "" {
_ = os.Remove(s.fileFor(key))
@@ -321,9 +338,15 @@ func (s *Server) localizeRequest(c *gin.Context) {
// lookup answers from the cache whenever possible and refreshes in the
// background; only unknown pages wait for the API.
func (s *Server) lookup(ctx context.Context, key, host, slug string) (entry, bool) {
now := s.now()
s.mu.Lock()
e, cached := s.cache[key]
s.visited[key] = s.now()
if cached {
s.visited[key] = now
} else if !s.mayAsk(key, now) {
s.mu.Unlock()
return entry{}, false
}
s.mu.Unlock()
if cached {
if s.now().Sub(e.Fetched) >= freshFor {
@@ -331,7 +354,29 @@ func (s *Server) lookup(ctx context.Context, key, host, slug string) (entry, boo
}
return e, true
}
return s.fetch(ctx, key, host, slug)
e, ok := s.fetch(ctx, key, host, slug)
if ok {
s.mu.Lock()
s.visited[key] = now
s.mu.Unlock()
}
return e, ok
}
// mayAsk reports whether an uncached key may be looked up at the API: not
// when it was just found missing, nor beyond unknownPerSecond. s.mu is held.
func (s *Server) mayAsk(key string, now time.Time) bool {
if t, ok := s.missing[key]; ok && now.Sub(t) < missFor {
return false
}
if sec := now.Unix(); sec != s.unknownSecond {
s.unknownSecond, s.unknownCount = sec, 0
}
if s.unknownCount >= unknownPerSecond {
return false
}
s.unknownCount++
return true
}
func (s *Server) fetch(ctx context.Context, key, host, slug string) (entry, bool) {
@@ -351,6 +396,12 @@ func (s *Server) fetch(ctx context.Context, key, host, slug string) (entry, bool
return old, true
case errors.Is(err, agentapi.ErrNotFound):
s.forget(key)
s.mu.Lock()
if len(s.missing) >= maxMissing {
clear(s.missing)
}
s.missing[key] = s.now()
s.mu.Unlock()
return entry{}, false
case err != nil:
s.log.WarnContext(ctx, "fetching status page failed", "page", key, "error", err)
@@ -362,6 +413,7 @@ func (s *Server) fetch(ctx context.Context, key, host, slug string) (entry, bool
e := entry{Key: key, Host: host, Slug: slug, Snap: snap, Fetched: s.now(), Checksum: snap.Checksum()}
s.mu.Lock()
s.cache[key] = e
delete(s.missing, key)
s.mu.Unlock()
s.persist(e)
return e, true