mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
No Router built by the internal/api tests was ever freed. At the end of a run
all 1,358 were still reachable, with 26,000 goroutines and 2.86 GiB live, so
peak memory grew with every new test until the release correctness job ran out
of memory on its 7 GiB runner.
Most test sites never stopped their Router's workers. The few that did were
pinned anyway, because the setup-token cleanup worker in ConfigHandlers had no
way to stop and holds callbacks back into the Router. That worker now joins
the Router's shutdown, and package tests build Routers through newTestRouter,
which stops them with the test. Two new tests fail if a Router stays reachable
after shutdown or a package test calls NewRouter directly.
Default command policies now share one compiled pattern set. A private copy
cost 230 KiB and 1.4 ms per call and is now 2.2 KiB and 1.6 us. It accounted
for a quarter of the live test heap. In production each organization's AI
service holds one, so this also saves about 230 KiB per organization.
Measured on pulse-dev at 88c2a21f65 with the same go test -count=1 -p 1 run:
peak RSS of the largest child 6.41 GiB before, 2.44 GiB after. That largest
child is now the compiler: a cold test build alone peaks at 2.34 GiB and the
test binary alone at 1.48 GiB. All package tests pass. CPU time fell about 5%;
wall clock followed the host's load and is not comparable between these runs.
Contract-Neutral: Stops test Router workers and shares compiled policy patterns; no API, policy decision or storage change.
132 lines
4.7 KiB
Go
132 lines
4.7 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/auth"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func adminParitySession(t *testing.T, user string) *http.Cookie {
|
|
t.Helper()
|
|
tok := "admin-parity-" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
GetSessionStore().CreateSession(tok, time.Hour, "browser", "127.0.0.1", user)
|
|
return &http.Cookie{Name: sessionCookieName(false), Value: tok}
|
|
}
|
|
|
|
func adminParityConfig(t *testing.T, adminUser string) *config.Config {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
// An API token is present so the export/import path treats auth as required
|
|
// on the OIDC-only instance too, which is the shape that actually 403s.
|
|
record, err := config.NewAPITokenRecord("admin-parity-token-123.12345678", "parity", []string{config.ScopeSettingsWrite})
|
|
if err != nil {
|
|
t.Fatalf("NewAPITokenRecord: %v", err)
|
|
}
|
|
cfg := &config.Config{DataPath: dir, ConfigPath: dir, APITokens: []config.APITokenRecord{*record}}
|
|
if adminUser != "" {
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte("admin-parity-password"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
t.Fatalf("bcrypt: %v", err)
|
|
}
|
|
cfg.AuthUser = adminUser
|
|
cfg.AuthPass = string(hashed)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func exportStatusFor(t *testing.T, router *Router, user string) int {
|
|
t.Helper()
|
|
cookie := adminParitySession(t, user)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/config/export",
|
|
strings.NewReader(`{"passphrase":"long-enough-passphrase"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.AddCookie(cookie)
|
|
req.Header.Set("X-CSRF-Token", generateCSRFToken(cookie.Value))
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
return rec.Code
|
|
}
|
|
|
|
// SSO-only deployments retain a usable administration path through an
|
|
// explicit RBAC administrator grant. Authentication without that grant is not
|
|
// instance administration.
|
|
func TestSSOOnlyExplicitAdminReachesEveryAdminGuard(t *testing.T) {
|
|
prev := auth.GetAuthorizer()
|
|
auth.SetAuthorizer(&auth.DefaultAuthorizer{})
|
|
defer auth.SetAuthorizer(prev)
|
|
|
|
cfg := adminParityConfig(t, "")
|
|
router := newTestRouter(t, cfg, nil, nil, nil, nil, "1.0.0")
|
|
manager := installTestRBACManager(t)
|
|
ssoAdmin := "sso:owner@example.com"
|
|
if err := manager.UpdateUserRoles(ssoAdmin, []string{auth.RoleAdmin}); err != nil {
|
|
t.Fatalf("assign SSO administrator role: %v", err)
|
|
}
|
|
|
|
if !sessionUserCarriesAdminPrivileges(cfg, ssoAdmin) {
|
|
t.Fatal("precondition: the canonical helper must treat this principal as an admin")
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
|
req.AddCookie(adminParitySession(t, ssoAdmin))
|
|
if !canCapturePublicURL(cfg, req) {
|
|
t.Error("canCapturePublicURL refused the explicit SSO administrator")
|
|
}
|
|
|
|
if code := exportStatusFor(t, router, ssoAdmin); code == http.StatusForbidden {
|
|
t.Error("config export refused the explicitly authorized SSO administrator")
|
|
}
|
|
|
|
ssoViewer := "sso:viewer@example.com"
|
|
if sessionUserCarriesAdminPrivileges(cfg, ssoViewer) {
|
|
t.Fatal("unassigned SSO user inherited instance administration")
|
|
}
|
|
req3 := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
|
req3.AddCookie(adminParitySession(t, ssoViewer))
|
|
if canCapturePublicURL(cfg, req3) {
|
|
t.Error("public URL capture admitted an unassigned SSO user")
|
|
}
|
|
if code := exportStatusFor(t, router, ssoViewer); code != http.StatusForbidden {
|
|
t.Errorf("config export for an unassigned SSO user = %d, want 403", code)
|
|
}
|
|
}
|
|
|
|
// The parity fix must not widen anything. On an instance that does configure a
|
|
// local admin, an unrelated SSO principal is not an administrator and every one
|
|
// of these guards must still refuse them.
|
|
func TestUnrelatedSSOUserStillRefusedWhenLocalAdminConfigured(t *testing.T) {
|
|
prev := auth.GetAuthorizer()
|
|
auth.SetAuthorizer(&auth.DefaultAuthorizer{})
|
|
defer auth.SetAuthorizer(prev)
|
|
|
|
cfg := adminParityConfig(t, "admin")
|
|
router := newTestRouter(t, cfg, nil, nil, nil, nil, "1.0.0")
|
|
outsider := "sso:outsider@example.com"
|
|
|
|
if sessionUserCarriesAdminPrivileges(cfg, outsider) {
|
|
t.Fatal("precondition: an unrelated SSO user must not be an admin when a local admin is configured")
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
|
req.AddCookie(adminParitySession(t, outsider))
|
|
if canCapturePublicURL(cfg, req) {
|
|
t.Error("canCapturePublicURL admitted a non-admin SSO user")
|
|
}
|
|
|
|
if code := exportStatusFor(t, router, outsider); code != http.StatusForbidden {
|
|
t.Errorf("config export for a non-admin SSO user = %d, want 403", code)
|
|
}
|
|
|
|
// The configured admin is unaffected.
|
|
if code := exportStatusFor(t, router, "admin"); code == http.StatusForbidden {
|
|
t.Error("config export refused the configured local admin")
|
|
}
|
|
}
|