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
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request)
|
||||
response.NewSuccessResponse(page).FPrint(w)
|
||||
return
|
||||
}
|
||||
if query.Kind != "config" && query.Kind != "operation" && query.Kind != "mail" {
|
||||
if query.Kind != "operation" && query.Kind != "mail" {
|
||||
writeConfigError(w, config.ErrInvalidAuditQuery)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -223,31 +223,12 @@ func configSectionRoute(path, operation string) bool {
|
||||
}
|
||||
func configAuditRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
controllers.EnsureConfigRequestID(w, r)
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
controllers.ConfigAuditQuery(ctx, w, r)
|
||||
case http.MethodPost:
|
||||
controllers.ExportConfigAudit(ctx, w, r)
|
||||
default:
|
||||
allowRouteMethod(w, r, http.MethodGet, http.MethodPost)
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
http.Error(w, "独立配置审计已停用,请查看运行日志", http.StatusGone)
|
||||
}
|
||||
|
||||
func configAuditSubRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
controllers.EnsureConfigRequestID(w, r)
|
||||
path := strings.TrimSuffix(r.URL.Path, "/")
|
||||
switch path {
|
||||
case "/api/v1/audits/config/export":
|
||||
if !allowRouteMethod(w, r, http.MethodPost) {
|
||||
return
|
||||
}
|
||||
controllers.ExportConfigAuditFormal(ctx, w, r)
|
||||
default:
|
||||
if !allowRouteMethod(w, r, http.MethodGet) {
|
||||
return
|
||||
}
|
||||
controllers.GetConfigAuditDetail(ctx, w, r)
|
||||
}
|
||||
configAuditRoute(ctx, w, r)
|
||||
}
|
||||
|
||||
func databaseMigrationSubRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -76,15 +76,6 @@ func startConfigManager() error {
|
||||
return nil
|
||||
}
|
||||
security := config.ConfigSecurity{}
|
||||
if store := logstore.Current(); store != nil {
|
||||
audit := &logstore.AuditWriter{Store: store}
|
||||
importCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
if err := audit.ImportLegacy(importCtx, config.ROOT_PATH); err != nil {
|
||||
return fmt.Errorf("迁移旧配置审计失败,源文件已保留: %w", err)
|
||||
}
|
||||
security.Audit = audit
|
||||
}
|
||||
manager, err := config.NewConfigManagerWithSecurity(config.ROOT_PATH, config.ConfigLoaderOptions{Validation: config.RuntimeValidationOptions{
|
||||
HasSuperAdmin: true,
|
||||
Production: !config.DevelopmentMode,
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db/migration"
|
||||
"git.pcux.cn/xuans/Smail/services/logstore"
|
||||
)
|
||||
|
||||
// PortableExecutor 使用版本化目标结构复制业务数据,不拼接用户提供的 SQL。
|
||||
@@ -221,13 +220,8 @@ func (e *PortableExecutor) Run(ctx context.Context, plan Plan, update func(Statu
|
||||
if err := update(StatusSwitching); err != nil {
|
||||
return err
|
||||
}
|
||||
// 源库此时仍持有写锁,切换审计必须写入已复制且校验完成的目标库。
|
||||
logStore, err := logstore.New(target, dialect)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switchContext := config.WithMigrationAuditWriter(ctx, &logstore.AuditWriter{Store: logStore})
|
||||
err = e.switchConfig(switchContext, plan.expectedGeneration, plan.targetConfig, plan.actor)
|
||||
// 配置切换结果只写运行日志,不再依赖源库或目标库保存独立审计。
|
||||
err = e.switchConfig(ctx, plan.expectedGeneration, plan.targetConfig, plan.actor)
|
||||
if err != nil && isKnownSwitchRejection(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -5,28 +5,13 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/utils/logpolicy"
|
||||
)
|
||||
|
||||
type AuditWriter struct{ Store *Store }
|
||||
|
||||
func (w *AuditWriter) WriteConfigAudit(ctx context.Context, event config.ConfigAuditEvent) error {
|
||||
if w == nil || w.Store == nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
normalized, err := config.NormalizeConfigAuditEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err := json.Marshal(normalized)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Store.Write(ctx, Entry{
|
||||
ID: normalized.ID, CreatedAt: normalized.CreatedAt, Kind: "config",
|
||||
ActorID: normalized.ActorID, Action: string(normalized.Operation),
|
||||
Section: string(normalized.Section), Result: normalized.Result, Payload: payload,
|
||||
})
|
||||
// 旧调用方也转到运行日志,避免再次接入数据库审计。
|
||||
return (config.RuntimeConfigLogWriter{}).WriteConfigAudit(ctx, event)
|
||||
}
|
||||
|
||||
func (w *AuditWriter) Query(ctx context.Context, query config.ConfigAuditQuery) ([]config.ConfigAuditEvent, error) {
|
||||
@@ -70,15 +55,7 @@ func (w *AuditWriter) ExportCSV(ctx context.Context, query config.ConfigAuditQue
|
||||
return config.ExportConfigAuditCSV(items)
|
||||
}
|
||||
|
||||
func (w *AuditWriter) ImportLegacy(ctx context.Context, root string) error {
|
||||
if w == nil || w.Store == nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
legacy, err := config.NewFileAuditWriter(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return legacy.Visit(ctx, logpolicy.Cutoff(w.Store.now()), func(event config.ConfigAuditEvent) error {
|
||||
return w.WriteConfigAudit(ctx, event)
|
||||
})
|
||||
func (w *AuditWriter) ImportLegacy(_ context.Context, _ string) error {
|
||||
// 独立配置审计已停用,历史文件留在原处,不再导入数据库或重复输出。
|
||||
return nil
|
||||
}
|
||||
@@ -92,6 +92,9 @@ func replacePlaceholders(statement string, index *int) string {
|
||||
|
||||
// Write 只接受业务点构造的脱敏载荷,禁止直接传入 HTTP 请求或邮件正文。
|
||||
func (s *Store) Write(ctx context.Context, entry Entry) error {
|
||||
if entry.Kind == "config" {
|
||||
return ErrInvalid
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user