实现PostgreSQL受控备份恢复并加固取消后的数据库回滚
This commit is contained in:
1 parent
943fb8c459
commit
626ba534f7
10 files changed
+1068
-16
No files matched your search
@@ -38,9 +38,29 @@ type backupManifestFile struct {
|
||||
}
|
||||
|
||||
func createBackup(ctx context.Context, cfg config.Config, configPath, output string) error {
|
||||
return createBackupWithPostgresTools(ctx, cfg, configPath, output, loadPostgresTools)
|
||||
}
|
||||
|
||||
func createBackupWithPostgresTools(ctx context.Context, cfg config.Config, configPath, output string, factory postgresToolsFactory) error {
|
||||
if err := validateBackupDriver(cfg.Database.Driver); err != nil {
|
||||
return err
|
||||
}
|
||||
var tools *postgresTools
|
||||
var postgresConnection config.DatabaseConfig
|
||||
if cfg.Database.Driver == "postgres" {
|
||||
var err error
|
||||
tools, err = factory(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
postgresConnection, err = resolveMaintenanceDatabase(ctx, cfg.Database, cfg.Paths.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, _, err := postgresToolConnection(postgresConnection); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
output = filepath.Clean(output)
|
||||
if err := ensureBackupDestination(output); err != nil {
|
||||
return err
|
||||
@@ -112,6 +132,11 @@ func createBackup(ctx context.Context, cfg config.Config, configPath, output str
|
||||
return err
|
||||
}
|
||||
}
|
||||
if cfg.Database.Driver == "postgres" {
|
||||
if err := tools.backup(ctx, postgresConnection, filepath.Join(staging, "scert.pgdump")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if info, err := os.Lstat(cfg.Paths.Certificate); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
@@ -127,12 +152,18 @@ func createBackup(ctx context.Context, cfg config.Config, configPath, output str
|
||||
return fmt.Errorf("检查证书根目录失败:%w", err)
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeBackupManifest(staging); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := syncDirectory(staging); err != nil {
|
||||
return fmt.Errorf("同步备份暂存目录失败:%w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(staging, output); err != nil {
|
||||
return fmt.Errorf("发布备份目录失败:%w", err)
|
||||
}
|
||||
@@ -242,6 +273,9 @@ func validateRestoreCompatibility(current, backup config.Config) error {
|
||||
if backup.Database.Driver == "mysql" && strings.TrimSpace(current.Database.DSN) != strings.TrimSpace(backup.Database.DSN) {
|
||||
return errors.New("备份 MySQL 连接配置与当前配置不一致,拒绝跨数据库恢复")
|
||||
}
|
||||
if backup.Database.Driver == "postgres" && strings.TrimSpace(current.Database.DSN) != strings.TrimSpace(backup.Database.DSN) {
|
||||
return errors.New("备份 PostgreSQL 连接配置与当前配置不一致,拒绝跨数据库恢复")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
@@ -11,15 +12,23 @@ import (
|
||||
|
||||
func validateBackupDriver(driver string) error {
|
||||
switch driver {
|
||||
case "sqlite", "mysql":
|
||||
case "sqlite", "mysql", "postgres":
|
||||
return nil
|
||||
case "postgres":
|
||||
return errors.New("PostgreSQL 完整备份与恢复尚未实现,拒绝生成或恢复不含数据库的备份;请使用数据库原生工具并单独保护配置、加密凭据和证书")
|
||||
default:
|
||||
return errors.New("当前数据库类型不支持备份与恢复")
|
||||
}
|
||||
}
|
||||
|
||||
// 原请求取消后仍需尝试恢复旧数据,但不能让回滚无限等待。
|
||||
func runDatabaseRollback(ctx context.Context, restore func(context.Context) error) error {
|
||||
rollbackCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute)
|
||||
defer cancel()
|
||||
if err := restore(rollbackCtx); err != nil {
|
||||
return errors.Join(errors.New("数据库自动回滚失败,请保留恢复前快照并停止服务,联系管理员手动恢复"), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 返回值仅供本次维护操作连接数据库,禁止写入配置、备份清单或日志。
|
||||
func resolveMaintenanceDatabase(ctx context.Context, cfg config.DatabaseConfig, directory string) (config.DatabaseConfig, error) {
|
||||
if err := database.ValidateConfig(cfg); err != nil {
|
||||
|
||||
@@ -2,10 +2,12 @@ package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
|
||||
@@ -14,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
func TestUnsupportedBackupDriverFailsBeforeFilesystemChanges(t *testing.T) {
|
||||
for _, driver := range []string{"postgres", "unknown", ""} {
|
||||
for _, driver := range []string{"unknown", ""} {
|
||||
t.Run(driver, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := config.Config{Database: config.DatabaseConfig{Driver: driver}}
|
||||
@@ -30,7 +32,7 @@ func TestUnsupportedBackupDriverFailsBeforeFilesystemChanges(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRestoreFailsBeforeReplacingExistingConfig(t *testing.T) {
|
||||
func TestPostgresRestoreWithoutSecretsFailsBeforeReplacingExistingConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
input := filepath.Join(root, "input")
|
||||
if err := os.Mkdir(input, 0o700); err != nil {
|
||||
@@ -66,8 +68,8 @@ func TestPostgresRestoreFailsBeforeReplacingExistingConfig(t *testing.T) {
|
||||
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 恢复没有明确拒绝")
|
||||
if err := command.Execute(); err == nil || !strings.Contains(err.Error(), "master.key") {
|
||||
t.Fatal("缺少数据库凭据的 PostgreSQL 恢复没有明确拒绝")
|
||||
}
|
||||
content, err := os.ReadFile(currentPath)
|
||||
if err != nil || string(content) != original {
|
||||
@@ -78,6 +80,32 @@ func TestPostgresRestoreFailsBeforeReplacingExistingConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresBackupDriverSupported(t *testing.T) {
|
||||
if err := validateBackupDriver("postgres"); err != nil {
|
||||
t.Fatalf("PostgreSQL 应支持完整备份恢复:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabaseRollbackSurvivesCanceledRequestWithDeadline(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
called := false
|
||||
err := runDatabaseRollback(ctx, func(rollbackCtx context.Context) error {
|
||||
called = true
|
||||
if rollbackCtx.Err() != nil {
|
||||
t.Fatal("原请求取消不应立即取消数据库回滚")
|
||||
}
|
||||
deadline, ok := rollbackCtx.Deadline()
|
||||
if !ok || time.Until(deadline) <= 0 || time.Until(deadline) > 5*time.Minute {
|
||||
t.Fatal("回滚必须使用独立且有界的超时")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil || !called {
|
||||
t.Fatal("取消后仍应执行回滚")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaintenanceResolvesEncryptedMySQLCredentialsOnlyInMemory(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
store, err := secretstore.Open(directory)
|
||||
|
||||
@@ -352,6 +352,10 @@ func newBackupCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
func newRestoreCommand() *cobra.Command {
|
||||
return newRestoreCommandWithPostgresTools(loadPostgresTools)
|
||||
}
|
||||
|
||||
func newRestoreCommandWithPostgresTools(factory postgresToolsFactory) *cobra.Command {
|
||||
var input string
|
||||
command := &cobra.Command{
|
||||
Use: "restore",
|
||||
@@ -375,6 +379,17 @@ func newRestoreCommand() *cobra.Command {
|
||||
if err := validateBackupSecretStore(cmd.Context(), input, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
var tools *postgresTools
|
||||
postgresSource := filepath.Join(input, "scert.pgdump")
|
||||
if cfg.Database.Driver == "postgres" {
|
||||
tools, err = factory(cmd.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tools.validateArchive(cmd.Context(), postgresSource); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
currentPath := filepath.Clean(resolveConfigPath())
|
||||
expectedConfigPath := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
@@ -449,17 +464,27 @@ func newRestoreCommand() *cobra.Command {
|
||||
|
||||
// 当前账号用于恢复前快照、导入和回滚,不能因替换配置或主密钥而丢失。
|
||||
// 无当前配置时才使用已校验的备份凭据;连接信息始终仅保留在内存。
|
||||
var mysqlConnection config.DatabaseConfig
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
var maintenanceConnection config.DatabaseConfig
|
||||
if cfg.Database.Driver == "mysql" || cfg.Database.Driver == "postgres" {
|
||||
connectionConfig, secretDirectory := cfg.Database, input
|
||||
if currentConfigErr == nil {
|
||||
connectionConfig, secretDirectory = currentConfig.Database, currentConfig.Paths.Config
|
||||
}
|
||||
mysqlConnection, err = resolveMaintenanceDatabase(cmd.Context(), connectionConfig, secretDirectory)
|
||||
maintenanceConnection, err = resolveMaintenanceDatabase(cmd.Context(), connectionConfig, secretDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
postgresBackupPath := ""
|
||||
if cfg.Database.Driver == "postgres" {
|
||||
postgresBackupPath, err = tools.createRestoreSnapshot(cmd.Context(), cfg, maintenanceConnection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tools.validateRestoreInventory(cmd.Context(), postgresSource, postgresBackupPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
configBackupPath := ""
|
||||
if isRegularFile(currentPath) {
|
||||
@@ -479,7 +504,7 @@ func newRestoreCommand() *cobra.Command {
|
||||
|
||||
mysqlBackupPath := ""
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
mysqlBackupPath, err = createMySQLRestoreBackup(cmd.Context(), cfg, mysqlConnection)
|
||||
mysqlBackupPath, err = createMySQLRestoreBackup(cmd.Context(), cfg, maintenanceConnection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -488,10 +513,18 @@ func newRestoreCommand() *cobra.Command {
|
||||
configRestoreAttempted := false
|
||||
sqliteRestoreAttempted := false
|
||||
mysqlRestoreAttempted := false
|
||||
postgresRestoreAttempted := false
|
||||
rollbackCore := func() error {
|
||||
var rollbackErr error
|
||||
if mysqlRestoreAttempted && mysqlBackupPath != "" {
|
||||
rollbackErr = errors.Join(rollbackErr, restoreMySQL(cmd.Context(), mysqlConnection, mysqlBackupPath))
|
||||
rollbackErr = errors.Join(rollbackErr, runDatabaseRollback(cmd.Context(), func(ctx context.Context) error {
|
||||
return restoreMySQL(ctx, maintenanceConnection, mysqlBackupPath)
|
||||
}))
|
||||
}
|
||||
if postgresRestoreAttempted && postgresBackupPath != "" {
|
||||
rollbackErr = errors.Join(rollbackErr, runDatabaseRollback(cmd.Context(), func(ctx context.Context) error {
|
||||
return tools.restore(ctx, maintenanceConnection, postgresBackupPath)
|
||||
}))
|
||||
}
|
||||
if sqliteRestoreAttempted {
|
||||
if databaseBackupPath != "" {
|
||||
@@ -510,6 +543,9 @@ func newRestoreCommand() *cobra.Command {
|
||||
return rollbackErr
|
||||
}
|
||||
|
||||
if err := cmd.Context().Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
configRestoreAttempted = true
|
||||
if err := copyFileAtomic(configFile, currentPath, 0o600); err != nil {
|
||||
return errors.Join(err, rollbackCore())
|
||||
@@ -522,10 +558,19 @@ func newRestoreCommand() *cobra.Command {
|
||||
}
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
mysqlRestoreAttempted = true
|
||||
if err := restoreMySQL(cmd.Context(), mysqlConnection, mysqlSource); err != nil {
|
||||
if err := restoreMySQL(cmd.Context(), maintenanceConnection, mysqlSource); err != nil {
|
||||
return errors.Join(fmt.Errorf("恢复 MySQL 数据库失败:%w", err), rollbackCore())
|
||||
}
|
||||
}
|
||||
if cfg.Database.Driver == "postgres" {
|
||||
postgresRestoreAttempted = true
|
||||
if err := tools.restore(cmd.Context(), maintenanceConnection, postgresSource); err != nil {
|
||||
return errors.Join(err, rollbackCore())
|
||||
}
|
||||
}
|
||||
if err := cmd.Context().Err(); err != nil {
|
||||
return errors.Join(err, rollbackCore())
|
||||
}
|
||||
if err := restoreAdditionalAssets(cfg, input); err != nil {
|
||||
return errors.Join(fmt.Errorf("恢复敏感配置或证书失败:%w", err), rollbackCore())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func (tools *postgresTools) backup(ctx context.Context, connection config.DatabaseConfig, output string) (result error) {
|
||||
dsn, env, err := postgresToolConnection(connection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.OpenFile(output, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return errors.New("创建 PostgreSQL 备份文件失败,目标不得已存在")
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
if result != nil {
|
||||
// 只清理本次独占创建的不完整文件,不触碰既有备份。
|
||||
_ = os.Remove(output)
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
defer cancel()
|
||||
args := []string{"--format=custom", "--no-password", "--lock-wait-timeout=30s", "--dbname=" + dsn}
|
||||
if err := tools.run(ctx, tools.dumpPath, args, env, file); err != nil {
|
||||
return errors.Join(errors.New("创建 PostgreSQL 一致性快照失败"), err)
|
||||
}
|
||||
if err := file.Sync(); err != nil {
|
||||
return errors.New("同步 PostgreSQL 备份文件失败")
|
||||
}
|
||||
if err := file.Close(); err != nil {
|
||||
return errors.New("关闭 PostgreSQL 备份文件失败")
|
||||
}
|
||||
return tools.validateArchive(ctx, output)
|
||||
}
|
||||
|
||||
func (tools *postgresTools) validateArchive(ctx context.Context, input string) error {
|
||||
if !isRegularFile(input) {
|
||||
return errors.New("PostgreSQL 备份目录缺少有效的 scert.pgdump 文件")
|
||||
}
|
||||
file, err := os.Open(input)
|
||||
if err != nil {
|
||||
return errors.New("无法读取 PostgreSQL 备份")
|
||||
}
|
||||
var magic [5]byte
|
||||
_, readErr := io.ReadFull(file, magic[:])
|
||||
_ = file.Close()
|
||||
if readErr != nil || string(magic[:]) != "PGDMP" {
|
||||
return errors.New("PostgreSQL 备份必须是 pg_dump 自定义格式归档")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
defer cancel()
|
||||
// 完整解码归档而非仅检查目录;不连接数据库,不保留含业务数据的 SQL 输出。
|
||||
if err := tools.run(ctx, tools.restorePath, []string{"--file=-", "--", input}, postgresToolEnvironment(), io.Discard); err != nil {
|
||||
return errors.Join(errors.New("PostgreSQL 归档完整性校验失败"), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var postgresArchiveEntry = regexp.MustCompile(`^\d+;\s+\d+\s+\d+\s+(.+)$`)
|
||||
|
||||
func (tools *postgresTools) inventory(ctx context.Context, input string) (map[string]struct{}, error) {
|
||||
output := &limitedPostgresOutput{remaining: 8 << 20}
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
if err := tools.run(ctx, tools.restorePath, []string{"--list", "--", input}, postgresToolEnvironment(), output); err != nil {
|
||||
return nil, errors.Join(errors.New("读取 PostgreSQL 归档对象清单失败"), err)
|
||||
}
|
||||
items := make(map[string]struct{})
|
||||
for _, line := range strings.Split(output.String(), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, ";") {
|
||||
continue
|
||||
}
|
||||
match := postgresArchiveEntry.FindStringSubmatch(line)
|
||||
if len(match) != 2 {
|
||||
return nil, errors.New("PostgreSQL 归档对象清单格式无效")
|
||||
}
|
||||
if _, duplicate := items[match[1]]; duplicate {
|
||||
return nil, errors.New("PostgreSQL 归档对象清单存在重复条目")
|
||||
}
|
||||
items[match[1]] = struct{}{}
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, errors.New("PostgreSQL 归档对象清单为空,拒绝无法验证的恢复")
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (tools *postgresTools) validateRestoreInventory(ctx context.Context, input, snapshot string) error {
|
||||
source, err := tools.inventory(ctx, input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
target, err := tools.inventory(ctx, snapshot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// pg_restore --clean 只删除归档列出的对象。严格相等保证导入及反向回滚
|
||||
// 都不遗留额外对象;跨迁移版本或空库灾难恢复需管理员使用原生流程。
|
||||
if len(source) != len(target) {
|
||||
return errors.New("PostgreSQL 备份与当前数据库对象清单不一致,拒绝自动恢复;请在隔离数据库使用原生工具演练迁移或灾难恢复")
|
||||
}
|
||||
for item := range source {
|
||||
if _, exists := target[item]; !exists {
|
||||
return errors.New("PostgreSQL 备份与当前数据库对象或所有者不一致,拒绝可能残留对象的恢复")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tools *postgresTools) createRestoreSnapshot(ctx context.Context, cfg config.Config, connection config.DatabaseConfig) (string, error) {
|
||||
root := filepath.Clean(strings.TrimSpace(cfg.Paths.Backup))
|
||||
if root == "." {
|
||||
return "", errors.New("PostgreSQL 恢复前快照目录不能为空")
|
||||
}
|
||||
if err := os.MkdirAll(root, 0o700); err != nil {
|
||||
return "", fmt.Errorf("创建 PostgreSQL 恢复前快照目录失败:%w", err)
|
||||
}
|
||||
info, err := os.Lstat(root)
|
||||
if err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return "", errors.New("PostgreSQL 恢复前快照目录必须为普通目录")
|
||||
}
|
||||
directory, err := os.MkdirTemp(root, "scert-postgres-before-restore-")
|
||||
if err != nil {
|
||||
return "", errors.New("创建 PostgreSQL 恢复前快照失败")
|
||||
}
|
||||
output := filepath.Join(directory, "scert.pgdump")
|
||||
if err := tools.backup(ctx, connection, output); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := syncDirectory(directory); err != nil {
|
||||
return "", errors.New("同步 PostgreSQL 恢复前快照目录失败,拒绝执行恢复")
|
||||
}
|
||||
if err := syncDirectory(root); err != nil {
|
||||
return "", errors.New("同步 PostgreSQL 快照父目录失败,拒绝执行恢复")
|
||||
}
|
||||
// 成功后也保留原数据库快照,便于人工恢复,不自动清理用户备份。
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (tools *postgresTools) restore(ctx context.Context, connection config.DatabaseConfig, input string) error {
|
||||
dsn, env, err := postgresToolConnection(connection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
defer cancel()
|
||||
// 保留对象所有者和授权,权限或角色不足时失败,而不是悄悄丢失访问控制。
|
||||
args := []string{"--clean", "--if-exists", "--single-transaction", "--exit-on-error", "--no-password", "--dbname=" + dsn, "--", input}
|
||||
if err := tools.run(ctx, tools.restorePath, args, env, io.Discard); err != nil {
|
||||
return errors.Join(errors.New("PostgreSQL 单事务恢复失败"), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
// 替身只模拟工具协议和数据库状态,不构成 PostgreSQL 实机恢复证据。
|
||||
type postgresFixture struct {
|
||||
t *testing.T
|
||||
state string
|
||||
restores int
|
||||
failDump bool
|
||||
failDecode bool
|
||||
failRestore bool
|
||||
failRollback bool
|
||||
extraObject bool
|
||||
cancelRestore context.CancelFunc
|
||||
tools *postgresTools
|
||||
}
|
||||
|
||||
func newPostgresFixture(t *testing.T) *postgresFixture {
|
||||
f := &postgresFixture{t: t, state: "current"}
|
||||
f.tools = &postgresTools{dumpPath: "dump", restorePath: "restore", run: f.run}
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *postgresFixture) factory(context.Context) (*postgresTools, error) { return f.tools, nil }
|
||||
|
||||
func (f *postgresFixture) run(ctx context.Context, tool string, args, env []string, output io.Writer) error {
|
||||
f.t.Helper()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
connected := slices.ContainsFunc(args, func(arg string) bool { return strings.HasPrefix(arg, "--dbname=") })
|
||||
if connected {
|
||||
if !slices.Contains(env, "PGPASSWORD=current-password") || strings.Contains(strings.Join(args, " "), "current-password") || strings.Contains(strings.Join(args, " "), "backup-password") {
|
||||
f.t.Fatal("维护操作必须固定当前凭据且不将密码放入命令行")
|
||||
}
|
||||
}
|
||||
if tool == "dump" {
|
||||
if !slices.Contains(args, "--format=custom") || !slices.Contains(args, "--no-password") || slices.Contains(args, "--no-acl") || slices.Contains(args, "--no-owner") {
|
||||
f.t.Fatal("数据库快照工具参数不完整")
|
||||
}
|
||||
_, err := io.WriteString(output, "PGDMP"+f.state)
|
||||
if f.failDump {
|
||||
return errors.New("模拟快照失败")
|
||||
}
|
||||
return err
|
||||
}
|
||||
input := args[len(args)-1]
|
||||
data, err := os.ReadFile(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if slices.Contains(args, "--list") {
|
||||
_, err := io.WriteString(output, "; Archive TOC\n10; 1259 100 TABLE public settings owner\n11; 0 100 TABLE DATA public settings owner\n")
|
||||
if f.extraObject && strings.HasPrefix(string(data), "PGDMPcurrent") {
|
||||
_, err = io.WriteString(output, "12; 1259 101 TABLE public extra owner\n")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if !connected {
|
||||
if !slices.Contains(args, "--file=-") || f.failDecode {
|
||||
return errors.New("模拟归档解码失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if slices.Contains(args, "--no-owner") || slices.Contains(args, "--no-acl") {
|
||||
f.t.Fatal("完整恢复不能静默丢弃所有者或对象授权")
|
||||
}
|
||||
for _, flag := range []string{"--clean", "--if-exists", "--single-transaction", "--exit-on-error", "--no-password"} {
|
||||
if !slices.Contains(args, flag) {
|
||||
f.t.Fatalf("恢复缺少必要参数:%s", flag)
|
||||
}
|
||||
}
|
||||
f.restores++
|
||||
if f.restores > 1 {
|
||||
deadline, ok := ctx.Deadline()
|
||||
if !ok || time.Until(deadline) > 5*time.Minute || ctx.Err() != nil {
|
||||
f.t.Fatal("回滚上下文必须独立且有界")
|
||||
}
|
||||
if f.failRollback {
|
||||
return errors.New("模拟数据库回滚失败")
|
||||
}
|
||||
}
|
||||
f.state = strings.TrimPrefix(string(data), "PGDMP")
|
||||
if f.restores == 1 && f.cancelRestore != nil {
|
||||
f.cancelRestore()
|
||||
return context.Canceled
|
||||
}
|
||||
if f.restores == 1 && f.failRestore {
|
||||
return errors.New("模拟导入失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func preparePostgresRestoreFixture(t *testing.T, blockedCertificate bool) (config.Config, string, string) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
cfg := config.Default(root)
|
||||
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/maintenance", MaxOpenConns: 4, MaxIdleConns: 2, ConnMaxLifetimeMins: 30,
|
||||
}
|
||||
cfg.Server.Port = 9004
|
||||
current := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(current); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := filepath.Join(root, "input")
|
||||
backup := cfg
|
||||
backup.Server.Port = 9005
|
||||
if err := backup.Save(filepath.Join(input, "scert.json")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for directory, password := range map[string]string{cfg.Paths.Config: "current-password", input: "backup-password"} {
|
||||
store, err := secretstore.Open(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Put(t.Context(), cfg.Database.CredentialRef, map[string]string{"username": "owner", "password": password}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if blockedCertificate {
|
||||
if err := os.WriteFile(cfg.Paths.Certificate, []byte("preserve-blocked-target"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := os.MkdirAll(cfg.Paths.Certificate, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(cfg.Paths.Certificate, "old.pem"), []byte("old-certificate"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(input, "certificates"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for path, value := range map[string]string{
|
||||
filepath.Join(input, "scert.pgdump"): "PGDMPrestored",
|
||||
filepath.Join(input, "certificates", "new.pem"): "new-certificate",
|
||||
} {
|
||||
if err := os.WriteFile(path, []byte(value), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := writeBackupManifest(input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previous := configPath
|
||||
configPath = current
|
||||
t.Cleanup(func() { configPath = previous })
|
||||
return cfg, current, input
|
||||
}
|
||||
|
||||
func TestPostgresCompleteBackupAndFailedPublication(t *testing.T) {
|
||||
for _, failure := range []string{"", "dump", "decode"} {
|
||||
t.Run("failure="+failure, func(t *testing.T) {
|
||||
cfg, current, _ := preparePostgresRestoreFixture(t, false)
|
||||
f := newPostgresFixture(t)
|
||||
f.failDump, f.failDecode = failure == "dump", failure == "decode"
|
||||
output := filepath.Join(t.TempDir(), "backup")
|
||||
err := createBackupWithPostgresTools(t.Context(), cfg, current, output, f.factory)
|
||||
if failure != "" {
|
||||
if err == nil {
|
||||
t.Fatal("快照或解码失败时不能报告备份成功")
|
||||
}
|
||||
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
||||
t.Fatal("失败备份不能发布")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := validateBackupManifest(output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"scert.pgdump", "scert.json", "master.key", "secrets.json", "certificates/old.pem"} {
|
||||
if !isRegularFile(filepath.Join(output, name)) {
|
||||
t.Fatalf("完整备份缺少:%s", name)
|
||||
}
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(output, "scert.json"))
|
||||
if err != nil || bytes.Contains(data, []byte("current-password")) {
|
||||
t.Fatal("备份配置不得写入内存连接密码")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRestoreSuccessAndRollback(t *testing.T) {
|
||||
for _, scenario := range []string{"success", "certificate", "import", "cancel", "rollback", "inventory", "snapshot", "decode"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
cfg, current, input := preparePostgresRestoreFixture(t, scenario == "certificate")
|
||||
before, _ := os.ReadFile(current)
|
||||
beforeKey, _ := os.ReadFile(filepath.Join(cfg.Paths.Config, "master.key"))
|
||||
beforeSecrets, _ := os.ReadFile(filepath.Join(cfg.Paths.Config, "secrets.json"))
|
||||
f := newPostgresFixture(t)
|
||||
f.failRestore = scenario == "import" || scenario == "rollback"
|
||||
f.failRollback, f.extraObject = scenario == "rollback", scenario == "inventory"
|
||||
f.failDump, f.failDecode = scenario == "snapshot", scenario == "decode"
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
if scenario == "cancel" {
|
||||
f.cancelRestore = cancel
|
||||
}
|
||||
command := newRestoreCommandWithPostgresTools(f.factory)
|
||||
command.SetArgs([]string{"--input", input})
|
||||
command.SetContext(ctx)
|
||||
var output bytes.Buffer
|
||||
command.SetOut(&output)
|
||||
command.SetErr(&output)
|
||||
err := command.Execute()
|
||||
if scenario == "success" {
|
||||
if err != nil || f.state != "restored" || f.restores != 1 {
|
||||
t.Fatalf("完整恢复未成功:%v", err)
|
||||
}
|
||||
loaded, err := config.Load(current)
|
||||
if err != nil || loaded.Server.Port != 9005 || !isRegularFile(filepath.Join(cfg.Paths.Certificate, "new.pem")) {
|
||||
t.Fatal("恢复未包含配置或证书")
|
||||
}
|
||||
store, err := secretstore.Open(cfg.Paths.Config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
credentials, err := store.Get(t.Context(), cfg.Database.CredentialRef)
|
||||
if err != nil || credentials["password"] != "backup-password" {
|
||||
t.Fatal("备份加密凭据未恢复")
|
||||
}
|
||||
} else {
|
||||
if err == nil || strings.Contains(output.String(), "恢复已完成") {
|
||||
t.Fatal("恢复失败不能报告成功")
|
||||
}
|
||||
if scenario == "rollback" {
|
||||
if !strings.Contains(err.Error(), "数据库自动回滚失败") {
|
||||
t.Fatal("回滚失败必须明确要求人工恢复")
|
||||
}
|
||||
} else if f.state != "current" {
|
||||
t.Fatal("失败时没有恢复原数据库状态")
|
||||
}
|
||||
after, _ := os.ReadFile(current)
|
||||
afterKey, _ := os.ReadFile(filepath.Join(cfg.Paths.Config, "master.key"))
|
||||
afterSecrets, _ := os.ReadFile(filepath.Join(cfg.Paths.Config, "secrets.json"))
|
||||
if !bytes.Equal(before, after) || !bytes.Equal(beforeKey, afterKey) || !bytes.Equal(beforeSecrets, afterSecrets) {
|
||||
t.Fatal("失败时未保留或回滚原配置与加密凭据")
|
||||
}
|
||||
if scenario == "inventory" || scenario == "snapshot" || scenario == "decode" {
|
||||
if f.restores != 0 {
|
||||
t.Fatal("预检查失败时不应执行数据库导入")
|
||||
}
|
||||
} else if f.restores != 2 {
|
||||
t.Fatal("导入后失败应执行数据库回滚")
|
||||
}
|
||||
}
|
||||
if scenario != "snapshot" && scenario != "decode" {
|
||||
matches, _ := filepath.Glob(filepath.Join(cfg.Paths.Backup, "scert-postgres-before-restore-*", "scert.pgdump"))
|
||||
if len(matches) != 1 {
|
||||
t.Fatal("恢复前快照应保留供管理员检查")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresRestoreRejectsDifferentDatabase(t *testing.T) {
|
||||
current := config.Config{Database: config.DatabaseConfig{Driver: "postgres", DSN: "postgres://db.test/current?sslmode=verify-full"}}
|
||||
backup := current
|
||||
backup.Database.DSN = "postgres://db.test/other?sslmode=verify-full"
|
||||
if err := validateRestoreCompatibility(current, backup); err == nil || !strings.Contains(err.Error(), "拒绝跨数据库恢复") {
|
||||
t.Fatal("恢复不得连接不同目标数据库")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresBackupDoesNotOverwriteExistingFile(t *testing.T) {
|
||||
cfg, _, _ := preparePostgresRestoreFixture(t, false)
|
||||
connection, err := resolveMaintenanceDatabase(t.Context(), cfg.Database, cfg.Paths.Config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output := filepath.Join(t.TempDir(), "existing.pgdump")
|
||||
if err := os.WriteFile(output, []byte("existing-backup"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := newPostgresFixture(t)
|
||||
if err := f.tools.backup(t.Context(), connection, output); err == nil {
|
||||
t.Fatal("既有文件不能被覆盖")
|
||||
}
|
||||
content, err := os.ReadFile(output)
|
||||
if err != nil || string(content) != "existing-backup" {
|
||||
t.Fatal("拒绝覆盖后必须保留原文件")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresArchiveRejectsInvalidMagicAndInventory(t *testing.T) {
|
||||
input := filepath.Join(t.TempDir(), "scert.pgdump")
|
||||
if err := os.WriteFile(input, []byte("not-a-custom-archive"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := newPostgresFixture(t)
|
||||
if err := f.tools.validateArchive(t.Context(), input); err == nil {
|
||||
t.Fatal("非自定义格式归档必须拒绝")
|
||||
}
|
||||
for _, listing := range []string{"", "; empty", "malformed", "1; 1259 1 TABLE public a owner\n2; 1259 2 TABLE public a owner\n", strings.Repeat("x", (8<<20)+1)} {
|
||||
tools := &postgresTools{restorePath: "restore", run: func(_ context.Context, _ string, _, _ []string, output io.Writer) error {
|
||||
_, err := io.WriteString(output, listing)
|
||||
return err
|
||||
}}
|
||||
if _, err := tools.inventory(t.Context(), input); err == nil {
|
||||
t.Fatal("空、畸形、重复或超限清单必须拒绝")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresCanceledBackupIsNotPublished(t *testing.T) {
|
||||
cfg, current, _ := preparePostgresRestoreFixture(t, false)
|
||||
f := newPostgresFixture(t)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
f.tools.run = func(ctx context.Context, tool string, args, env []string, output io.Writer) error {
|
||||
err := f.run(ctx, tool, args, env, output)
|
||||
if tool == "restore" && slices.Contains(args, "--file=-") {
|
||||
cancel()
|
||||
}
|
||||
return err
|
||||
}
|
||||
output := filepath.Join(t.TempDir(), "backup")
|
||||
if err := createBackupWithPostgresTools(ctx, cfg, current, output, f.factory); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("完成快照后取消仍应停止发布:%v", err)
|
||||
}
|
||||
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
||||
t.Fatal("取消的备份不能发布")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresMissingRestoreToolsPreserveCurrentAssets(t *testing.T) {
|
||||
cfg, current, input := preparePostgresRestoreFixture(t, false)
|
||||
before, _ := os.ReadFile(current)
|
||||
factory := func(context.Context) (*postgresTools, error) {
|
||||
return nil, errors.New("模拟官方工具未安装")
|
||||
}
|
||||
command := newRestoreCommandWithPostgresTools(factory)
|
||||
command.SetArgs([]string{"--input", input})
|
||||
command.SetOut(io.Discard)
|
||||
command.SetErr(io.Discard)
|
||||
if err := command.Execute(); err == nil {
|
||||
t.Fatal("缺少工具不能恢复")
|
||||
}
|
||||
after, _ := os.ReadFile(current)
|
||||
if !bytes.Equal(before, after) || !isRegularFile(filepath.Join(cfg.Paths.Certificate, "old.pem")) {
|
||||
t.Fatal("缺少工具不得改变当前配置或证书")
|
||||
}
|
||||
if _, err := os.Stat(cfg.Paths.Data); !os.IsNotExist(err) {
|
||||
t.Fatal("缺少工具时不应进入锁和数据库恢复阶段")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
type postgresToolRunner func(context.Context, string, []string, []string, io.Writer) error
|
||||
type postgresToolsFactory func(context.Context) (*postgresTools, error)
|
||||
|
||||
type postgresTools struct {
|
||||
dumpPath string
|
||||
restorePath string
|
||||
run postgresToolRunner
|
||||
}
|
||||
|
||||
var postgresVersionPattern = regexp.MustCompile(`^pg_(?:dump|restore) \(PostgreSQL\) (\d+)\.`)
|
||||
|
||||
func loadPostgresTools(ctx context.Context) (*postgresTools, error) {
|
||||
dump, err := exec.LookPath("pg_dump")
|
||||
if err != nil {
|
||||
return nil, errors.New("PostgreSQL 备份需要安装官方 pg_dump 和 pg_restore(相同主版本,16 或更高),并加入 PATH")
|
||||
}
|
||||
restore, err := exec.LookPath("pg_restore")
|
||||
if err != nil {
|
||||
return nil, errors.New("PostgreSQL 恢复及备份校验需要 pg_restore,请安装与 pg_dump 相同主版本的官方工具")
|
||||
}
|
||||
tools := &postgresTools{dumpPath: dump, restorePath: restore, run: executePostgresTool}
|
||||
if err := tools.validateVersions(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tools, nil
|
||||
}
|
||||
|
||||
func executePostgresTool(ctx context.Context, path string, args, env []string, output io.Writer) error {
|
||||
command := exec.CommandContext(ctx, path, args...)
|
||||
command.Env = env
|
||||
command.Stdout = output
|
||||
// 工具可能在错误中回显数据库内容或连接信息,不捕获、不透传原始输出。
|
||||
command.Stderr = io.Discard
|
||||
command.WaitDelay = 2 * time.Second
|
||||
if err := command.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
return errors.New("PostgreSQL 工具执行失败,请检查工具版本、TLS 证书、数据库权限、连接及备份完整性;原始输出已隐藏")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 清除继承的连接设置,避免 PGHOST、PGSERVICE 等环境变量改变目标或降低 TLS。
|
||||
func postgresToolEnvironment() []string {
|
||||
env := make([]string, 0, len(os.Environ())+6)
|
||||
for _, entry := range os.Environ() {
|
||||
key, _, _ := strings.Cut(entry, "=")
|
||||
key = strings.ToUpper(key)
|
||||
if strings.HasPrefix(key, "PG") || key == "LC_ALL" || key == "LANG" {
|
||||
continue
|
||||
}
|
||||
env = append(env, entry)
|
||||
}
|
||||
return append(env, "LC_ALL=C", "LANG=C", "PGPASSFILE="+os.DevNull, "PGSERVICEFILE="+os.DevNull)
|
||||
}
|
||||
|
||||
func postgresToolConnection(cfg config.DatabaseConfig) (string, []string, error) {
|
||||
parsed, err := url.Parse(strings.TrimSpace(cfg.DSN))
|
||||
if err != nil || cfg.Driver != "postgres" || parsed.User == nil {
|
||||
return "", nil, errors.New("PostgreSQL 维护连接无效或未解析加密账号")
|
||||
}
|
||||
username := parsed.User.Username()
|
||||
password, exists := parsed.User.Password()
|
||||
if username == "" || !exists || password == "" || strings.ContainsRune(username+password, 0) {
|
||||
return "", nil, errors.New("PostgreSQL 维护账号或密码无效")
|
||||
}
|
||||
parsed.User = nil
|
||||
if err := config.ValidatePostgresDSN(parsed.String()); err != nil {
|
||||
return "", nil, errors.New("PostgreSQL 维护连接参数不满足安全要求")
|
||||
}
|
||||
query := parsed.Query()
|
||||
// 只接受已确认在 pgx 和 libpq 中具有一致连接语义的参数。
|
||||
for key := range query {
|
||||
switch key {
|
||||
case "sslmode", "sslrootcert", "sslcert", "sslkey", "connect_timeout", "application_name":
|
||||
default:
|
||||
return "", nil, errors.New("PostgreSQL 维护连接包含不支持的参数,请仅使用 TLS 证书、连接超时及应用名称设置")
|
||||
}
|
||||
}
|
||||
if query.Get("sslrootcert") == "" {
|
||||
// libpq 16 起支持系统信任库;不同平台的库来源仍需实机核对,不能降级 TLS。
|
||||
query.Set("sslrootcert", "system")
|
||||
}
|
||||
if query.Get("connect_timeout") == "" {
|
||||
query.Set("connect_timeout", "10")
|
||||
}
|
||||
query.Set("gssencmode", "disable")
|
||||
parsed.RawQuery = query.Encode()
|
||||
env := append(postgresToolEnvironment(), "PGUSER="+username, "PGPASSWORD="+password)
|
||||
return parsed.String(), env, nil
|
||||
}
|
||||
|
||||
func (tools *postgresTools) validateVersions(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
major := 0
|
||||
for _, path := range []string{tools.dumpPath, tools.restorePath} {
|
||||
output := &limitedPostgresOutput{remaining: 4096}
|
||||
if err := tools.run(ctx, path, []string{"--version"}, postgresToolEnvironment(), output); err != nil {
|
||||
return errors.New("无法读取 PostgreSQL 工具版本,请检查 PATH 和可执行文件")
|
||||
}
|
||||
match := postgresVersionPattern.FindStringSubmatch(strings.TrimSpace(output.String()))
|
||||
if len(match) != 2 {
|
||||
return errors.New("无法识别 PostgreSQL 工具版本")
|
||||
}
|
||||
version, err := strconv.Atoi(match[1])
|
||||
if err != nil || version < 16 || (major != 0 && major != version) {
|
||||
return errors.New("pg_dump 和 pg_restore 必须具有相同主版本且不低于 16;pg_dump 也不得低于数据库服务端主版本")
|
||||
}
|
||||
major = version
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type limitedPostgresOutput struct {
|
||||
buffer bytes.Buffer
|
||||
remaining int
|
||||
}
|
||||
|
||||
func (output *limitedPostgresOutput) String() string { return output.buffer.String() }
|
||||
|
||||
func (output *limitedPostgresOutput) Write(value []byte) (int, error) {
|
||||
if len(value) > output.remaining {
|
||||
return 0, fmt.Errorf("PostgreSQL 工具输出超过安全上限")
|
||||
}
|
||||
output.remaining -= len(value)
|
||||
return output.buffer.Write(value)
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestPostgresToolConnectionKeepsSecretsOutOfArguments(t *testing.T) {
|
||||
t.Setenv("PGHOST", "unauthorized.example.test")
|
||||
t.Setenv("PGSERVICE", "unexpected-service")
|
||||
t.Setenv("PGOPTIONS", "-c search_path=unexpected")
|
||||
t.Setenv("PGSSLMODE", "disable")
|
||||
t.Setenv("PGPASSWORD", "unexpected-password")
|
||||
const password = "p@ss:' /\\\nword"
|
||||
parsed, _ := url.Parse("postgres://db.example.test:5432/scert?sslmode=verify-full&application_name=scert")
|
||||
parsed.User = url.UserPassword("backup-user", password)
|
||||
dsn, env, err := postgresToolConnection(config.DatabaseConfig{Driver: "postgres", DSN: parsed.String()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(dsn, password) || strings.Contains(dsn, "backup-user") {
|
||||
t.Fatal("账号密码不能进入命令行连接参数")
|
||||
}
|
||||
target, err := url.Parse(dsn)
|
||||
if err != nil || target.Host != "db.example.test:5432" || target.Path != "/scert" || target.User != nil {
|
||||
t.Fatal("维护连接改变了数据库目标")
|
||||
}
|
||||
for key, value := range map[string]string{"sslmode": "verify-full", "sslrootcert": "system", "connect_timeout": "10", "gssencmode": "disable"} {
|
||||
if target.Query().Get(key) != value {
|
||||
t.Fatalf("维护连接参数未保留安全值:%s", key)
|
||||
}
|
||||
}
|
||||
for _, entry := range env {
|
||||
if strings.HasPrefix(entry, "PGHOST=") || strings.HasPrefix(entry, "PGSERVICE=") || strings.HasPrefix(entry, "PGOPTIONS=") || strings.Contains(entry, "unexpected-password") {
|
||||
t.Fatal("工具不应继承外部 PostgreSQL 连接环境")
|
||||
}
|
||||
}
|
||||
if !slices.Contains(env, "PGUSER=backup-user") || !slices.Contains(env, "PGPASSWORD="+password) || !slices.Contains(env, "PGPASSFILE="+os.DevNull) {
|
||||
t.Fatal("子进程专属凭据环境不完整")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresToolConnectionRejectsAmbiguousParameters(t *testing.T) {
|
||||
for _, raw := range []string{
|
||||
"postgres://owner:password@db.test/scert?sslmode=disable",
|
||||
"postgres://owner:password@db.test/scert?sslmode=verify-full&password=override",
|
||||
"postgres://owner:password@db.test/scert?sslmode=verify-full&host=other.test",
|
||||
"postgres://owner:password@db.test/scert?sslmode=verify-full&options=-c+search_path%3Dother",
|
||||
"postgres://owner:password@db.test/scert?sslmode=verify-full&service=other",
|
||||
"postgres://owner:p%00word@db.test/scert?sslmode=verify-full",
|
||||
"postgres://db.test/scert?sslmode=verify-full",
|
||||
} {
|
||||
if _, _, err := postgresToolConnection(config.DatabaseConfig{Driver: "postgres", DSN: raw}); err == nil {
|
||||
t.Fatal("不安全或语义不一致的维护连接未被拒绝")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresToolConnectionPreservesExplicitTLSFiles(t *testing.T) {
|
||||
parsed, _ := url.Parse("postgres://owner:password@db.test/scert?sslmode=verify-full")
|
||||
query := parsed.Query()
|
||||
for key, value := range map[string]string{"sslrootcert": "/secure/ca bundle.pem", "sslcert": "/secure/client.pem", "sslkey": "/secure/client.key", "connect_timeout": "7"} {
|
||||
query.Set(key, value)
|
||||
}
|
||||
parsed.RawQuery = query.Encode()
|
||||
dsn, _, err := postgresToolConnection(config.DatabaseConfig{Driver: "postgres", DSN: parsed.String()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, _ := url.Parse(dsn)
|
||||
for key, value := range query {
|
||||
if actual.Query().Get(key) != value[0] {
|
||||
t.Fatalf("维护连接改变了显式 TLS 或超时配置:%s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresToolOutputLimitAppliesToCopy(t *testing.T) {
|
||||
output := &limitedPostgresOutput{remaining: 4}
|
||||
if _, err := io.Copy(output, strings.NewReader("output-over-limit")); err == nil {
|
||||
t.Fatal("工具输出不能通过流式复制绕过容量限制")
|
||||
}
|
||||
if len(output.String()) > 4 {
|
||||
t.Fatal("超限输出不应保留在内存中")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresToolsRequireCompatibleVersions(t *testing.T) {
|
||||
for _, pair := range [][2]string{{"16.4", "16.9"}, {"15.9", "15.9"}, {"16.4", "17.2"}, {"invalid", "16.4"}} {
|
||||
t.Run(strings.Join(pair[:], "-"), func(t *testing.T) {
|
||||
tools := &postgresTools{dumpPath: "pg_dump", restorePath: "pg_restore"}
|
||||
tools.run = func(ctx context.Context, path string, args, env []string, output io.Writer) error {
|
||||
if ctx.Err() != nil || !slices.Equal(args, []string{"--version"}) {
|
||||
t.Fatal("版本检查参数无效")
|
||||
}
|
||||
version := pair[0]
|
||||
if path == "pg_restore" {
|
||||
version = pair[1]
|
||||
}
|
||||
_, err := io.WriteString(output, path+" (PostgreSQL) "+version+"\n")
|
||||
return err
|
||||
}
|
||||
err := tools.validateVersions(t.Context())
|
||||
if (err == nil) != (pair[0] == "16.4" && pair[1] == "16.9") {
|
||||
t.Fatalf("版本检查结果不符:%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresMissingToolsFailBeforeBackupFilesystemChanges(t *testing.T) {
|
||||
t.Setenv("PATH", t.TempDir())
|
||||
if _, err := loadPostgresTools(t.Context()); err == nil {
|
||||
t.Fatal("没有官方工具时必须明确拒绝")
|
||||
}
|
||||
root := t.TempDir()
|
||||
output := filepath.Join(root, "missing", "backup")
|
||||
err := createBackup(t.Context(), config.Config{Database: config.DatabaseConfig{Driver: "postgres"}}, "unused.json", output)
|
||||
if err == nil {
|
||||
t.Fatal("缺少工具时不能生成备份")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Dir(output)); !os.IsNotExist(err) {
|
||||
t.Fatal("工具检查失败前不应创建输出目录")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresToolHelperProcess(t *testing.T) {
|
||||
mode := os.Getenv("SCERT_POSTGRES_TOOL_TEST")
|
||||
if mode == "" {
|
||||
return
|
||||
}
|
||||
if mode == "cancel" {
|
||||
time.Sleep(30 * time.Second)
|
||||
} else {
|
||||
_, _ = io.WriteString(os.Stderr, "sensitive-database-value")
|
||||
os.Exit(7)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresToolExecutionRedactsErrorsAndCancels(t *testing.T) {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, mode := range []string{"failure", "cancel"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
err := executePostgresTool(ctx, executable, []string{"-test.run=^TestPostgresToolHelperProcess$"}, append(postgresToolEnvironment(), "SCERT_POSTGRES_TOOL_TEST="+mode), io.Discard)
|
||||
if err == nil || strings.Contains(err.Error(), "sensitive-database-value") || strings.Contains(err.Error(), executable) {
|
||||
t.Fatal("工具错误必须脱敏并保持失败状态")
|
||||
}
|
||||
if mode == "cancel" && !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal("超时必须终止工具并返回取消原因")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user