1 parent
d92dfdacff
commit
f68c367042
34 files changed
+2121
-74
No files matched your search
@@ -3,14 +3,14 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrDatabaseMigrationTarget = errors.New("database migration target is invalid")
|
||||
|
||||
// SwitchDatabase commits only the database name or SQLite local reference.
|
||||
// It deliberately does not open a connection or restart the service; the
|
||||
// caller must complete the physical migration before invoking this method.
|
||||
// SwitchDatabase 提交已完成复制与校验的目标连接,保留旧库并标记重启。
|
||||
// 普通配置编辑不能绕过本迁移入口切换数据库类型。
|
||||
func (m *ConfigManager) SwitchDatabase(ctx context.Context, expectedGeneration uint64, target DatabaseConfig, actor AuditActor) error {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return err
|
||||
@@ -42,14 +42,7 @@ func (m *ConfigManager) SwitchDatabase(ctx context.Context, expectedGeneration u
|
||||
if err := ValidateDatabaseConfig(target); err != nil {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
if !sameDatabaseConnection(current.Root.Database, target) {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
if target.Type == DatabaseTypeSQLite {
|
||||
if target.LocalRef == current.Root.Database.LocalRef {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
} else if target.Name == current.Root.Database.Name {
|
||||
if SameDatabaseLocation(current.Root.Database, target) {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
|
||||
@@ -95,6 +88,9 @@ func (m *ConfigManager) SwitchDatabase(ctx context.Context, expectedGeneration u
|
||||
if target.Type == DatabaseTypeSQLite {
|
||||
changedField = "root.database.localRef"
|
||||
}
|
||||
if !sameDatabaseConnection(current.Root.Database, target) {
|
||||
changedField = "root.database"
|
||||
}
|
||||
return m.writeAudit(ctx, ConfigAuditEvent{
|
||||
Operation: ConfigOperationMigration,
|
||||
Section: SectionRoot,
|
||||
@@ -109,6 +105,20 @@ func (m *ConfigManager) SwitchDatabase(ctx context.Context, expectedGeneration u
|
||||
})
|
||||
}
|
||||
|
||||
// SameDatabaseLocation 不把用户名或密码不同误认为不同数据库。
|
||||
func SameDatabaseLocation(current, target DatabaseConfig) bool {
|
||||
current = NormalizeDatabaseConfig(current)
|
||||
target = NormalizeDatabaseConfig(target)
|
||||
if current.Type != target.Type {
|
||||
return false
|
||||
}
|
||||
if current.Type == DatabaseTypeSQLite {
|
||||
return strings.EqualFold(current.LocalRef, target.LocalRef)
|
||||
}
|
||||
return strings.EqualFold(current.Host, target.Host) && current.Port == target.Port &&
|
||||
strings.EqualFold(current.Name, target.Name)
|
||||
}
|
||||
|
||||
func sameDatabaseConnection(current, target DatabaseConfig) bool {
|
||||
if current.Type != target.Type ||
|
||||
current.Host != target.Host ||
|
||||
|
||||
@@ -2,8 +2,11 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConfigManagerSwitchDatabaseCommitsSQLiteReference(t *testing.T) {
|
||||
@@ -36,6 +39,43 @@ func TestConfigManagerSwitchDatabaseCommitsSQLiteReference(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerSwitchAcrossTypesPersistsAndRedactsAudit(t *testing.T) {
|
||||
for _, kind := range []DatabaseType{DatabaseTypeMySQL, DatabaseTypePostgreSQL} {
|
||||
t.Run(string(kind), func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
|
||||
manager, err := NewConfigManager(root, options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret, err := ApplySecretChange(manager.vault, StoredSecret{}, SecretChange{Operation: SecretReplace, Value: &SecretInput{Plaintext: "migration-test-secret"}}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := NormalizeDatabaseConfig(DatabaseConfig{Type: kind, Host: "127.0.0.1", Username: "review", Name: "smail_next", Password: secret})
|
||||
if err := manager.SwitchDatabase(context.Background(), 1, target, authorizedConfigActor()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reloaded, err := NewConfigManager(root, options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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})
|
||||
if err != nil || len(events) != 1 {
|
||||
t.Fatal("migration audit missing")
|
||||
}
|
||||
data, _ := json.Marshal(events)
|
||||
if strings.Contains(string(data), "migration-test-secret") || strings.Contains(string(data), secret.Ciphertext) || events[0].ChangedFields[0] != "root.database" {
|
||||
t.Fatal("migration audit disclosed secret or omitted connection change")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerSwitchDatabaseRejectsImmutableChangesAndStaleGeneration(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
|
||||
@@ -101,9 +101,12 @@ func compatibilityDatabase(database DatabaseConfig, vault *credential.Vault) (st
|
||||
settings.User, settings.Passwd = database.Username, password
|
||||
settings.Net, settings.Addr, settings.DBName = "tcp", address, database.Name
|
||||
settings.ParseTime = true
|
||||
// 与迁移连接保持相同时区,避免重启后 TIMESTAMP 会话到期时间偏移。
|
||||
settings.Params = map[string]string{"charset": "utf8mb4", "time_zone": "'+00:00'"}
|
||||
return DBTypeMySQL, settings.FormatDSN(), nil
|
||||
case DatabaseTypePostgreSQL:
|
||||
uri := url.URL{Scheme: "postgres", User: url.UserPassword(database.Username, password), Host: address, Path: "/" + database.Name}
|
||||
uri.RawQuery = "sslmode=require&timezone=UTC&search_path=public"
|
||||
return DBTypePostgres, uri.String(), nil
|
||||
default:
|
||||
return "", "", fmt.Errorf("不支持的数据库类型")
|
||||
|
||||
@@ -2,11 +2,48 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func TestPartitionedDatabaseConnectionPreservesMigrationTimezone(t *testing.T) {
|
||||
vault, err := credential.NewVault(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret, err := ApplySecretChange(vault, StoredSecret{}, SecretChange{Operation: SecretReplace, Value: &SecretInput{Plaintext: "test-password"}}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, kind := range []DatabaseType{DatabaseTypeMySQL, DatabaseTypePostgreSQL} {
|
||||
_, dsn, err := compatibilityDatabase(DatabaseConfig{Type: kind, Host: "::1", Port: 3306, Username: "test", Name: "test", Password: secret}, vault)
|
||||
if err != nil {
|
||||
t.Fatal("runtime connection failed")
|
||||
}
|
||||
if kind == DatabaseTypeMySQL {
|
||||
settings, err := mysql.ParseDSN(dsn)
|
||||
_, rawQuery, _ := strings.Cut(dsn, "?")
|
||||
query, queryErr := url.ParseQuery(rawQuery)
|
||||
// charset 会被驱动解析为内部选项,不再保留在 Params 中。
|
||||
if err != nil || queryErr != nil || !settings.ParseTime || settings.Params["time_zone"] != "'+00:00'" || query.Get("charset") != "utf8mb4" {
|
||||
t.Fatal("MySQL runtime timezone differs from migration")
|
||||
}
|
||||
} else {
|
||||
uri, err := url.Parse(dsn)
|
||||
if err != nil || uri.Query().Get("timezone") != "UTC" || uri.Query().Get("search_path") != "public" || uri.Query().Get("sslmode") != "require" {
|
||||
t.Fatal("PostgreSQL runtime options differ from migration")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartitionedCompatibilityStartupAndWriteProtection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
oldRoot, oldArgs, oldDevelopment := ROOT_PATH, os.Args, DevelopmentMode
|
||||
|
||||
Reference in new issue
Block a user