1 parent
dc188b1ed8
commit
d6cc81ed87
53 files changed
+909
-127
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user