X-Portal-User-Subject and X-Portal-User-Email named the acting person for any service client. Any tenants.read client (werkmeister-svc, pruefwerk-svc, firmwerk-svc) could thereby set the audit actor of a document download, and a future tenants.write client could act in a user's name on the team routes. portalUser honours the headers only from a client that holds the users group (group users.act, satisfied by the existing users grant: only the portal's service client in stage and prod), as staffActor does for X-Staff-*. A user token speaks for itself and the headers are ignored. Used by teamActor, projectActor/projectActorType, the document download audit and the trial extension. No AUTHZ_RULES change is needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
147 lines
5.3 KiB
Go
147 lines
5.3 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)
|
|
}
|
|
}
|
|
|
|
func TestPortalUser_onlyFromThePortalClient(t *testing.T) {
|
|
s := &Server{Authz: ParseAuthzRules("portal-svc:tenants,users;werkmeister-svc:tenants.read;ops-svc:tenants")}
|
|
req := func(client string, user bool) *http.Request {
|
|
r := httptest.NewRequest("GET", "/v1/tenants/x/documents/d/download", nil)
|
|
r.Header.Set(headerPortalUserSubject, "u-1")
|
|
r.Header.Set(headerPortalUserEmail, "cxo@acme.test")
|
|
if client == "" {
|
|
return r
|
|
}
|
|
p := &authn.Principal{Subject: "svc", ClientID: client}
|
|
if user {
|
|
p.TenantID = "t1"
|
|
}
|
|
return r.WithContext(authn.WithPrincipal(r.Context(), p))
|
|
}
|
|
if sub, email, ok := s.portalUser(req("portal-svc", false)); !ok || sub != "u-1" || email != "cxo@acme.test" {
|
|
t.Fatalf("portal client: %v %q %q", ok, sub, email)
|
|
}
|
|
for _, c := range []string{"werkmeister-svc", "ops-svc", "unknown-svc"} {
|
|
if _, _, ok := s.portalUser(req(c, false)); ok {
|
|
t.Errorf("%s must not name a user", c)
|
|
}
|
|
if got := s.projectActor(req(c, false)); got != "" {
|
|
t.Errorf("%s: project actor %q", c, got)
|
|
}
|
|
if got := s.projectActorType(req(c, false)); got != "service" {
|
|
t.Errorf("%s: project actor type %q", c, got)
|
|
}
|
|
if a := s.teamActor(req(c, false)); a.Type != "service" || a.ID != "svc" {
|
|
t.Errorf("%s: team actor %+v", c, a)
|
|
}
|
|
}
|
|
if _, _, ok := s.portalUser(req("portal-svc", true)); ok {
|
|
t.Fatal("a user token speaks for itself, not through the headers")
|
|
}
|
|
if _, _, ok := s.portalUser(httptest.NewRequest("GET", "/", nil)); ok {
|
|
t.Fatal("no headers")
|
|
}
|
|
if _, _, ok := s.portalUser(req("", false)); !ok {
|
|
t.Fatal("without a principal (auth disabled, dev) the headers are taken as given")
|
|
}
|
|
if a := s.teamActor(req("portal-svc", false)); a.Type != "user" || a.ID != "u-1" || a.Email != "cxo@acme.test" {
|
|
t.Fatalf("portal team actor %+v", a)
|
|
}
|
|
}
|