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
156 lines
5.8 KiB
Go
156 lines
5.8 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)
|
|
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")
|
|
}
|
|
}
|