1 parent
42a173f742
commit
ae224649f3
25 files changed
+291
-313
No files matched your search
@@ -12,7 +12,8 @@ import (
|
||||
func TestConfigManagerSwitchDatabaseCommitsSQLiteReference(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
audit := &MemoryAuditWriter{}
|
||||
manager, err := NewConfigManagerWithSecurity(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}, ConfigSecurity{Audit: audit})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -29,7 +30,6 @@ func TestConfigManagerSwitchDatabaseCommitsSQLiteReference(t *testing.T) {
|
||||
if len(status.PendingRestart) != 1 || status.PendingRestart[0] != ServiceDatabase {
|
||||
t.Fatalf("pending restart services = %+v", status.PendingRestart)
|
||||
}
|
||||
audit := manager.security.Audit.(*FileAuditWriter)
|
||||
events, err := audit.Query(context.Background(), ConfigAuditQuery{Operation: ConfigOperationMigration, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -45,7 +45,8 @@ func TestConfigManagerSwitchAcrossTypesPersistsAndRedactsAudit(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
|
||||
manager, err := NewConfigManager(root, options)
|
||||
audit := &MemoryAuditWriter{}
|
||||
manager, err := NewConfigManagerWithSecurity(root, options, ConfigSecurity{Audit: audit})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -64,7 +65,7 @@ func TestConfigManagerSwitchAcrossTypesPersistsAndRedactsAudit(t *testing.T) {
|
||||
if got := reloaded.Snapshot(); got.Generation != 2 || got.Root.Database != target {
|
||||
t.Fatal("target configuration was not persisted")
|
||||
}
|
||||
events, err := manager.security.Audit.(*FileAuditWriter).Query(context.Background(), ConfigAuditQuery{Operation: ConfigOperationMigration, Limit: 10})
|
||||
events, err := audit.Query(context.Background(), ConfigAuditQuery{Operation: ConfigOperationMigration, Limit: 10})
|
||||
if err != nil || len(events) != 1 {
|
||||
t.Fatal("migration audit missing")
|
||||
}
|
||||
|
||||
@@ -135,15 +135,7 @@ type ConfigManager struct {
|
||||
}
|
||||
|
||||
func NewConfigManager(root string, options ConfigLoaderOptions) (*ConfigManager, error) {
|
||||
audit, err := NewFileAuditWriter(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newConfigManager(root, options, ConfigSecurity{
|
||||
Authorize: DefaultConfigAuthorization,
|
||||
Confirm: DefaultConfigConfirmation,
|
||||
Audit: audit,
|
||||
})
|
||||
return NewConfigManagerWithSecurity(root, options, ConfigSecurity{})
|
||||
}
|
||||
|
||||
func NewConfigManagerWithSecurity(root string, options ConfigLoaderOptions, security ConfigSecurity) (*ConfigManager, error) {
|
||||
@@ -154,11 +146,7 @@ func NewConfigManagerWithSecurity(root string, options ConfigLoaderOptions, secu
|
||||
security.Confirm = DefaultConfigConfirmation
|
||||
}
|
||||
if security.Audit == nil {
|
||||
audit, err := NewFileAuditWriter(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
security.Audit = audit
|
||||
security.Audit = RuntimeConfigLogWriter{}
|
||||
}
|
||||
return newConfigManager(root, options, security)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// RuntimeConfigLogWriter 复用运行日志记录配置结果,不生成独立审计文件或数据库记录。
|
||||
// 只保留定位操作需要的元数据,不输出配置值、凭据、用户名称或完整变更载荷。
|
||||
type RuntimeConfigLogWriter struct{}
|
||||
|
||||
func (RuntimeConfigLogWriter) WriteConfigAudit(_ context.Context, event ConfigAuditEvent) error {
|
||||
event, err := normalizeAuditEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
action := map[ConfigOperation]string{
|
||||
ConfigOperationUpdate: "保存配置",
|
||||
ConfigOperationRollback: "回滚配置",
|
||||
ConfigOperationMigration: "迁移配置",
|
||||
}[event.Operation]
|
||||
entry := log.WithFields(log.Fields{
|
||||
"operation": event.Operation,
|
||||
"section": event.Section,
|
||||
"actorId": event.ActorID,
|
||||
"fromGeneration": event.FromGeneration,
|
||||
"toGeneration": event.ToGeneration,
|
||||
"result": event.Result,
|
||||
})
|
||||
if event.Result == "failure" {
|
||||
entry.Warn(action + "失败")
|
||||
} else {
|
||||
entry.Info(action + "成功")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user