修复数据库维护凭据与不完整备份误报并同步首次初始化部署说明
This commit is contained in:
1 parent
5509a611f0
commit
f3cce10664
8 files changed
+302
-29
No files matched your search
@@ -38,6 +38,9 @@ type backupManifestFile struct {
|
||||
}
|
||||
|
||||
func createBackup(ctx context.Context, cfg config.Config, configPath, output string) error {
|
||||
if err := validateBackupDriver(cfg.Database.Driver); err != nil {
|
||||
return err
|
||||
}
|
||||
output = filepath.Clean(output)
|
||||
if err := ensureBackupDestination(output); err != nil {
|
||||
return err
|
||||
@@ -207,6 +210,9 @@ func sqliteDatabasePath(dsn string) (string, bool, error) {
|
||||
}
|
||||
|
||||
func validateRestoreCompatibility(current, backup config.Config) error {
|
||||
if err := validateBackupDriver(backup.Database.Driver); err != nil {
|
||||
return err
|
||||
}
|
||||
if current.Database.Driver != backup.Database.Driver {
|
||||
return fmt.Errorf("备份数据库类型(%s)与当前配置数据库类型(%s)不一致,拒绝恢复", backup.Database.Driver, current.Database.Driver)
|
||||
}
|
||||
@@ -248,7 +254,7 @@ func validateBackupSecretStore(ctx context.Context, directory string, cfg config
|
||||
if secretsExists {
|
||||
return errors.New("敏感配置包含 secrets.json,但缺少 master.key")
|
||||
}
|
||||
if cfg.SetupReady {
|
||||
if cfg.SetupReady || strings.TrimSpace(cfg.Database.CredentialRef) != "" {
|
||||
return errors.New("已初始化配置缺少 master.key")
|
||||
}
|
||||
return nil
|
||||
@@ -268,7 +274,7 @@ func validateBackupSecretStore(ctx context.Context, directory string, cfg config
|
||||
}
|
||||
available[item.Reference] = struct{}{}
|
||||
}
|
||||
for _, reference := range []string{cfg.DNS.CredentialRef, cfg.Notification.HTTPCredentialRef, cfg.Notification.SMTPCredentialRef} {
|
||||
for _, reference := range []string{cfg.Database.CredentialRef, cfg.DNS.CredentialRef, cfg.Notification.HTTPCredentialRef, cfg.Notification.SMTPCredentialRef} {
|
||||
reference = strings.TrimSpace(reference)
|
||||
if reference == "" {
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
func validateBackupDriver(driver string) error {
|
||||
switch driver {
|
||||
case "sqlite", "mysql":
|
||||
return nil
|
||||
case "postgres":
|
||||
return errors.New("PostgreSQL 完整备份与恢复尚未实现,拒绝生成或恢复不含数据库的备份;请使用数据库原生工具并单独保护配置、加密凭据和证书")
|
||||
default:
|
||||
return errors.New("当前数据库类型不支持备份与恢复")
|
||||
}
|
||||
}
|
||||
|
||||
// 返回值仅供本次维护操作连接数据库,禁止写入配置、备份清单或日志。
|
||||
func resolveMaintenanceDatabase(ctx context.Context, cfg config.DatabaseConfig, directory string) (config.DatabaseConfig, error) {
|
||||
if err := database.ValidateConfig(cfg); err != nil {
|
||||
return config.DatabaseConfig{}, errors.New("数据库维护配置无效,请检查数据库类型与加密凭据引用")
|
||||
}
|
||||
if cfg.Driver == "sqlite" {
|
||||
return cfg, nil
|
||||
}
|
||||
store, err := secretstore.Open(directory)
|
||||
if err != nil {
|
||||
return config.DatabaseConfig{}, errors.New("无法打开数据库维护凭据存储,请检查目录及主密钥权限")
|
||||
}
|
||||
resolved, err := database.ResolveConfig(ctx, cfg, store)
|
||||
if err != nil {
|
||||
return config.DatabaseConfig{}, errors.New("无法读取数据库维护账号,请检查加密凭据及主密钥")
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
func TestUnsupportedBackupDriverFailsBeforeFilesystemChanges(t *testing.T) {
|
||||
for _, driver := range []string{"postgres", "unknown", ""} {
|
||||
t.Run(driver, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := config.Config{Database: config.DatabaseConfig{Driver: driver}}
|
||||
output := filepath.Join(root, "not-created", "backup")
|
||||
err := createBackup(t.Context(), cfg, filepath.Join(root, "scert.json"), output)
|
||||
if err == nil || !strings.Contains(err.Error(), "备份") {
|
||||
t.Fatal("不支持的数据库不能报告备份成功")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Dir(output)); !os.IsNotExist(err) {
|
||||
t.Fatal("拒绝备份前不应创建输出目录")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRestoreFailsBeforeReplacingExistingConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
input := filepath.Join(root, "input")
|
||||
if err := os.Mkdir(input, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := config.Default(root)
|
||||
// 显式覆盖路径,避免 Linux 测试使用正式安装目录。
|
||||
cfg.Paths = config.PathsConfig{
|
||||
Install: root, Config: filepath.Join(root, "conf"), Data: filepath.Join(root, "data"),
|
||||
Certificate: filepath.Join(root, "ssl"), Log: filepath.Join(root, "log"), Backup: filepath.Join(root, "backups"),
|
||||
}
|
||||
cfg.Database = config.DatabaseConfig{
|
||||
Driver: "postgres", DSN: "postgres://db.example.test:5432/scert?sslmode=verify-full",
|
||||
CredentialRef: "secret://database/restore-test",
|
||||
MaxOpenConns: 4, MaxIdleConns: 2, ConnMaxLifetimeMins: 30,
|
||||
}
|
||||
if err := cfg.Save(filepath.Join(input, "scert.json")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeBackupManifest(input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
currentPath := filepath.Join(root, "current.json")
|
||||
const original = "preserve-current-config"
|
||||
if err := os.WriteFile(currentPath, []byte(original), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previous := configPath
|
||||
configPath = currentPath
|
||||
t.Cleanup(func() { configPath = previous })
|
||||
command := newRestoreCommand()
|
||||
var output strings.Builder
|
||||
command.SetOut(&output)
|
||||
command.SetErr(&output)
|
||||
command.SetArgs([]string{"--input", input})
|
||||
if err := command.Execute(); err == nil || !strings.Contains(err.Error(), "PostgreSQL 完整备份与恢复尚未实现") {
|
||||
t.Fatal("未支持的 PostgreSQL 恢复没有明确拒绝")
|
||||
}
|
||||
content, err := os.ReadFile(currentPath)
|
||||
if err != nil || string(content) != original {
|
||||
t.Fatal("拒绝恢复时改变了现有配置")
|
||||
}
|
||||
if _, err := os.Stat(cfg.Paths.Data); !os.IsNotExist(err) {
|
||||
t.Fatal("拒绝恢复前不应创建锁目录或数据库")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaintenanceResolvesEncryptedMySQLCredentialsOnlyInMemory(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
store, err := secretstore.Open(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := config.DatabaseConfig{
|
||||
Driver: "mysql", DSN: "tcp(db.example.test:3306)/scert?tls=true",
|
||||
CredentialRef: "secret://database/maintenance-test",
|
||||
}
|
||||
const password = "maintenance-test-password"
|
||||
if err := store.Put(t.Context(), cfg.CredentialRef, map[string]string{"username": "backup-user", "password": password}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := os.ReadFile(filepath.Join(directory, "secrets.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolved, err := resolveMaintenanceDatabase(t.Context(), cfg, directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
parsed, err := mysql.ParseDSN(resolved.DSN)
|
||||
if err != nil || parsed.User != "backup-user" || parsed.Passwd != password || parsed.TLSConfig != "true" {
|
||||
t.Fatal("维护连接没有正确补全加密凭据或保留 TLS 设置")
|
||||
}
|
||||
if strings.Contains(cfg.DSN, "backup-user") || strings.Contains(cfg.DSN, password) {
|
||||
t.Fatal("原始配置被写入数据库账号密码")
|
||||
}
|
||||
after, err := os.ReadFile(filepath.Join(directory, "secrets.json"))
|
||||
if err != nil || !bytes.Equal(before, after) || bytes.Contains(after, []byte(password)) {
|
||||
t.Fatal("维护连接改变了敏感文件或泄露明文密码")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupValidatesDatabaseCredentialReference(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
if err := createTestSecretStore(directory); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := config.Config{Database: config.DatabaseConfig{CredentialRef: "secret://database/missing"}}
|
||||
if err := validateBackupSecretStore(t.Context(), directory, cfg); err == nil {
|
||||
t.Fatal("备份不能忽略缺失的数据库加密凭据")
|
||||
}
|
||||
if _, err := resolveMaintenanceDatabase(t.Context(), config.DatabaseConfig{
|
||||
Driver: "mysql", DSN: "tcp(db.example.test:3306)/scert?tls=true", CredentialRef: cfg.Database.CredentialRef,
|
||||
}, directory); err == nil || strings.Contains(err.Error(), cfg.Database.CredentialRef) {
|
||||
t.Fatal("维护连接缺失凭据时必须失败且不泄露完整引用")
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,15 @@ import (
|
||||
|
||||
// backupMySQL 将 MySQL 的表结构和数据导出为可在 MySQL 5.7/8.0 导入的 SQL 文件。
|
||||
func backupMySQL(ctx context.Context, cfg config.Config, output string) error {
|
||||
db, err := database.Open(ctx, cfg.Database)
|
||||
resolved, err := resolveMaintenanceDatabase(ctx, cfg.Database, cfg.Paths.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return backupMySQLDatabase(ctx, resolved, output)
|
||||
}
|
||||
|
||||
func backupMySQLDatabase(ctx context.Context, cfg config.DatabaseConfig, output string) error {
|
||||
db, err := database.Open(ctx, cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开 MySQL 备份连接失败:%w", err)
|
||||
}
|
||||
@@ -84,7 +92,7 @@ func backupMySQL(ctx context.Context, cfg config.Config, output string) error {
|
||||
|
||||
// createMySQLRestoreBackup 在恢复前保留目标数据库快照,供失败时回滚使用。
|
||||
// 快照目录不会在恢复成功后自动删除,便于管理员在需要时继续排查或手动恢复。
|
||||
func createMySQLRestoreBackup(ctx context.Context, cfg config.Config) (string, error) {
|
||||
func createMySQLRestoreBackup(ctx context.Context, cfg config.Config, connection config.DatabaseConfig) (string, error) {
|
||||
backupRoot := filepath.Clean(strings.TrimSpace(cfg.Paths.Backup))
|
||||
if backupRoot == "." || backupRoot == "" {
|
||||
return "", fmt.Errorf("MySQL 恢复前备份目录不能为空")
|
||||
@@ -107,7 +115,7 @@ func createMySQLRestoreBackup(ctx context.Context, cfg config.Config) (string, e
|
||||
return "", fmt.Errorf("创建 MySQL 恢复快照目录失败:%w", err)
|
||||
}
|
||||
rollbackPath := filepath.Join(rollbackDirectory, "scert.sql")
|
||||
if err := backupMySQL(ctx, cfg, rollbackPath); err != nil {
|
||||
if err := backupMySQLDatabase(ctx, connection, rollbackPath); err != nil {
|
||||
return "", fmt.Errorf("创建 MySQL 恢复前快照失败:%w", err)
|
||||
}
|
||||
return rollbackPath, nil
|
||||
@@ -225,12 +233,12 @@ func mysqlStringLiteral(value string) string {
|
||||
}
|
||||
|
||||
// restoreMySQL 执行 Scert 生成的 MySQL SQL 备份。
|
||||
func restoreMySQL(ctx context.Context, cfg config.Config, input string) error {
|
||||
func restoreMySQL(ctx context.Context, cfg config.DatabaseConfig, input string) error {
|
||||
script, err := os.ReadFile(input)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取 MySQL 备份文件失败:%w", err)
|
||||
}
|
||||
db, err := database.Open(ctx, cfg.Database)
|
||||
db, err := database.Open(ctx, cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开 MySQL 恢复连接失败:%w", err)
|
||||
}
|
||||
|
||||
@@ -369,6 +369,9 @@ func newRestoreCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取备份配置失败:%w", err)
|
||||
}
|
||||
if err := validateBackupDriver(cfg.Database.Driver); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateBackupSecretStore(cmd.Context(), input, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -444,6 +447,20 @@ func newRestoreCommand() *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
// 当前账号用于恢复前快照、导入和回滚,不能因替换配置或主密钥而丢失。
|
||||
// 无当前配置时才使用已校验的备份凭据;连接信息始终仅保留在内存。
|
||||
var mysqlConnection config.DatabaseConfig
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
connectionConfig, secretDirectory := cfg.Database, input
|
||||
if currentConfigErr == nil {
|
||||
connectionConfig, secretDirectory = currentConfig.Database, currentConfig.Paths.Config
|
||||
}
|
||||
mysqlConnection, err = resolveMaintenanceDatabase(cmd.Context(), connectionConfig, secretDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
configBackupPath := ""
|
||||
if isRegularFile(currentPath) {
|
||||
configBackupPath = currentPath + ".before-restore-" + time.Now().UTC().Format("20060102-150405.000000000")
|
||||
@@ -462,7 +479,7 @@ func newRestoreCommand() *cobra.Command {
|
||||
|
||||
mysqlBackupPath := ""
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
mysqlBackupPath, err = createMySQLRestoreBackup(cmd.Context(), cfg)
|
||||
mysqlBackupPath, err = createMySQLRestoreBackup(cmd.Context(), cfg, mysqlConnection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -474,7 +491,7 @@ func newRestoreCommand() *cobra.Command {
|
||||
rollbackCore := func() error {
|
||||
var rollbackErr error
|
||||
if mysqlRestoreAttempted && mysqlBackupPath != "" {
|
||||
rollbackErr = errors.Join(rollbackErr, restoreMySQL(cmd.Context(), cfg, mysqlBackupPath))
|
||||
rollbackErr = errors.Join(rollbackErr, restoreMySQL(cmd.Context(), mysqlConnection, mysqlBackupPath))
|
||||
}
|
||||
if sqliteRestoreAttempted {
|
||||
if databaseBackupPath != "" {
|
||||
@@ -505,7 +522,7 @@ func newRestoreCommand() *cobra.Command {
|
||||
}
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
mysqlRestoreAttempted = true
|
||||
if err := restoreMySQL(cmd.Context(), cfg, mysqlSource); err != nil {
|
||||
if err := restoreMySQL(cmd.Context(), mysqlConnection, mysqlSource); err != nil {
|
||||
return errors.Join(fmt.Errorf("恢复 MySQL 数据库失败:%w", err), rollbackCore())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user