A new route group "admin" (the portal's service client only) serves the staff admin area: tenant search with 30-day usage and last activity, one detail call with entitlements, subscriptions, licences, members and recent audit, trial extension (moves trial entitlements with it and reopens a trial that expired into read-only), invite resend, and the audit log across tenants. Changes made from the admin area are audited under the staff member the portal names in X-Staff-Subject / X-Staff-Email; the header only counts from a client that holds the admin group. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
101 lines
3.5 KiB
Go
101 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
|
)
|
|
|
|
func TestRequireAuthz_middleware(t *testing.T) {
|
|
ok := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })
|
|
s := &Server{Log: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
|
|
|
// nil rules → passthrough
|
|
rr := httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, httptest.NewRequest("POST", "/v1/tenants", nil))
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("nil rules: %d", rr.Code)
|
|
}
|
|
|
|
s.Authz = ParseAuthzRules("pruefwerk-svc:entitlements,tenants.read")
|
|
// no principal (auth disabled) → passthrough
|
|
rr = httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, httptest.NewRequest("POST", "/v1/tenants", nil))
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("no principal: %d", rr.Code)
|
|
}
|
|
// allowed group
|
|
req := httptest.NewRequest("GET", "/v1/tenants", nil)
|
|
req = req.WithContext(authn.WithPrincipal(req.Context(), &authn.Principal{Subject: "svc", ClientID: "pruefwerk-svc"}))
|
|
rr = httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("allowed: %d", rr.Code)
|
|
}
|
|
// forbidden group
|
|
req = httptest.NewRequest("POST", "/v1/tenants", nil)
|
|
req = req.WithContext(authn.WithPrincipal(req.Context(), &authn.Principal{Subject: "svc", ClientID: "pruefwerk-svc"}))
|
|
rr = httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusForbidden || !strings.Contains(rr.Body.String(), "FORBIDDEN_CLIENT") {
|
|
t.Fatalf("forbidden: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLogAuthz(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
log := slog.New(slog.NewTextHandler(&buf, nil))
|
|
LogAuthz(log, nil)
|
|
if !strings.Contains(buf.String(), "AUTHZ_RULES not set") {
|
|
t.Fatalf("nil rules must warn: %s", buf.String())
|
|
}
|
|
buf.Reset()
|
|
LogAuthz(log, ParseAuthzRules("portal-svc:tenants;root:*"))
|
|
if !strings.Contains(buf.String(), "portal-svc") || !strings.Contains(buf.String(), "root") {
|
|
t.Fatalf("rules must be logged: %s", buf.String())
|
|
}
|
|
}
|
|
|
|
func TestStaffActor_onlyFromAdminClients(t *testing.T) {
|
|
s := &Server{Authz: ParseAuthzRules("portal-svc:admin,tenants;pruefwerk-svc:tenants.read")}
|
|
req := func(client string, user bool) *http.Request {
|
|
r := httptest.NewRequest("POST", "/v1/admin/tenants/x/trial", nil)
|
|
r.Header.Set("X-Staff-Subject", "staff-1")
|
|
r.Header.Set("X-Staff-Email", "ops@breakpilot.com")
|
|
p := &authn.Principal{Subject: "svc", ClientID: client}
|
|
if user {
|
|
p.TenantID = "t1" // a user token carries tenant context
|
|
}
|
|
return r.WithContext(authn.WithPrincipal(r.Context(), p))
|
|
}
|
|
if sub, email, ok := s.staffActor(req("portal-svc", false)); !ok || sub != "staff-1" || email != "ops@breakpilot.com" {
|
|
t.Fatalf("admin client: %v %q %q", ok, sub, email)
|
|
}
|
|
if _, _, ok := s.staffActor(req("portal-svc", true)); ok {
|
|
t.Fatal("a user token must not claim a staff actor")
|
|
}
|
|
if _, _, ok := s.staffActor(req("pruefwerk-svc", false)); ok {
|
|
t.Fatal("a non-admin client must not claim a staff actor")
|
|
}
|
|
if _, _, ok := s.staffActor(httptest.NewRequest("GET", "/", nil)); ok {
|
|
t.Fatal("no header, no principal")
|
|
}
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.Header.Set("X-Staff-Subject", "staff-1")
|
|
if _, _, ok := s.staffActor(r); ok {
|
|
t.Fatal("header without a principal")
|
|
}
|
|
if _, _, ok := s.staffActor(nil); ok {
|
|
t.Fatal("nil request")
|
|
}
|
|
if g := routeGroup("GET", "/v1/admin/tenants"); g != "admin" {
|
|
t.Fatalf("route group %q", g)
|
|
}
|
|
}
|