feat(auth/rbac): scope_type+scope_id+expires_at on role grants (HIGH-10)

Audit 2026-05-10 — close HIGH-10 from the HANDOFF.md backend batch
(item 1). Per-actor scoped + time-bound role grants are now
expressible via the API.

Migration 000043: adds scope_type TEXT NOT NULL DEFAULT 'global' +
scope_id TEXT to actor_roles. Constraints:
  - actor_roles_scope_type_enum: scope_type ∈ {global, profile, issuer}
  - actor_roles_scope_id_required_when_not_global: scope_id is NULL
    iff scope_type='global'
  - Uniqueness extended: (actor_id, actor_type, role_id, scope_type,
    scope_id, tenant_id) — so an operator can grant the same role to
    the same actor scoped to multiple profiles/issuers (e.g.
    r-operator on p-finance AND on p-engineering).
Index idx_actor_roles_scope for non-global lookup hot paths.

Domain: ActorRole.ScopeType (ScopeType enum) + ScopeID (*string).
Authorizer.CheckPermission already understands the tuple via the
parallel role_permissions columns; this addition gives operators a
per-actor knob without forking roles.

Postgres repo: Grant writes scope_type+scope_id with ON CONFLICT keyed
on the new uniqueness tuple. Defaults to (global, NULL) when caller
omits.

Handler: assignRoleRequest extended with scope_type / scope_id /
expires_at. Validation:
  - role_id required (unchanged)
  - scope_type defaults to 'global'; allowed values global/profile/
    issuer; anything else → 400
  - scope_id required when scope_type ∈ {profile, issuer}; rejected
    (must be empty) when scope_type='global'
  - expires_at must be in the future when present; nil = standing

Regression matrix in internal/api/handler/auth_test.go (6 cases):
  - TestAssignRoleToKey_HIGH10_ProfileScopeBoundGrantPersists
  - TestAssignRoleToKey_HIGH10_TimeBoundGrantPersists
  - TestAssignRoleToKey_HIGH10_RejectsScopeIDWithGlobalScope
  - TestAssignRoleToKey_HIGH10_RejectsMissingScopeIDOnProfile
  - TestAssignRoleToKey_HIGH10_RejectsPastExpiry
  - TestAssignRoleToKey_HIGH10_RejectsInvalidScopeType

HIGH-10 marked CLOSED in audit-doc — the v3 deferral from the prior
session is reversed; everything lands in v2.

Refs: cowork/auth-bundles-fixes-2026-05-10/HANDOFF.md item 1
      cowork/auth-bundles-audit-2026-05-10.md HIGH-10
