完善配置审计与数据库迁移安全链路
This commit is contained in:
1 parent
84440d8761
commit
3af1fc4147
117 files changed
+4703
-1110
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func exportAuditCSV(events []ConfigAuditEvent) ([]byte, error) {
|
||||
var builder strings.Builder
|
||||
writer := csv.NewWriter(&builder)
|
||||
if err := writer.Write([]string{"id", "operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, event := range events {
|
||||
if err := writer.Write([]string{
|
||||
event.ID,
|
||||
string(event.Operation),
|
||||
string(event.Section),
|
||||
event.ActorID,
|
||||
event.ActorRole,
|
||||
fmt.Sprintf("%d", event.FromGeneration),
|
||||
fmt.Sprintf("%d", event.ToGeneration),
|
||||
fmt.Sprintf("%d", event.TargetGeneration),
|
||||
strings.Join(event.ChangedFields, ","),
|
||||
event.ConfirmationMethod,
|
||||
event.Result,
|
||||
event.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
writer.Flush()
|
||||
if err := writer.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(builder.String()), nil
|
||||
}
|
||||
@@ -3,12 +3,44 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrAuditQueryUnavailable = errors.New("configuration audit query unavailable")
|
||||
var (
|
||||
ErrAuditQueryUnavailable = errors.New("configuration audit query unavailable")
|
||||
ErrInvalidAuditQuery = errors.New("configuration audit query is invalid")
|
||||
ErrInvalidAuditFormat = errors.New("configuration audit export format is invalid")
|
||||
ErrAuditNotFound = errors.New("configuration audit event not found")
|
||||
)
|
||||
|
||||
// ValidateConfigAuditQuery 校验审计查询条件,拒绝未知或越界参数。
|
||||
func ValidateConfigAuditQuery(query ConfigAuditQuery) error {
|
||||
if query.ID != "" && !validAuditEventID(query.ID) {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if query.Limit < 0 || query.Limit > 1000 {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if !query.From.IsZero() && !query.To.IsZero() && query.From.After(query.To) {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if len(query.ActorID) > 128 || strings.ContainsAny(query.ActorID, "\r\n") {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if query.Operation != "" && query.Operation != ConfigOperationUpdate && query.Operation != ConfigOperationRollback && query.Operation != ConfigOperationMigration {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if query.Section != "" && query.Section != SectionRoot && query.Section != SectionSMTP && query.Section != SectionEmail && query.Section != SectionSSL && query.Section != SectionPlugins {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AuditQuery 读取配置审计的只读接口,写入器不支持查询时明确拒绝。
|
||||
func (m *ConfigManager) AuditQuery(ctx context.Context, query ConfigAuditQuery) ([]ConfigAuditEvent, error) {
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.security.Audit == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
@@ -23,6 +55,9 @@ func (m *ConfigManager) AuditQuery(ctx context.Context, query ConfigAuditQuery)
|
||||
|
||||
// ExportAuditJSON 导出脱敏配置审计;导出内容由审计存储实现生成。
|
||||
func (m *ConfigManager) ExportAuditJSON(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.security.Audit == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
@@ -37,6 +72,9 @@ func (m *ConfigManager) ExportAuditJSON(ctx context.Context, query ConfigAuditQu
|
||||
|
||||
// ExportAuditCSV 导出脱敏配置审计;导出内容由审计存储实现生成。
|
||||
func (m *ConfigManager) ExportAuditCSV(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.security.Audit == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
@@ -48,3 +86,19 @@ func (m *ConfigManager) ExportAuditCSV(ctx context.Context, query ConfigAuditQue
|
||||
}
|
||||
return reader.ExportCSV(ctx, query)
|
||||
}
|
||||
|
||||
// AuditDetail 按稳定标识查询一条配置审计事件。
|
||||
func (m *ConfigManager) AuditDetail(ctx context.Context, id string) (ConfigAuditEvent, error) {
|
||||
id = strings.TrimSpace(id)
|
||||
if !validAuditEventID(id) {
|
||||
return ConfigAuditEvent{}, ErrInvalidAuditQuery
|
||||
}
|
||||
items, err := m.AuditQuery(ctx, ConfigAuditQuery{ID: id, Limit: 1})
|
||||
if err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return ConfigAuditEvent{}, ErrAuditNotFound
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestValidateConfigAuditQueryAcceptsSupportedFilters(t *testing.T) {
|
||||
query := ConfigAuditQuery{
|
||||
From: time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC),
|
||||
To: time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC),
|
||||
ActorID: "42",
|
||||
Operation: ConfigOperationUpdate,
|
||||
Section: SectionSMTP,
|
||||
Limit: 1000,
|
||||
}
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
t.Fatalf("valid audit query rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfigAuditQueryRejectsAmbiguousFilters(t *testing.T) {
|
||||
tests := []ConfigAuditQuery{
|
||||
{Limit: -1},
|
||||
{Limit: 1001},
|
||||
{From: time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC), To: time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC)},
|
||||
{ActorID: "bad\nactor"},
|
||||
{Operation: ConfigOperation("unknown")},
|
||||
{Section: Section("unknown")},
|
||||
}
|
||||
for index, query := range tests {
|
||||
if err := ValidateConfigAuditQuery(query); !errors.Is(err, ErrInvalidAuditQuery) {
|
||||
t.Fatalf("case %d returned %v, want ErrInvalidAuditQuery", index, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerRejectsInvalidAuditQueryBeforeStorageAccess(t *testing.T) {
|
||||
var manager *ConfigManager
|
||||
_, err := manager.AuditQuery(nil, ConfigAuditQuery{Limit: 1001})
|
||||
if !errors.Is(err, ErrInvalidAuditQuery) {
|
||||
t.Fatalf("AuditQuery returned %v, want ErrInvalidAuditQuery", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerAuditDetailUsesStableIDAndRedactsExport(t *testing.T) {
|
||||
writer := &MemoryAuditWriter{}
|
||||
manager := &ConfigManager{security: ConfigSecurity{Audit: writer}}
|
||||
event := ConfigAuditEvent{
|
||||
Operation: ConfigOperationUpdate,
|
||||
Section: SectionSMTP,
|
||||
ActorID: "42",
|
||||
ActorRole: "super_admin",
|
||||
FromGeneration: 1,
|
||||
ToGeneration: 2,
|
||||
ChangedFields: []string{"relay.password"},
|
||||
ConfirmationMethod: "recent_auth",
|
||||
Result: "success",
|
||||
CreatedAt: time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC),
|
||||
}
|
||||
if err := writer.WriteConfigAudit(nil, event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := writer.Events()
|
||||
if len(items) != 1 || items[0].ID == "" || !strings.HasPrefix(items[0].ID, "audit-") {
|
||||
t.Fatalf("unexpected stable audit ID: %+v", items)
|
||||
}
|
||||
detail, err := manager.AuditDetail(nil, items[0].ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.ID != items[0].ID || detail.ChangedFields[0] != "relay.password" {
|
||||
t.Fatalf("unexpected audit detail: %+v", detail)
|
||||
}
|
||||
if _, err := manager.AuditDetail(nil, "audit-not-valid"); !errors.Is(err, ErrInvalidAuditQuery) {
|
||||
t.Fatalf("invalid audit ID returned %v", err)
|
||||
}
|
||||
if _, err := manager.AuditDetail(nil, "audit-00000000000000000000000000000000"); !errors.Is(err, ErrAuditNotFound) {
|
||||
t.Fatalf("missing audit ID returned %v", err)
|
||||
}
|
||||
jsonData, err := manager.ExportAuditJSON(nil, ConfigAuditQuery{ID: items[0].ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(jsonData), "password-value") || !strings.Contains(string(jsonData), items[0].ID) {
|
||||
t.Fatalf("unexpected audit export: %s", jsonData)
|
||||
}
|
||||
csvData, err := manager.ExportAuditCSV(nil, ConfigAuditQuery{ID: items[0].ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(csvData), "id,operation") || !strings.Contains(string(csvData), items[0].ID) {
|
||||
t.Fatalf("CSV export does not include stable ID: %s", csvData)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package config
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/csv"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -24,6 +27,7 @@ var (
|
||||
)
|
||||
|
||||
type ConfigAuditQuery struct {
|
||||
ID string
|
||||
From time.Time
|
||||
To time.Time
|
||||
ActorID string
|
||||
@@ -119,11 +123,16 @@ func (w *FileAuditWriter) Query(ctx context.Context, query ConfigAuditQuery) ([]
|
||||
result := make([]ConfigAuditEvent, 0, minAuditLimit(limit, 32))
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 4096), 256*1024)
|
||||
lineNumber := 0
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
var event ConfigAuditEvent
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil {
|
||||
return nil, ErrAuditStorageUnavailable
|
||||
}
|
||||
if event.ID == "" {
|
||||
event.ID = legacyAuditEventID(scanner.Bytes(), lineNumber)
|
||||
}
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return nil, ErrAuditStorageUnavailable
|
||||
}
|
||||
@@ -156,12 +165,12 @@ func (w *FileAuditWriter) ExportCSV(ctx context.Context, query ConfigAuditQuery)
|
||||
}
|
||||
var builder strings.Builder
|
||||
writer := csv.NewWriter(&builder)
|
||||
if err := writer.Write([]string{"operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
if err := writer.Write([]string{"id", "operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, event := range events {
|
||||
if err := writer.Write([]string{
|
||||
string(event.Operation), string(event.Section), event.ActorID, event.ActorRole,
|
||||
event.ID, string(event.Operation), string(event.Section), event.ActorID, event.ActorRole,
|
||||
fmt.Sprintf("%d", event.FromGeneration), fmt.Sprintf("%d", event.ToGeneration),
|
||||
fmt.Sprintf("%d", event.TargetGeneration), strings.Join(event.ChangedFields, ","),
|
||||
event.ConfirmationMethod, event.Result, event.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
@@ -203,16 +212,28 @@ func (w *FileAuditWriter) Cleanup(ctx context.Context, now time.Time) (int, erro
|
||||
deleted := 0
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 4096), 256*1024)
|
||||
lineNumber := 0
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
var event ConfigAuditEvent
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil || validateAuditEvent(event) != nil {
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
}
|
||||
if event.ID == "" {
|
||||
event.ID = legacyAuditEventID(scanner.Bytes(), lineNumber)
|
||||
}
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
}
|
||||
if event.CreatedAt.Before(cutoff) {
|
||||
deleted++
|
||||
continue
|
||||
}
|
||||
kept = append(kept, append([]byte(nil), scanner.Bytes()...))
|
||||
data, marshalErr := json.Marshal(event)
|
||||
if marshalErr != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
}
|
||||
kept = append(kept, data)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
@@ -250,21 +271,42 @@ func ensureAuditFile(path string) error {
|
||||
}
|
||||
|
||||
func normalizeAuditEvent(event ConfigAuditEvent) (ConfigAuditEvent, error) {
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
if event.ID == "" {
|
||||
id, err := newAuditEventID()
|
||||
if err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
}
|
||||
event.ID = id
|
||||
}
|
||||
event.ActorID = strings.TrimSpace(event.ActorID)
|
||||
event.ActorRole = strings.TrimSpace(event.ActorRole)
|
||||
event.ConfirmationMethod = strings.ToLower(strings.TrimSpace(event.ConfirmationMethod))
|
||||
event.ChangedFields = append([]string(nil), event.ChangedFields...)
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func newAuditEventID() (string, error) {
|
||||
value := make([]byte, 16)
|
||||
if _, err := rand.Read(value); err != nil {
|
||||
return "", ErrAuditEventInvalid
|
||||
}
|
||||
return "audit-" + hex.EncodeToString(value), nil
|
||||
}
|
||||
|
||||
func legacyAuditEventID(raw []byte, lineNumber int) string {
|
||||
hash := sha256.New()
|
||||
hash.Write(raw)
|
||||
hash.Write([]byte(fmt.Sprintf(":%d", lineNumber)))
|
||||
return "legacy-" + hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
func validateAuditEvent(event ConfigAuditEvent) error {
|
||||
if event.Operation != ConfigOperationUpdate && event.Operation != ConfigOperationRollback && event.Operation != ConfigOperationMigration {
|
||||
return ErrAuditEventInvalid
|
||||
}
|
||||
if strings.TrimSpace(event.ActorID) == "" || strings.TrimSpace(event.ActorRole) == "" || event.FromGeneration == 0 || event.ToGeneration == 0 || event.CreatedAt.IsZero() {
|
||||
if !validAuditEventID(event.ID) || strings.TrimSpace(event.ActorID) == "" || strings.TrimSpace(event.ActorRole) == "" || event.FromGeneration == 0 || event.ToGeneration == 0 || event.CreatedAt.IsZero() {
|
||||
return ErrAuditEventInvalid
|
||||
}
|
||||
if event.Result != "success" && event.Result != "failure" {
|
||||
@@ -282,6 +324,9 @@ func validateAuditEvent(event ConfigAuditEvent) error {
|
||||
}
|
||||
|
||||
func auditEventMatches(event ConfigAuditEvent, query ConfigAuditQuery) bool {
|
||||
if query.ID != "" && event.ID != query.ID {
|
||||
return false
|
||||
}
|
||||
if !query.From.IsZero() && event.CreatedAt.Before(query.From) {
|
||||
return false
|
||||
}
|
||||
@@ -300,6 +345,24 @@ func auditEventMatches(event ConfigAuditEvent, query ConfigAuditQuery) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func validAuditEventID(value string) bool {
|
||||
if len(value) != 38 && len(value) != 71 {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(value, "audit-") {
|
||||
value = strings.TrimPrefix(value, "audit-")
|
||||
} else if strings.HasPrefix(value, "legacy-") {
|
||||
value = strings.TrimPrefix(value, "legacy-")
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
for _, character := range value {
|
||||
if !(character >= '0' && character <= '9') && !(character >= 'a' && character <= 'f') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func auditContextError(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ListBackups 枚举合法备份的代次和时间,不返回内部路径。
|
||||
@@ -28,7 +29,10 @@ func (s *TransactionStore) ListBackups() ([]ConfigBackupInfo, error) {
|
||||
}
|
||||
result := make([]ConfigBackupInfo, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
if strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 || !entry.IsDir() {
|
||||
return nil, ErrTransactionInvalid
|
||||
}
|
||||
runtime, manifest, loadErr := loadConfigBackup(filepath.Join(backupsDirectory, entry.Name()), s.validation)
|
||||
|
||||
@@ -324,7 +324,8 @@ func secureManagerConfigDirectory(root string) (string, error) {
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", ErrManagerStorage
|
||||
}
|
||||
// 鐩綍鏉冮檺 0700 鏄厤缃枃浠剁殑鏈€灏忔潈闄愯姹傦紱G302 灏嗙洰褰曡鍒や负鏂囦欢銆? // #nosec G302
|
||||
// 目录权限 0700 是配置目录的最低要求;G302 将目录误判为文件。
|
||||
// #nosec G302
|
||||
if err := os.Chmod(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -344,7 +345,8 @@ func secureBackupDirectory(root string) (string, error) {
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", ErrManagerStorage
|
||||
}
|
||||
// 澶囦唤鐩綍鍙厑璁告湇鍔¤处鎴疯闂紱G302 灏嗙洰褰曡鍒や负鏂囦欢銆? // #nosec G302
|
||||
// 备份目录只允许服务账户访问;G302 将目录误判为文件。
|
||||
// #nosec G302
|
||||
if err := os.Chmod(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -343,6 +343,12 @@ func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor Audi
|
||||
if err := ValidateRuntimeConfig(candidate, m.options); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, current); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, _, err := m.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -405,7 +411,11 @@ func (m *ConfigManager) PreviewRollback(ctx context.Context, generation uint64,
|
||||
if pending || generation >= currentGeneration {
|
||||
return nil, ErrGenerationConflict
|
||||
}
|
||||
if _, err := m.transactions.LoadBackup(generation); err != nil {
|
||||
backup, err := m.transactions.LoadBackup(generation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, backup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &RollbackPreview{
|
||||
@@ -436,7 +446,11 @@ func (m *ConfigManager) IssueRollbackTicket(ctx context.Context, generation uint
|
||||
if pending || generation >= currentGeneration {
|
||||
return "", ErrGenerationConflict
|
||||
}
|
||||
if _, err := m.transactions.LoadBackup(generation); err != nil {
|
||||
backup, err := m.transactions.LoadBackup(generation)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, backup); err != nil {
|
||||
return "", err
|
||||
}
|
||||
token, err := newRollbackTicketID()
|
||||
@@ -501,6 +515,9 @@ func (m *ConfigManager) rollback(ctx context.Context, generation uint64, actor A
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCandidateGeneration(&candidate, current.Generation+1)
|
||||
candidate.Root.UpdatedAt = time.Now().UTC()
|
||||
if err := ValidateRuntimeConfig(candidate, m.options); err != nil {
|
||||
|
||||
@@ -264,6 +264,91 @@ func TestConfigBackupRejectsTamperedPartition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerRejectsUndecryptableRollbackBackup(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
current := validMySQLRuntimeConfig(t, root)
|
||||
writeRuntimeFixture(t, root, current)
|
||||
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
|
||||
manager, err := NewConfigManager(root, options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updatedEmail := manager.Snapshot().Email
|
||||
updatedEmail.Policy.MaxMessageBytes = 8 << 20
|
||||
payload, err := json.Marshal(updatedEmail)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updated, err := manager.Update(context.Background(), UpdateRequest{
|
||||
Section: SectionEmail, ExpectedGeneration: 1, Payload: payload, Actor: authorizedConfigActor(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := manager.Apply(context.Background(), updated.Generation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("backup entries = %v, err = %v", entries, err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2", entries[0].Name())
|
||||
rootPath := filepath.Join(backupDirectory, "files", "config.json")
|
||||
rootData, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var backupRoot RootConfig
|
||||
if err := ParseStrict(rootData, &backupRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreignRoot := t.TempDir()
|
||||
foreignVault, err := credential.NewVault(foreignRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreignCiphertext, err := foreignVault.Encrypt("foreign-database-secret")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupRoot.Database.Password.Ciphertext = foreignCiphertext
|
||||
rootData, err = json.Marshal(backupRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(rootPath, rootData, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifestPath := filepath.Join(backupDirectory, "backup.json")
|
||||
manifestData, err := os.ReadFile(manifestPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var manifest ConfigBackupManifest
|
||||
if err := ParseStrict(manifestData, &manifest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest.Files["config.json"] = SectionDigest(rootData)
|
||||
manifestData, err = json.Marshal(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(manifestPath, manifestData, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
actor := authorizedConfigActor()
|
||||
if _, err := manager.PreviewRollback(context.Background(), 1, actor); !errors.Is(err, ErrSecretUnavailable) {
|
||||
t.Fatalf("undecryptable backup preview error = %v, want secret unavailable", err)
|
||||
}
|
||||
if _, err := manager.RollbackAuthorized(context.Background(), 1, actor, StepUpConfirmation{Confirmed: true, Method: "webauthn"}); !errors.Is(err, ErrSecretUnavailable) {
|
||||
t.Fatalf("undecryptable backup rollback error = %v, want secret unavailable", err)
|
||||
}
|
||||
if manager.Snapshot().Generation != 2 {
|
||||
t.Fatalf("failed rollback changed active generation to %d", manager.Snapshot().Generation)
|
||||
}
|
||||
}
|
||||
func TestConfigBackupRetentionKeepsFourRecentBackups(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
@@ -782,6 +867,139 @@ func TestConfigBackupScheduleUsesSevenDayInterval(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupScheduleCreatesSameGenerationBackupWhenDue(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
firstAt := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
firstPath, created, err := store.CreateBackupIfDue(runtime, firstAt)
|
||||
if err != nil || !created || firstPath == "" {
|
||||
t.Fatalf("first backup path=%q created=%v err=%v", firstPath, created, err)
|
||||
}
|
||||
secondPath, created, err := store.CreateBackupIfDue(runtime, firstAt.Add(configBackupInterval))
|
||||
if err != nil || !created || secondPath == "" || secondPath == firstPath {
|
||||
t.Fatalf("due backup path=%q created=%v err=%v, want a new backup", secondPath, created, err)
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("same generation backup count=%d, want 2", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupListingAndLoadingIgnoreTemporaryDirectories(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
if _, err := store.CreateBackup(runtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
if err := os.Mkdir(filepath.Join(backupDirectory, ".interrupted.tmp"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := store.ListBackups()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(items) != 1 || items[0].Generation != runtime.Generation {
|
||||
t.Fatalf("backup list = %+v, want generation %d", items, runtime.Generation)
|
||||
}
|
||||
loaded, err := store.LoadBackup(runtime.Generation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Generation != runtime.Generation {
|
||||
t.Fatalf("loaded generation=%d, want %d", loaded.Generation, runtime.Generation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupCleanupKeepsOnlyExpiredValidBackup(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
createdAt := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
if _, err := store.createBackupAt(runtime, createdAt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
if err := cleanupConfigBackups(backupDirectory, createdAt.Add(configBackupMaxAge+time.Nanosecond), store.validation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("backup count=%d, want the only valid backup retained", len(entries))
|
||||
}
|
||||
}
|
||||
func TestConfigBackupRetentionHonorsThirtyDayBoundary(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
first := validRuntimeConfig()
|
||||
if _, err := store.createBackupAt(first, base); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second := cloneRuntimeConfig(first)
|
||||
setCandidateGeneration(&second, 2)
|
||||
if _, err := store.createBackupAt(second, base.Add(configBackupMaxAge)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
entries, err := os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("backup count at exact age=%d, want 2", len(entries))
|
||||
}
|
||||
third := cloneRuntimeConfig(second)
|
||||
setCandidateGeneration(&third, 3)
|
||||
if _, err := store.createBackupAt(third, base.Add(configBackupMaxAge+time.Nanosecond)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err = os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("backup count after expiration=%d, want 2", len(entries))
|
||||
}
|
||||
if _, err := store.LoadBackup(1); !errors.Is(err, ErrNoBackup) {
|
||||
t.Fatalf("expired generation load error=%v, want no backup", err)
|
||||
}
|
||||
if _, err := store.LoadBackup(2); err != nil {
|
||||
t.Fatalf("boundary generation was removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupFailureDoesNotDeleteExistingValidBackup(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
if _, err := store.CreateBackup(runtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid := cloneRuntimeConfig(runtime)
|
||||
invalid.Generation = 0
|
||||
if _, err := store.CreateBackup(invalid); err == nil {
|
||||
t.Fatal("invalid backup unexpectedly succeeded")
|
||||
}
|
||||
if _, err := store.LoadBackup(runtime.Generation); err != nil {
|
||||
t.Fatalf("existing valid backup was removed after failure: %v", err)
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("backup count after failed creation=%d, want 1", len(entries))
|
||||
}
|
||||
}
|
||||
func TestConfigManagerStatusReportsPendingRestartServices(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
|
||||
@@ -29,6 +29,8 @@ var (
|
||||
ErrMigrationTargetExists = errors.New("partitioned configuration files already exist")
|
||||
)
|
||||
|
||||
const legacyMigrationSourceCopy = "config.json.legacy"
|
||||
|
||||
type LegacyMigrationPlan struct {
|
||||
SourceDigest string
|
||||
TargetGeneration uint64
|
||||
@@ -178,6 +180,9 @@ func MigrateLegacyConfig(root string, options RuntimeValidationOptions) (LegacyM
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if err := preserveLegacyMigrationSource(directory, legacyData); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
@@ -324,8 +329,24 @@ func equivalentLegacyMigrationSection(section Section, expected, existing []byte
|
||||
}
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig.Meta, existingConfig.Meta), nil
|
||||
case SectionEmail, SectionSSL:
|
||||
return false, nil
|
||||
case SectionEmail:
|
||||
var expectedConfig, existingConfig EmailSectionConfig
|
||||
if err := ParseStrict(expected, &expectedConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := ParseStrict(existing, &existingConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig, existingConfig), nil
|
||||
case SectionSSL:
|
||||
var expectedConfig, existingConfig SSLSectionConfig
|
||||
if err := ParseStrict(expected, &expectedConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := ParseStrict(existing, &existingConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig, existingConfig), nil
|
||||
default:
|
||||
return false, ErrInvalidSection
|
||||
}
|
||||
@@ -846,3 +867,17 @@ func writeLegacyMigrationBackup(directory string, data []byte, now time.Time) (s
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func preserveLegacyMigrationSource(directory string, data []byte) error {
|
||||
existing, err := readManagerConfigFile(directory, legacyMigrationSourceCopy)
|
||||
if err == nil {
|
||||
if bytes.Equal(existing, data) {
|
||||
return nil
|
||||
}
|
||||
return ErrMigrationTargetExists
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
return atomicfile.Write(filepath.Join(directory, legacyMigrationSourceCopy), data, 0600)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
@@ -63,6 +64,13 @@ func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
if !bytes.Equal(backup, original) {
|
||||
t.Fatal("legacy backup does not match the original configuration")
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "config", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(legacyCopy, original) {
|
||||
t.Fatal("legacy source copy does not match the original configuration")
|
||||
}
|
||||
loader := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
runtime, err := loader.Load()
|
||||
if err != nil {
|
||||
@@ -71,6 +79,16 @@ func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
if runtime.Root.Database.LocalRef != "smail.db" || runtime.Plugins.Plugins["wechat_push"].Secret.Ciphertext == "" {
|
||||
t.Fatalf("unexpected migrated runtime: %+v", runtime)
|
||||
}
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := manager.Load(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if manager.Snapshot().Generation != result.Generation {
|
||||
t.Fatalf("manager generation = %d, want %d", manager.Snapshot().Generation, result.Generation)
|
||||
}
|
||||
for _, name := range []string{"config.json", "smtp.json", "email.json", "ssl.json", "plugins.json"} {
|
||||
data, readErr := os.ReadFile(filepath.Join(root, "config", name))
|
||||
if readErr != nil {
|
||||
@@ -82,6 +100,62 @@ func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigResumesAfterNonSecretPartitionWrite(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "1",
|
||||
"SSLPrivateKeyPath": "./config/ssl/private.key",
|
||||
"SSLPublicKeyPath": "./config/ssl/public.crt",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
var legacy legacyMigrationConfig
|
||||
if err := json.Unmarshal(original, &legacy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, manualFields, err := legacyRuntimeConfig(root, legacy)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(manualFields) != 0 {
|
||||
t.Fatalf("unexpected manual fields: %+v", manualFields)
|
||||
}
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
directory := filepath.Join(root, "config")
|
||||
for _, section := range []Section{SectionEmail, SectionSSL} {
|
||||
name, ok := FixedSectionFile(section)
|
||||
if !ok {
|
||||
t.Fatalf("missing fixed file for section %q", section)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(directory, name), files[name], 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(directory, legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(legacyCopy, original) {
|
||||
t.Fatal("resumed migration did not preserve the legacy source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanLegacyMigrationReportsUnknownFieldsWithoutOverwritingConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
@@ -177,6 +251,123 @@ func TestMigrateLegacyConfigReencryptsExistingLegacyCredential(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsCorruptedLegacyCredential(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
"tgBotToken": "enc:v1:not-a-valid-ciphertext",
|
||||
})
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationUnsafeValue) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
current, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("corrupted credential migration overwrote the legacy configuration")
|
||||
}
|
||||
for _, name := range []string{"smtp.json", "email.json", "ssl.json", "plugins.json"} {
|
||||
if _, err := os.Stat(filepath.Join(root, "config", name)); !os.IsNotExist(err) {
|
||||
t.Fatalf("corrupted credential migration created %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsConflictingPreservedSource(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
legacyCopyPath := filepath.Join(root, "config", legacyMigrationSourceCopy)
|
||||
conflictingCopy := []byte(`{"dbDSN":"different.db","dbType":"sqlite"}`)
|
||||
if err := os.WriteFile(legacyCopyPath, conflictingCopy, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationTargetExists) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
current, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("conflicting preserved source changed the legacy configuration")
|
||||
}
|
||||
storedCopy, err := os.ReadFile(legacyCopyPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(storedCopy, conflictingCopy) {
|
||||
t.Fatal("conflicting preserved source was overwritten")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsRepeatedMigrationAfterSuccess(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rootPath := filepath.Join(root, "config", "config.json")
|
||||
migrated, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Equal(migrated, original) {
|
||||
t.Fatal("successful migration did not replace the legacy root")
|
||||
}
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationNotRequired) {
|
||||
t.Fatalf("repeated migration error = %v", err)
|
||||
}
|
||||
after, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after, migrated) {
|
||||
t.Fatal("repeated migration changed the partitioned root")
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "config", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(legacyCopy, original) {
|
||||
t.Fatal("repeated migration changed the preserved legacy source")
|
||||
}
|
||||
}
|
||||
func TestMigrateLegacyConfigResumesWithEquivalentExistingSecretSection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
legacy := map[string]any{
|
||||
|
||||
@@ -143,6 +143,27 @@ func OpenStoredSecret(vault *credential.Vault, stored StoredSecret) (string, err
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
// ValidateRuntimeSecrets 验证运行时配置中的持久化 Secret 可以由当前密钥解密。
|
||||
func ValidateRuntimeSecrets(vault *credential.Vault, runtime RuntimeConfig) error {
|
||||
for _, stored := range []StoredSecret{runtime.Root.Database.Password, runtime.SMTP.Relay.Password} {
|
||||
if !stored.Configured() {
|
||||
continue
|
||||
}
|
||||
if _, err := OpenStoredSecret(vault, stored); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, plugin := range runtime.Plugins.Plugins {
|
||||
if !plugin.Secret.Configured() {
|
||||
continue
|
||||
}
|
||||
if _, err := OpenStoredSecret(vault, plugin.Secret); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateStoredSecret(stored StoredSecret, required bool) error {
|
||||
if !stored.Configured() {
|
||||
if stored.Ciphertext != "" || stored.Version != 0 || !stored.UpdatedAt.IsZero() || required {
|
||||
|
||||
@@ -135,3 +135,41 @@ func TestSecretJSONOutputIsRedacted(t *testing.T) {
|
||||
t.Fatal("secret status exposed ciphertext")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRuntimeSecretsChecksPluginSecrets(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pluginSecret, err := ApplySecretChange(vault, StoredSecret{}, SecretChange{
|
||||
Operation: SecretReplace,
|
||||
Value: &SecretInput{Plaintext: "plugin-secret"},
|
||||
}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime := validRuntimeConfig()
|
||||
plugin := runtime.Plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = pluginSecret
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
if err := ValidateRuntimeSecrets(vault, runtime); err != nil {
|
||||
t.Fatalf("valid plugin secret was rejected: %v", err)
|
||||
}
|
||||
|
||||
tampered := runtime
|
||||
tamperedPlugin := tampered.Plugins.Plugins["telegram_push"]
|
||||
last := len(tamperedPlugin.Secret.Ciphertext) - 1
|
||||
if last < 0 {
|
||||
t.Fatal("plugin secret ciphertext is empty")
|
||||
}
|
||||
replacement := byte('A')
|
||||
if tamperedPlugin.Secret.Ciphertext[last] == replacement {
|
||||
replacement = 'B'
|
||||
}
|
||||
tamperedPlugin.Secret.Ciphertext = tamperedPlugin.Secret.Ciphertext[:last] + string(replacement)
|
||||
tampered.Plugins.Plugins["telegram_push"] = tamperedPlugin
|
||||
if err := ValidateRuntimeSecrets(vault, tampered); !errors.Is(err, ErrSecretUnavailable) {
|
||||
t.Fatalf("tampered plugin secret error = %v, want ErrSecretUnavailable", err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -25,6 +26,7 @@ const (
|
||||
)
|
||||
|
||||
type ConfigAuditEvent struct {
|
||||
ID string `json:"id"`
|
||||
Operation ConfigOperation `json:"operation"`
|
||||
Section Section `json:"section,omitempty"`
|
||||
ActorID string `json:"actorId"`
|
||||
@@ -37,7 +39,6 @@ type ConfigAuditEvent struct {
|
||||
Result string `json:"result"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type ConfigAuditWriter interface {
|
||||
WriteConfigAudit(context.Context, ConfigAuditEvent) error
|
||||
}
|
||||
@@ -64,12 +65,63 @@ func (w *MemoryAuditWriter) WriteConfigAudit(_ context.Context, event ConfigAudi
|
||||
if w == nil {
|
||||
return ErrAuditWrite
|
||||
}
|
||||
var err error
|
||||
event, err = normalizeAuditEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
event.ChangedFields = append([]string(nil), event.ChangedFields...)
|
||||
w.mu.Lock()
|
||||
w.events = append(w.events, event)
|
||||
w.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
func (w *MemoryAuditWriter) Query(ctx context.Context, query ConfigAuditQuery) ([]ConfigAuditEvent, error) {
|
||||
if err := auditContextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if w == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
limit := query.Limit
|
||||
if limit <= 0 {
|
||||
limit = 1000
|
||||
}
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
result := make([]ConfigAuditEvent, 0, minAuditLimit(limit, len(w.events)))
|
||||
for _, event := range w.events {
|
||||
if !auditEventMatches(event, query) {
|
||||
continue
|
||||
}
|
||||
copyEvent := event
|
||||
copyEvent.ChangedFields = append([]string(nil), event.ChangedFields...)
|
||||
result = append(result, copyEvent)
|
||||
if len(result) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (w *MemoryAuditWriter) ExportJSON(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
events, err := w.Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(events)
|
||||
}
|
||||
|
||||
func (w *MemoryAuditWriter) ExportCSV(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
events, err := w.Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return exportAuditCSV(events)
|
||||
}
|
||||
|
||||
func (w *MemoryAuditWriter) Events() []ConfigAuditEvent {
|
||||
if w == nil {
|
||||
|
||||
@@ -594,7 +594,7 @@ func (s *TransactionStore) CreateBackupIfDue(runtime RuntimeConfig, now time.Tim
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if found && (latest.Generation == runtime.Generation || now.Before(latest.CreatedAt.Add(configBackupInterval))) {
|
||||
if found && now.Before(latest.CreatedAt.Add(configBackupInterval)) {
|
||||
return filepath.Join(s.root, "config", "backups-v2", latest.ID), false, nil
|
||||
}
|
||||
path, err := s.createBackupAt(runtime, now)
|
||||
@@ -728,10 +728,10 @@ func (s *TransactionStore) LoadBackup(generation uint64) (RuntimeConfig, error)
|
||||
var selectedAt time.Time
|
||||
found := false
|
||||
for _, entry := range entries {
|
||||
if strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 || !entry.IsDir() {
|
||||
if strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
return RuntimeConfig{}, ErrTransactionInvalid
|
||||
}
|
||||
if !transactionIDPattern.MatchString(entry.Name()) {
|
||||
|
||||
Reference in new issue
Block a user