完善证书管理与邮件通知流程
This commit is contained in:
1 parent
c74372284a
commit
b2833b761d
60 files changed
+7340
-254
No files matched your search
@@ -50,9 +50,32 @@ func Open(ctx context.Context, cfg config.DatabaseConfig) (*Database, error) {
|
||||
_ = db.Close()
|
||||
return nil, fmt.Errorf("连接数据库失败:%w", err)
|
||||
}
|
||||
if cfg.Driver == "sqlite" {
|
||||
if err := configureSQLite(pingCtx, db); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &Database{DB: db}, nil
|
||||
}
|
||||
|
||||
func configureSQLite(ctx context.Context, db *sql.DB) error {
|
||||
if _, err := db.ExecContext(ctx, "PRAGMA busy_timeout = 5000"); err != nil {
|
||||
return fmt.Errorf("配置 SQLite 忙等待失败:%w", err)
|
||||
}
|
||||
var mode string
|
||||
if _, err := db.ExecContext(ctx, "PRAGMA journal_mode = WAL"); err != nil {
|
||||
return fmt.Errorf("配置 SQLite WAL 失败:%w", err)
|
||||
}
|
||||
if err := db.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&mode); err != nil {
|
||||
return fmt.Errorf("读取 SQLite 日志模式失败:%w", err)
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(mode), "memory") || strings.EqualFold(strings.TrimSpace(mode), "wal") {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("SQLite 日志模式无效:%s", mode)
|
||||
}
|
||||
|
||||
func normalizeMySQLDSN(dsn string) (string, error) {
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
@@ -63,6 +86,21 @@ func normalizeMySQLDSN(dsn string) (string, error) {
|
||||
return cfg.FormatDSN(), nil
|
||||
}
|
||||
|
||||
// SanitizeDSN 返回可写入日志的数据库连接信息,不包含 MySQL 密码。
|
||||
func SanitizeDSN(driver, dsn string) string {
|
||||
if strings.EqualFold(strings.TrimSpace(driver), "mysql") {
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
return "mysql://[连接信息无效]"
|
||||
}
|
||||
if cfg.Passwd != "" {
|
||||
cfg.Passwd = "********"
|
||||
}
|
||||
return cfg.FormatDSN()
|
||||
}
|
||||
return dsn
|
||||
}
|
||||
|
||||
func ensureSQLiteDir(dsn string) error {
|
||||
path := dsn
|
||||
if strings.HasPrefix(path, "file:") {
|
||||
@@ -88,3 +126,14 @@ func (d *Database) Close() error {
|
||||
}
|
||||
return d.DB.Close()
|
||||
}
|
||||
|
||||
// Ping 检查数据库连接是否可用。
|
||||
func (d *Database) Ping(ctx context.Context) error {
|
||||
if d == nil || d.DB == nil {
|
||||
return fmt.Errorf("数据库连接不能为空")
|
||||
}
|
||||
if err := d.DB.PingContext(ctx); err != nil {
|
||||
return fmt.Errorf("数据库健康检查失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func TestOpenAndMigrateSQLite(t *testing.T) {
|
||||
if err := db.DB.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&count); err != nil {
|
||||
t.Fatalf("读取迁移版本失败:%v", err)
|
||||
}
|
||||
if count != 3 {
|
||||
if count != 4 {
|
||||
t.Fatalf("迁移版本数量错误:%d", count)
|
||||
}
|
||||
var autoRenew bool
|
||||
@@ -33,3 +33,31 @@ func TestOpenAndMigrateSQLite(t *testing.T) {
|
||||
t.Fatalf("检查域名选项字段失败:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMySQLMigrationStatementsRecognizeRecoverableChanges(t *testing.T) {
|
||||
cases := []struct {
|
||||
statement string
|
||||
table string
|
||||
name string
|
||||
}{
|
||||
{statement: "ALTER TABLE domains ADD COLUMN auto_renew BOOLEAN NOT NULL DEFAULT TRUE", table: "domains", name: "auto_renew"},
|
||||
{statement: "ALTER TABLE `tasks` ADD COLUMN `stage` VARCHAR(32) NOT NULL DEFAULT 'queued'", table: "tasks", name: "stage"},
|
||||
{statement: "CREATE INDEX idx_tasks_status ON tasks(status)", table: "tasks", name: "idx_tasks_status"},
|
||||
{statement: "CREATE INDEX `idx_tasks_idempotency` ON `tasks`(idempotency_key)", table: "tasks", name: "idx_tasks_idempotency"},
|
||||
}
|
||||
for _, item := range cases {
|
||||
if matches := mysqlAddColumnPattern.FindStringSubmatch(item.statement); len(matches) == 3 {
|
||||
if matches[1] != item.table || matches[2] != item.name {
|
||||
t.Errorf("字段迁移解析结果不正确:statement=%q matches=%v", item.statement, matches)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if matches := mysqlCreateIndexPattern.FindStringSubmatch(item.statement); len(matches) == 3 {
|
||||
if matches[1] != item.name || matches[2] != item.table {
|
||||
t.Errorf("索引迁移解析结果不正确:statement=%q matches=%v", item.statement, matches)
|
||||
}
|
||||
continue
|
||||
}
|
||||
t.Errorf("可恢复的 MySQL 迁移语句未被识别:%q", item.statement)
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,11 @@ import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/sqlite/0002_domain_options.sql migrations/sqlite/0003_deployment.sql migrations/mysql/0001_initial.sql migrations/mysql/0002_domain_options.sql migrations/mysql/0003_deployment.sql
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/sqlite/0002_domain_options.sql migrations/sqlite/0003_deployment.sql migrations/sqlite/0004_task_attempts.sql migrations/mysql/0001_initial.sql migrations/mysql/0002_domain_options.sql migrations/mysql/0003_deployment.sql migrations/mysql/0004_task_attempts.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
@@ -37,7 +38,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const latestMigrationVersion = 3
|
||||
const latestMigrationVersion = 4
|
||||
|
||||
func applyMigration(ctx context.Context, db *sql.DB, driver string, version int) error {
|
||||
path := fmt.Sprintf("migrations/%s/%04d_%s.sql", driver, version, migrationName(version))
|
||||
@@ -48,6 +49,13 @@ func applyMigration(ctx context.Context, db *sql.DB, driver string, version int)
|
||||
statements := migrationStatements(content)
|
||||
if driver == "mysql" {
|
||||
for _, statement := range statements {
|
||||
skip, err := shouldSkipMySQLStatement(ctx, db, statement)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查数据库迁移状态失败:%w", err)
|
||||
}
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("执行数据库迁移失败:%w", err)
|
||||
}
|
||||
@@ -77,6 +85,34 @@ func applyMigration(ctx context.Context, db *sql.DB, driver string, version int)
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
mysqlAddColumnPattern = regexp.MustCompile(`(?is)^ALTER\s+TABLE\s+` + "`?([a-zA-Z0-9_]+)`?" + `\s+ADD\s+COLUMN\s+` + "`?([a-zA-Z0-9_]+)`?")
|
||||
mysqlCreateIndexPattern = regexp.MustCompile(`(?is)^CREATE\s+INDEX\s+` + "`?([a-zA-Z0-9_]+)`?" + `\s+ON\s+` + "`?([a-zA-Z0-9_]+)`?")
|
||||
)
|
||||
|
||||
// shouldSkipMySQLStatement 检查可恢复迁移中的字段和索引是否已经存在。
|
||||
// MySQL 5.7 不支持 ADD COLUMN IF NOT EXISTS,因此需要在执行前查询元数据。
|
||||
func shouldSkipMySQLStatement(ctx context.Context, db *sql.DB, statement string) (bool, error) {
|
||||
statement = strings.TrimSpace(statement)
|
||||
if matches := mysqlAddColumnPattern.FindStringSubmatch(statement); len(matches) == 3 {
|
||||
var count int
|
||||
err := db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`, matches[1], matches[2]).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
if matches := mysqlCreateIndexPattern.FindStringSubmatch(statement); len(matches) == 3 {
|
||||
var count int
|
||||
err := db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?`, matches[2], matches[1]).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func migrationName(version int) string {
|
||||
switch version {
|
||||
case 1:
|
||||
@@ -85,6 +121,8 @@ func migrationName(version int) string {
|
||||
return "domain_options"
|
||||
case 3:
|
||||
return "deployment"
|
||||
case 4:
|
||||
return "task_attempts"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE tasks ADD COLUMN stage VARCHAR(32) NOT NULL DEFAULT 'queued';
|
||||
ALTER TABLE tasks ADD COLUMN operator_id VARCHAR(128) NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN certificate_version_id VARCHAR(128) NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN idempotency_key VARCHAR(255) NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
||||
CREATE INDEX idx_tasks_status ON tasks(status);
|
||||
CREATE INDEX idx_tasks_idempotency ON tasks(idempotency_key);
|
||||
CREATE TABLE IF NOT EXISTS task_attempts (
|
||||
id VARCHAR(36) NOT NULL PRIMARY KEY,
|
||||
task_id VARCHAR(36) NOT NULL,
|
||||
attempt_number INT NOT NULL,
|
||||
stage VARCHAR(32) NOT NULL,
|
||||
status VARCHAR(32) NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
started_at DATETIME NULL,
|
||||
finished_at DATETIME NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE KEY uq_task_attempt (task_id, attempt_number),
|
||||
INDEX idx_task_attempts_task_id (task_id)
|
||||
);
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE tasks ADD COLUMN stage TEXT NOT NULL DEFAULT 'queued';
|
||||
ALTER TABLE tasks ADD COLUMN operator_id TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN certificate_version_id TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN idempotency_key TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_status ON tasks(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_idempotency ON tasks(idempotency_key);
|
||||
CREATE TABLE IF NOT EXISTS task_attempts (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
task_id TEXT NOT NULL,
|
||||
attempt_number INTEGER NOT NULL,
|
||||
stage TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
started_at DATETIME NULL,
|
||||
finished_at DATETIME NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (task_id, attempt_number)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_task_attempts_task_id ON task_attempts(task_id);
|
||||
@@ -0,0 +1,42 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestDatabasePingAndSQLitePragmas(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir()).Database
|
||||
db, err := Open(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("打开 SQLite 失败:%v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := db.Ping(context.Background()); err != nil {
|
||||
t.Fatalf("数据库健康检查失败:%v", err)
|
||||
}
|
||||
var timeout int
|
||||
if err := db.DB.QueryRow("PRAGMA busy_timeout").Scan(&timeout); err != nil {
|
||||
t.Fatalf("读取 SQLite 忙等待失败:%v", err)
|
||||
}
|
||||
if timeout != 5000 {
|
||||
t.Fatalf("SQLite 忙等待不正确:%d", timeout)
|
||||
}
|
||||
var mode string
|
||||
if err := db.DB.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil {
|
||||
t.Fatalf("读取 SQLite 日志模式失败:%v", err)
|
||||
}
|
||||
if mode != "memory" && mode != "wal" {
|
||||
t.Fatalf("SQLite 日志模式不正确:%s", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeDSNRedactsMySQLPassword(t *testing.T) {
|
||||
sanitized := SanitizeDSN("mysql", "user:secret@tcp(127.0.0.1:3306)/scert?charset=utf8mb4")
|
||||
if sanitized == "" || strings.Contains(sanitized, "secret") {
|
||||
t.Fatalf("MySQL DSN 未正确脱敏:%s", sanitized)
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func TestSQLiteMigrationIsIdempotentAndCreatesBaseTables(t *testing.T) {
|
||||
if err := db.DB.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&versions); err != nil {
|
||||
t.Fatalf("读取迁移版本失败:%v", err)
|
||||
}
|
||||
if versions != 3 {
|
||||
if versions != 4 {
|
||||
t.Fatalf("重复迁移写入了多条版本记录:%d", versions)
|
||||
}
|
||||
var tables int
|
||||
|
||||
Reference in new issue
Block a user