1 parent
9dc861d560
commit
fd475cd88d
54 files changed
+987
-91
No files matched your search
@@ -254,6 +254,21 @@ func (m *ConfigManager) Status() ConfigStatus {
|
||||
}
|
||||
|
||||
func (m *ConfigManager) Validate(ctx context.Context, req ValidateRequest) (*ValidationResult, error) {
|
||||
return m.validate(ctx, req, nil)
|
||||
}
|
||||
|
||||
// ValidateAuthorized 对外部请求使用与保存一致的授权;内部校验不暴露为 HTTP 入口。
|
||||
func (m *ConfigManager) ValidateAuthorized(ctx context.Context, req ValidateRequest, actor AuditActor) (*ValidationResult, error) {
|
||||
if m == nil {
|
||||
return nil, ErrManagerStorage
|
||||
}
|
||||
if err := m.authorize(ctx, actor, ConfigOperationUpdate, req.Section); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m.validate(ctx, req, &actor)
|
||||
}
|
||||
|
||||
func (m *ConfigManager) validate(ctx context.Context, req ValidateRequest, actor *AuditActor) (*ValidationResult, error) {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -273,6 +288,11 @@ func (m *ConfigManager) Validate(ctx context.Context, req ValidateRequest) (*Val
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if actor != nil && req.Section == SectionPlugins {
|
||||
if err := AuthorizePluginChanges(*actor, current, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if req.Section == SectionSSL {
|
||||
if _, err := prepareCustomSSLPaths(m.loader.root, &candidate); err != nil {
|
||||
issue, ok := SSLValidationIssue(err)
|
||||
@@ -317,6 +337,11 @@ func (m *ConfigManager) Update(ctx context.Context, req UpdateRequest) (*UpdateR
|
||||
return nil, err
|
||||
}
|
||||
var sslMaterial *customSSLMaterial
|
||||
if req.Section == SectionPlugins {
|
||||
if err := AuthorizePluginChanges(req.Actor, current, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if req.Section == SectionSSL {
|
||||
sslMaterial, err = prepareCustomSSLPaths(m.loader.root, &candidate)
|
||||
if err != nil {
|
||||
@@ -406,6 +431,12 @@ func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor Audi
|
||||
if err := m.authorize(ctx, actor, ConfigOperationUpdate, pending.Section); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 暂存后角色可能变化,应用时必须重新检查程序与权限边界。
|
||||
if pending.Section == SectionPlugins {
|
||||
if err := AuthorizePluginChanges(actor, current, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if generation != candidate.Generation || candidate.Generation != current.Generation+1 {
|
||||
return nil, ErrGenerationConflict
|
||||
|
||||
@@ -2,6 +2,10 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -10,7 +14,7 @@ import (
|
||||
// 只保留定位操作需要的元数据,不输出配置值、凭据、用户名称或完整变更载荷。
|
||||
type RuntimeConfigLogWriter struct{}
|
||||
|
||||
func (RuntimeConfigLogWriter) WriteConfigAudit(_ context.Context, event ConfigAuditEvent) error {
|
||||
func (RuntimeConfigLogWriter) WriteConfigAudit(ctx context.Context, event ConfigAuditEvent) error {
|
||||
event, err := normalizeAuditEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -20,7 +24,10 @@ func (RuntimeConfigLogWriter) WriteConfigAudit(_ context.Context, event ConfigAu
|
||||
ConfigOperationRollback: "回滚配置",
|
||||
ConfigOperationMigration: "迁移配置",
|
||||
}[event.Operation]
|
||||
entry := log.WithFields(log.Fields{
|
||||
// 操作者来自配置管理器已授权的事件,不从客户端载荷或任意日志字段恢复归属。
|
||||
actorID, _ := strconv.Atoi(event.ActorID)
|
||||
actorContext := &appcontext.Context{Context: ctx, UserID: actorID, Role: access.Role(event.ActorRole)}
|
||||
entry := log.WithContext(actorContext).WithFields(log.Fields{
|
||||
"action": action,
|
||||
"object": event.Section,
|
||||
"operation": event.Operation,
|
||||
|
||||
@@ -24,6 +24,9 @@ func (m *ConfigManager) Pending(actor AuditActor) *PendingView {
|
||||
if m.pending == nil {
|
||||
return nil
|
||||
}
|
||||
if actor.Role != "super_admin" && (actor.Role != "admin" || m.pending.Section != SectionPlugins || actor.UserID == "" || actor.UserID != m.pending.Actor.UserID) {
|
||||
return nil
|
||||
}
|
||||
return &PendingView{
|
||||
ID: m.pending.ID,
|
||||
Generation: m.pending.Candidate.Generation, Section: m.pending.Section,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -14,6 +16,13 @@ func runtimeEventFields(entry *log.Entry) map[string]any {
|
||||
fields[key] = value
|
||||
}
|
||||
fields["logSchema"] = 1
|
||||
// 归属只来自服务端认证上下文,调用点不能用任意字段冒充其他账号。
|
||||
delete(fields, "actorId")
|
||||
delete(fields, "actorRole")
|
||||
if ctx, ok := entry.Context.(*appcontext.Context); ok && ctx != nil && ctx.UserID > 0 {
|
||||
fields["actorId"] = strconv.Itoa(ctx.UserID)
|
||||
fields["actorRole"] = string(ctx.EffectiveRole())
|
||||
}
|
||||
if action, ok := fields["action"].(string); !ok || strings.TrimSpace(action) == "" {
|
||||
action = "运行事件"
|
||||
if entry.Caller != nil && entry.Caller.Function != "" {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -152,6 +153,9 @@ func DefaultConfigAuthorization(_ context.Context, actor AuditActor, operation C
|
||||
return ErrAuthorizationRequired
|
||||
}
|
||||
capability := access.CapabilityConfigWriteGlobal
|
||||
if operation == ConfigOperationUpdate && section == SectionPlugins {
|
||||
capability = access.CapabilityPluginWriteGlobal
|
||||
}
|
||||
if operation == ConfigOperationRollback {
|
||||
capability = access.CapabilityConfigRollback
|
||||
} else if operation == ConfigOperationMigration {
|
||||
@@ -178,6 +182,23 @@ func DefaultConfigAuthorization(_ context.Context, actor AuditActor, operation C
|
||||
return nil
|
||||
}
|
||||
|
||||
// AuthorizePluginChanges 限制管理员只能修改已有插件参数,不允许安装、替换程序或提升权限。
|
||||
func AuthorizePluginChanges(actor AuditActor, current, candidate RuntimeConfig) error {
|
||||
if actor.Role == string(access.RoleSuperAdmin) {
|
||||
return nil
|
||||
}
|
||||
if actor.Role != string(access.RoleAdmin) || len(current.Plugins.Plugins) != len(candidate.Plugins.Plugins) {
|
||||
return ErrAuthorizationRequired
|
||||
}
|
||||
for id, next := range candidate.Plugins.Plugins {
|
||||
previous, ok := current.Plugins.Plugins[id]
|
||||
if !ok || next.Executable != previous.Executable || !reflect.DeepEqual(next.Permissions, previous.Permissions) {
|
||||
return ErrAuthorizationRequired
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DefaultConfigConfirmation(_ context.Context, _ AuditActor, confirmation StepUpConfirmation, operation ConfigOperation, _ Section) error {
|
||||
if operation != ConfigOperationRollback && !strings.EqualFold(confirmation.Method, "webauthn") && !strings.EqualFold(confirmation.Method, "recent_auth") {
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user