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
162 lines
6.0 KiB
Go
162 lines
6.0 KiB
Go
package api_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io/fs"
|
|
"log/slog"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/api"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/statuspage"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
func TestCertStorage(t *testing.T) {
|
|
e := newEnv(t)
|
|
ctx := context.Background()
|
|
platform := statuspage.NewCertStorage(e.statuspageClient(t, nil), "")
|
|
base := "certificates/" + uuid.NewString()[:8]
|
|
|
|
key := base + "/example.com/example.com.key"
|
|
if err := platform.Store(ctx, key, []byte("private key")); err != nil {
|
|
t.Fatalf("Store() failed: %v", err)
|
|
}
|
|
if err := platform.Store(ctx, base+"/example.com/example.com.crt", []byte("cert")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := platform.Load(ctx, key); err != nil || !bytes.Equal(got, []byte("private key")) {
|
|
t.Errorf("Load() = %q, %v", got, err)
|
|
}
|
|
if _, err := platform.Load(ctx, base+"/missing"); !errors.Is(err, fs.ErrNotExist) {
|
|
t.Errorf("Load(missing) error = %v, want fs.ErrNotExist", err)
|
|
}
|
|
if !platform.Exists(ctx, key) || platform.Exists(ctx, base+"/missing") {
|
|
t.Error("Exists() wrong")
|
|
}
|
|
if info, err := platform.Stat(ctx, base+"/example.com"); err != nil || info.IsTerminal {
|
|
t.Errorf("Stat(directory) = %+v, %v; want non-terminal", info, err)
|
|
}
|
|
if info, err := platform.Stat(ctx, key); err != nil || !info.IsTerminal || info.Size == 0 {
|
|
t.Errorf("Stat(key) = %+v, %v", info, err)
|
|
}
|
|
if got, err := platform.List(ctx, base, false); err != nil || !slices.Equal(got, []string{base + "/example.com"}) {
|
|
t.Errorf("List(non-recursive) = %v, %v", got, err)
|
|
}
|
|
if got, err := platform.List(ctx, base, true); err != nil || len(got) != 2 {
|
|
t.Errorf("List(recursive) = %v, %v", got, err)
|
|
}
|
|
|
|
var stored []byte
|
|
_ = e.dbPool.QueryRow(ctx, `SELECT value FROM agent_storage WHERE key = $1`, key).Scan(&stored)
|
|
if bytes.Contains(stored, []byte("private key")) {
|
|
t.Error("value stored in plain text")
|
|
}
|
|
|
|
other := e.browser(t)
|
|
other.signup("x@"+uniq("cs")+".test", uniq("cs-"))
|
|
org := uuid.MustParse(other.orgID())
|
|
orgStore := statuspage.NewCertStorage(e.statuspageClient(t, &org), "")
|
|
if _, err := orgStore.Load(ctx, key); !errors.Is(err, fs.ErrNotExist) {
|
|
t.Errorf("org server loaded a platform key: %v", err)
|
|
}
|
|
|
|
if err := platform.Lock(ctx, "issue-example.com"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
locked, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
|
|
defer cancel()
|
|
if err := platform.Lock(locked, "issue-example.com"); !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Errorf("second Lock() = %v, want to wait until the deadline", err)
|
|
}
|
|
if err := orgStore.Lock(ctx, "issue-example.com"); err != nil {
|
|
t.Errorf("org Lock() on its own namespace failed: %v", err)
|
|
}
|
|
_ = platform.Unlock(ctx, "issue-example.com")
|
|
if err := platform.Lock(ctx, "issue-example.com"); err != nil {
|
|
t.Errorf("Lock() after Unlock() failed: %v", err)
|
|
}
|
|
|
|
if err := platform.Delete(ctx, base+"/example.com"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := platform.List(ctx, base, true); !errors.Is(err, fs.ErrNotExist) {
|
|
t.Errorf("List() after deleting the directory = %v, want fs.ErrNotExist", err)
|
|
}
|
|
}
|
|
|
|
func TestAllowCertificate(t *testing.T) {
|
|
e := newEnv(t)
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("ac")+".test", uniq("ac-"))
|
|
_, body := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": uniq("ac-"), "title": "T", "published": true})
|
|
pageID := body["id"].(string)
|
|
domain := uniq("s") + ".example.test"
|
|
_, body = owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/domain", map[string]any{"domain": domain})
|
|
record := body["domain_record"].(map[string]any)
|
|
|
|
cat, _ := i18n.Load()
|
|
srv, err := statuspage.New(e.statuspageClient(t, nil), cat, slog.New(slog.DiscardHandler), statuspage.Options{Hosts: []string{"status.test"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
if err := srv.AllowCertificate(ctx, "status.test"); err != nil {
|
|
t.Errorf("AllowCertificate(default host) = %v", err)
|
|
}
|
|
if err := srv.AllowCertificate(ctx, domain); err == nil {
|
|
t.Error("AllowCertificate(unverified domain) = nil, want refusal")
|
|
}
|
|
if err := srv.AllowCertificate(ctx, "random.example.test"); err == nil {
|
|
t.Error("AllowCertificate(unknown name) = nil, want refusal")
|
|
}
|
|
verifyDomain(t, owner, pageID, record)
|
|
// A name just found without a page is not asked about again for a
|
|
// while; the sync picks the newly verified domain up.
|
|
if err := srv.AllowCertificate(ctx, domain); err == nil {
|
|
t.Error("AllowCertificate(domain just found missing) = nil, want the miss remembered")
|
|
}
|
|
srv.Sync(ctx)
|
|
if err := srv.AllowCertificate(ctx, domain); err != nil {
|
|
t.Errorf("AllowCertificate(verified domain) = %v", err)
|
|
}
|
|
}
|
|
|
|
// TestCertStorageMirror loads certificates from the local mirror when the
|
|
// API is unreachable, as after a restart during an outage.
|
|
func TestCertStorageMirror(t *testing.T) {
|
|
e := newEnv(t)
|
|
ctx := context.Background()
|
|
dir := t.TempDir()
|
|
tok, params := api.NewAgentToken(nil, "statuspage", "mirror", nil)
|
|
if err := tenant.New(e.dbPool).Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
_, err := q.CreateAgent(ctx, params)
|
|
return err
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
online := statuspage.NewCertStorage(agentapi.NewClient(e.srv.URL, tok, uuid.New(), "t").WithHTTPClient(e.srv.Client()), dir)
|
|
key := "certificates/acme/example.com/example.com.crt"
|
|
if err := online.Store(ctx, key, []byte("cert")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
offline := statuspage.NewCertStorage(agentapi.NewClient("https://127.0.0.1:1", tok, uuid.New(), "t"), dir)
|
|
if got, err := offline.Load(ctx, key); err != nil || string(got) != "cert" {
|
|
t.Errorf("Load() with API down = %q, %v; want mirrored value", got, err)
|
|
}
|
|
if !offline.Exists(ctx, key) {
|
|
t.Error("Exists() with API down = false, want true from mirror")
|
|
}
|
|
if _, err := offline.Load(ctx, "../../etc/passwd"); err == nil {
|
|
t.Error("Load() escaped the mirror directory")
|
|
}
|
|
}
|