feat(api): give status page servers sealed key-value storage
Certificates and ACME keys are stored encrypted in the database, namespaced per owner so an organization's servers cannot read platform or other organizations' keys. Locks live in Redis and expire. Refs #22 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
f52ae33cf0
commit
46f1f82481
11 files changed
+611
-1
No files matched your search
@@ -0,0 +1,118 @@
|
||||
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/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/statuspage"
|
||||
)
|
||||
|
||||
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 := statuspage.New(e.statuspageClient(t, nil), cat, slog.New(slog.DiscardHandler), []string{"status.test"})
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user