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:
@@ -227,7 +227,8 @@ func (s *modelService) UpdateModel(ctx context.Context, model *types.Model) erro
|
||||
logger.Info(ctx, "Start updating model")
|
||||
logger.Infof(ctx, "Updating model ID: %s, name: %s", model.ID, model.Name)
|
||||
|
||||
// Check if the model is builtin - builtin models cannot be updated
|
||||
// Built-in models are platform-wide. Tenant administrators may view them,
|
||||
// but only a system administrator may change their shared configuration.
|
||||
tenantID := types.MustTenantIDFromContext(ctx)
|
||||
existingModel, err := s.repo.GetByID(ctx, tenantID, model.ID)
|
||||
if err != nil {
|
||||
@@ -237,8 +238,15 @@ func (s *modelService) UpdateModel(ctx context.Context, model *types.Model) erro
|
||||
return err
|
||||
}
|
||||
if existingModel != nil && existingModel.IsBuiltin {
|
||||
logger.Warnf(ctx, "Attempted to update builtin model: %s", model.ID)
|
||||
return errors.New("builtin models cannot be updated")
|
||||
if !types.IsSystemAdminFromContext(ctx) {
|
||||
logger.Warnf(ctx, "Non-system-admin attempted to update builtin model: %s", model.ID)
|
||||
return apperrors.NewForbiddenError("only system administrators can update builtin models")
|
||||
}
|
||||
// A UI edit is an explicit runtime override. Clear YAML ownership so
|
||||
// the startup reconciler does not silently replace the saved values.
|
||||
model.TenantID = existingModel.TenantID
|
||||
model.IsBuiltin = true
|
||||
model.ManagedBy = ""
|
||||
}
|
||||
|
||||
// Update model in repository
|
||||
@@ -271,8 +279,9 @@ func (s *modelService) UpdateModelCredentials(
|
||||
if existing == nil {
|
||||
return nil, ErrModelNotFound
|
||||
}
|
||||
if existing.IsBuiltin {
|
||||
return nil, errors.New("builtin models cannot have credentials modified")
|
||||
if existing.IsBuiltin && !types.IsSystemAdminFromContext(ctx) {
|
||||
return nil, apperrors.NewForbiddenError(
|
||||
"only system administrators can modify builtin model credentials")
|
||||
}
|
||||
|
||||
changed := false
|
||||
@@ -287,6 +296,10 @@ func (s *modelService) UpdateModelCredentials(
|
||||
if !changed {
|
||||
return existing, nil
|
||||
}
|
||||
if existing.IsBuiltin {
|
||||
// Credential changes are also runtime overrides of YAML-managed data.
|
||||
existing.ManagedBy = ""
|
||||
}
|
||||
if err := s.repo.Update(ctx, existing); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -304,8 +317,9 @@ func (s *modelService) ClearModelCredential(ctx context.Context, id, field strin
|
||||
if existing == nil {
|
||||
return ErrModelNotFound
|
||||
}
|
||||
if existing.IsBuiltin {
|
||||
return errors.New("builtin models cannot have credentials modified")
|
||||
if existing.IsBuiltin && !types.IsSystemAdminFromContext(ctx) {
|
||||
return apperrors.NewForbiddenError(
|
||||
"only system administrators can modify builtin model credentials")
|
||||
}
|
||||
|
||||
changed := false
|
||||
@@ -326,6 +340,9 @@ func (s *modelService) ClearModelCredential(ctx context.Context, id, field strin
|
||||
if !changed {
|
||||
return nil
|
||||
}
|
||||
if existing.IsBuiltin {
|
||||
existing.ManagedBy = ""
|
||||
}
|
||||
if err := s.repo.Update(ctx, existing); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func builtinModelContext(systemAdmin bool) context.Context {
|
||||
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7))
|
||||
return context.WithValue(ctx, types.SystemAdminContextKey, systemAdmin)
|
||||
}
|
||||
|
||||
func TestUpdateBuiltinModel_RequiresSystemAdmin(t *testing.T) {
|
||||
stored := &types.Model{
|
||||
ID: "builtin-chat", TenantID: 10000, IsBuiltin: true,
|
||||
ManagedBy: types.BuiltinModelManagedBy,
|
||||
}
|
||||
updated := false
|
||||
svc := NewModelService(&stubModelRepoForDelete{
|
||||
model: stored,
|
||||
update: func(*types.Model) error {
|
||||
updated = true
|
||||
return nil
|
||||
},
|
||||
}, nil, nil, nil, nil, nil)
|
||||
|
||||
err := svc.UpdateModel(builtinModelContext(false), &types.Model{ID: stored.ID})
|
||||
require.Error(t, err)
|
||||
appErr, ok := apperrors.IsAppError(err)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, apperrors.ErrForbidden, appErr.Code)
|
||||
assert.False(t, updated)
|
||||
}
|
||||
|
||||
func TestUpdateBuiltinModel_SystemAdminCreatesRuntimeOverride(t *testing.T) {
|
||||
stored := &types.Model{
|
||||
ID: "builtin-chat", TenantID: 10000, IsBuiltin: true,
|
||||
ManagedBy: types.BuiltinModelManagedBy,
|
||||
}
|
||||
var saved *types.Model
|
||||
svc := NewModelService(&stubModelRepoForDelete{
|
||||
model: stored,
|
||||
update: func(model *types.Model) error {
|
||||
copy := *model
|
||||
saved = ©
|
||||
return nil
|
||||
},
|
||||
}, nil, nil, nil, nil, nil)
|
||||
|
||||
input := &types.Model{ID: stored.ID, Name: "edited"}
|
||||
require.NoError(t, svc.UpdateModel(builtinModelContext(true), input))
|
||||
require.NotNil(t, saved)
|
||||
assert.Equal(t, uint64(10000), saved.TenantID)
|
||||
assert.True(t, saved.IsBuiltin)
|
||||
assert.Empty(t, saved.ManagedBy, "UI edit must stop later YAML reconciliation")
|
||||
}
|
||||
|
||||
func TestUpdateBuiltinModelCredentials_SystemAdminOnly(t *testing.T) {
|
||||
newKey := "sk-new"
|
||||
|
||||
t.Run("tenant admin denied", func(t *testing.T) {
|
||||
stored := &types.Model{ID: "builtin-chat", TenantID: 10000, IsBuiltin: true}
|
||||
svc := NewModelService(&stubModelRepoForDelete{model: stored}, nil, nil, nil, nil, nil)
|
||||
_, err := svc.UpdateModelCredentials(builtinModelContext(false), stored.ID, &newKey, nil)
|
||||
require.Error(t, err)
|
||||
appErr, ok := apperrors.IsAppError(err)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, apperrors.ErrForbidden, appErr.Code)
|
||||
})
|
||||
|
||||
t.Run("system admin saves runtime override", func(t *testing.T) {
|
||||
stored := &types.Model{
|
||||
ID: "builtin-chat", TenantID: 10000, IsBuiltin: true,
|
||||
ManagedBy: types.BuiltinModelManagedBy,
|
||||
}
|
||||
var saved *types.Model
|
||||
svc := NewModelService(&stubModelRepoForDelete{
|
||||
model: stored,
|
||||
update: func(model *types.Model) error {
|
||||
copy := *model
|
||||
saved = ©
|
||||
return nil
|
||||
},
|
||||
}, nil, nil, nil, nil, nil)
|
||||
|
||||
updated, err := svc.UpdateModelCredentials(
|
||||
builtinModelContext(true), stored.ID, &newKey, nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, newKey, updated.Parameters.APIKey)
|
||||
require.NotNil(t, saved)
|
||||
assert.Empty(t, saved.ManagedBy)
|
||||
})
|
||||
}
|
||||
@@ -75,6 +75,7 @@ func (s *stubAgentRepoForModelDelete) CountByModelID(context.Context, uint64, st
|
||||
type stubModelRepoForDelete struct {
|
||||
model *types.Model
|
||||
delete func(id string) error
|
||||
update func(model *types.Model) error
|
||||
}
|
||||
|
||||
func (s *stubModelRepoForDelete) Create(context.Context, *types.Model) error { return nil }
|
||||
@@ -87,7 +88,12 @@ func (s *stubModelRepoForDelete) GetByID(_ context.Context, _ uint64, id string)
|
||||
func (s *stubModelRepoForDelete) List(context.Context, uint64, types.ModelType, types.ModelSource) ([]*types.Model, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *stubModelRepoForDelete) Update(context.Context, *types.Model) error { return nil }
|
||||
func (s *stubModelRepoForDelete) Update(_ context.Context, model *types.Model) error {
|
||||
if s.update != nil {
|
||||
return s.update(model)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *stubModelRepoForDelete) Delete(_ context.Context, _ uint64, id string) error {
|
||||
if s.delete != nil {
|
||||
return s.delete(id)
|
||||
|
||||
Reference in New Issue
Block a user