Refine scoped API key capabilities
This commit is contained in:
@@ -11,20 +11,16 @@ import (
|
||||
|
||||
func newTestAuthorizer() *APIKeyRouteAuthorizer {
|
||||
a := NewAPIKeyRouteAuthorizer()
|
||||
// Data-plane routes (Viewer read, Contributor write, Admin destructive)
|
||||
// plus one tenant-level Owner route.
|
||||
a.Register(http.MethodGet, "/api/v1/knowledge-bases/:id", policyMinRole(types.TenantRoleViewer))
|
||||
a.Register(http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file", policyMinRole(types.TenantRoleContributor))
|
||||
a.Register(http.MethodDelete, "/api/v1/knowledge-bases/:id/knowledge", policyMinRole(types.TenantRoleAdmin))
|
||||
a.Register(http.MethodGet, "/api/v1/models", policyMinRole(types.TenantRoleOwner))
|
||||
a.Register(http.MethodPut, "/api/v1/tenants/kv/:key", policyMinRole(types.TenantRoleOwner))
|
||||
a.Register(http.MethodGet, "/api/v1/auth/me", APIKeyRoutePolicy{})
|
||||
a.Register(http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.WithCapability(types.APIKeyCapabilityIngest))
|
||||
a.Register(http.MethodGet, "/api/v1/models",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.WithCapability(types.APIKeyCapabilityManageModels))
|
||||
a.Register(http.MethodPut, "/api/v1/tenants/kv/:key",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.WithCapability(types.APIKeyCapabilityManageTenantSettings))
|
||||
return a
|
||||
}
|
||||
|
||||
func policyMinRole(r types.TenantRole) APIKeyRoutePolicy {
|
||||
return APIKeyRoutePolicy{MinRole: r}
|
||||
}
|
||||
|
||||
// runGate exercises the gate middleware with a given scope, method and route
|
||||
// full-path, returning whether the request was allowed to proceed.
|
||||
func runGate(t *testing.T, a *APIKeyRouteAuthorizer, scope *types.TenantAPIKeyScope, method, fullPath string) bool {
|
||||
@@ -56,8 +52,6 @@ func concretePath(tmpl string) string {
|
||||
return "/api/v1/knowledge-bases/kb-1"
|
||||
case "/api/v1/knowledge-bases/:id/knowledge/file":
|
||||
return "/api/v1/knowledge-bases/kb-1/knowledge/file"
|
||||
case "/api/v1/knowledge-bases/:id/knowledge":
|
||||
return "/api/v1/knowledge-bases/kb-1/knowledge"
|
||||
case "/api/v1/tenants/kv/:key":
|
||||
return "/api/v1/tenants/kv/some-key"
|
||||
default:
|
||||
@@ -67,7 +61,7 @@ func concretePath(tmpl string) string {
|
||||
|
||||
func TestGateJWTPassesThrough(t *testing.T) {
|
||||
a := newTestAuthorizer()
|
||||
// No scope => JWT principal => always allowed, even on an Owner route.
|
||||
// No scope => JWT principal => always allowed, even on a full-access route.
|
||||
if !runGate(t, a, nil, http.MethodGet, "/api/v1/models") {
|
||||
t.Fatal("JWT principal must pass the gate")
|
||||
}
|
||||
@@ -75,11 +69,11 @@ func TestGateJWTPassesThrough(t *testing.T) {
|
||||
|
||||
func TestGateDefaultDeny(t *testing.T) {
|
||||
a := newTestAuthorizer()
|
||||
owner := &types.TenantAPIKeyScope{Role: types.TenantRoleOwner}
|
||||
full := &types.TenantAPIKeyScope{FullAccess: true}
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.Use(func(c *gin.Context) {
|
||||
c.Request = c.Request.WithContext(types.WithTenantAPIKeyScope(c.Request.Context(), *owner))
|
||||
c.Request = c.Request.WithContext(types.WithTenantAPIKeyScope(c.Request.Context(), *full))
|
||||
c.Next()
|
||||
})
|
||||
engine.Use(a.Middleware())
|
||||
@@ -87,54 +81,80 @@ func TestGateDefaultDeny(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/v1/agents", nil))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("undeclared route should default-deny even an Owner key: status=%d", w.Code)
|
||||
t.Fatalf("undeclared route should default-deny even a full-access key: status=%d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateDataPlaneRoleLadder(t *testing.T) {
|
||||
func TestGateAnyPolicyAllowsScopedKey(t *testing.T) {
|
||||
a := newTestAuthorizer()
|
||||
viewer := &types.TenantAPIKeyScope{Role: types.TenantRoleViewer}
|
||||
contributor := &types.TenantAPIKeyScope{Role: types.TenantRoleContributor}
|
||||
admin := &types.TenantAPIKeyScope{Role: types.TenantRoleAdmin}
|
||||
scoped := &types.TenantAPIKeyScope{}
|
||||
|
||||
// Safe read: any data-plane key (Viewer) passes.
|
||||
if !runGate(t, a, viewer, http.MethodGet, "/api/v1/knowledge-bases/:id") {
|
||||
t.Fatal("viewer key should read a declared KB route")
|
||||
}
|
||||
// Contributor write.
|
||||
if runGate(t, a, viewer, http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file") {
|
||||
t.Fatal("viewer key must not perform a contributor write")
|
||||
}
|
||||
if !runGate(t, a, contributor, http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file") {
|
||||
t.Fatal("contributor key should perform a contributor write")
|
||||
}
|
||||
// Admin destructive.
|
||||
if runGate(t, a, contributor, http.MethodDelete, "/api/v1/knowledge-bases/:id/knowledge") {
|
||||
t.Fatal("contributor key must not perform an admin destructive op")
|
||||
}
|
||||
if !runGate(t, a, admin, http.MethodDelete, "/api/v1/knowledge-bases/:id/knowledge") {
|
||||
t.Fatal("admin key should perform an admin destructive op")
|
||||
if !runGate(t, a, scoped, http.MethodGet, "/api/v1/auth/me") {
|
||||
t.Fatal("scoped key should call a declared route with an empty policy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateTenantRoutesRequireOwner(t *testing.T) {
|
||||
func TestGateFullAccessAndCapabilityPolicies(t *testing.T) {
|
||||
a := newTestAuthorizer()
|
||||
admin := &types.TenantAPIKeyScope{Role: types.TenantRoleAdmin}
|
||||
owner := &types.TenantAPIKeyScope{Role: types.TenantRoleOwner}
|
||||
scoped := &types.TenantAPIKeyScope{}
|
||||
full := &types.TenantAPIKeyScope{FullAccess: true}
|
||||
ingest := &types.TenantAPIKeyScope{Capabilities: types.StringArray{"ingest"}}
|
||||
models := &types.TenantAPIKeyScope{Capabilities: types.StringArray{"manage_models"}}
|
||||
|
||||
// Even a safe GET on a tenant-level route requires Owner (no Viewer floor).
|
||||
if runGate(t, a, admin, http.MethodGet, "/api/v1/models") {
|
||||
t.Fatal("admin key must NOT read a tenant-level route")
|
||||
if runGate(t, a, scoped, http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file") {
|
||||
t.Fatal("plain scoped key must not write content")
|
||||
}
|
||||
if !runGate(t, a, owner, http.MethodGet, "/api/v1/models") {
|
||||
t.Fatal("owner key should read a tenant-level route")
|
||||
if !runGate(t, a, full, http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file") {
|
||||
t.Fatal("full-access key should write content")
|
||||
}
|
||||
// Unsafe tenant write also requires Owner.
|
||||
if runGate(t, a, admin, http.MethodPut, "/api/v1/tenants/kv/:key") {
|
||||
t.Fatal("admin key must NOT write a tenant-level route")
|
||||
if !runGate(t, a, ingest, http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file") {
|
||||
t.Fatal("ingest capability should write content")
|
||||
}
|
||||
if !runGate(t, a, owner, http.MethodPut, "/api/v1/tenants/kv/:key") {
|
||||
t.Fatal("owner key should write a tenant-level route")
|
||||
if runGate(t, a, ingest, http.MethodGet, "/api/v1/models") {
|
||||
t.Fatal("ingest capability must not read model management routes")
|
||||
}
|
||||
if !runGate(t, a, models, http.MethodGet, "/api/v1/models") {
|
||||
t.Fatal("manage_models capability should read model management routes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateAnyOfCapabilities(t *testing.T) {
|
||||
a := NewAPIKeyRouteAuthorizer()
|
||||
a.Register(http.MethodPost, "/api/v1/sessions",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.WithCapability(types.APIKeyCapabilityChat))
|
||||
a.Register(http.MethodGet, "/api/v1/agents",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.
|
||||
WithCapability(types.APIKeyCapabilityChat).
|
||||
WithCapability(types.APIKeyCapabilityManageAgents))
|
||||
a.Register(http.MethodPost, "/api/v1/agents",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.WithCapability(types.APIKeyCapabilityManageAgents))
|
||||
a.Register(http.MethodPut, "/api/v1/knowledge-bases/:id",
|
||||
APIKeyRoutePolicy{RequireFullAccess: true}.WithCapability(types.APIKeyCapabilityManageKnowledgeBases))
|
||||
|
||||
chat := &types.TenantAPIKeyScope{Capabilities: types.StringArray{"chat"}}
|
||||
manage := &types.TenantAPIKeyScope{Capabilities: types.StringArray{"manage_agents"}}
|
||||
manageKBs := &types.TenantAPIKeyScope{Capabilities: types.StringArray{"manage_kbs"}}
|
||||
|
||||
// Either capability satisfies the any-of read route.
|
||||
if !runGate(t, a, chat, http.MethodGet, "/api/v1/agents") {
|
||||
t.Fatal("chat should read agents (any-of)")
|
||||
}
|
||||
if !runGate(t, a, manage, http.MethodGet, "/api/v1/agents") {
|
||||
t.Fatal("manage_agents should read agents (any-of)")
|
||||
}
|
||||
// Only manage_agents may author agents.
|
||||
if runGate(t, a, chat, http.MethodPost, "/api/v1/agents") {
|
||||
t.Fatal("chat must not author agents")
|
||||
}
|
||||
if !runGate(t, a, manage, http.MethodPost, "/api/v1/agents") {
|
||||
t.Fatal("manage_agents should author agents")
|
||||
}
|
||||
// Only manage_kbs may manage KB metadata/config.
|
||||
if runGate(t, a, manage, http.MethodPut, "/api/v1/knowledge-bases/:id") {
|
||||
t.Fatal("manage_agents must not manage knowledge bases")
|
||||
}
|
||||
if !runGate(t, a, manageKBs, http.MethodPut, "/api/v1/knowledge-bases/:id") {
|
||||
t.Fatal("manage_kbs should manage knowledge bases")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,11 +163,11 @@ func TestGateKBScopeDoesNotBlockDataPlane(t *testing.T) {
|
||||
// A KB-restricted key is NOT blocked by the gate on data-plane routes;
|
||||
// its KB allow-list is enforced downstream by KBAccess/handler checks.
|
||||
restricted := &types.TenantAPIKeyScope{
|
||||
Role: types.TenantRoleContributor,
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1"},
|
||||
Capabilities: types.StringArray{"ingest"},
|
||||
}
|
||||
if !runGate(t, a, restricted, http.MethodPost, "/api/v1/knowledge-bases/:id/knowledge/file") {
|
||||
t.Fatal("KB-restricted contributor key should pass the gate on a data-plane write")
|
||||
t.Fatal("KB-restricted ingest key should pass the gate on a data-plane write")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,8 +196,8 @@ func runDenyAPIKey(t *testing.T, scope *types.TenantAPIKeyScope) (reached bool,
|
||||
// engine-root routes (outside /api/v1) that rely on RequireRole must not be
|
||||
// reachable by API-key principals, since RequireRole short-circuits them.
|
||||
func TestDenyAPIKeyPrincipalBlocksAPIKeys(t *testing.T) {
|
||||
// Even an Owner-role API key must be rejected outright.
|
||||
reached, status := runDenyAPIKey(t, &types.TenantAPIKeyScope{Role: types.TenantRoleOwner})
|
||||
// Even a full-access API key must be rejected outright.
|
||||
reached, status := runDenyAPIKey(t, &types.TenantAPIKeyScope{FullAccess: true})
|
||||
if reached {
|
||||
t.Fatal("API-key principal must not reach a DenyAPIKeyPrincipal-guarded handler")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user