This commit is contained in:
shankar0123
2026-05-10 22:47:45 +00:00
parent acaa81472d
commit 551812b2ca
6 changed files with 261 additions and 5 deletions
+120
View File
@@ -9,6 +9,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/certctl-io/certctl/internal/auth"
"github.com/certctl-io/certctl/internal/domain"
@@ -304,6 +305,125 @@ func TestAuthHandler_AssignRoleToKey(t *testing.T) {
}
}
// Audit 2026-05-10 HIGH-10 regression matrix — pin the new
// scope_type / scope_id / expires_at fields on assignRoleRequest.
// Pre-fix, the request body accepted only `{role_id}` so per-actor
// scope-bound grants and time-bound grants weren't expressible via
// the API even though the schema reserved the columns. Post-fix,
// validation rules:
//
// - scope_type ∈ {global, profile, issuer}; defaults to global.
// - scope_id required when scope_type != global; rejected when
// scope_type == global.
// - expires_at must be in the future when present.
func TestAssignRoleToKey_HIGH10_ProfileScopeBoundGrantPersists(t *testing.T) {
h, _, _, actorSvc := newAuthHandlerWithFakes()
scopeID := "p-finance"
body, _ := json.Marshal(assignRoleRequest{
RoleID: "r-operator",
ScopeType: "profile",
ScopeID: &scopeID,
})
req := withAuthCtx(httptest.NewRequest(http.MethodPost, "/api/v1/auth/keys/alice/roles", bytes.NewReader(body)), "admin", auth.ActorTypeAPIKey)
req.SetPathValue("id", "alice")
rec := httptest.NewRecorder()
h.AssignRoleToKey(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d; body=%s", rec.Code, rec.Body.String())
}
if len(actorSvc.roles) != 1 {
t.Fatalf("expected 1 grant; got %d", len(actorSvc.roles))
}
if got := string(actorSvc.roles[0].ScopeType); got != "profile" {
t.Errorf("ScopeType = %q; want profile", got)
}
if actorSvc.roles[0].ScopeID == nil || *actorSvc.roles[0].ScopeID != "p-finance" {
t.Errorf("ScopeID = %v; want p-finance", actorSvc.roles[0].ScopeID)
}
}
func TestAssignRoleToKey_HIGH10_TimeBoundGrantPersists(t *testing.T) {
h, _, _, actorSvc := newAuthHandlerWithFakes()
future := time.Now().Add(24 * time.Hour).UTC()
body, _ := json.Marshal(assignRoleRequest{
RoleID: "r-operator",
ExpiresAt: &future,
})
req := withAuthCtx(httptest.NewRequest(http.MethodPost, "/api/v1/auth/keys/alice/roles", bytes.NewReader(body)), "admin", auth.ActorTypeAPIKey)
req.SetPathValue("id", "alice")
rec := httptest.NewRecorder()
h.AssignRoleToKey(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d; body=%s", rec.Code, rec.Body.String())
}
if len(actorSvc.roles) != 1 || actorSvc.roles[0].ExpiresAt == nil {
t.Fatalf("expected 1 grant with ExpiresAt; got %+v", actorSvc.roles)
}
}
func TestAssignRoleToKey_HIGH10_RejectsScopeIDWithGlobalScope(t *testing.T) {
h, _, _, _ := newAuthHandlerWithFakes()
bad := "p-finance"
body, _ := json.Marshal(assignRoleRequest{
RoleID: "r-operator",
ScopeType: "global",
ScopeID: &bad,
})
req := withAuthCtx(httptest.NewRequest(http.MethodPost, "/api/v1/auth/keys/alice/roles", bytes.NewReader(body)), "admin", auth.ActorTypeAPIKey)
req.SetPathValue("id", "alice")
rec := httptest.NewRecorder()
h.AssignRoleToKey(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("scope_id with scope_type=global should be 400; got %d", rec.Code)
}
}
func TestAssignRoleToKey_HIGH10_RejectsMissingScopeIDOnProfile(t *testing.T) {
h, _, _, _ := newAuthHandlerWithFakes()
body, _ := json.Marshal(assignRoleRequest{
RoleID: "r-operator",
ScopeType: "profile",
})
req := withAuthCtx(httptest.NewRequest(http.MethodPost, "/api/v1/auth/keys/alice/roles", bytes.NewReader(body)), "admin", auth.ActorTypeAPIKey)
req.SetPathValue("id", "alice")
rec := httptest.NewRecorder()
h.AssignRoleToKey(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("missing scope_id on scope_type=profile should be 400; got %d", rec.Code)
}
}
func TestAssignRoleToKey_HIGH10_RejectsPastExpiry(t *testing.T) {
h, _, _, _ := newAuthHandlerWithFakes()
past := time.Now().Add(-1 * time.Hour).UTC()
body, _ := json.Marshal(assignRoleRequest{
RoleID: "r-operator",
ExpiresAt: &past,
})
req := withAuthCtx(httptest.NewRequest(http.MethodPost, "/api/v1/auth/keys/alice/roles", bytes.NewReader(body)), "admin", auth.ActorTypeAPIKey)
req.SetPathValue("id", "alice")
rec := httptest.NewRecorder()
h.AssignRoleToKey(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("past expires_at should be 400; got %d", rec.Code)
}
}
func TestAssignRoleToKey_HIGH10_RejectsInvalidScopeType(t *testing.T) {
h, _, _, _ := newAuthHandlerWithFakes()
body, _ := json.Marshal(assignRoleRequest{
RoleID: "r-operator",
ScopeType: "tenant", // not a valid scope_type
})
req := withAuthCtx(httptest.NewRequest(http.MethodPost, "/api/v1/auth/keys/alice/roles", bytes.NewReader(body)), "admin", auth.ActorTypeAPIKey)
req.SetPathValue("id", "alice")
rec := httptest.NewRecorder()
h.AssignRoleToKey(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("invalid scope_type should be 400; got %d", rec.Code)
}
}
func TestAuthHandler_AssignRoleSelfRoleAssignReturns403(t *testing.T) {
h, _, _, actorSvc := newAuthHandlerWithFakes()
actorSvc.grantErr = errors.New("auth.role.assign required: " + authsvc.ErrSelfRoleAssignment.Error())