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.
116 lines
3.8 KiB
Go
116 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const changePasswordProbeCurrent = "correct-admin-password"
|
|
|
|
func newChangePasswordRouter(t *testing.T) (*Router, *config.Config) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(changePasswordProbeCurrent), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
t.Fatalf("bcrypt: %v", err)
|
|
}
|
|
cfg := &config.Config{DataPath: dir, ConfigPath: dir, AuthUser: "admin", AuthPass: string(hashed)}
|
|
router := newTestRouter(t, cfg, nil, nil, nil, nil, "1.0.0")
|
|
cleanupTestRouter(t, router)
|
|
return router, cfg
|
|
}
|
|
|
|
func changePasswordRequest(t *testing.T, current string) *http.Request {
|
|
t.Helper()
|
|
body := `{"currentPassword":"` + current + `","newPassword":"Str0ng-New-Passw0rd!x"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/security/change-password", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
return req
|
|
}
|
|
|
|
func attachChangePasswordSession(t *testing.T, req *http.Request, user string) {
|
|
t.Helper()
|
|
tok := "change-pw-" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
GetSessionStore().CreateSession(tok, time.Hour, "browser", "127.0.0.1", user)
|
|
req.AddCookie(&http.Cookie{Name: sessionCookieName(false), Value: tok})
|
|
req.Header.Set("X-CSRF-Token", generateCSRFToken(tok))
|
|
}
|
|
|
|
// The proxy branch of handleChangePassword refuses non-admin proxy users. The
|
|
// session branch did not, so any authenticated non-admin session could submit
|
|
// guesses at the local admin password and read the answer from the 401, and
|
|
// could change it outright once it knew it.
|
|
func TestChangePasswordRefusesNonAdminSession(t *testing.T) {
|
|
router, _ := newChangePasswordRouter(t)
|
|
|
|
req := changePasswordRequest(t, changePasswordProbeCurrent)
|
|
attachChangePasswordSession(t, req, "sso:outsider@example.com")
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Fatalf("non-admin session change-password = %d, want 403 (body %s)", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// The configured admin must still be able to change their own password.
|
|
func TestChangePasswordAllowsConfiguredAdminSession(t *testing.T) {
|
|
router, _ := newChangePasswordRouter(t)
|
|
|
|
req := changePasswordRequest(t, changePasswordProbeCurrent)
|
|
attachChangePasswordSession(t, req, "admin")
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("configured admin change-password = %d, want 200 (body %s)", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// Basic Auth carries no session cookie, so the session gate must not touch it.
|
|
func TestChangePasswordBasicAuthPathUnaffected(t *testing.T) {
|
|
router, _ := newChangePasswordRouter(t)
|
|
|
|
req := changePasswordRequest(t, changePasswordProbeCurrent)
|
|
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(
|
|
[]byte("admin:"+changePasswordProbeCurrent)))
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
|
|
if rec.Code == http.StatusForbidden {
|
|
t.Fatalf("Basic Auth change-password must not be refused by the session gate, got 403 (body %s)", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestChangePasswordFixtureOwnsRouterLifecycle(t *testing.T) {
|
|
var router *Router
|
|
workerDone := make(chan struct{})
|
|
t.Run("fixture", func(t *testing.T) {
|
|
router, _ = newChangePasswordRouter(t)
|
|
router.startLifecycleWorker(func() {
|
|
<-router.lifecycleCtx.Done()
|
|
close(workerDone)
|
|
})
|
|
})
|
|
// Ensure a failing regression does not itself leave workers running.
|
|
cleanupTestRouter(t, router)
|
|
select {
|
|
case <-router.lifecycleCtx.Done():
|
|
default:
|
|
t.Fatal("password-change fixture left router lifecycle active")
|
|
}
|
|
select {
|
|
case <-workerDone:
|
|
default:
|
|
t.Fatal("password-change fixture did not join its router worker")
|
|
}
|
|
}
|