1 parent
dc188b1ed8
commit
d6cc81ed87
53 files changed
+909
-127
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
// main 提供部署端显式配置迁移,不在网页或普通启动中自动改写配置。
|
||||
@@ -21,6 +22,7 @@ func run(args []string) error {
|
||||
flags := flag.NewFlagSet("smail-config", flag.ContinueOnError)
|
||||
root := flags.String("root", "", "Smail 服务根目录,包含 config 子目录")
|
||||
apply := flags.Bool("apply", false, "确认已停止后端,备份并执行迁移;默认只检查")
|
||||
runtimeData := flags.Bool("runtime-data", false, "将运行数据迁到 data,保留当前五个配置文件")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -32,6 +34,27 @@ func run(args []string) error {
|
||||
return fmt.Errorf("服务根目录无效")
|
||||
}
|
||||
options := config.RuntimeValidationOptions{HasSuperAdmin: true, Production: true}
|
||||
if *runtimeData {
|
||||
moves, err := runtimepath.Plan(absolute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !*apply {
|
||||
fmt.Printf("检查通过,共 %d 项待迁移;停止后端后加 --apply 执行。\n", len(moves))
|
||||
return nil
|
||||
}
|
||||
if _, err := config.NewConfigLoader(absolute, config.ConfigLoaderOptions{Validation: options}).Load(); err != nil {
|
||||
// 旧单文件部署先搬运行数据,再执行原有的分区配置迁移。
|
||||
if _, legacyErr := config.PlanLegacyMigration(absolute, options); legacyErr != nil {
|
||||
return fmt.Errorf("当前配置校验失败,未执行迁移")
|
||||
}
|
||||
}
|
||||
if _, err := runtimepath.Migrate(absolute); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("运行数据迁移完成,共 %d 项;当前五个配置文件未改写,废弃配置保留在 data/legacy-config。\n", len(moves))
|
||||
return nil
|
||||
}
|
||||
plan, err := config.PlanLegacyMigration(absolute, options)
|
||||
if err != nil {
|
||||
// 不输出配置原文、未知字段或底层错误,避免凭据进入终端记录。
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
const ConfigAuditRetention = 365 * 24 * time.Hour
|
||||
@@ -45,7 +46,7 @@ func NewFileAuditWriter(root string) (*FileAuditWriter, error) {
|
||||
if strings.TrimSpace(root) == "" {
|
||||
return nil, ErrAuditStorageUnavailable
|
||||
}
|
||||
directory, err := secureManagerConfigDirectory(root)
|
||||
directory, err := runtimepath.Directory(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
"git.pcux.cn/xuans/Smail/utils/errors"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -275,7 +276,7 @@ func legacyInitReady() bool {
|
||||
}
|
||||
|
||||
func ReadPrivateKey() (*ecdsa.PrivateKey, bool, error) {
|
||||
privateKey, created, err := loadOrCreateACMEPrivateKey(filepath.Join(ROOT_PATH, "config", "ssl"), "account_private.pem")
|
||||
privateKey, created, err := loadOrCreateACMEPrivateKey(filepath.Join(ROOT_PATH, "data", "ssl"), "account_private.pem")
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
@@ -331,9 +332,9 @@ func ReadConfig() (*Config, error) {
|
||||
|
||||
func readConfig() (*Config, error) {
|
||||
configData := Config{
|
||||
DkimPrivateKeyPath: ROOT_PATH + "config/dkim/dkim.priv",
|
||||
SSLPrivateKeyPath: ROOT_PATH + "config/ssl/private.key",
|
||||
SSLPublicKeyPath: ROOT_PATH + "config/ssl/public.crt",
|
||||
DkimPrivateKeyPath: ROOT_PATH + "data/dkim/dkim.priv",
|
||||
SSLPrivateKeyPath: ROOT_PATH + "data/ssl/private.key",
|
||||
SSLPublicKeyPath: ROOT_PATH + "data/ssl/public.crt",
|
||||
VerificationEnabled: true,
|
||||
ExternalRecoveryEnabled: true,
|
||||
LoginPolicy: "new_device",
|
||||
@@ -445,7 +446,7 @@ func persistControlledSSLPath(value string) string {
|
||||
}
|
||||
|
||||
func resolvePath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
value = runtimepath.LegacyReference(strings.TrimSpace(value))
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
import "git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
|
||||
// normalizeSSLDataReferences 在原始摘要校验后兼容旧引用,不修改历史备份的字节及摘要。
|
||||
func normalizeSSLDataReferences(ssl SSLConfig) SSLConfig {
|
||||
ssl.DefaultDirectory = runtimepath.LegacyReference(ssl.DefaultDirectory)
|
||||
ssl.CustomDirectory = runtimepath.LegacyReference(ssl.CustomDirectory)
|
||||
ssl.CertificateRef = runtimepath.LegacyReference(ssl.CertificateRef)
|
||||
ssl.PrivateKeyRef = runtimepath.LegacyReference(ssl.PrivateKeyRef)
|
||||
return ssl
|
||||
}
|
||||
@@ -89,6 +89,7 @@ func (l *ConfigLoader) Load() (RuntimeConfig, error) {
|
||||
if err := ValidateRuntimeConfig(runtime, l.options.Validation); err != nil {
|
||||
return RuntimeConfig{}, err
|
||||
}
|
||||
runtime.SSL.TLS = normalizeSSLDataReferences(runtime.SSL.TLS)
|
||||
return cloneRuntimeConfig(runtime), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
fileutil "git.pcux.cn/xuans/Smail/utils/file"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -335,7 +336,7 @@ func secureManagerConfigDirectory(root string) (string, error) {
|
||||
}
|
||||
|
||||
func secureBackupDirectory(root string) (string, error) {
|
||||
configDirectory, err := secureManagerConfigDirectory(root)
|
||||
configDirectory, err := runtimepath.Directory(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -141,10 +141,10 @@ func TestLoadManagerRejectsSymlinkConfigFile(t *testing.T) {
|
||||
func TestBackupRejectsSymlinkDirectoryAndEntry(t *testing.T) {
|
||||
t.Run("directory", func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(root, "config"), 0700); err != nil {
|
||||
if err := os.MkdirAll(filepath.Join(root, "data"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(t.TempDir(), filepath.Join(root, "config", "backups")); err != nil {
|
||||
if err := os.Symlink(t.TempDir(), filepath.Join(root, "data", "backups")); err != nil {
|
||||
t.Skipf("symlink is unavailable: %v", err)
|
||||
}
|
||||
if _, err := BackupConfig(root, RuntimeSnapshot{Config: testRootConfig(), Generation: 1}, time.Now()); err == nil {
|
||||
@@ -153,7 +153,7 @@ func TestBackupRejectsSymlinkDirectoryAndEntry(t *testing.T) {
|
||||
})
|
||||
t.Run("entry", func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
directory := filepath.Join(root, "config", "backups")
|
||||
directory := filepath.Join(root, "data", "backups")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -196,7 +196,7 @@ func TestBackupRetentionKeepsExactlyFourRecentManagedFiles(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "backups"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -810,11 +810,11 @@ func TestConfigBackupRejectsTamperedPartition(t *testing.T) {
|
||||
if _, err := store.CreateBackup(runtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "backups-v2"))
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("backup entries = %v, err = %v", entries, err)
|
||||
}
|
||||
path := filepath.Join(root, "config", "backups-v2", entries[0].Name(), "files", "email.json")
|
||||
path := filepath.Join(root, "data", "backups-v2", entries[0].Name(), "files", "email.json")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -853,11 +853,11 @@ func TestConfigManagerRejectsUndecryptableRollbackBackup(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "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())
|
||||
backupDirectory := filepath.Join(root, "data", "backups-v2", entries[0].Name())
|
||||
rootPath := filepath.Join(backupDirectory, "files", "config.json")
|
||||
rootData, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
@@ -925,7 +925,7 @@ func TestConfigBackupRetentionKeepsFourRecentBackups(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1303,14 +1303,14 @@ func containsAny(value string, terms ...string) bool {
|
||||
|
||||
func findTransactionDirectory(t *testing.T, root string) string {
|
||||
t.Helper()
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "transaction"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "transaction"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 || !entries[0].IsDir() {
|
||||
t.Fatalf("transaction entries = %+v", entries)
|
||||
}
|
||||
return filepath.Join(root, "config", "transaction", entries[0].Name())
|
||||
return filepath.Join(root, "data", "transaction", entries[0].Name())
|
||||
}
|
||||
|
||||
func TestTransactionRecoveryRejectsTamperedArtifacts(t *testing.T) {
|
||||
@@ -1462,7 +1462,7 @@ func TestConfigBackupScheduleUsesSevenDayInterval(t *testing.T) {
|
||||
if err != nil || !created || atDue == path {
|
||||
t.Fatalf("at due path=%q created=%v err=%v, want new backup", atDue, created, err)
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1484,7 +1484,7 @@ func TestConfigBackupScheduleCreatesSameGenerationBackupWhenDue(t *testing.T) {
|
||||
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"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1500,7 +1500,7 @@ func TestConfigBackupListingAndLoadingIgnoreTemporaryDirectories(t *testing.T) {
|
||||
if _, err := store.CreateBackup(runtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
backupDirectory := filepath.Join(root, "data", "backups-v2")
|
||||
if err := os.Mkdir(filepath.Join(backupDirectory, ".interrupted.tmp"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1528,7 +1528,7 @@ func TestConfigBackupCleanupKeepsOnlyExpiredValidBackup(t *testing.T) {
|
||||
if _, err := store.createBackupAt(runtime, createdAt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
backupDirectory := filepath.Join(root, "data", "backups-v2")
|
||||
if err := cleanupConfigBackups(backupDirectory, createdAt.Add(configBackupMaxAge+time.Nanosecond), store.validation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1553,7 +1553,7 @@ func TestConfigBackupRetentionHonorsThirtyDayBoundary(t *testing.T) {
|
||||
if _, err := store.createBackupAt(second, base.Add(configBackupMaxAge)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
backupDirectory := filepath.Join(root, "data", "backups-v2")
|
||||
entries, err := os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -1596,7 +1596,7 @@ func TestConfigBackupFailureDoesNotDeleteExistingValidBackup(t *testing.T) {
|
||||
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"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/plugins/catalog"
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -178,11 +179,15 @@ func MigrateLegacyConfig(root string, options RuntimeValidationOptions) (LegacyM
|
||||
if err := ValidateRuntimeConfig(runtime, options); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
backupPath, err := writeLegacyMigrationBackup(directory, legacyData, time.Now().UTC())
|
||||
dataDirectory, err := runtimepath.Directory(root)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if err := preserveLegacyMigrationSource(directory, legacyData); err != nil {
|
||||
backupPath, err := writeLegacyMigrationBackup(dataDirectory, legacyData, time.Now().UTC())
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if err := preserveLegacyMigrationSource(dataDirectory, legacyData); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
@@ -614,6 +619,7 @@ func legacySQLiteLocalRef(value string) (string, bool) {
|
||||
value = filepath.ToSlash(value)
|
||||
value = strings.TrimPrefix(value, "./")
|
||||
value = strings.TrimPrefix(value, "config/")
|
||||
value = strings.TrimPrefix(value, "data/")
|
||||
if value == "" || strings.HasPrefix(value, "../") || value == ".." || strings.Contains(value, "/../") || strings.Contains(value, "\\") || strings.Contains(value, ":") {
|
||||
return "", false
|
||||
}
|
||||
@@ -736,7 +742,9 @@ func legacySMTPConfig(root string, vault *credential.Vault, legacy legacyMigrati
|
||||
func legacySSLConfig(legacy legacyMigrationConfig) (SSLConfig, []string) {
|
||||
certificateRef, certificateOK := legacyControlledReference(legacy.SSLPublicKeyPath)
|
||||
privateKeyRef, privateKeyOK := legacyControlledReference(legacy.SSLPrivateKeyPath)
|
||||
ssl := SSLConfig{Management: SSLManagementProject, Mode: ManagedSSLModeDefault, DefaultDirectory: "config/ssl"}
|
||||
certificateRef = runtimepath.LegacyReference(certificateRef)
|
||||
privateKeyRef = runtimepath.LegacyReference(privateKeyRef)
|
||||
ssl := SSLConfig{Management: SSLManagementProject, Mode: ManagedSSLModeDefault, DefaultDirectory: "data/ssl"}
|
||||
switch legacy.SSLType {
|
||||
case SSLTypeAutoDNS:
|
||||
ssl.Challenge = SSLChallengeDNS
|
||||
@@ -749,7 +757,7 @@ func legacySSLConfig(legacy legacyMigrationConfig) (SSLConfig, []string) {
|
||||
ssl.Management = SSLManagementWebServer
|
||||
}
|
||||
ssl.Mode = ManagedSSLModeUpload
|
||||
ssl.CustomDirectory = "config/ssl"
|
||||
ssl.CustomDirectory = "data/ssl"
|
||||
}
|
||||
if legacy.SSLPublicKeyPath == "" && legacy.SSLPrivateKeyPath == "" {
|
||||
return ssl, nil
|
||||
|
||||
@@ -64,7 +64,7 @@ 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))
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "data", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -206,7 +206,7 @@ func TestMigrateLegacyConfigResumesAfterNonSecretPartitionWrite(t *testing.T) {
|
||||
if _, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(directory, legacyMigrationSourceCopy))
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "data", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -357,7 +357,10 @@ func TestMigrateLegacyConfigRejectsConflictingPreservedSource(t *testing.T) {
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
legacyCopyPath := filepath.Join(root, "config", legacyMigrationSourceCopy)
|
||||
if err := os.MkdirAll(filepath.Join(root, "data"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyCopyPath := filepath.Join(root, "data", legacyMigrationSourceCopy)
|
||||
conflictingCopy := []byte(`{"dbDSN":"different.db","dbType":"sqlite"}`)
|
||||
if err := os.WriteFile(legacyCopyPath, conflictingCopy, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -419,7 +422,7 @@ func TestMigrateLegacyConfigRejectsRepeatedMigrationAfterSuccess(t *testing.T) {
|
||||
if !bytes.Equal(after, migrated) {
|
||||
t.Fatal("repeated migration changed the partitioned root")
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "config", legacyMigrationSourceCopy))
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "data", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) {
|
||||
HttpPort: root.HTTP.Port, HttpsPort: root.HTTP.HTTPSPort,
|
||||
HttpsEnabled: 2, TLSMode: root.HTTP.TLSMode,
|
||||
TrustedProxyCIDRs: append([]string(nil), root.HTTP.TrustedProxyCIDRs...),
|
||||
DkimPrivateKeyPath: filepath.Join(ROOT_PATH, "config", "dkim", "dkim.priv"),
|
||||
DkimPrivateKeyPath: filepath.Join(ROOT_PATH, "data", "dkim", "dkim.priv"),
|
||||
SSLType: SSLTypeUser,
|
||||
SpamFilterLevel: runtime.Email.Policy.SpamFilterLevel,
|
||||
LoginPolicy: runtime.SMTP.Relay.LoginPolicy,
|
||||
@@ -77,7 +77,7 @@ func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
tls := runtime.SSL.TLS
|
||||
tls := normalizeSSLDataReferences(runtime.SSL.TLS)
|
||||
if EffectiveSSLManagement(tls) == SSLManagementProject {
|
||||
if tls.Challenge == SSLChallengeDNS {
|
||||
cfg.SSLType = SSLTypeAutoDNS
|
||||
@@ -97,7 +97,8 @@ func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) {
|
||||
|
||||
func compatibilityDatabase(database DatabaseConfig, vault *credential.Vault) (string, string, error) {
|
||||
if database.Type == DatabaseTypeSQLite {
|
||||
return DBTypeSQLite, filepath.Join(ROOT_PATH, "config", database.LocalRef), nil
|
||||
path := filepath.Join(ROOT_PATH, "data", database.LocalRef)
|
||||
return DBTypeSQLite, path, nil
|
||||
}
|
||||
password, err := OpenStoredSecret(vault, database.Password)
|
||||
if err != nil {
|
||||
|
||||
@@ -73,10 +73,10 @@ func TestPartitionedCompatibilityStartupAndWriteProtection(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := CurrentConfig()
|
||||
if !cfg.IsSetupReady() || cfg.DbType != DBTypeSQLite || cfg.DbDSN != filepath.Join(root, "config", "existing.db") {
|
||||
if !cfg.IsSetupReady() || cfg.DbType != DBTypeSQLite || cfg.DbDSN != filepath.Join(root, "data", "existing.db") {
|
||||
t.Fatal("partitioned startup lost initialization or database identity")
|
||||
}
|
||||
if cfg.WebDomain != "mail.example.com" || cfg.SSLPublicKeyPath != filepath.Join(root, "config", "ssl", "site.crt") {
|
||||
if cfg.WebDomain != "mail.example.com" || cfg.SSLPublicKeyPath != filepath.Join(root, "data", "ssl", "site.crt") {
|
||||
t.Fatal("partitioned startup lost domain or certificate path")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type customSSLMaterial struct {
|
||||
}
|
||||
|
||||
func prepareCustomSSLPaths(root string, candidate *RuntimeConfig) (*customSSLMaterial, error) {
|
||||
candidate.SSL.TLS = normalizeSSLDataReferences(candidate.SSL.TLS)
|
||||
ssl := &candidate.SSL.TLS
|
||||
if EffectiveSSLManagement(*ssl) != SSLManagementCustom {
|
||||
return nil, nil
|
||||
@@ -56,8 +57,8 @@ func prepareCustomSSLPaths(root string, candidate *RuntimeConfig) (*customSSLMat
|
||||
return nil, err
|
||||
}
|
||||
ssl.Mode = ManagedSSLModeUpload
|
||||
ssl.DefaultDirectory = "config/ssl"
|
||||
ssl.CustomDirectory = "config/ssl"
|
||||
ssl.DefaultDirectory = "data/ssl"
|
||||
ssl.CustomDirectory = "data/ssl"
|
||||
// 仅在内存中使用合法占位引用完成跨分区校验,最终候选必须换成独立版本。
|
||||
ssl.CertificateRef = managedSSLReference(ManagedSSLModeUpload, "public.crt")
|
||||
ssl.PrivateKeyRef = managedSSLReference(ManagedSSLModeUpload, "private.key")
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,7 +26,7 @@ const (
|
||||
SSLManagementProject = "project"
|
||||
SSLManagementCustom = "custom"
|
||||
|
||||
managedSSLRelativeRoot = "config/ssl/managed"
|
||||
managedSSLRelativeRoot = "data/ssl/managed"
|
||||
maxManagedSSLFileSize = 1 << 20
|
||||
)
|
||||
|
||||
@@ -207,6 +208,7 @@ func ResolveManagedSSLReferences(ssl SSLConfig) (ManagedSSLReference, error) {
|
||||
}
|
||||
|
||||
func resolveManagedSSLReferencesAtRoot(root string, ssl SSLConfig) (ManagedSSLReference, error) {
|
||||
ssl = normalizeSSLDataReferences(ssl)
|
||||
if !validManagedSSLMode(ssl.Mode) {
|
||||
return ManagedSSLReference{}, ErrInvalidSSLMode
|
||||
}
|
||||
@@ -402,7 +404,7 @@ func resolveManagedReference(rootPath, reference, expectedName string) (string,
|
||||
}
|
||||
|
||||
func resolveManagedReferenceAtRoot(root, rootPath, reference, expectedName string) (string, error) {
|
||||
rawReference := strings.TrimSpace(reference)
|
||||
rawReference := runtimepath.LegacyReference(strings.TrimSpace(reference))
|
||||
if rawReference == "" || strings.Contains(rawReference, `\`) || filepath.IsAbs(rawReference) || isWindowsAbsolute(rawReference) {
|
||||
return "", ErrInvalidSSLReference
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestImportManagedSSLUsesInternalReferencesAndAtomicCurrent(t *testing.T) {
|
||||
if filepath.IsAbs(first.CertificateRef) || filepath.IsAbs(first.PrivateKeyRef) {
|
||||
t.Fatal("SSL 内部引用不应为绝对路径")
|
||||
}
|
||||
if first.CertificateRef != "config/ssl/managed/default/current/public.crt" || first.PrivateKeyRef != "config/ssl/managed/default/current/private.key" {
|
||||
if first.CertificateRef != "data/ssl/managed/default/current/public.crt" || first.PrivateKeyRef != "data/ssl/managed/default/current/private.key" {
|
||||
t.Fatalf("内部引用不符合预期: %+v", first)
|
||||
}
|
||||
resolved, err := ResolveManagedSSLReferences(SSLConfig{Mode: ManagedSSLModeDefault, DefaultDirectory: "config/ssl", CertificateRef: first.CertificateRef, PrivateKeyRef: first.PrivateKeyRef})
|
||||
@@ -236,7 +236,7 @@ func TestPrepareCustomSSLPathsImportsExternalFilesOnlyWhenSaving(t *testing.T) {
|
||||
if runtime.SSL.TLS.CertificateRef == certificatePath || runtime.SSL.TLS.PrivateKeyRef == privateKeyPath {
|
||||
t.Fatal("仅校验阶段不应保留外部证书路径")
|
||||
}
|
||||
managedRoot := filepath.Join(root, "config", "ssl", "managed", "upload")
|
||||
managedRoot := filepath.Join(root, "data", "ssl", "managed", "upload")
|
||||
if _, err := os.Stat(managedRoot); !os.IsNotExist(err) {
|
||||
t.Fatalf("仅校验阶段不应创建受管目录,error = %v", err)
|
||||
}
|
||||
@@ -247,7 +247,7 @@ func TestPrepareCustomSSLPathsImportsExternalFilesOnlyWhenSaving(t *testing.T) {
|
||||
if runtime.SSL.TLS.CertificateRef == certificatePath || runtime.SSL.TLS.PrivateKeyRef == privateKeyPath {
|
||||
t.Fatal("保存后不应继续使用外部证书路径")
|
||||
}
|
||||
if !strings.HasPrefix(runtime.SSL.TLS.CertificateRef, "config/ssl/managed/upload/version-") {
|
||||
if !strings.HasPrefix(runtime.SSL.TLS.CertificateRef, "data/ssl/managed/upload/version-") {
|
||||
t.Fatalf("证书引用未切换到受管版本: %q", runtime.SSL.TLS.CertificateRef)
|
||||
}
|
||||
if _, err := ResolveManagedSSLReferences(runtime.SSL.TLS); err != nil {
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
fileutil "git.pcux.cn/xuans/Smail/utils/file"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
type TransactionPhase string
|
||||
@@ -518,7 +519,11 @@ func validateTransactionManifest(manifest TransactionManifest) error {
|
||||
}
|
||||
|
||||
func secureTransactionDirectory(configDirectory string) (string, error) {
|
||||
directory := filepath.Join(configDirectory, "transaction")
|
||||
dataDirectory, err := runtimepath.Directory(filepath.Dir(configDirectory))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
directory := filepath.Join(dataDirectory, "transaction")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -595,7 +600,7 @@ func (s *TransactionStore) CreateBackupIfDue(runtime RuntimeConfig, now time.Tim
|
||||
return "", false, err
|
||||
}
|
||||
if found && now.Before(latest.CreatedAt.Add(configBackupInterval)) {
|
||||
return filepath.Join(s.root, "config", "backups-v2", latest.ID), false, nil
|
||||
return filepath.Join(s.root, "data", "backups-v2", latest.ID), false, nil
|
||||
}
|
||||
path, err := s.createBackupAt(runtime, now)
|
||||
return path, true, err
|
||||
@@ -757,7 +762,11 @@ func (s *TransactionStore) LoadBackup(generation uint64) (RuntimeConfig, error)
|
||||
}
|
||||
|
||||
func secureConfigBackupDirectory(configDirectory string) (string, error) {
|
||||
directory := filepath.Join(configDirectory, "backups-v2")
|
||||
dataDirectory, err := runtimepath.Directory(filepath.Dir(configDirectory))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
directory := filepath.Join(dataDirectory, "backups-v2")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -862,6 +871,7 @@ func parseRuntimeFiles(files map[string][]byte, validation RuntimeValidationOpti
|
||||
if err := ValidateRuntimeConfig(runtime, validation); err != nil {
|
||||
return RuntimeConfig{}, err
|
||||
}
|
||||
runtime.SSL.TLS = normalizeSSLDataReferences(runtime.SSL.TLS)
|
||||
return runtime, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ func Init() error {
|
||||
}
|
||||
|
||||
func loadPrivateKey(path string) (crypto.Signer, error) {
|
||||
basePath := filepath.Join(config.ROOT_PATH, "config", "dkim")
|
||||
basePath := filepath.Join(config.ROOT_PATH, "data", "dkim")
|
||||
privateName, err := securefile.ControlledRelativePath(basePath, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -93,7 +93,7 @@ func prepareDKIMTestDirectory(t *testing.T) (string, string) {
|
||||
oldRoot := config.ROOT_PATH
|
||||
root := t.TempDir()
|
||||
t.Cleanup(func() { config.ROOT_PATH = oldRoot })
|
||||
directory := filepath.Join(root, "config", "dkim")
|
||||
directory := filepath.Join(root, "data", "dkim")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -8,11 +8,15 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/internal/buildinfo"
|
||||
"git.pcux.cn/xuans/Smail/listen/cron_server"
|
||||
"git.pcux.cn/xuans/Smail/res_init"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Run 启动 Smail 主服务。
|
||||
func Run() error {
|
||||
if err := runtimepath.CheckReady(config.ROOT_PATH); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := config.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func testInit(t *testing.T) {
|
||||
Domains: []string{"test.domain"},
|
||||
DbType: config.DBTypeSQLite,
|
||||
DbDSN: filepath.Join(t.TempDir(), "smtp.db"),
|
||||
DkimPrivateKeyPath: filepath.Join(config.ROOT_PATH, "config", "dkim", "dkim.priv"),
|
||||
DkimPrivateKeyPath: filepath.Join(config.ROOT_PATH, "data", "dkim", "dkim.priv"),
|
||||
IsInit: true,
|
||||
}
|
||||
if _, err := auth.DkimGen(); err != nil {
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestDirInitUsesRuntimeRoot(t *testing.T) {
|
||||
if err := dirInit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"config", "config/dkim", "config/ssl"} {
|
||||
for _, name := range []string{"config", "data", "data/dkim", "data/ssl"} {
|
||||
info, err := os.Stat(filepath.Join(root, name))
|
||||
if err != nil || !info.IsDir() {
|
||||
t.Fatalf("运行目录未创建: %s", name)
|
||||
@@ -40,7 +40,7 @@ func TestDirInitPreservesExistingFiles(t *testing.T) {
|
||||
if err := dirInit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files := []string{"config/config.json", "config/smail.db", "config/credential.key", "config/dkim/dkim.priv", "config/ssl/private.key"}
|
||||
files := []string{"config/config.json", "data/smail.db", "data/credential.key", "data/dkim/dkim.priv", "data/ssl/private.key"}
|
||||
data := []byte("existing-runtime-fixture")
|
||||
for _, name := range files {
|
||||
if err := os.WriteFile(filepath.Join(root, name), data, 0600); err != nil {
|
||||
@@ -63,10 +63,10 @@ func TestDirInitRejectsFileAsDirectory(t *testing.T) {
|
||||
previous := config.ROOT_PATH
|
||||
config.ROOT_PATH = root
|
||||
t.Cleanup(func() { config.ROOT_PATH = previous })
|
||||
if err := os.Mkdir(filepath.Join(root, "config"), 0700); err != nil {
|
||||
if err := os.Mkdir(filepath.Join(root, "data"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "config", "dkim"), []byte("blocked"), 0600); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(root, "data", "dkim"), []byte("blocked"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := dirInit(); err == nil {
|
||||
|
||||
@@ -438,8 +438,8 @@ func stopAdminResetControl() {
|
||||
|
||||
func dirInit() error {
|
||||
// 初始化与配置读写使用同一个运行根目录,不依赖启动时的工作目录。
|
||||
root := filepath.Join(config.ROOT_PATH, "config")
|
||||
for _, directory := range []string{root, filepath.Join(root, "dkim"), filepath.Join(root, "ssl")} {
|
||||
root := filepath.Join(config.ROOT_PATH, "data")
|
||||
for _, directory := range []string{filepath.Join(config.ROOT_PATH, "config"), root, filepath.Join(root, "dkim"), filepath.Join(root, "ssl")} {
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
return fmt.Errorf("创建运行配置目录失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ func HasAuth(ctx *context.Context, email *models.Email) bool {
|
||||
}
|
||||
|
||||
func DkimGen() (string, error) {
|
||||
basePath := filepath.Join(config.ROOT_PATH, "config", "dkim")
|
||||
basePath := filepath.Join(config.ROOT_PATH, "data", "dkim")
|
||||
cfg := config.CurrentConfig()
|
||||
if cfg == nil {
|
||||
return "", fmt.Errorf("runtime configuration is unavailable")
|
||||
|
||||
@@ -19,10 +19,10 @@ import (
|
||||
func portableFixture(t *testing.T) (string, config.DatabaseConfig) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "config"), 0700); err != nil {
|
||||
if err := os.Mkdir(filepath.Join(root, "data"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
database, err := sql.Open("sqlite", filepath.Join(root, "config", "source.db"))
|
||||
database, err := sql.Open("sqlite", filepath.Join(root, "data", "source.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func TestPortableSQLiteCopyPreservesDataAndLocksUntilRestart(t *testing.T) {
|
||||
if err := executor.Preview(context.Background(), plan); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "config", "target.db")); !os.IsNotExist(err) {
|
||||
if _, err := os.Stat(filepath.Join(root, "data", "target.db")); !os.IsNotExist(err) {
|
||||
t.Fatal("preview created target")
|
||||
}
|
||||
if err := executor.Run(context.Background(), plan, func(Status) error { return nil }); err != nil {
|
||||
@@ -87,7 +87,7 @@ func TestPortableSQLiteCopyPreservesDataAndLocksUntilRestart(t *testing.T) {
|
||||
if err := target.QueryRow(`SELECT id FROM "user" WHERE account='next'`).Scan(&next); err != nil || next <= 40 {
|
||||
t.Fatal("identity sequence not advanced")
|
||||
}
|
||||
old, err := sql.Open("sqlite", filepath.Join(root, "config", "source.db")+"?_pragma=busy_timeout(1)")
|
||||
old, err := sql.Open("sqlite", filepath.Join(root, "data", "source.db")+"?_pragma=busy_timeout(1)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -113,7 +113,7 @@ func TestPortableSwitchFailurePreservesSourceAndTarget(t *testing.T) {
|
||||
t.Fatal("rejected switch kept write lock")
|
||||
}
|
||||
for _, name := range []string{"source.db", "target.db"} {
|
||||
if _, err := os.Stat(filepath.Join(root, "config", name)); err != nil {
|
||||
if _, err := os.Stat(filepath.Join(root, "data", name)); err != nil {
|
||||
t.Fatal("database removed")
|
||||
}
|
||||
}
|
||||
@@ -205,7 +205,7 @@ func TestPortableRejectsCustomObjectsBeforeCreatingTarget(t *testing.T) {
|
||||
if err := executor.Run(context.Background(), plan, func(Status) error { return nil }); !errors.Is(err, ErrSQLUnsupportedObject) {
|
||||
t.Fatalf("custom view accepted: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "config", "target.db")); !os.IsNotExist(err) {
|
||||
if _, err := os.Stat(filepath.Join(root, "data", "target.db")); !os.IsNotExist(err) {
|
||||
t.Fatal("unsupported source created target")
|
||||
}
|
||||
}
|
||||
@@ -241,7 +241,7 @@ func TestExecuteAuthorizedPassesActorToSQLiteSwitch(t *testing.T) {
|
||||
root = t.TempDir()
|
||||
executor = NewAuthorizedSQLiteExecutor(root, switchConfig)
|
||||
service = NewService(executor)
|
||||
configDirectory := filepath.Join(root, "config")
|
||||
configDirectory := filepath.Join(root, "data")
|
||||
if err := os.Mkdir(configDirectory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func (e *SQLiteExecutor) resolveReference(reference string) (string, error) {
|
||||
if err != nil {
|
||||
return "", ErrInvalidTargetName
|
||||
}
|
||||
configDirectory := filepath.Join(root, "config")
|
||||
configDirectory := filepath.Join(root, "data")
|
||||
info, err := os.Lstat(configDirectory)
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", ErrInvalidTargetName
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
func newSQLiteMigrationFixture(t *testing.T, switchConfig SQLiteSwitch) (*Service, string) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
configDirectory := filepath.Join(root, "config")
|
||||
configDirectory := filepath.Join(root, "data")
|
||||
if err := os.Mkdir(configDirectory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func TestSQLiteExecutorCopiesDatabaseAndSwitchesConfiguration(t *testing.T) {
|
||||
if switched.LocalRef != "smail-next.db" || switched.Type != config.DatabaseTypeSQLite {
|
||||
t.Fatalf("unexpected switched configuration: %+v", switched)
|
||||
}
|
||||
database, err := sql.Open("sqlite", filepath.Join(root, "config", "smail-next.db"))
|
||||
database, err := sql.Open("sqlite", filepath.Join(root, "data", "smail-next.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func TestSQLiteExecutorCopiesDatabaseAndSwitchesConfiguration(t *testing.T) {
|
||||
if body != "测试邮件" {
|
||||
t.Fatalf("copied row = %q, want copied content", body)
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(root, "config", "smail-next.db"))
|
||||
info, err := os.Stat(filepath.Join(root, "data", "smail-next.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -84,10 +84,10 @@ func TestSQLiteExecutorSupportsNestedReferences(t *testing.T) {
|
||||
switched = candidate
|
||||
return nil
|
||||
})
|
||||
if err := os.MkdirAll(filepath.Join(root, "config", "archive"), 0700); err != nil {
|
||||
if err := os.MkdirAll(filepath.Join(root, "data", "archive"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Rename(filepath.Join(root, "config", "smail.db"), filepath.Join(root, "config", "archive", "review.db")); err != nil {
|
||||
if err := os.Rename(filepath.Join(root, "data", "smail.db"), filepath.Join(root, "data", "archive", "review.db")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record, token, err := service.Preview(context.Background(), config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "archive/review.db"}, 1, "archive/next.db")
|
||||
@@ -101,14 +101,14 @@ func TestSQLiteExecutorSupportsNestedReferences(t *testing.T) {
|
||||
if switched.LocalRef != "archive/next.db" {
|
||||
t.Fatalf("switched localRef = %q, want archive/next.db", switched.LocalRef)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "config", "archive", "next.db")); err != nil {
|
||||
if _, err := os.Stat(filepath.Join(root, "data", "archive", "next.db")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteExecutorRejectsExistingTarget(t *testing.T) {
|
||||
service, root := newSQLiteMigrationFixture(t, func(context.Context, config.DatabaseConfig) error { return nil })
|
||||
if err := os.WriteFile(filepath.Join(root, "config", "smail-next.db"), []byte("existing"), 0600); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(root, "data", "smail-next.db"), []byte("existing"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record, token, err := service.Preview(context.Background(), config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "smail.db"}, 1, "smail-next.db")
|
||||
@@ -126,10 +126,10 @@ func TestSQLiteExecutorRejectsExistingTarget(t *testing.T) {
|
||||
|
||||
func TestSQLiteExecutorRejectsCorruptSource(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "config"), 0700); err != nil {
|
||||
if err := os.Mkdir(filepath.Join(root, "data"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "config", "smail.db"), []byte("not sqlite"), 0600); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(root, "data", "smail.db"), []byte("not sqlite"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
executor := NewSQLiteExecutor(root, func(context.Context, config.DatabaseConfig) error { return nil })
|
||||
@@ -162,7 +162,7 @@ func TestSQLiteExecutorRequiresConfigurationSwitch(t *testing.T) {
|
||||
if failed.ErrorCode != "SWITCH_UNAVAILABLE" {
|
||||
t.Fatalf("error code = %q, want SWITCH_UNAVAILABLE", failed.ErrorCode)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "config", "next.db")); !errors.Is(err, os.ErrNotExist) {
|
||||
if _, err := os.Stat(filepath.Join(root, "data", "next.db")); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("target database remained after switch failure, stat error = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -179,7 +179,7 @@ func TestSQLiteExecutorCleansTargetForKnownConfigurationSwitchRejection(t *testi
|
||||
if !errors.Is(err, config.ErrGenerationConflict) || errors.Is(err, ErrSwitchOutcomeUnknown) {
|
||||
t.Fatalf("known switch rejection = %v, record = %+v", err, failed)
|
||||
}
|
||||
if _, statErr := os.Stat(filepath.Join(root, "config", "known-rejection.db")); !errors.Is(statErr, os.ErrNotExist) {
|
||||
if _, statErr := os.Stat(filepath.Join(root, "data", "known-rejection.db")); !errors.Is(statErr, os.ErrNotExist) {
|
||||
t.Fatalf("target database remained after known switch rejection, stat error = %v", statErr)
|
||||
}
|
||||
}
|
||||
@@ -199,7 +199,7 @@ func TestSQLiteExecutorPreservesTargetWhenConfigurationSwitchOutcomeIsUnknown(t
|
||||
if !errors.Is(err, ErrSwitchOutcomeUnknown) {
|
||||
t.Fatalf("switch failure error = %v, want unknown switch outcome", err)
|
||||
}
|
||||
if _, statErr := os.Stat(filepath.Join(root, "config", "next.db")); statErr != nil {
|
||||
if _, statErr := os.Stat(filepath.Join(root, "data", "next.db")); statErr != nil {
|
||||
t.Fatalf("target database was removed after unknown switch outcome: %v", statErr)
|
||||
}
|
||||
}
|
||||
@@ -209,8 +209,8 @@ func TestSQLiteExecutorRejectsTargetSymlink(t *testing.T) {
|
||||
t.Skip("Windows 创建符号链接需要额外权限")
|
||||
}
|
||||
service, root := newSQLiteMigrationFixture(t, func(context.Context, config.DatabaseConfig) error { return nil })
|
||||
target := filepath.Join(root, "config", "next.db")
|
||||
if err := os.Symlink(filepath.Join(root, "config", "smail.db"), target); err != nil {
|
||||
target := filepath.Join(root, "data", "next.db")
|
||||
if err := os.Symlink(filepath.Join(root, "data", "smail.db"), target); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record, token, err := service.Preview(context.Background(), config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "smail.db"}, 1, "next.db")
|
||||
@@ -267,7 +267,7 @@ func TestSQLiteExecutorReportsOrderedStatuses(t *testing.T) {
|
||||
if len(statuses) != 2 || statuses[0] != StatusVerifying || statuses[1] != StatusSwitching {
|
||||
t.Fatalf("statuses = %v, want verifying then switching", statuses)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "config", "next.db")); err != nil {
|
||||
if _, err := os.Stat(filepath.Join(root, "data", "next.db")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -41,8 +43,13 @@ func NewVault(root string) (*Vault, error) {
|
||||
if strings.TrimSpace(root) == "" {
|
||||
return nil, ErrVaultUnavailable
|
||||
}
|
||||
configDir := filepath.Join(root, "config")
|
||||
if err := os.MkdirAll(configDir, 0700); err != nil {
|
||||
for _, name := range []string{"external-email-encryption.key", "external-email-fingerprint.key"} {
|
||||
if _, err := os.Lstat(filepath.Join(root, "config", name)); err == nil || !os.IsNotExist(err) {
|
||||
return nil, ErrVaultUnavailable
|
||||
}
|
||||
}
|
||||
configDir, err := runtimepath.Directory(root)
|
||||
if err != nil {
|
||||
return nil, ErrVaultUnavailable
|
||||
}
|
||||
encryptionKey, err := loadKey(filepath.Join(configDir, "external-email-encryption.key"))
|
||||
|
||||
@@ -80,7 +80,7 @@ func GetDatabaseSettings(ctx *context.Context) (string, string, error) {
|
||||
return "", "", errors.Wrap(err)
|
||||
}
|
||||
if configData.DbType == "" && configData.DbDSN == "" {
|
||||
return config.DBTypeSQLite, config.ROOT_PATH + "./config/smail.db", nil
|
||||
return config.DBTypeSQLite, config.ROOT_PATH + "./data/smail.db", nil
|
||||
}
|
||||
return configData.DbType, configData.DbDSN, nil
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func GetDatabaseSettingsDetail(ctx *context.Context) (map[string]string, error)
|
||||
}
|
||||
if dbType == config.DBTypeSQLite {
|
||||
if strings.TrimSpace(result["db_dsn"]) == "" {
|
||||
result["db_dsn"] = config.ROOT_PATH + "./config/smail.db"
|
||||
result["db_dsn"] = config.ROOT_PATH + "./data/smail.db"
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -238,7 +238,7 @@ func SetDatabaseSettingsWithOptions(ctx *context.Context, settings DatabaseSetti
|
||||
}
|
||||
if dbType == config.DBTypeSQLite {
|
||||
if dbDSN == "" {
|
||||
dbDSN = config.ROOT_PATH + "./config/smail.db"
|
||||
dbDSN = config.ROOT_PATH + "./data/smail.db"
|
||||
}
|
||||
if err = prepareSQLiteDatabase(dbDSN); err != nil {
|
||||
return databaseError("准备 SQLite 数据文件失败", err)
|
||||
|
||||
@@ -116,7 +116,7 @@ func SetSSL(sslType, priKey, crtKey string) error {
|
||||
cfg.SSL = config.SSLConfig{
|
||||
Management: config.SSLManagementProject,
|
||||
Mode: config.ManagedSSLModeDefault,
|
||||
DefaultDirectory: "config/ssl",
|
||||
DefaultDirectory: "data/ssl",
|
||||
}
|
||||
if cfg.SSLType == config.SSLTypeAutoDNS {
|
||||
cfg.SSL.Challenge = config.SSLChallengeDNS
|
||||
@@ -190,12 +190,12 @@ func applyManagedSSLReference(cfg *config.Config, reference config.ManagedSSLRef
|
||||
cfg.SSL = config.SSLConfig{
|
||||
Management: management,
|
||||
Mode: reference.Mode,
|
||||
DefaultDirectory: "config/ssl",
|
||||
DefaultDirectory: "data/ssl",
|
||||
CertificateRef: reference.CertificateRef,
|
||||
PrivateKeyRef: reference.PrivateKeyRef,
|
||||
}
|
||||
if reference.Mode == config.ManagedSSLModeUpload {
|
||||
cfg.SSL.CustomDirectory = "config/ssl/managed/upload"
|
||||
cfg.SSL.CustomDirectory = "data/ssl/managed/upload"
|
||||
} else if cfg.SSLType == config.SSLTypeAutoDNS {
|
||||
cfg.SSL.Challenge = config.SSLChallengeDNS
|
||||
} else {
|
||||
|
||||
@@ -31,7 +31,7 @@ func writeCertificateBundle(cfg *config.Config, privateKey, certificate, issuer
|
||||
return err
|
||||
}
|
||||
defer workspaceRoot.Close()
|
||||
baseRelative := filepath.Join("config", "ssl")
|
||||
baseRelative := filepath.Join("data", "ssl")
|
||||
if err := ensureSecureRootDirectory(workspaceRoot, baseRelative); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func writeCertificateBundle(cfg *config.Config, privateKey, certificate, issuer
|
||||
}
|
||||
relativePath, err := securefile.ControlledRelativePath(basePath, item.path)
|
||||
if err != nil {
|
||||
return errors.New("SSL file path must stay inside config/ssl")
|
||||
return errors.New("SSL file path must stay inside data/ssl")
|
||||
}
|
||||
if _, exists := seen[relativePath]; exists {
|
||||
return errors.New("SSL file paths must be unique")
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestWriteCertificateBundleUsesRestrictedAtomicStorage(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
config.ROOT_PATH = root + string(os.PathSeparator)
|
||||
t.Cleanup(func() { config.ROOT_PATH = oldRoot })
|
||||
basePath := filepath.Join(root, "config", "ssl")
|
||||
basePath := filepath.Join(root, "data", "ssl")
|
||||
cfg := &config.Config{
|
||||
SSLPrivateKeyPath: filepath.Join(basePath, "private.key"),
|
||||
SSLPublicKeyPath: filepath.Join(basePath, "public.crt"),
|
||||
@@ -52,7 +52,7 @@ func TestWriteCertificateBundleUsesRestrictedAtomicStorage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if runtime.GOOS != "windows" {
|
||||
for _, directory := range []string{filepath.Join(root, "config"), basePath} {
|
||||
for _, directory := range []string{filepath.Join(root, "data"), basePath} {
|
||||
info, err := os.Stat(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -79,7 +79,7 @@ func TestWriteCertificateBundleRejectsSymlinkDirectory(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
config.ROOT_PATH = root + string(os.PathSeparator)
|
||||
t.Cleanup(func() { config.ROOT_PATH = oldRoot })
|
||||
configDirectory := filepath.Join(root, "config")
|
||||
configDirectory := filepath.Join(root, "data")
|
||||
if err := os.Mkdir(configDirectory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func TestWriteCertificateBundleRejectsSymlinkFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
config.ROOT_PATH = root + string(os.PathSeparator)
|
||||
t.Cleanup(func() { config.ROOT_PATH = oldRoot })
|
||||
basePath := filepath.Join(root, "config", "ssl")
|
||||
basePath := filepath.Join(root, "data", "ssl")
|
||||
if err := os.MkdirAll(basePath, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -139,7 +139,7 @@ func TestWriteCertificateBundleRejectsMismatchedPair(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
config.ROOT_PATH = root + string(os.PathSeparator)
|
||||
t.Cleanup(func() { config.ROOT_PATH = oldRoot })
|
||||
basePath := filepath.Join(root, "config", "ssl")
|
||||
basePath := filepath.Join(root, "data", "ssl")
|
||||
cfg := &config.Config{
|
||||
SSLPrivateKeyPath: filepath.Join(basePath, "private.key"),
|
||||
SSLPublicKeyPath: filepath.Join(basePath, "public.crt"),
|
||||
|
||||
@@ -94,7 +94,7 @@ func TestApplyManagedSSLReferenceDoesNotPersistLegacyPaths(t *testing.T) {
|
||||
if cfg.SSL.Mode != config.ManagedSSLModeUpload || cfg.SSL.CertificateRef != reference.CertificateRef || cfg.SSL.PrivateKeyRef != reference.PrivateKeyRef {
|
||||
t.Fatalf("受控 SSL 内部引用写入错误: %+v", cfg.SSL)
|
||||
}
|
||||
if cfg.SSL.CustomDirectory != "config/ssl/managed/upload" {
|
||||
if cfg.SSL.CustomDirectory != "data/ssl/managed/upload" {
|
||||
t.Fatalf("自定义受控目录写入错误: %s", cfg.SSL.CustomDirectory)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ func loadOrCreateKey(root string) ([]byte, error) {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
directory := filepath.Join(root, "config")
|
||||
directory := filepath.Join(root, "data")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -158,7 +158,13 @@ func newCredentialKeyTemporaryName() (string, error) {
|
||||
}
|
||||
|
||||
func readKey(root string) ([]byte, error) {
|
||||
directory := filepath.Join(root, "config")
|
||||
// 旧密钥尚未迁移时拒绝生成新密钥,避免现有密文永久失去读取能力。
|
||||
if _, err := os.Lstat(filepath.Join(root, "config", "credential.key")); err == nil {
|
||||
return nil, errors.New("凭据密钥尚未迁移到 data")
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
directory := filepath.Join(root, "data")
|
||||
if err := validateKeyDirectory(directory); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestEncryptDecrypt(t *testing.T) {
|
||||
if err != nil || decrypted != "secret-value" {
|
||||
t.Fatal("credential could not be decrypted")
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(root, "config", "credential.key"))
|
||||
info, err := os.Stat(filepath.Join(root, "data", "credential.key"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func TestReadKeyRejectsDirectorySymlink(t *testing.T) {
|
||||
if err := os.WriteFile(filepath.Join(target, "credential.key"), make([]byte, 32), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(target, filepath.Join(root, "config")); err != nil {
|
||||
if err := os.Symlink(target, filepath.Join(root, "data")); err != nil {
|
||||
t.Skipf("symlink is unavailable: %v", err)
|
||||
}
|
||||
if _, err := readKey(root); err == nil {
|
||||
@@ -105,7 +105,7 @@ func TestReadKeyRejectsDirectorySymlink(t *testing.T) {
|
||||
|
||||
func TestReadKeyRejectsKeySymlink(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
configDir := filepath.Join(root, "config")
|
||||
configDir := filepath.Join(root, "data")
|
||||
if err := os.MkdirAll(configDir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func TestConcurrentEncryptUsesSingleCompleteKey(t *testing.T) {
|
||||
t.Fatalf("concurrent credential could not be decrypted: %v", err)
|
||||
}
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config"))
|
||||
entries, err := os.ReadDir(filepath.Join(root, "data"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestVaultRejectsBroadKeyPermissions(t *testing.T) {
|
||||
t.Skip("POSIX permission bits are unavailable")
|
||||
}
|
||||
root := t.TempDir()
|
||||
configDir := filepath.Join(root, "config")
|
||||
configDir := filepath.Join(root, "data")
|
||||
if err := os.Mkdir(configDir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func TestVaultRejectsBroadDirectoryPermissions(t *testing.T) {
|
||||
t.Skip("POSIX permission bits are unavailable")
|
||||
}
|
||||
root := t.TempDir()
|
||||
configDir := filepath.Join(root, "config")
|
||||
configDir := filepath.Join(root, "data")
|
||||
if err := os.Mkdir(configDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
// Package runtimepath 统一运行数据目录;配置文件仍固定保存在 config 中。
|
||||
package runtimepath
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrMigrationRequired = errors.New("运行数据仍在 config 中,请停止服务并运行 smail-config --runtime-data --apply")
|
||||
|
||||
const migrationLock = ".runtime-migration.lock"
|
||||
|
||||
// Directory 创建受保护的数据目录,拒绝符号链接及普通文件。
|
||||
func Directory(root string) (string, error) {
|
||||
if strings.TrimSpace(root) == "" {
|
||||
return "", errors.New("运行根目录为空")
|
||||
}
|
||||
directory := filepath.Join(root, "data")
|
||||
if err := os.Mkdir(directory, 0700); err != nil && !os.IsExist(err) {
|
||||
return "", err
|
||||
}
|
||||
info, err := os.Lstat(directory)
|
||||
if err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return "", errors.New("运行数据目录无效")
|
||||
}
|
||||
if err := os.Chmod(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return directory, nil
|
||||
}
|
||||
|
||||
// LegacyReference 只转换旧运行目录的相对引用,不放宽路径安全检查。
|
||||
func LegacyReference(value string) string {
|
||||
valueWithoutDot := strings.TrimPrefix(value, "./")
|
||||
for _, prefix := range []string{"config/ssl", "config/dkim"} {
|
||||
if valueWithoutDot == prefix || strings.HasPrefix(valueWithoutDot, prefix+"/") {
|
||||
return "data/" + strings.TrimPrefix(valueWithoutDot, "config/")
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
var configurationFiles = map[string]bool{
|
||||
"config.json": true, "smtp.json": true, "email.json": true,
|
||||
"ssl.json": true, "plugins.json": true,
|
||||
}
|
||||
|
||||
var runtimeEntries = map[string]bool{
|
||||
"credential.key": true, "external-email-encryption.key": true,
|
||||
"external-email-fingerprint.key": true, "config-audit-v2.jsonl": true,
|
||||
"ssl": true, "dkim": true, "backups": true, "backups-v2": true,
|
||||
"migration-backups": true, "transaction": true,
|
||||
}
|
||||
|
||||
var obsoleteFiles = map[string]bool{
|
||||
"config.dev.json": true, "config_mysql.json": true, "config.json.legacy": true,
|
||||
}
|
||||
|
||||
type Move struct {
|
||||
Source string
|
||||
Target string
|
||||
}
|
||||
|
||||
// Plan 检查整个迁移集合,未知文件、目标冲突或链接均拒绝自动处理。
|
||||
func Plan(root string) ([]Move, error) {
|
||||
absolute, err := filepath.Abs(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
configDirectory := filepath.Join(absolute, "config")
|
||||
if err := safeTree(configDirectory); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
dataDirectory := filepath.Join(absolute, "data")
|
||||
// 只检查目标父目录,不在每次启动时遍历全部邮件备份和证书历史。
|
||||
if err := safeDirectory(dataDirectory); err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(configDirectory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var moves []Move
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
if configurationFiles[name] && !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
target := filepath.Join(absolute, "data", name)
|
||||
switch {
|
||||
case obsoleteFiles[name] && !entry.IsDir():
|
||||
target = filepath.Join(absolute, "data", "legacy-config", name)
|
||||
case runtimeEntries[name], !entry.IsDir() && databaseFile(name):
|
||||
default:
|
||||
return nil, fmt.Errorf("config 中存在未识别项目,请人工核对:%s", name)
|
||||
}
|
||||
if err := safeDirectory(filepath.Dir(target)); err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := os.Lstat(target); err == nil {
|
||||
return nil, fmt.Errorf("迁移目标已存在,拒绝覆盖:%s", name)
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
moves = append(moves, Move{Source: filepath.Join(configDirectory, name), Target: target})
|
||||
}
|
||||
return moves, nil
|
||||
}
|
||||
|
||||
func databaseFile(name string) bool {
|
||||
for _, suffix := range []string{"-wal", "-shm", "-journal"} {
|
||||
name = strings.TrimSuffix(name, suffix)
|
||||
}
|
||||
switch filepath.Ext(name) {
|
||||
case ".db", ".sqlite", ".sqlite3":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func safeTree(path string) error {
|
||||
return filepath.WalkDir(path, func(_ string, entry os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 || (!entry.IsDir() && !entry.Type().IsRegular()) {
|
||||
return errors.New("迁移目录中存在链接或特殊文件")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func safeDirectory(path string) error {
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return errors.New("运行目录必须是实际目录,不能是链接或普通文件")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckReady 防止遗漏迁移时创建空账号库或重新生成加密密钥。
|
||||
func CheckReady(root string) error {
|
||||
if _, err := os.Lstat(filepath.Join(root, "data", migrationLock)); err == nil || !os.IsNotExist(err) {
|
||||
return errors.New("运行目录迁移尚未结束,请保留现场并核对迁移锁")
|
||||
}
|
||||
moves, err := Plan(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(moves) != 0 {
|
||||
return ErrMigrationRequired
|
||||
}
|
||||
// 已初始化部署缺失账号库或加密密钥时必须报错,不能按首次安装处理。
|
||||
raw, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var config struct {
|
||||
SetupState string `json:"setupState"`
|
||||
Database struct {
|
||||
Type string `json:"type"`
|
||||
LocalRef string `json:"localRef"`
|
||||
} `json:"database"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &config); err != nil {
|
||||
return err
|
||||
}
|
||||
if config.SetupState != "ready" {
|
||||
return nil
|
||||
}
|
||||
required := []string{"credential.key", "external-email-encryption.key", "external-email-fingerprint.key"}
|
||||
if config.Database.Type == "sqlite" {
|
||||
if !filepath.IsLocal(config.Database.LocalRef) {
|
||||
return errors.New("SQLite 本地引用无效")
|
||||
}
|
||||
required = append(required, config.Database.LocalRef)
|
||||
}
|
||||
for _, name := range required {
|
||||
parent := filepath.Dir(name)
|
||||
for parent != "." {
|
||||
if err := safeDirectory(filepath.Join(root, "data", parent)); err != nil {
|
||||
return err
|
||||
}
|
||||
parent = filepath.Dir(parent)
|
||||
}
|
||||
info, err := os.Lstat(filepath.Join(root, "data", name))
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() == 0 {
|
||||
return fmt.Errorf("已初始化部署缺少必要运行文件,拒绝重新创建:%s", name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Migrate 必须停服调用;同卷重命名保留文件内容,失败逆序回滚,重试保持幂等。
|
||||
func Migrate(root string) ([]Move, error) {
|
||||
return migrate(root, os.Rename)
|
||||
}
|
||||
|
||||
func migrate(root string, rename func(string, string) error) ([]Move, error) {
|
||||
moves, err := Plan(root)
|
||||
if err != nil {
|
||||
return moves, err
|
||||
}
|
||||
directory, err := Directory(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lockPath := filepath.Join(directory, migrationLock)
|
||||
lock, err := os.OpenFile(lockPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if err != nil {
|
||||
return nil, errors.New("无法取得迁移锁;请确认没有其他迁移进程,并人工核对中断现场")
|
||||
}
|
||||
if err := lock.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keepLock := false
|
||||
defer func() {
|
||||
if !keepLock {
|
||||
_ = os.Remove(lockPath)
|
||||
}
|
||||
}()
|
||||
// 取得独占锁后重新检查,避免两个迁移进程使用同一份旧计划。
|
||||
moves, err = Plan(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var completed []Move
|
||||
for _, move := range moves {
|
||||
if err = os.MkdirAll(filepath.Dir(move.Target), 0700); err == nil {
|
||||
err = rename(move.Source, move.Target)
|
||||
}
|
||||
if err != nil {
|
||||
for index := len(completed) - 1; index >= 0; index-- {
|
||||
item := completed[index]
|
||||
rollbackErr := rename(item.Target, item.Source)
|
||||
keepLock = keepLock || rollbackErr != nil
|
||||
err = errors.Join(err, rollbackErr)
|
||||
}
|
||||
return nil, fmt.Errorf("运行数据迁移失败,已尝试回滚:%w", err)
|
||||
}
|
||||
completed = append(completed, move)
|
||||
}
|
||||
return completed, nil
|
||||
}
|
||||
Reference in new issue
Block a user