Files
uptimesebi/internal/agentapi/storage.go
T
Sebastian UnterschützandClaude Opus 5.5 db36d918fe
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
feat: guard certificate orders for custom domains and explain the setup
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
2026-11-25 22:39:01 +01:00

119 lines
3.7 KiB
Go

package agentapi
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
// StorageInfo describes a stored key; IsTerminal is false for keys that only
// exist as a prefix of others.
type StorageInfo struct {
Key string `json:"key"`
Modified time.Time `json:"modified"`
Size int64 `json:"size"`
IsTerminal bool `json:"is_terminal"`
}
// ErrLocked means another instance holds a storage lock.
var ErrLocked = errors.New("agentapi: locked")
func (c *Client) raw(ctx context.Context, method, path string, body []byte) ([]byte, error) {
var rd io.Reader
if body != nil {
rd = bytes.NewReader(body)
}
req, err := http.NewRequestWithContext(ctx, method, c.base+"/agent/v1"+path, rd)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set(VersionHeader, strconv.Itoa(Version))
req.Header.Set(InstanceHeader, c.instance.String())
req.Header.Set(ReleaseHeader, c.release)
resp, err := c.hc.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
out, err := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
switch {
case resp.StatusCode == http.StatusNotFound:
return nil, ErrNotFound
case resp.StatusCode == http.StatusConflict:
return nil, ErrLocked
case resp.StatusCode == http.StatusUnauthorized:
return nil, ErrUnauthorized
case resp.StatusCode >= 300:
return nil, fmt.Errorf("agentapi: %s %s: %d", method, path, resp.StatusCode)
}
return out, err
}
func keyQuery(name, value string) string { return "?" + url.Values{name: {value}}.Encode() }
// StorageGet returns the value of key or ErrNotFound.
func (c *Client) StorageGet(ctx context.Context, key string) ([]byte, error) {
return c.raw(ctx, http.MethodGet, "/storage"+keyQuery("key", key), nil)
}
// StoragePut stores value under key.
func (c *Client) StoragePut(ctx context.Context, key string, value []byte) error {
if value == nil {
value = []byte{}
}
_, err := c.raw(ctx, http.MethodPut, "/storage"+keyQuery("key", key), value)
return err
}
// StorageDelete removes key and everything below it.
func (c *Client) StorageDelete(ctx context.Context, key string) error {
_, err := c.raw(ctx, http.MethodDelete, "/storage"+keyQuery("key", key), nil)
return err
}
// StorageStat describes key.
func (c *Client) StorageStat(ctx context.Context, key string) (StorageInfo, error) {
var info StorageInfo
err := c.do(ctx, http.MethodGet, "/storage/stat"+keyQuery("key", key), nil, &info)
return info, err
}
// StorageList lists keys below prefix.
func (c *Client) StorageList(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var out struct {
Keys []string `json:"keys"`
}
q := url.Values{"prefix": {prefix}, "recursive": {strconv.FormatBool(recursive)}}
err := c.do(ctx, http.MethodGet, "/storage/list?"+q.Encode(), nil, &out)
return out.Keys, err
}
// StorageLock tries to take the lock name once.
func (c *Client) StorageLock(ctx context.Context, name string) error {
_, err := c.raw(ctx, http.MethodPost, "/storage/lock"+keyQuery("name", name), nil)
return err
}
// StorageUnlock releases the lock name.
func (c *Client) StorageUnlock(ctx context.Context, name string) error {
_, err := c.raw(ctx, http.MethodDelete, "/storage/lock"+keyQuery("name", name), nil)
return err
}
// ErrRateLimited means the API refused because a limit was reached.
var ErrRateLimited = errors.New("agentapi: rate limited")
// AllowCertificate asks whether this server may order a new certificate
// for name. ErrNotFound means it serves no page there, ErrRateLimited that
// a limit on orders was reached.
func (c *Client) AllowCertificate(ctx context.Context, name string) error {
return c.do(ctx, http.MethodPost, "/certificates", map[string]string{"name": name}, nil)
}