feat(models): let system admins edit builtin model config
Allow platform system administrators to update builtin model parameters and credentials from the management UI while keeping secrets masked in API responses. UI edits clear YAML ownership so startup reconciliation no longer overwrites runtime overrides.
This commit is contained in:
@@ -110,6 +110,26 @@ func RequireRole(min types.TenantRole, cfg *config.Config) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// RequireRoleOrSystemAdmin applies the tenant role floor while also allowing
|
||||
// platform system administrators. Use it for routes that normally mutate
|
||||
// tenant infrastructure but have a narrowly-scoped platform-owned resource
|
||||
// (for example built-in models) that system administrators must be able to
|
||||
// maintain independently of their role in the active tenant.
|
||||
//
|
||||
// API-key behavior remains identical to RequireRole: the APIKeyGate is the
|
||||
// source of truth for machine principals, so they short-circuit the role
|
||||
// check here as well.
|
||||
func RequireRoleOrSystemAdmin(min types.TenantRole, cfg *config.Config) gin.HandlerFunc {
|
||||
requireRole := RequireRole(min, cfg)
|
||||
return func(c *gin.Context) {
|
||||
if types.IsSystemAdminFromContext(c.Request.Context()) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
requireRole(c)
|
||||
}
|
||||
}
|
||||
|
||||
// RequireSystemAdmin returns a gin middleware that aborts the request with
|
||||
// HTTP 403 unless the caller is a system administrator
|
||||
// (User.IsSystemAdmin = true).
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// rbacTestHarness builds a tiny gin engine with the RBAC middleware in
|
||||
@@ -80,6 +81,33 @@ func TestRequireRole_RejectsBelowMin(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireRoleOrSystemAdmin_AllowsSystemAdminBelowTenantRole(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.Use(func(c *gin.Context) {
|
||||
ctx := context.WithValue(c.Request.Context(), types.TenantRoleContextKey, types.TenantRoleViewer)
|
||||
ctx = context.WithValue(ctx, types.SystemAdminContextKey, true)
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
})
|
||||
router.GET("/protected",
|
||||
RequireRoleOrSystemAdmin(types.TenantRoleAdmin, cfgRBAC(true)),
|
||||
func(c *gin.Context) { c.Status(http.StatusOK) },
|
||||
)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/protected", nil))
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func TestRequireRoleOrSystemAdmin_RejectsOrdinaryViewer(t *testing.T) {
|
||||
w := rbacTestHarness(types.TenantRoleViewer, "u1",
|
||||
RequireRoleOrSystemAdmin(types.TenantRoleAdmin, cfgRBAC(true)))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("ordinary Viewer must not clear Admin-or-SystemAdmin gate, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireRole_FailOpenWhenRBACDisabled(t *testing.T) {
|
||||
// EnableRBAC=false: the middleware should log but not block, so the
|
||||
// downstream handler still runs. This is the rollout-safety guarantee.
|
||||
|
||||
Reference in New Issue
Block a user