完成配置迁移、插件安全整改与初始化向导优化
This commit is contained in:
1 parent
ce1b557ea9
commit
84440d8761
71 files changed
+14140
-836
No files matched your search
@@ -261,7 +261,7 @@ func normalizeAuditEvent(event ConfigAuditEvent) (ConfigAuditEvent, error) {
|
||||
}
|
||||
|
||||
func validateAuditEvent(event ConfigAuditEvent) error {
|
||||
if event.Operation != ConfigOperationUpdate && event.Operation != ConfigOperationRollback {
|
||||
if event.Operation != ConfigOperationUpdate && event.Operation != ConfigOperationRollback && event.Operation != ConfigOperationMigration {
|
||||
return ErrAuditEventInvalid
|
||||
}
|
||||
if strings.TrimSpace(event.ActorID) == "" || strings.TrimSpace(event.ActorRole) == "" || event.FromGeneration == 0 || event.ToGeneration == 0 || event.CreatedAt.IsZero() {
|
||||
|
||||
@@ -28,6 +28,7 @@ type Config struct {
|
||||
SSLType string `json:"sslType"` // 0表示自动生成证书,HTTP挑战模式,1表示用户上传证书,2表示自动-DNS挑战模式
|
||||
SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
|
||||
SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
|
||||
SSL SSLConfig `json:"ssl,omitempty"`
|
||||
DbDSN string `json:"dbDSN"`
|
||||
DbType string `json:"dbType"`
|
||||
HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
|
||||
@@ -255,6 +256,8 @@ func writeProtectedConfigFile(name string, cfg *Config) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
stored.SSLPrivateKeyPath = persistControlledSSLPath(stored.SSLPrivateKeyPath)
|
||||
stored.SSLPublicKeyPath = persistControlledSSLPath(stored.SSLPublicKeyPath)
|
||||
data, err := json.Marshal(stored)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
@@ -328,6 +331,30 @@ func (c *Config) fixPath() {
|
||||
c.DkimPrivateKeyPath = resolvePath(c.DkimPrivateKeyPath)
|
||||
}
|
||||
|
||||
func persistControlledSSLPath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
root, err := filepath.Abs(filepath.Clean(ROOT_PATH))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
target := value
|
||||
if !filepath.IsAbs(target) {
|
||||
target = filepath.Join(root, target)
|
||||
}
|
||||
target, err = filepath.Abs(filepath.Clean(target))
|
||||
if err != nil || !pathWithinRoot(root, target) {
|
||||
return ""
|
||||
}
|
||||
relative, err := filepath.Rel(root, target)
|
||||
if err != nil || filepath.IsAbs(relative) || relative == "." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) || relative == ".." {
|
||||
return ""
|
||||
}
|
||||
return filepath.ToSlash(relative)
|
||||
}
|
||||
|
||||
func resolvePath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
|
||||
+107
-19
@@ -98,6 +98,16 @@ type rollbackTicket struct {
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type LegacyPluginSecret struct {
|
||||
PluginID string
|
||||
FieldName string
|
||||
Plaintext string
|
||||
}
|
||||
|
||||
type LegacyPluginSecretImporter func(context.Context, []LegacyPluginSecret) error
|
||||
|
||||
var ErrPluginSecretMigrationRequired = errors.New("plugin secret migration is required")
|
||||
|
||||
type ConfigManager struct {
|
||||
mu sync.RWMutex
|
||||
writeMu sync.Mutex
|
||||
@@ -666,13 +676,14 @@ func applySectionPayload(current RuntimeConfig, section Section, payload json.Ra
|
||||
if err := ParseStrict(payload, &value); err != nil {
|
||||
return RuntimeConfig{}, err
|
||||
}
|
||||
for logicalID, plugin := range value.Plugins {
|
||||
for _, plugin := range value.Plugins {
|
||||
if storedSecretHasMaterial(plugin.Secret) {
|
||||
return RuntimeConfig{}, ErrInvalidSecretChange
|
||||
}
|
||||
if currentPlugin, ok := current.Plugins.Plugins[logicalID]; ok {
|
||||
plugin.Secret = currentPlugin.Secret
|
||||
value.Plugins[logicalID] = plugin
|
||||
}
|
||||
for _, plugin := range current.Plugins.Plugins {
|
||||
if storedSecretHasMaterial(plugin.Secret) {
|
||||
return RuntimeConfig{}, ErrPluginSecretMigrationRequired
|
||||
}
|
||||
}
|
||||
candidate.Plugins = value
|
||||
@@ -742,21 +753,6 @@ func applySecretChanges(candidate *RuntimeConfig, section Section, changes map[s
|
||||
return err
|
||||
}
|
||||
candidate.SMTP.Relay.Password = stored
|
||||
case section == SectionPlugins && strings.HasPrefix(name, "plugins.") && strings.HasSuffix(name, ".secret"):
|
||||
logicalID := strings.TrimSuffix(strings.TrimPrefix(name, "plugins."), ".secret")
|
||||
if !logicalIDPattern.MatchString(logicalID) {
|
||||
return ErrInvalidSecretChange
|
||||
}
|
||||
plugin, ok := candidate.Plugins.Plugins[logicalID]
|
||||
if !ok {
|
||||
return ErrInvalidSecretChange
|
||||
}
|
||||
stored, err := ApplySecretChange(vault, plugin.Secret, change, SecretPolicy{AllowClear: true, MaxLength: 4096}, now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
plugin.Secret = stored
|
||||
candidate.Plugins.Plugins[logicalID] = plugin
|
||||
default:
|
||||
return ErrInvalidSecretChange
|
||||
}
|
||||
@@ -768,6 +764,98 @@ func storedSecretHasMaterial(secret StoredSecret) bool {
|
||||
return secret.Ciphertext != "" || secret.Version != 0 || !secret.UpdatedAt.IsZero()
|
||||
}
|
||||
|
||||
// MigrateLegacyPluginSecrets moves compatibility-only plugin secrets out of plugins.json.
|
||||
// The importer must persist all received values atomically before this method commits the new configuration generation.
|
||||
func (m *ConfigManager) MigrateLegacyPluginSecrets(ctx context.Context, importer LegacyPluginSecretImporter) (bool, error) {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if m == nil || importer == nil || m.vault == nil {
|
||||
return false, ErrPluginSecretMigrationRequired
|
||||
}
|
||||
m.writeMu.Lock()
|
||||
defer m.writeMu.Unlock()
|
||||
m.mu.RLock()
|
||||
current := cloneRuntimeConfig(m.current)
|
||||
pending := m.pending != nil
|
||||
m.mu.RUnlock()
|
||||
if pending {
|
||||
return false, ErrGenerationConflict
|
||||
}
|
||||
|
||||
values := make([]LegacyPluginSecret, 0, len(current.Plugins.Plugins))
|
||||
for logicalID, instance := range current.Plugins.Plugins {
|
||||
if !storedSecretHasMaterial(instance.Secret) {
|
||||
continue
|
||||
}
|
||||
fieldName, ok := legacyPluginSecretField(logicalID)
|
||||
if !ok || !instance.Secret.Configured() {
|
||||
return false, ErrPluginSecretMigrationRequired
|
||||
}
|
||||
plaintext, err := m.vault.Decrypt(instance.Secret.Version, instance.Secret.Ciphertext)
|
||||
if err != nil || plaintext == "" {
|
||||
return false, ErrPluginSecretMigrationRequired
|
||||
}
|
||||
values = append(values, LegacyPluginSecret{PluginID: logicalID, FieldName: fieldName, Plaintext: plaintext})
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
if err := importer(ctx, values); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
candidate := cloneRuntimeConfig(current)
|
||||
for logicalID, instance := range candidate.Plugins.Plugins {
|
||||
instance.Secret = StoredSecret{}
|
||||
candidate.Plugins.Plugins[logicalID] = instance
|
||||
}
|
||||
setCandidateGeneration(&candidate, current.Generation+1)
|
||||
if err := ValidateRuntimeConfig(candidate, m.options); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, _, err := m.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := m.transactions.Commit(current, candidate); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := m.snapshots.Publish(candidate); err != nil {
|
||||
return false, err
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
m.mu.Lock()
|
||||
m.current = cloneRuntimeConfig(candidate)
|
||||
m.pending = nil
|
||||
m.status = ConfigStatus{SetupState: candidate.SetupState, ActiveGeneration: candidate.Generation, LastValidAt: &now, PendingRestart: []ServiceName{ServicePlugins}}
|
||||
m.mu.Unlock()
|
||||
if err := m.writeAudit(ctx, ConfigAuditEvent{
|
||||
Operation: ConfigOperationMigration,
|
||||
Section: SectionPlugins,
|
||||
ActorID: "system",
|
||||
ActorRole: "system",
|
||||
FromGeneration: current.Generation,
|
||||
ToGeneration: candidate.Generation,
|
||||
ChangedFields: []string{"plugins.legacySecret"},
|
||||
Result: "success",
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func legacyPluginSecretField(logicalID string) (string, bool) {
|
||||
switch logicalID {
|
||||
case "wechat_push":
|
||||
return "appSecret", true
|
||||
case "telegram_push":
|
||||
return "botToken", true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func requiresSecretStepUp(changes map[string]SecretChange) bool {
|
||||
for _, change := range changes {
|
||||
if change.Operation == SecretClear || change.Operation == SecretReplace {
|
||||
|
||||
+103
-119
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -19,7 +20,7 @@ func writeRuntimeFixture(t *testing.T, root string, runtime RuntimeConfig) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files, err := marshalRuntimeFiles(runtime)
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -476,7 +477,7 @@ func TestConfigManagerSecretChangesUseVaultAndRejectUnknownFields(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerPluginSecretChangesUseUnifiedVaultSemantics(t *testing.T) {
|
||||
func TestConfigManagerRejectsPluginSecretWrites(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
writeRuntimeFixture(t, root, runtime)
|
||||
@@ -487,145 +488,128 @@ func TestConfigManagerPluginSecretChangesUseUnifiedVaultSemantics(t *testing.T)
|
||||
}
|
||||
|
||||
plugins := manager.Snapshot().Plugins
|
||||
plugin := plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = StoredSecret{}
|
||||
plugins.Plugins["telegram_push"] = plugin
|
||||
payload, err := json.Marshal(plugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
replaceRequest := UpdateRequest{
|
||||
Section: SectionPlugins,
|
||||
ExpectedGeneration: 1,
|
||||
Payload: payload,
|
||||
SecretChanges: map[string]SecretChange{
|
||||
"plugins.telegram_push.secret": {Operation: SecretReplace, Value: &SecretInput{Plaintext: "plugin-secret"}},
|
||||
},
|
||||
Actor: authorizedConfigActor(),
|
||||
Confirmation: StepUpConfirmation{Confirmed: true, Method: "recent_auth"},
|
||||
}
|
||||
updated, err := manager.Update(context.Background(), replaceRequest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored := updated.Candidate.Plugins.Plugins["telegram_push"].Secret
|
||||
if !stored.Configured() || stored.Ciphertext == "plugin-secret" {
|
||||
t.Fatalf("plugin secret was not encrypted: %+v", stored)
|
||||
}
|
||||
if _, err := manager.Apply(context.Background(), updated.Generation); err != nil {
|
||||
t.Fatal(err)
|
||||
if _, err = manager.Validate(context.Background(), ValidateRequest{
|
||||
Section: SectionPlugins,
|
||||
Payload: payload,
|
||||
SecretChanges: map[string]SecretChange{"plugins.telegram_push.secret": {Operation: SecretReplace, Value: &SecretInput{Plaintext: "plugin-secret"}}},
|
||||
}); !errors.Is(err, ErrInvalidSecretChange) {
|
||||
t.Fatalf("plugin secret update error = %v, want invalid secret change", err)
|
||||
}
|
||||
|
||||
plugins = manager.Snapshot().Plugins
|
||||
plugin = plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = StoredSecret{}
|
||||
plugin := plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = StoredSecret{Ciphertext: "smail:v1:encrypted", Version: credential.CurrentVersion, UpdatedAt: time.Now().UTC()}
|
||||
plugins.Plugins["telegram_push"] = plugin
|
||||
payload, err = json.Marshal(plugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kept, err := manager.Update(context.Background(), UpdateRequest{
|
||||
Section: SectionPlugins,
|
||||
ExpectedGeneration: 2,
|
||||
Payload: payload,
|
||||
SecretChanges: map[string]SecretChange{"plugins.telegram_push.secret": {Operation: SecretKeep}},
|
||||
Actor: authorizedConfigActor(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if kept.Candidate.Plugins.Plugins["telegram_push"].Secret != stored {
|
||||
t.Fatalf("plugin keep changed the stored secret: got %+v want %+v", kept.Candidate.Plugins.Plugins["telegram_push"].Secret, stored)
|
||||
}
|
||||
if _, err := manager.Apply(context.Background(), kept.Generation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins = manager.Snapshot().Plugins
|
||||
plugin = plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = StoredSecret{}
|
||||
plugins.Plugins["telegram_push"] = plugin
|
||||
payload, err = json.Marshal(plugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cleared, err := manager.Update(context.Background(), UpdateRequest{
|
||||
Section: SectionPlugins,
|
||||
ExpectedGeneration: 3,
|
||||
Payload: payload,
|
||||
SecretChanges: map[string]SecretChange{"plugins.telegram_push.secret": {Operation: SecretClear, Confirmed: true}},
|
||||
Actor: authorizedConfigActor(),
|
||||
Confirmation: StepUpConfirmation{Confirmed: true, Method: "webauthn"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cleared.Candidate.Plugins.Plugins["telegram_push"].Secret.Configured() {
|
||||
t.Fatal("plugin clear left the secret configured")
|
||||
if _, err = manager.Validate(context.Background(), ValidateRequest{Section: SectionPlugins, Payload: payload}); !errors.Is(err, ErrInvalidSecretChange) {
|
||||
t.Fatalf("direct plugin secret error = %v, want invalid secret change", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerRejectsInvalidPluginSecretChanges(t *testing.T) {
|
||||
func TestConfigManagerMigratesLegacyPluginSecrets(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
writeRuntimeFixture(t, root, runtime)
|
||||
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
|
||||
manager, err := NewConfigManager(root, options)
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored, err := ApplySecretChange(vault, StoredSecret{}, SecretChange{Operation: SecretReplace, Value: &SecretInput{Plaintext: "legacy-token"}}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plugin := runtime.Plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = stored
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
writeRuntimeFixture(t, root, runtime)
|
||||
|
||||
base := manager.Snapshot().Plugins
|
||||
tests := []struct {
|
||||
name string
|
||||
payload PluginsSectionConfig
|
||||
secretPath string
|
||||
}{
|
||||
{
|
||||
name: "unknown plugin",
|
||||
payload: base,
|
||||
secretPath: "plugins.unknown.secret",
|
||||
},
|
||||
{
|
||||
name: "invalid path",
|
||||
payload: base,
|
||||
secretPath: "plugins.telegram_push.secret.extra",
|
||||
},
|
||||
{
|
||||
name: "direct ciphertext",
|
||||
payload: func() PluginsSectionConfig {
|
||||
value := base
|
||||
plugin := value.Plugins["telegram_push"]
|
||||
plugin.Secret = StoredSecret{Ciphertext: "enc:v1:plaintext", Version: credential.CurrentVersion, UpdatedAt: time.Now().UTC()}
|
||||
value.Plugins["telegram_push"] = plugin
|
||||
return value
|
||||
}(),
|
||||
secretPath: "plugins.telegram_push.secret",
|
||||
},
|
||||
writer := &MemoryAuditWriter{}
|
||||
manager, err := NewConfigManagerWithSecurity(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}, ConfigSecurity{Audit: writer})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
payloadValue := clonePluginsSection(test.payload)
|
||||
if test.name != "direct ciphertext" {
|
||||
plugin := payloadValue.Plugins["telegram_push"]
|
||||
plugin.Secret = StoredSecret{}
|
||||
payloadValue.Plugins["telegram_push"] = plugin
|
||||
}
|
||||
payload, err := json.Marshal(payloadValue)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = manager.Validate(context.Background(), ValidateRequest{
|
||||
Section: SectionPlugins,
|
||||
Payload: payload,
|
||||
SecretChanges: map[string]SecretChange{test.secretPath: {Operation: SecretKeep}},
|
||||
})
|
||||
if !errors.Is(err, ErrInvalidSecretChange) {
|
||||
t.Fatalf("error = %v, want invalid secret change", err)
|
||||
}
|
||||
})
|
||||
var imported []LegacyPluginSecret
|
||||
migrated, err := manager.MigrateLegacyPluginSecrets(context.Background(), func(_ context.Context, values []LegacyPluginSecret) error {
|
||||
imported = append([]LegacyPluginSecret(nil), values...)
|
||||
return nil
|
||||
})
|
||||
if err != nil || !migrated {
|
||||
t.Fatalf("migrate legacy plugin secrets = %v, %v", migrated, err)
|
||||
}
|
||||
if len(imported) != 1 || imported[0].PluginID != "telegram_push" || imported[0].FieldName != "botToken" || imported[0].Plaintext != "legacy-token" {
|
||||
t.Fatalf("imported legacy values = %+v", imported)
|
||||
}
|
||||
if manager.Snapshot().Generation != 2 || manager.Snapshot().Plugins.Plugins["telegram_push"].Secret.Configured() {
|
||||
t.Fatalf("migrated snapshot = %+v", manager.Snapshot())
|
||||
}
|
||||
pluginData, err := os.ReadFile(filepath.Join(root, "config", "plugins.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(pluginData) == "" || containsAny(string(pluginData), "legacy-token", "ciphertext", "\"secret\"") {
|
||||
t.Fatalf("plugins.json retained legacy secret material: %s", pluginData)
|
||||
}
|
||||
events := writer.Events()
|
||||
if len(events) != 1 || events[0].Operation != ConfigOperationMigration || events[0].Section != SectionPlugins {
|
||||
t.Fatalf("migration audit events = %+v", events)
|
||||
}
|
||||
migrated, err = manager.MigrateLegacyPluginSecrets(context.Background(), func(_ context.Context, values []LegacyPluginSecret) error {
|
||||
t.Fatalf("unexpected repeated importer invocation: %+v", values)
|
||||
return nil
|
||||
})
|
||||
if err != nil || migrated {
|
||||
t.Fatalf("repeat migration = %v, %v", migrated, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerRetainsLegacySecretWhenImportFails(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored, err := ApplySecretChange(vault, StoredSecret{}, SecretChange{Operation: SecretReplace, Value: &SecretInput{Plaintext: "legacy-token"}}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plugin := runtime.Plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = stored
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
writeRuntimeFixture(t, root, runtime)
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = manager.MigrateLegacyPluginSecrets(context.Background(), func(_ context.Context, _ []LegacyPluginSecret) error {
|
||||
return errors.New("database unavailable")
|
||||
}); err == nil {
|
||||
t.Fatal("legacy import failure unexpectedly succeeded")
|
||||
}
|
||||
if manager.Snapshot().Generation != 1 || !manager.Snapshot().Plugins.Plugins["telegram_push"].Secret.Configured() {
|
||||
t.Fatal("failed import altered the in-memory legacy configuration")
|
||||
}
|
||||
pluginData, readErr := os.ReadFile(filepath.Join(root, "config", "plugins.json"))
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if !containsAny(string(pluginData), "ciphertext", "\"secret\"") {
|
||||
t.Fatal("failed import removed compatibility secret from plugins.json")
|
||||
}
|
||||
}
|
||||
|
||||
func containsAny(value string, terms ...string) bool {
|
||||
for _, term := range terms {
|
||||
if strings.Contains(value, term) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func findTransactionDirectory(t *testing.T, root string) string {
|
||||
|
||||
@@ -0,0 +1,848 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/plugins/catalog"
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMigrationNotRequired = errors.New("legacy configuration migration is not required")
|
||||
ErrMigrationUnknownField = errors.New("legacy configuration contains unknown fields")
|
||||
ErrMigrationUnsafeValue = errors.New("legacy configuration contains a value that requires manual migration")
|
||||
ErrMigrationTargetExists = errors.New("partitioned configuration files already exist")
|
||||
)
|
||||
|
||||
type LegacyMigrationPlan struct {
|
||||
SourceDigest string
|
||||
TargetGeneration uint64
|
||||
UnknownFields []string
|
||||
ManualFields []string
|
||||
}
|
||||
|
||||
type LegacyMigrationResult struct {
|
||||
Generation uint64
|
||||
BackupPath string
|
||||
}
|
||||
|
||||
type legacyMigrationConfig struct {
|
||||
LogLevel string `json:"logLevel"`
|
||||
Domain string `json:"domain"`
|
||||
Domains []string `json:"domains"`
|
||||
WebDomain string `json:"webDomain"`
|
||||
AccessHosts []string `json:"accessHosts"`
|
||||
DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
|
||||
SSLType string `json:"sslType"`
|
||||
SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
|
||||
SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
|
||||
DbDSN string `json:"dbDSN"`
|
||||
DbType string `json:"dbType"`
|
||||
HttpsEnabled int `json:"httpsEnabled"`
|
||||
SpamFilterLevel int `json:"spamFilterLevel"`
|
||||
HttpPort int `json:"httpPort"`
|
||||
HttpsPort int `json:"httpsPort"`
|
||||
WeChatPushAppID string `json:"weChatPushAppId"`
|
||||
WeChatPushSecret string `json:"weChatPushSecret"`
|
||||
WeChatPushTemplateID string `json:"weChatPushTemplateId"`
|
||||
WeChatPushUserID string `json:"weChatPushUserId"`
|
||||
TGBotToken string `json:"tgBotToken"`
|
||||
TGChatID string `json:"tgChatId"`
|
||||
IsInit bool `json:"isInit"`
|
||||
SetupState SetupState `json:"setupState"`
|
||||
WebPushURL string `json:"webPushUrl"`
|
||||
WebPushToken string `json:"webPushToken"`
|
||||
SMTPHost string `json:"smtpHost"`
|
||||
SMTPPort int `json:"smtpPort"`
|
||||
SMTPUsername string `json:"smtpUsername"`
|
||||
SMTPPassword string `json:"smtpPassword"`
|
||||
SMTPSecurity string `json:"smtpSecurity"`
|
||||
SMTPFrom string `json:"smtpFrom"`
|
||||
SMTPFromDomain string `json:"smtpFromDomain"`
|
||||
VerificationEnabled bool `json:"verificationEnabled"`
|
||||
ExternalRecoveryEnabled bool `json:"externalRecoveryEnabled"`
|
||||
LoginPolicy string `json:"loginPolicy"`
|
||||
VerificationLoginPolicy string `json:"verificationLoginPolicy"`
|
||||
PluginAllowList []string `json:"pluginAllowList"`
|
||||
}
|
||||
|
||||
var legacyMigrationKnownFields = map[string]struct{}{
|
||||
"logLevel": {}, "domain": {}, "domains": {}, "webDomain": {}, "accessHosts": {},
|
||||
"dkimPrivateKeyPath": {}, "sslType": {}, "SSLPrivateKeyPath": {}, "SSLPublicKeyPath": {},
|
||||
"dbDSN": {}, "dbType": {}, "httpsEnabled": {}, "spamFilterLevel": {}, "httpPort": {}, "httpsPort": {},
|
||||
"weChatPushAppId": {}, "weChatPushSecret": {}, "weChatPushTemplateId": {}, "weChatPushUserId": {},
|
||||
"tgBotToken": {}, "tgChatId": {}, "isInit": {}, "setupState": {}, "webPushUrl": {}, "webPushToken": {},
|
||||
"smtpHost": {}, "smtpPort": {}, "smtpUsername": {}, "smtpPassword": {}, "smtpSecurity": {}, "smtpFrom": {},
|
||||
"smtpFromDomain": {}, "verificationEnabled": {}, "externalRecoveryEnabled": {}, "loginPolicy": {},
|
||||
"verificationLoginPolicy": {}, "pluginAllowList": {},
|
||||
}
|
||||
|
||||
var legacyPluginExecutableMap = map[string]string{
|
||||
"spam_block": "spam_block",
|
||||
"spam_block.exe": "spam_block",
|
||||
"wechat_push": "wechat_push",
|
||||
"wechat_push.exe": "wechat_push",
|
||||
"telegram_push": "telegram_push",
|
||||
"telegram_push.exe": "telegram_push",
|
||||
"pmail_telegram_push": "telegram_push",
|
||||
"pmail_telegram_push.exe": "telegram_push",
|
||||
}
|
||||
|
||||
var legacyMySQLDSNPattern = regexp.MustCompile(`^([^:@/[:space:]]+):([^@\r\n]*)@tcp\(([^():/[:space:]]+)(?::([0-9]{1,5}))?\)/([A-Za-z0-9][A-Za-z0-9._-]{0,127})(?:\?[^#[:space:]]*)?$`)
|
||||
|
||||
func PlanLegacyMigration(root string, _ RuntimeValidationOptions) (LegacyMigrationPlan, error) {
|
||||
directory, err := secureManagerConfigDirectory(root)
|
||||
if err != nil {
|
||||
return LegacyMigrationPlan{}, err
|
||||
}
|
||||
data, err := readManagerConfigFile(directory, "config.json")
|
||||
if err != nil {
|
||||
return LegacyMigrationPlan{}, err
|
||||
}
|
||||
var raw map[string]json.RawMessage
|
||||
if err := ParseStrict(data, &raw); err != nil {
|
||||
return LegacyMigrationPlan{}, err
|
||||
}
|
||||
if _, exists := raw["sections"]; exists {
|
||||
return LegacyMigrationPlan{}, ErrMigrationNotRequired
|
||||
}
|
||||
plan := LegacyMigrationPlan{SourceDigest: SectionDigest(data), TargetGeneration: 1}
|
||||
for field := range raw {
|
||||
if _, known := legacyMigrationKnownFields[field]; !known {
|
||||
plan.UnknownFields = append(plan.UnknownFields, field)
|
||||
}
|
||||
}
|
||||
sort.Strings(plan.UnknownFields)
|
||||
if len(plan.UnknownFields) > 0 {
|
||||
return plan, ErrMigrationUnknownField
|
||||
}
|
||||
var legacy legacyMigrationConfig
|
||||
if err := json.Unmarshal(data, &legacy); err != nil {
|
||||
return LegacyMigrationPlan{}, err
|
||||
}
|
||||
plan.ManualFields = legacyManualFields(legacy)
|
||||
if len(plan.ManualFields) > 0 {
|
||||
return plan, ErrMigrationUnsafeValue
|
||||
}
|
||||
return plan, nil
|
||||
}
|
||||
func MigrateLegacyConfig(root string, options RuntimeValidationOptions) (LegacyMigrationResult, error) {
|
||||
plan, err := PlanLegacyMigration(root, options)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
directory, err := secureManagerConfigDirectory(root)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
legacyData, err := readManagerConfigFile(directory, "config.json")
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if SectionDigest(legacyData) != plan.SourceDigest {
|
||||
return LegacyMigrationResult{}, ErrGenerationConflict
|
||||
}
|
||||
var legacy legacyMigrationConfig
|
||||
if err := json.Unmarshal(legacyData, &legacy); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
runtime, manualFields, err := legacyRuntimeConfig(root, legacy)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if len(manualFields) > 0 {
|
||||
return LegacyMigrationResult{}, ErrMigrationUnsafeValue
|
||||
}
|
||||
if err := resolveLegacySetupState(&runtime, legacy, options); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if err := ValidateRuntimeConfig(runtime, options); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
backupPath, err := writeLegacyMigrationBackup(directory, legacyData, time.Now().UTC())
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
resolvedFiles := make(map[string][]byte, len(files))
|
||||
for name, data := range files {
|
||||
resolvedFiles[name] = data
|
||||
}
|
||||
for _, section := range RequiredPartitionSections() {
|
||||
name, _ := FixedSectionFile(section)
|
||||
resolved, resolveErr := resolveLegacyMigrationSection(directory, section, files[name], vault)
|
||||
if resolveErr != nil {
|
||||
return LegacyMigrationResult{}, resolveErr
|
||||
}
|
||||
resolvedFiles[name] = resolved
|
||||
}
|
||||
files = resolvedFiles
|
||||
if err := refreshLegacyMigrationRoot(runtime, files); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
for _, section := range RequiredPartitionSections() {
|
||||
name, _ := FixedSectionFile(section)
|
||||
path := filepath.Join(directory, name)
|
||||
if _, statErr := os.Lstat(path); os.IsNotExist(statErr) {
|
||||
if err := atomicfile.Write(path, files[name], 0600); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
} else if statErr != nil {
|
||||
return LegacyMigrationResult{}, statErr
|
||||
}
|
||||
}
|
||||
rootPath := filepath.Join(directory, "config.json")
|
||||
currentRoot, err := readManagerConfigFile(directory, "config.json")
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if !bytes.Equal(currentRoot, legacyData) && !bytes.Equal(currentRoot, files["config.json"]) {
|
||||
return LegacyMigrationResult{}, ErrGenerationConflict
|
||||
}
|
||||
if bytes.Equal(currentRoot, legacyData) {
|
||||
if err := atomicfile.Write(rootPath, files["config.json"], 0600); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
}
|
||||
if err := atomicfile.SyncDirectory(directory); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
return LegacyMigrationResult{Generation: runtime.Generation, BackupPath: backupPath}, nil
|
||||
}
|
||||
|
||||
func refreshLegacyMigrationRoot(runtime RuntimeConfig, files map[string][]byte) error {
|
||||
references := make(map[Section]SectionRef, len(requiredPartitionSections))
|
||||
for _, section := range requiredPartitionSections {
|
||||
name, ok := FixedSectionFile(section)
|
||||
if !ok {
|
||||
return ErrInvalidSection
|
||||
}
|
||||
schema, ok := FixedSectionSchema(section)
|
||||
if !ok {
|
||||
return ErrInvalidSection
|
||||
}
|
||||
data, ok := files[name]
|
||||
if !ok {
|
||||
return ErrConfigSectionMissing
|
||||
}
|
||||
references[section] = SectionRef{File: name, Schema: schema, Generation: runtime.Generation, SHA256: SectionDigest(data)}
|
||||
}
|
||||
runtime.Root.Generation = runtime.Generation
|
||||
runtime.Root.SetupState = runtime.SetupState
|
||||
runtime.Root.Sections = references
|
||||
data, err := json.Marshal(runtime.Root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files["config.json"] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolveLegacyMigrationSection(directory string, section Section, expected []byte, vault *credential.Vault) ([]byte, error) {
|
||||
name, ok := FixedSectionFile(section)
|
||||
if !ok || section == SectionRoot {
|
||||
return nil, ErrInvalidSection
|
||||
}
|
||||
existing, err := readManagerConfigFile(directory, name)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return expected, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if bytes.Equal(existing, expected) {
|
||||
return existing, nil
|
||||
}
|
||||
equivalent, err := equivalentLegacyMigrationSection(section, expected, existing, vault)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !equivalent {
|
||||
return nil, ErrMigrationTargetExists
|
||||
}
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
func equivalentLegacyMigrationSection(section Section, expected, existing []byte, vault *credential.Vault) (bool, error) {
|
||||
switch section {
|
||||
case SectionSMTP:
|
||||
var expectedConfig, existingConfig SMTPSectionConfig
|
||||
if err := ParseStrict(expected, &expectedConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := ParseStrict(existing, &existingConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !equivalentStoredSecret(expectedConfig.Relay.Password, existingConfig.Relay.Password, vault) {
|
||||
return false, nil
|
||||
}
|
||||
expectedConfig.Relay.Password = StoredSecret{}
|
||||
existingConfig.Relay.Password = StoredSecret{}
|
||||
return reflect.DeepEqual(expectedConfig, existingConfig), nil
|
||||
case SectionPlugins:
|
||||
var expectedConfig, existingConfig PluginsSectionConfig
|
||||
if err := ParseStrict(expected, &expectedConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := ParseStrict(existing, &existingConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(expectedConfig.Plugins) != len(existingConfig.Plugins) {
|
||||
return false, nil
|
||||
}
|
||||
for logicalID, expectedPlugin := range expectedConfig.Plugins {
|
||||
existingPlugin, ok := existingConfig.Plugins[logicalID]
|
||||
if !ok || !equivalentStoredSecret(expectedPlugin.Secret, existingPlugin.Secret, vault) {
|
||||
return false, nil
|
||||
}
|
||||
expectedPlugin.Secret = StoredSecret{}
|
||||
existingPlugin.Secret = StoredSecret{}
|
||||
if !reflect.DeepEqual(expectedPlugin, existingPlugin) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig.Meta, existingConfig.Meta), nil
|
||||
case SectionEmail, SectionSSL:
|
||||
return false, nil
|
||||
default:
|
||||
return false, ErrInvalidSection
|
||||
}
|
||||
}
|
||||
|
||||
func equivalentStoredSecret(expected, existing StoredSecret, vault *credential.Vault) bool {
|
||||
if expected.Configured() != existing.Configured() {
|
||||
return false
|
||||
}
|
||||
if !expected.Configured() {
|
||||
return true
|
||||
}
|
||||
expectedPlaintext, expectedErr := OpenStoredSecret(vault, expected)
|
||||
existingPlaintext, existingErr := OpenStoredSecret(vault, existing)
|
||||
return expectedErr == nil && existingErr == nil && expectedPlaintext == existingPlaintext
|
||||
}
|
||||
|
||||
func legacyManualFields(legacy legacyMigrationConfig) []string {
|
||||
fields := make([]string, 0)
|
||||
_, _, databaseManualFields := legacyDatabasePlan(legacy)
|
||||
fields = append(fields, databaseManualFields...)
|
||||
if _, ok := legacyControlledReference(legacy.SSLPublicKeyPath); legacy.SSLPublicKeyPath != "" && !ok {
|
||||
fields = append(fields, "SSLPublicKeyPath")
|
||||
}
|
||||
if _, ok := legacyControlledReference(legacy.SSLPrivateKeyPath); legacy.SSLPrivateKeyPath != "" && !ok {
|
||||
fields = append(fields, "SSLPrivateKeyPath")
|
||||
}
|
||||
if legacy.SMTPUsername == "" && legacy.SMTPPassword != "" {
|
||||
fields = append(fields, "smtpPassword")
|
||||
}
|
||||
for field, value := range map[string]string{
|
||||
"smtpPassword": legacy.SMTPPassword, "weChatPushSecret": legacy.WeChatPushSecret, "tgBotToken": legacy.TGBotToken,
|
||||
} {
|
||||
if value != "" && strings.TrimSpace(value) != value {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
}
|
||||
wechatEnabled := legacy.WeChatPushAppID != "" || legacy.WeChatPushSecret != "" || legacy.WeChatPushTemplateID != "" || legacy.WeChatPushUserID != ""
|
||||
if wechatEnabled && legacy.WeChatPushSecret == "" {
|
||||
fields = append(fields, "weChatPushSecret")
|
||||
}
|
||||
telegramEnabled := legacy.TGBotToken != "" || legacy.TGChatID != ""
|
||||
if telegramEnabled && legacy.TGBotToken == "" {
|
||||
fields = append(fields, "tgBotToken")
|
||||
}
|
||||
if legacy.TGChatID != "" {
|
||||
fields = append(fields, "tgChatId")
|
||||
}
|
||||
if legacy.WebPushURL != "" {
|
||||
fields = append(fields, "webPushUrl")
|
||||
}
|
||||
if legacy.WebPushToken != "" {
|
||||
fields = append(fields, "webPushToken")
|
||||
}
|
||||
enabledPlugins, pluginManualFields := legacyPluginEnabledSet(legacy.PluginAllowList)
|
||||
fields = append(fields, pluginManualFields...)
|
||||
if enabledPlugins["wechat_push"] && !wechatEnabled {
|
||||
fields = append(fields, "weChatPushSecret")
|
||||
}
|
||||
if enabledPlugins["telegram_push"] && !telegramEnabled {
|
||||
fields = append(fields, "tgBotToken")
|
||||
}
|
||||
sort.Strings(fields)
|
||||
return compactMigrationFields(fields)
|
||||
}
|
||||
|
||||
func compactMigrationFields(fields []string) []string {
|
||||
if len(fields) == 0 {
|
||||
return nil
|
||||
}
|
||||
result := fields[:0]
|
||||
for _, field := range fields {
|
||||
if len(result) == 0 || result[len(result)-1] != field {
|
||||
result = append(result, field)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
func legacyRuntimeConfig(root string, legacy legacyMigrationConfig) (RuntimeConfig, []string, error) {
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
return RuntimeConfig{}, nil, err
|
||||
}
|
||||
manualFields := make([]string, 0)
|
||||
database, databaseManual, err := legacyDatabaseConfig(root, vault, legacy)
|
||||
if err != nil {
|
||||
return RuntimeConfig{}, nil, err
|
||||
}
|
||||
manualFields = append(manualFields, databaseManual...)
|
||||
generation := uint64(1)
|
||||
setupState := legacy.SetupState
|
||||
if !setupState.Valid() {
|
||||
if legacy.IsInit {
|
||||
setupState = SetupStateReady
|
||||
} else {
|
||||
setupState = SetupStateNew
|
||||
}
|
||||
}
|
||||
accepted := append([]string(nil), legacy.Domains...)
|
||||
if len(accepted) == 0 && legacy.Domain != "" {
|
||||
accepted = []string{legacy.Domain}
|
||||
}
|
||||
if legacy.Domain == "" && len(accepted) > 0 {
|
||||
legacy.Domain = accepted[0]
|
||||
}
|
||||
httpsEnabled := legacy.HttpsEnabled != 2
|
||||
httpsPort := legacy.HttpsPort
|
||||
if httpsEnabled && httpsPort == 0 {
|
||||
httpsPort = 443
|
||||
}
|
||||
if !httpsEnabled {
|
||||
httpsPort = 0
|
||||
}
|
||||
ssl, sslManual := legacySSLConfig(legacy)
|
||||
manualFields = append(manualFields, sslManual...)
|
||||
relay, relayManual, err := legacySMTPConfig(root, vault, legacy)
|
||||
if err != nil {
|
||||
return RuntimeConfig{}, nil, err
|
||||
}
|
||||
manualFields = append(manualFields, relayManual...)
|
||||
plugins, pluginManual, err := legacyPluginsConfig(root, vault, legacy)
|
||||
if err != nil {
|
||||
return RuntimeConfig{}, nil, err
|
||||
}
|
||||
manualFields = append(manualFields, pluginManual...)
|
||||
rootConfig := RootConfig{
|
||||
ConfigVersion: CurrentConfigVersion,
|
||||
SchemaVersion: CurrentSchemaVersion,
|
||||
Generation: generation,
|
||||
SetupState: setupState,
|
||||
LogLevel: legacy.LogLevel,
|
||||
Database: database,
|
||||
Domains: DomainConfig{
|
||||
Primary: legacy.Domain,
|
||||
Accepted: accepted,
|
||||
Web: legacy.WebDomain,
|
||||
AccessHosts: append([]string(nil), legacy.AccessHosts...),
|
||||
},
|
||||
HTTP: HTTPConfig{Port: legacy.HttpPort, HTTPSPort: httpsPort, HTTPSEnabled: httpsEnabled},
|
||||
Security: SecurityConfig{PasswordVersion: "argon2id", EncryptionKeyRef: "credential.key"},
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
}
|
||||
meta := SectionMeta{Schema: CurrentSectionSchema, Generation: generation}
|
||||
runtime := RuntimeConfig{
|
||||
Generation: generation,
|
||||
SetupState: setupState,
|
||||
Root: rootConfig,
|
||||
SMTP: SMTPSectionConfig{Meta: meta, Relay: relay},
|
||||
Email: EmailSectionConfig{Meta: meta, Policy: EmailConfig{
|
||||
SMTPPort: 25,
|
||||
IMAPPort: 143,
|
||||
POP3Port: 110,
|
||||
SpamFilterLevel: legacy.SpamFilterLevel,
|
||||
LoginVerificationPolicy: legacyLoginPolicy(legacy),
|
||||
ExternalRecoveryEnabled: legacy.ExternalRecoveryEnabled,
|
||||
}},
|
||||
SSL: SSLSectionConfig{Meta: meta, TLS: ssl},
|
||||
Plugins: PluginsSectionConfig{Meta: meta, Plugins: plugins},
|
||||
LoadedAt: time.Now().UTC(),
|
||||
}
|
||||
prepared, err := prepareLegacyRuntime(runtime)
|
||||
if err != nil {
|
||||
return RuntimeConfig{}, nil, err
|
||||
}
|
||||
return prepared, manualFields, nil
|
||||
}
|
||||
|
||||
func resolveLegacySetupState(runtime *RuntimeConfig, legacy legacyMigrationConfig, options RuntimeValidationOptions) error {
|
||||
if runtime == nil || legacy.SetupState.Valid() || !legacy.IsInit {
|
||||
return nil
|
||||
}
|
||||
readyErr := ValidateRuntimeConfig(*runtime, options)
|
||||
if readyErr == nil {
|
||||
return nil
|
||||
}
|
||||
degraded := *runtime
|
||||
degraded.SetupState = SetupStateDegraded
|
||||
degraded.Root.SetupState = SetupStateDegraded
|
||||
prepared, err := prepareLegacyRuntime(degraded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ValidateRuntimeConfig(prepared, options); err != nil {
|
||||
return readyErr
|
||||
}
|
||||
*runtime = prepared
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareLegacyRuntime(runtime RuntimeConfig) (RuntimeConfig, error) {
|
||||
files, err := marshalRuntimeFiles(runtime)
|
||||
if err != nil {
|
||||
return RuntimeConfig{}, err
|
||||
}
|
||||
var root RootConfig
|
||||
if err := ParseStrict(files["config.json"], &root); err != nil {
|
||||
return RuntimeConfig{}, err
|
||||
}
|
||||
runtime.Root = root
|
||||
return runtime, nil
|
||||
}
|
||||
|
||||
func legacyDatabaseConfig(root string, vault *credential.Vault, legacy legacyMigrationConfig) (DatabaseConfig, []string, error) {
|
||||
database, password, manualFields := legacyDatabasePlan(legacy)
|
||||
if len(manualFields) > 0 {
|
||||
return DatabaseConfig{}, manualFields, nil
|
||||
}
|
||||
if database.Type == DatabaseTypeSQLite {
|
||||
return database, nil, nil
|
||||
}
|
||||
stored, err := legacyStoredSecret(root, vault, password)
|
||||
if err != nil {
|
||||
return DatabaseConfig{}, nil, err
|
||||
}
|
||||
database.Password = stored
|
||||
return database, nil, nil
|
||||
}
|
||||
|
||||
func legacyDatabasePlan(legacy legacyMigrationConfig) (DatabaseConfig, string, []string) {
|
||||
switch legacy.DbType {
|
||||
case "", DBTypeSQLite:
|
||||
name := strings.TrimSpace(legacy.DbDSN)
|
||||
if name == "" {
|
||||
name = "smail.db"
|
||||
}
|
||||
name = filepath.Base(filepath.Clean(name))
|
||||
if !databaseLocalRefPattern.MatchString(name) {
|
||||
return DatabaseConfig{}, "", []string{"dbDSN"}
|
||||
}
|
||||
return DatabaseConfig{Type: DatabaseTypeSQLite, LocalRef: name}, "", nil
|
||||
case DBTypeMySQL:
|
||||
database, password, ok := parseLegacyMySQLDSN(legacy.DbDSN)
|
||||
if !ok {
|
||||
return DatabaseConfig{}, "", []string{"dbDSN"}
|
||||
}
|
||||
return database, password, nil
|
||||
case DBTypePostgres:
|
||||
database, password, ok := parseLegacyPostgreSQLDSN(legacy.DbDSN)
|
||||
if !ok {
|
||||
return DatabaseConfig{}, "", []string{"dbDSN"}
|
||||
}
|
||||
return database, password, nil
|
||||
default:
|
||||
return DatabaseConfig{}, "", []string{"dbType"}
|
||||
}
|
||||
}
|
||||
|
||||
func parseLegacyMySQLDSN(value string) (DatabaseConfig, string, bool) {
|
||||
matches := legacyMySQLDSNPattern.FindStringSubmatch(value)
|
||||
if matches == nil {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
port := int(DefaultMySQLPort)
|
||||
if matches[4] != "" {
|
||||
parsed, err := strconv.Atoi(matches[4])
|
||||
if err != nil || !validPort(parsed) {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
port = parsed
|
||||
}
|
||||
host, ok := normalizeLegacyDatabaseHost(matches[3])
|
||||
if !ok || !validLegacyDatabaseUser(matches[1]) || !validLegacyDatabasePassword(matches[2]) {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
return DatabaseConfig{Type: DatabaseTypeMySQL, Host: host, Port: uint16(port), Username: matches[1], Name: matches[5]}, matches[2], true
|
||||
}
|
||||
|
||||
func parseLegacyPostgreSQLDSN(value string) (DatabaseConfig, string, bool) {
|
||||
parsed, err := url.ParseRequestURI(value)
|
||||
if err != nil || (parsed.Scheme != "postgres" && parsed.Scheme != "postgresql") || parsed.RawQuery != "" || parsed.Fragment != "" || parsed.User == nil || parsed.Host == "" {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
username := parsed.User.Username()
|
||||
password, ok := parsed.User.Password()
|
||||
if !ok || !validLegacyDatabaseUser(username) || !validLegacyDatabasePassword(password) {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
host, ok := normalizeLegacyDatabaseHost(parsed.Hostname())
|
||||
if !ok {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
port := int(DefaultPostgreSQLPort)
|
||||
if rawPort := parsed.Port(); rawPort != "" {
|
||||
parsedPort, parseErr := strconv.Atoi(rawPort)
|
||||
if parseErr != nil || !validPort(parsedPort) {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
port = parsedPort
|
||||
}
|
||||
name := strings.TrimPrefix(parsed.EscapedPath(), "/")
|
||||
if name == "" || strings.Contains(name, "/") || !databaseLocalRefPattern.MatchString(name) {
|
||||
return DatabaseConfig{}, "", false
|
||||
}
|
||||
return DatabaseConfig{Type: DatabaseTypePostgreSQL, Host: host, Port: uint16(port), Username: username, Name: name}, password, true
|
||||
}
|
||||
|
||||
func normalizeLegacyDatabaseHost(value string) (string, bool) {
|
||||
if value == "" || strings.TrimSpace(value) != value {
|
||||
return "", false
|
||||
}
|
||||
if address := net.ParseIP(value); address != nil {
|
||||
return address.String(), true
|
||||
}
|
||||
normalized, err := normalizeConfigDomain(value)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return normalized, true
|
||||
}
|
||||
|
||||
func validLegacyDatabaseUser(value string) bool {
|
||||
return value != "" && strings.TrimSpace(value) == value && !strings.ContainsAny(value, "\r\n\x00")
|
||||
}
|
||||
|
||||
func validLegacyDatabasePassword(value string) bool {
|
||||
return value != "" && strings.TrimSpace(value) == value && !strings.ContainsAny(value, "\r\n\x00")
|
||||
}
|
||||
|
||||
func legacySMTPConfig(root string, vault *credential.Vault, legacy legacyMigrationConfig) (SMTPConfig, []string, error) {
|
||||
relay := SMTPConfig{
|
||||
RelayHost: legacy.SMTPHost,
|
||||
RelayPort: legacy.SMTPPort,
|
||||
Username: legacy.SMTPUsername,
|
||||
Security: legacy.SMTPSecurity,
|
||||
FromDomain: legacy.SMTPFromDomain,
|
||||
VerificationEnabled: legacy.VerificationEnabled,
|
||||
LoginPolicy: legacyLoginPolicy(legacy),
|
||||
}
|
||||
if relay.FromDomain == "" {
|
||||
relay.FromDomain = legacy.Domain
|
||||
}
|
||||
if !relay.VerificationEnabled && relay.RelayHost == "" && relay.RelayPort == 0 && relay.Username == "" && legacy.SMTPPassword == "" {
|
||||
return relay, nil, nil
|
||||
}
|
||||
if relay.RelayPort == 0 {
|
||||
relay.RelayPort = 587
|
||||
}
|
||||
if relay.Security == "" {
|
||||
relay.Security = "starttls"
|
||||
}
|
||||
if relay.Username == "" && legacy.SMTPPassword != "" {
|
||||
return SMTPConfig{}, []string{"smtpPassword"}, nil
|
||||
}
|
||||
if relay.Username != "" {
|
||||
stored, err := legacyStoredSecret(root, vault, legacy.SMTPPassword)
|
||||
if err != nil {
|
||||
return SMTPConfig{}, nil, err
|
||||
}
|
||||
relay.Password = stored
|
||||
}
|
||||
return relay, nil, nil
|
||||
}
|
||||
|
||||
func legacySSLConfig(legacy legacyMigrationConfig) (SSLConfig, []string) {
|
||||
certificateRef, certificateOK := legacyControlledReference(legacy.SSLPublicKeyPath)
|
||||
privateKeyRef, privateKeyOK := legacyControlledReference(legacy.SSLPrivateKeyPath)
|
||||
ssl := SSLConfig{Mode: ManagedSSLModeDefault, DefaultDirectory: "config/ssl"}
|
||||
if legacy.SSLType == SSLTypeUser {
|
||||
ssl.Mode = ManagedSSLModeUpload
|
||||
ssl.CustomDirectory = "config/ssl"
|
||||
}
|
||||
if legacy.SSLPublicKeyPath == "" && legacy.SSLPrivateKeyPath == "" {
|
||||
return ssl, nil
|
||||
}
|
||||
if !certificateOK || !privateKeyOK {
|
||||
return ssl, []string{"SSLPublicKeyPath", "SSLPrivateKeyPath"}
|
||||
}
|
||||
ssl.CertificateRef = certificateRef
|
||||
ssl.PrivateKeyRef = privateKeyRef
|
||||
return ssl, nil
|
||||
}
|
||||
|
||||
func legacyPluginsConfig(root string, vault *credential.Vault, legacy legacyMigrationConfig) (map[string]PluginInstance, []string, error) {
|
||||
enabled, manualFields := legacyPluginEnabledSet(legacy.PluginAllowList)
|
||||
spamExecutable, err := catalog.CurrentExecutableName("spam_block")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
wechatExecutable, err := catalog.CurrentExecutableName("wechat_push")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
telegramExecutable, err := catalog.CurrentExecutableName("telegram_push")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
plugins := map[string]PluginInstance{
|
||||
"spam_block": {Enabled: enabled["spam_block"], Executable: spamExecutable, Permissions: PluginPermissions{Capabilities: []string{"network"}}},
|
||||
}
|
||||
wechatEnabled := legacy.WeChatPushAppID != "" || legacy.WeChatPushSecret != "" || legacy.WeChatPushTemplateID != "" || legacy.WeChatPushUserID != ""
|
||||
wechat := PluginInstance{
|
||||
Enabled: enabled["wechat_push"],
|
||||
Executable: wechatExecutable,
|
||||
Permissions: PluginPermissions{Capabilities: []string{"network"}, Network: []PluginNetworkTarget{{Scheme: "https", Host: "api.weixin.qq.com", Port: 443}}},
|
||||
}
|
||||
if wechatEnabled {
|
||||
settings, err := json.Marshal(map[string]string{"appId": legacy.WeChatPushAppID, "templateId": legacy.WeChatPushTemplateID, "userId": legacy.WeChatPushUserID, "webDomain": legacy.WebDomain})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
wechat.Settings = settings
|
||||
if legacy.WeChatPushSecret == "" {
|
||||
manualFields = append(manualFields, "weChatPushSecret")
|
||||
} else {
|
||||
stored, storeErr := legacyStoredSecret(root, vault, legacy.WeChatPushSecret)
|
||||
if storeErr != nil {
|
||||
return nil, nil, storeErr
|
||||
}
|
||||
wechat.Secret = stored
|
||||
}
|
||||
}
|
||||
if wechat.Enabled && !wechatEnabled {
|
||||
manualFields = append(manualFields, "weChatPushSecret")
|
||||
}
|
||||
plugins["wechat_push"] = wechat
|
||||
telegramEnabled := legacy.TGBotToken != "" || legacy.TGChatID != ""
|
||||
telegram := PluginInstance{
|
||||
Enabled: enabled["telegram_push"],
|
||||
Executable: telegramExecutable,
|
||||
Permissions: PluginPermissions{Capabilities: []string{"network"}, Network: []PluginNetworkTarget{{Scheme: "https", Host: "api.telegram.org", Port: 443}}},
|
||||
}
|
||||
if telegramEnabled {
|
||||
if legacy.TGBotToken == "" {
|
||||
manualFields = append(manualFields, "tgBotToken")
|
||||
} else {
|
||||
stored, storeErr := legacyStoredSecret(root, vault, legacy.TGBotToken)
|
||||
if storeErr != nil {
|
||||
return nil, nil, storeErr
|
||||
}
|
||||
telegram.Secret = stored
|
||||
}
|
||||
if legacy.TGChatID != "" {
|
||||
manualFields = append(manualFields, "tgChatId")
|
||||
}
|
||||
}
|
||||
if telegram.Enabled && !telegramEnabled {
|
||||
manualFields = append(manualFields, "tgBotToken")
|
||||
}
|
||||
plugins["telegram_push"] = telegram
|
||||
if legacy.WebPushURL != "" || legacy.WebPushToken != "" {
|
||||
manualFields = append(manualFields, "webPushUrl", "webPushToken")
|
||||
}
|
||||
sort.Strings(manualFields)
|
||||
return plugins, compactMigrationFields(manualFields), nil
|
||||
}
|
||||
|
||||
func legacyPluginEnabledSet(values []string) (map[string]bool, []string) {
|
||||
enabled := make(map[string]bool, len(legacyPluginExecutableMap))
|
||||
manualFields := make([]string, 0)
|
||||
for _, value := range values {
|
||||
name := strings.TrimSpace(value)
|
||||
if name == "" || name != value || name != strings.ToLower(name) || filepath.Base(name) != name || strings.ContainsAny(name, `/\\*?[]`) {
|
||||
manualFields = append(manualFields, "pluginAllowList")
|
||||
continue
|
||||
}
|
||||
logicalID, ok := legacyPluginExecutableMap[name]
|
||||
if !ok || enabled[logicalID] {
|
||||
manualFields = append(manualFields, "pluginAllowList")
|
||||
continue
|
||||
}
|
||||
enabled[logicalID] = true
|
||||
}
|
||||
sort.Strings(manualFields)
|
||||
return enabled, compactMigrationFields(manualFields)
|
||||
}
|
||||
|
||||
func legacyStoredSecret(root string, vault *credential.Vault, value string) (StoredSecret, error) {
|
||||
if strings.TrimSpace(value) != value || value == "" {
|
||||
return StoredSecret{}, ErrMigrationUnsafeValue
|
||||
}
|
||||
plaintext := value
|
||||
if credential.IsEncrypted(value) {
|
||||
decrypted, err := credential.Decrypt(root, value)
|
||||
if err != nil {
|
||||
return StoredSecret{}, ErrMigrationUnsafeValue
|
||||
}
|
||||
plaintext = decrypted
|
||||
}
|
||||
return ApplySecretChange(vault, StoredSecret{}, SecretChange{Operation: SecretReplace, Value: &SecretInput{Plaintext: plaintext}}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
}
|
||||
|
||||
func legacyLoginPolicy(legacy legacyMigrationConfig) string {
|
||||
if legacy.LoginPolicy != "" {
|
||||
return legacy.LoginPolicy
|
||||
}
|
||||
return legacy.VerificationLoginPolicy
|
||||
}
|
||||
|
||||
func legacyControlledReference(value string) (string, bool) {
|
||||
value = strings.TrimPrefix(strings.TrimSpace(value), "./")
|
||||
value = filepath.ToSlash(value)
|
||||
if !isControlledRelativePath(value) {
|
||||
return "", false
|
||||
}
|
||||
return value, true
|
||||
}
|
||||
|
||||
func writeLegacyMigrationBackup(directory string, data []byte, now time.Time) (string, error) {
|
||||
backupDirectory := filepath.Join(directory, "migration-backups")
|
||||
if err := os.MkdirAll(backupDirectory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
info, err := os.Lstat(backupDirectory)
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", ErrManagerStorage
|
||||
}
|
||||
if err := os.Chmod(backupDirectory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
token := make([]byte, 8)
|
||||
if _, err := rand.Read(token); err != nil {
|
||||
return "", err
|
||||
}
|
||||
name := "legacy-config-" + now.Format("20060102T150405.000000000Z") + "-" + hex.EncodeToString(token) + ".json"
|
||||
path := filepath.Join(backupDirectory, name)
|
||||
if err := atomicfile.Write(path, data, 0600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
@@ -0,0 +1,473 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
)
|
||||
|
||||
func writeLegacyMigrationFixture(t *testing.T, root string, value any) []byte {
|
||||
t.Helper()
|
||||
directory := filepath.Join(root, "config")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(directory, "config.json"), data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
legacy := map[string]any{
|
||||
"logLevel": "info",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com", "example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "1",
|
||||
"SSLPrivateKeyPath": "./config/ssl/private.key",
|
||||
"SSLPublicKeyPath": "./config/ssl/public.crt",
|
||||
"dbDSN": "./config/smail.db",
|
||||
"dbType": "sqlite",
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
"weChatPushAppId": "app-id",
|
||||
"weChatPushSecret": "wechat-secret",
|
||||
"weChatPushTemplateId": "template-id",
|
||||
"isInit": true,
|
||||
}
|
||||
original := writeLegacyMigrationFixture(t, root, legacy)
|
||||
|
||||
result, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Generation != 1 || result.BackupPath == "" {
|
||||
t.Fatalf("unexpected migration result: %+v", result)
|
||||
}
|
||||
backup, err := os.ReadFile(result.BackupPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(backup, original) {
|
||||
t.Fatal("legacy backup does not match the original configuration")
|
||||
}
|
||||
loader := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
runtime, err := loader.Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if runtime.Root.Database.LocalRef != "smail.db" || runtime.Plugins.Plugins["wechat_push"].Secret.Ciphertext == "" {
|
||||
t.Fatalf("unexpected migrated runtime: %+v", runtime)
|
||||
}
|
||||
for _, name := range []string{"config.json", "smtp.json", "email.json", "ssl.json", "plugins.json"} {
|
||||
data, readErr := os.ReadFile(filepath.Join(root, "config", name))
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if bytes.Contains(data, []byte("wechat-secret")) {
|
||||
t.Fatalf("plaintext credential leaked to %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanLegacyMigrationReportsUnknownFieldsWithoutOverwritingConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"unsupported": true,
|
||||
})
|
||||
plan, err := PlanLegacyMigration(root, RuntimeValidationOptions{})
|
||||
if !errors.Is(err, ErrMigrationUnknownField) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if len(plan.UnknownFields) != 1 || plan.UnknownFields[0] != "unsupported" {
|
||||
t.Fatalf("unexpected unknown fields: %+v", plan.UnknownFields)
|
||||
}
|
||||
current, readErr := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("migration planning overwrote the original configuration")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsManualCredentialsWithoutOverwritingConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpsPort": 443,
|
||||
"tgBotToken": "token",
|
||||
"tgChatId": "123456",
|
||||
"httpPort": 80,
|
||||
"spamFilterLevel": 0,
|
||||
})
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationUnsafeValue) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
current, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("failed migration overwrote the original configuration")
|
||||
}
|
||||
for _, name := range []string{"smtp.json", "email.json", "ssl.json", "plugins.json"} {
|
||||
if _, err := os.Stat(filepath.Join(root, "config", name)); !os.IsNotExist(err) {
|
||||
t.Fatalf("failed migration created %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigReencryptsExistingLegacyCredential(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if _, err := credential.NewVault(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encrypted, err := credential.Encrypt(root, "telegram-token")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpsPort": 443,
|
||||
"tgBotToken": encrypted,
|
||||
"httpPort": 80,
|
||||
})
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opened, err := OpenStoredSecret(mustMigrationVault(t, root), runtime.Plugins.Plugins["telegram_push"].Secret)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if opened != "telegram-token" {
|
||||
t.Fatalf("opened secret = %q", opened)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigResumesWithEquivalentExistingSecretSection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
legacy := map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
"tgBotToken": "telegram-token",
|
||||
"pluginAllowList": []string{"telegram_push"},
|
||||
}
|
||||
original := writeLegacyMigrationFixture(t, root, legacy)
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pluginsPath := filepath.Join(root, "config", "plugins.json")
|
||||
pluginsBefore, err := os.ReadFile(pluginsPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"smtp.json", "email.json", "ssl.json"} {
|
||||
if err := os.Remove(filepath.Join(root, "config", name)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "config", "config.json"), original, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pluginsAfter, err := os.ReadFile(pluginsPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(pluginsBefore, pluginsAfter) {
|
||||
t.Fatal("resume replaced an equivalent existing secret section")
|
||||
}
|
||||
if _, err := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}).Load(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsInconsistentExistingSectionBeforeWriting(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
conflicting := []byte(`{"meta":{"schema":1,"generation":1},"policy":{"smtpPort":2525}}`)
|
||||
conflictingPath := filepath.Join(root, "config", "email.json")
|
||||
if err := os.WriteFile(conflictingPath, conflicting, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationTargetExists) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
current, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("conflicting migration changed the legacy root configuration")
|
||||
}
|
||||
stored, err := os.ReadFile(conflictingPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(stored, conflicting) {
|
||||
t.Fatal("conflicting section was overwritten")
|
||||
}
|
||||
for _, name := range []string{"smtp.json", "ssl.json", "plugins.json"} {
|
||||
if _, err := os.Stat(filepath.Join(root, "config", name)); !os.IsNotExist(err) {
|
||||
t.Fatalf("conflicting migration created %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustMigrationVault(t *testing.T, root string) *credential.Vault {
|
||||
t.Helper()
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return vault
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigMapsPluginAllowListToFixedLogicalIDs(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
"weChatPushAppId": "app-id",
|
||||
"weChatPushSecret": "wechat-secret",
|
||||
"weChatPushTemplateId": "template-id",
|
||||
"tgBotToken": "telegram-token",
|
||||
"pluginAllowList": []string{"spam_block.exe", "wechat_push", "pmail_telegram_push.exe"},
|
||||
})
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, logicalID := range []string{"spam_block", "wechat_push", "telegram_push"} {
|
||||
plugin, ok := runtime.Plugins.Plugins[logicalID]
|
||||
if !ok || !plugin.Enabled {
|
||||
t.Fatalf("plugin %s was not enabled from the legacy allow list: %+v", logicalID, runtime.Plugins.Plugins)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanLegacyMigrationRejectsUnsafePluginAllowList(t *testing.T) {
|
||||
cases := map[string][]string{
|
||||
"路径": {"../spam_block"},
|
||||
"大小写冲突": {"Spam_Block"},
|
||||
"未知插件": {"unknown_plugin"},
|
||||
"逻辑重复": {"spam_block", "spam_block.exe"},
|
||||
}
|
||||
for name, allowList := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"pluginAllowList": allowList,
|
||||
})
|
||||
plan, err := PlanLegacyMigration(root, RuntimeValidationOptions{})
|
||||
if !errors.Is(err, ErrMigrationUnsafeValue) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if len(plan.ManualFields) != 1 || plan.ManualFields[0] != "pluginAllowList" {
|
||||
t.Fatalf("manual fields = %+v", plan.ManualFields)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanLegacyMigrationRejectsEnabledPluginWithoutRequiredCredential(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"pluginAllowList": []string{"wechat_push"},
|
||||
})
|
||||
plan, err := PlanLegacyMigration(root, RuntimeValidationOptions{})
|
||||
if !errors.Is(err, ErrMigrationUnsafeValue) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if len(plan.ManualFields) != 1 || plan.ManualFields[0] != "weChatPushSecret" {
|
||||
t.Fatalf("manual fields = %+v", plan.ManualFields)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigParsesKnownStructuredDatabaseDSNs(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
database string
|
||||
dsn string
|
||||
wantType DatabaseType
|
||||
wantPort uint16
|
||||
wantHost string
|
||||
wantName string
|
||||
password string
|
||||
}{
|
||||
{
|
||||
name: "MySQL",
|
||||
database: DBTypeMySQL,
|
||||
dsn: "smail:database-secret@tcp(db.example.com:3307)/smail?charset=utf8mb4",
|
||||
wantType: DatabaseTypeMySQL,
|
||||
wantPort: 3307,
|
||||
wantHost: "db.example.com",
|
||||
wantName: "smail",
|
||||
password: "database-secret",
|
||||
},
|
||||
{
|
||||
name: "PostgreSQL",
|
||||
database: DBTypePostgres,
|
||||
dsn: "postgres://smail:database-secret@db.example.com:5433/smail",
|
||||
wantType: DatabaseTypePostgreSQL,
|
||||
wantPort: 5433,
|
||||
wantHost: "db.example.com",
|
||||
wantName: "smail",
|
||||
password: "database-secret",
|
||||
},
|
||||
}
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": testCase.dsn,
|
||||
"dbType": testCase.database,
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
database := runtime.Root.Database
|
||||
if database.Type != testCase.wantType || database.Port != testCase.wantPort || database.Host != testCase.wantHost || database.Name != testCase.wantName || database.Username != "smail" {
|
||||
t.Fatalf("database = %+v", database)
|
||||
}
|
||||
opened, err := OpenStoredSecret(mustMigrationVault(t, root), database.Password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if opened != testCase.password {
|
||||
t.Fatalf("database password = %q", opened)
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Contains(data, []byte(testCase.password)) || bytes.Contains(data, []byte(testCase.dsn)) {
|
||||
t.Fatal("database credential leaked to the migrated configuration")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanLegacyMigrationRejectsAmbiguousDatabaseDSN(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "postgres://smail:password@db.example.com:5432/smail?sslmode=disable",
|
||||
"dbType": DBTypePostgres,
|
||||
})
|
||||
plan, err := PlanLegacyMigration(root, RuntimeValidationOptions{})
|
||||
if !errors.Is(err, ErrMigrationUnsafeValue) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if len(plan.ManualFields) != 1 || plan.ManualFields[0] != "dbDSN" {
|
||||
t.Fatalf("manual fields = %+v", plan.ManualFields)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigDerivesSetupStateFromFullPreflight(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
options := RuntimeValidationOptions{HasSuperAdmin: false, Production: true}
|
||||
if _, err := MigrateLegacyConfig(root, options); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewConfigLoader(root, ConfigLoaderOptions{Validation: options}).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if runtime.SetupState != SetupStateDegraded || runtime.Root.SetupState != SetupStateDegraded {
|
||||
t.Fatalf("setup state = %q / %q", runtime.SetupState, runtime.Root.SetupState)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.pcux.cn/xuans/Smail/plugins/catalog"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -148,7 +149,11 @@ func validateRuntimePorts(http HTTPConfig, email EmailConfig) error {
|
||||
|
||||
func validatePluginSection(section PluginsSectionConfig) error {
|
||||
for logicalID, plugin := range section.Plugins {
|
||||
if !logicalIDPattern.MatchString(logicalID) || filepath.Base(plugin.Executable) != plugin.Executable || plugin.Executable == "" {
|
||||
if !logicalIDPattern.MatchString(logicalID) || filepath.Base(plugin.Executable) != plugin.Executable || !catalog.IsCurrentExecutable(logicalID, plugin.Executable) {
|
||||
return ErrInvalidPlugin
|
||||
}
|
||||
definition, ok := catalog.Lookup(logicalID)
|
||||
if !ok {
|
||||
return ErrInvalidPlugin
|
||||
}
|
||||
capabilities := make(map[string]struct{}, len(plugin.Permissions.Capabilities))
|
||||
@@ -161,6 +166,9 @@ func validatePluginSection(section PluginsSectionConfig) error {
|
||||
}
|
||||
capabilities[capability] = struct{}{}
|
||||
}
|
||||
if !matchesCatalogCapabilities(capabilities, definition.Capabilities) {
|
||||
return ErrPluginCapability
|
||||
}
|
||||
if len(plugin.Permissions.Network) > 0 {
|
||||
if _, ok := capabilities["network"]; !ok {
|
||||
return ErrPluginCapability
|
||||
@@ -191,6 +199,18 @@ func validatePluginSection(section PluginsSectionConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func matchesCatalogCapabilities(actual map[string]struct{}, expected []string) bool {
|
||||
if len(actual) != len(expected) {
|
||||
return false
|
||||
}
|
||||
for _, capability := range expected {
|
||||
if _, ok := actual[capability]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func validPluginSettings(data json.RawMessage) bool {
|
||||
if len(data) == 0 {
|
||||
return true
|
||||
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.pcux.cn/xuans/Smail/plugins/catalog"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -27,7 +28,7 @@ func validRuntimeConfig() RuntimeConfig {
|
||||
Plugins: PluginsSectionConfig{Meta: meta, Plugins: map[string]PluginInstance{
|
||||
"telegram_push": {
|
||||
Enabled: true,
|
||||
Executable: "telegram_push",
|
||||
Executable: currentPluginExecutable("telegram_push"),
|
||||
Permissions: PluginPermissions{
|
||||
Capabilities: []string{"network"},
|
||||
Network: []PluginNetworkTarget{{Scheme: "https", Host: "api.telegram.org", Port: 443}},
|
||||
@@ -38,6 +39,14 @@ func validRuntimeConfig() RuntimeConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func currentPluginExecutable(logicalID string) string {
|
||||
executable, err := catalog.CurrentExecutableName(logicalID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return executable
|
||||
}
|
||||
|
||||
func TestValidateRuntimeConfig(t *testing.T) {
|
||||
runtime := validRuntimeConfig()
|
||||
runtime.Root.Domains.AccessHosts = []string{"192.0.2.10", "reset.example.com"}
|
||||
@@ -73,6 +82,16 @@ func TestValidateRuntimeConfigRejectsCrossSectionViolations(t *testing.T) {
|
||||
plugin.Settings = json.RawMessage(`{"botToken":"secret"}`)
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
}, want: ErrInvalidPlugin},
|
||||
{name: "plugin_executable_mismatch", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) {
|
||||
plugin := runtime.Plugins.Plugins["telegram_push"]
|
||||
plugin.Executable = "untrusted_plugin.exe"
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
}, want: ErrInvalidPlugin},
|
||||
{name: "plugin_capability_escalation", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) {
|
||||
plugin := runtime.Plugins.Plugins["telegram_push"]
|
||||
plugin.Permissions.Capabilities = append(plugin.Permissions.Capabilities, "filesystem")
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
}, want: ErrPluginCapability},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
@@ -19,8 +19,9 @@ var (
|
||||
type ConfigOperation string
|
||||
|
||||
const (
|
||||
ConfigOperationUpdate ConfigOperation = "update"
|
||||
ConfigOperationRollback ConfigOperation = "rollback"
|
||||
ConfigOperationUpdate ConfigOperation = "update"
|
||||
ConfigOperationRollback ConfigOperation = "rollback"
|
||||
ConfigOperationMigration ConfigOperation = "migration"
|
||||
)
|
||||
|
||||
type ConfigAuditEvent struct {
|
||||
|
||||
@@ -0,0 +1,519 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/atomicfile"
|
||||
)
|
||||
|
||||
const (
|
||||
ManagedSSLModeDefault = "managed_default"
|
||||
ManagedSSLModeUpload = "managed_upload"
|
||||
|
||||
managedSSLRelativeRoot = "config/ssl/managed"
|
||||
maxManagedSSLFileSize = 1 << 20
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidSSLMode = errors.New("invalid SSL managed mode")
|
||||
ErrInvalidSSLMaterial = errors.New("invalid SSL certificate material")
|
||||
ErrInvalidSSLReference = errors.New("invalid SSL certificate reference")
|
||||
ErrUnsafeSSLFile = errors.New("unsafe SSL certificate file")
|
||||
ErrSSLCertificateMismatch = errors.New("SSL certificate and private key do not match")
|
||||
ErrSSLCertificateDomain = errors.New("SSL certificate domain does not match")
|
||||
ErrSSLCertificateExpired = errors.New("SSL certificate is expired or not yet valid")
|
||||
ErrSSLPrivateKeyPermission = errors.New("SSL private key permissions are too broad")
|
||||
)
|
||||
|
||||
type ManagedSSLReference struct {
|
||||
Mode string
|
||||
CertificateRef string
|
||||
PrivateKeyRef string
|
||||
CertificatePath string
|
||||
PrivateKeyPath string
|
||||
}
|
||||
|
||||
// ResolveRuntimeSSLPaths 返回服务启动时使用的受控证书路径。
|
||||
// 新配置优先使用内部引用,旧配置只通过只读兼容适配层加载。
|
||||
func ResolveRuntimeSSLPaths(cfg *Config) (string, string, error) {
|
||||
if cfg == nil {
|
||||
return "", "", ErrInvalidSSLReference
|
||||
}
|
||||
if cfg.SSL.Mode != "" || cfg.SSL.CertificateRef != "" || cfg.SSL.PrivateKeyRef != "" {
|
||||
if !validManagedSSLMode(cfg.SSL.Mode) || cfg.SSL.CertificateRef == "" || cfg.SSL.PrivateKeyRef == "" {
|
||||
return "", "", ErrInvalidSSLReference
|
||||
}
|
||||
resolved, err := ResolveManagedSSLReferences(cfg.SSL)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return resolved.CertificatePath, resolved.PrivateKeyPath, nil
|
||||
}
|
||||
return resolveLegacySSLPaths(cfg.SSLPublicKeyPath, cfg.SSLPrivateKeyPath)
|
||||
}
|
||||
|
||||
// LoadRuntimeTLSCertificate 在所有 TLS 服务中统一解析并加载证书。
|
||||
func LoadRuntimeTLSCertificate(cfg *Config) (tls.Certificate, error) {
|
||||
certificatePath, privateKeyPath, err := ResolveRuntimeSSLPaths(cfg)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
certificate, err := tls.LoadX509KeyPair(certificatePath, privateKeyPath)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
return certificate, nil
|
||||
}
|
||||
|
||||
// ImportManagedSSL 校验证书材料后写入候选目录,并原子切换 current 目录。
|
||||
func ImportManagedSSL(mode string, privateKeyPEM, certificatePEM, issuerPEM []byte, domains []string) (ManagedSSLReference, error) {
|
||||
if !validManagedSSLMode(mode) {
|
||||
return ManagedSSLReference{}, ErrInvalidSSLMode
|
||||
}
|
||||
if err := validateSSLMaterial(privateKeyPEM, certificatePEM, issuerPEM, domains); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
|
||||
rootPath, err := managedSSLRootPath(mode)
|
||||
if err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
if err := ensureManagedDirectory(rootPath); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
|
||||
token, err := randomStorageToken()
|
||||
if err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
candidatePath := filepath.Join(rootPath, ".candidate-"+token)
|
||||
if err := os.Mkdir(candidatePath, 0700); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
defer func() {
|
||||
if _, statErr := os.Stat(candidatePath); statErr == nil {
|
||||
_ = os.RemoveAll(candidatePath)
|
||||
}
|
||||
}()
|
||||
|
||||
candidateRoot, err := os.OpenRoot(candidatePath)
|
||||
if err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
write := func(name string, data []byte, permission os.FileMode) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return atomicfile.WriteRoot(candidateRoot, name, data, permission)
|
||||
}
|
||||
if err := write("private.key", privateKeyPEM, 0600); err != nil {
|
||||
candidateRoot.Close()
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
if err := write("public.crt", certificatePEM, 0600); err != nil {
|
||||
candidateRoot.Close()
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
if err := write("issuer.crt", issuerPEM, 0600); err != nil {
|
||||
candidateRoot.Close()
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
if err := candidateRoot.Close(); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
|
||||
activePath := filepath.Join(rootPath, "current")
|
||||
backupPath := filepath.Join(rootPath, ".previous-"+token)
|
||||
previousMoved := false
|
||||
activeInstalled := false
|
||||
if info, statErr := os.Lstat(activePath); statErr == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return ManagedSSLReference{}, ErrUnsafeSSLFile
|
||||
}
|
||||
if err := os.Rename(activePath, backupPath); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
previousMoved = true
|
||||
if err := os.Rename(candidatePath, activePath); err != nil {
|
||||
_ = os.Rename(backupPath, activePath)
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
activeInstalled = true
|
||||
} else if !os.IsNotExist(statErr) {
|
||||
return ManagedSSLReference{}, statErr
|
||||
} else if err := os.Rename(candidatePath, activePath); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
} else {
|
||||
activeInstalled = true
|
||||
}
|
||||
|
||||
certificatePath := filepath.Join(activePath, "public.crt")
|
||||
privateKeyPath := filepath.Join(activePath, "private.key")
|
||||
if err := validateManagedSSLFiles(certificatePath, privateKeyPath); err != nil {
|
||||
rollbackErr := rollbackManagedSSLActivation(activePath, backupPath, previousMoved, activeInstalled)
|
||||
if rollbackErr != nil {
|
||||
return ManagedSSLReference{}, fmt.Errorf("%w; rollback failed: %v", err, rollbackErr)
|
||||
}
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
return ManagedSSLReference{
|
||||
Mode: mode,
|
||||
CertificateRef: managedSSLReference(mode, "public.crt"),
|
||||
PrivateKeyRef: managedSSLReference(mode, "private.key"),
|
||||
CertificatePath: certificatePath,
|
||||
PrivateKeyPath: privateKeyPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ResolveManagedSSLReferences 将内部引用解析为受控文件路径,拒绝任意路径输入。
|
||||
func ResolveManagedSSLReferences(ssl SSLConfig) (ManagedSSLReference, error) {
|
||||
if !validManagedSSLMode(ssl.Mode) {
|
||||
return ManagedSSLReference{}, ErrInvalidSSLMode
|
||||
}
|
||||
if ssl.CertificateRef == "" || ssl.PrivateKeyRef == "" {
|
||||
return ManagedSSLReference{}, ErrInvalidSSLReference
|
||||
}
|
||||
rootPath, err := managedSSLRootPath(ssl.Mode)
|
||||
if err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
certificatePath, err := resolveManagedReference(rootPath, ssl.CertificateRef, "public.crt")
|
||||
if err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
privateKeyPath, err := resolveManagedReference(rootPath, ssl.PrivateKeyRef, "private.key")
|
||||
if err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
if err := validateManagedSSLFiles(certificatePath, privateKeyPath); err != nil {
|
||||
return ManagedSSLReference{}, err
|
||||
}
|
||||
return ManagedSSLReference{
|
||||
Mode: ssl.Mode,
|
||||
CertificateRef: ssl.CertificateRef,
|
||||
PrivateKeyRef: ssl.PrivateKeyRef,
|
||||
CertificatePath: certificatePath,
|
||||
PrivateKeyPath: privateKeyPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ValidateSSLMaterial 校验证书链、私钥匹配、有效期和可选域名。
|
||||
func ValidateSSLMaterial(privateKeyPEM, certificatePEM, issuerPEM []byte, domains []string) error {
|
||||
return validateSSLMaterial(privateKeyPEM, certificatePEM, issuerPEM, domains)
|
||||
}
|
||||
|
||||
func validateSSLMaterial(privateKeyPEM, certificatePEM, issuerPEM []byte, domains []string) error {
|
||||
if len(privateKeyPEM) == 0 || len(certificatePEM) == 0 || len(privateKeyPEM) > maxManagedSSLFileSize || len(certificatePEM) > maxManagedSSLFileSize || len(issuerPEM) > maxManagedSSLFileSize {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
certificates, err := parseCertificatePEM(certificatePEM)
|
||||
if err != nil || len(certificates) == 0 {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
if len(issuerPEM) > 0 {
|
||||
issuerCertificates, issuerErr := parseCertificatePEM(issuerPEM)
|
||||
if issuerErr != nil {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
certificates = append(certificates, issuerCertificates...)
|
||||
}
|
||||
if _, err := tls.X509KeyPair(certificatePEM, privateKeyPEM); err != nil {
|
||||
return ErrSSLCertificateMismatch
|
||||
}
|
||||
leaf := certificates[0]
|
||||
now := time.Now()
|
||||
if now.Before(leaf.NotBefore) || !now.Before(leaf.NotAfter) {
|
||||
return ErrSSLCertificateExpired
|
||||
}
|
||||
if err := validateCertificateChain(certificates); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateCertificateKeyStrength(leaf); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, domain := range domains {
|
||||
domain = strings.TrimSpace(domain)
|
||||
if domain == "" {
|
||||
continue
|
||||
}
|
||||
if err := leaf.VerifyHostname(domain); err != nil {
|
||||
return fmt.Errorf("%w: %s", ErrSSLCertificateDomain, domain)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseCertificatePEM(data []byte) ([]*x509.Certificate, error) {
|
||||
var certificates []*x509.Certificate
|
||||
remaining := data
|
||||
for {
|
||||
block, rest := pem.Decode(remaining)
|
||||
if block == nil {
|
||||
if len(strings.TrimSpace(string(remaining))) != 0 {
|
||||
return nil, ErrInvalidSSLMaterial
|
||||
}
|
||||
break
|
||||
}
|
||||
if block.Type != "CERTIFICATE" || len(block.Bytes) == 0 {
|
||||
return nil, ErrInvalidSSLMaterial
|
||||
}
|
||||
certificate, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidSSLMaterial
|
||||
}
|
||||
certificates = append(certificates, certificate)
|
||||
remaining = rest
|
||||
}
|
||||
return certificates, nil
|
||||
}
|
||||
|
||||
func validateCertificateChain(certificates []*x509.Certificate) error {
|
||||
for index := 0; index+1 < len(certificates); index++ {
|
||||
issuer := certificates[index+1]
|
||||
if !issuer.IsCA || issuer.BasicConstraintsValid && !issuer.IsCA {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
if err := certificates[index].CheckSignatureFrom(issuer); err != nil {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateCertificateKeyStrength(certificate *x509.Certificate) error {
|
||||
switch publicKey := certificate.PublicKey.(type) {
|
||||
case *rsa.PublicKey:
|
||||
return validateRSAKeyStrength(publicKey)
|
||||
case *ecdsa.PublicKey:
|
||||
if publicKey.Curve == nil || publicKey.Curve.Params().BitSize < 256 {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
}
|
||||
|
||||
func validateRSAKeyStrength(publicKey *rsa.PublicKey) error {
|
||||
if publicKey == nil || publicKey.N == nil || publicKey.N.BitLen() < 2048 {
|
||||
return ErrInvalidSSLMaterial
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validManagedSSLMode(mode string) bool {
|
||||
return mode == ManagedSSLModeDefault || mode == ManagedSSLModeUpload
|
||||
}
|
||||
|
||||
func managedSSLRootPath(mode string) (string, error) {
|
||||
if !validManagedSSLMode(mode) {
|
||||
return "", ErrInvalidSSLMode
|
||||
}
|
||||
rootPath, err := filepath.Abs(filepath.Clean(ROOT_PATH))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
folder := "default"
|
||||
if mode == ManagedSSLModeUpload {
|
||||
folder = "upload"
|
||||
}
|
||||
return filepath.Join(rootPath, managedSSLRelativeRoot, folder), nil
|
||||
}
|
||||
|
||||
func managedSSLReference(mode, name string) string {
|
||||
folder := "default"
|
||||
if mode == ManagedSSLModeUpload {
|
||||
folder = "upload"
|
||||
}
|
||||
return filepath.ToSlash(filepath.Join(managedSSLRelativeRoot, folder, "current", name))
|
||||
}
|
||||
|
||||
func resolveManagedReference(rootPath, reference, expectedName string) (string, error) {
|
||||
rawReference := strings.TrimSpace(reference)
|
||||
if rawReference == "" || strings.Contains(rawReference, `\`) || filepath.IsAbs(rawReference) || isWindowsAbsolute(rawReference) {
|
||||
return "", ErrInvalidSSLReference
|
||||
}
|
||||
reference = filepath.FromSlash(rawReference)
|
||||
if filepath.ToSlash(filepath.Clean(reference)) != rawReference {
|
||||
return "", ErrInvalidSSLReference
|
||||
}
|
||||
workspaceRoot, err := filepath.Abs(filepath.Clean(ROOT_PATH))
|
||||
if err != nil {
|
||||
return "", ErrInvalidSSLReference
|
||||
}
|
||||
target, err := filepath.Abs(filepath.Join(workspaceRoot, reference))
|
||||
if err != nil || filepath.Base(target) != expectedName || !pathWithin(rootPath, target) {
|
||||
return "", ErrInvalidSSLReference
|
||||
}
|
||||
if filepath.Dir(target) != filepath.Join(rootPath, "current") {
|
||||
return "", ErrInvalidSSLReference
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func validateManagedSSLFiles(certificatePath, privateKeyPath string) error {
|
||||
for _, item := range []struct {
|
||||
path string
|
||||
isPrivate bool
|
||||
}{{certificatePath, false}, {privateKeyPath, true}} {
|
||||
info, err := os.Lstat(item.path)
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > maxManagedSSLFileSize || hasMultipleFileLinks(item.path) {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
if item.isPrivate && info.Mode().Perm()&0077 != 0 && runtimeSupportsPermissionCheck() {
|
||||
return ErrSSLPrivateKeyPermission
|
||||
}
|
||||
}
|
||||
privateKeyPEM, err := os.ReadFile(privateKeyPath)
|
||||
if err != nil {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
certificatePEM, err := os.ReadFile(certificatePath)
|
||||
if err != nil {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
return validateSSLMaterial(privateKeyPEM, certificatePEM, nil, nil)
|
||||
}
|
||||
|
||||
func resolveLegacySSLPaths(certificatePath, privateKeyPath string) (string, string, error) {
|
||||
certificatePath, certificatePEM, err := readLegacySSLFile(certificatePath, false)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
privateKeyPath, privateKeyPEM, err := readLegacySSLFile(privateKeyPath, true)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if err := validateSSLMaterial(privateKeyPEM, certificatePEM, nil, nil); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return certificatePath, privateKeyPath, nil
|
||||
}
|
||||
|
||||
func readLegacySSLFile(value string, privateKey bool) (string, []byte, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || !filepath.IsAbs(value) {
|
||||
return "", nil, ErrInvalidSSLReference
|
||||
}
|
||||
path := filepath.Clean(value)
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > maxManagedSSLFileSize || hasMultipleFileLinks(path) {
|
||||
return "", nil, ErrUnsafeSSLFile
|
||||
}
|
||||
resolvedDirectory, err := filepath.EvalSymlinks(filepath.Dir(path))
|
||||
if err != nil || filepath.Clean(resolvedDirectory) != filepath.Clean(filepath.Dir(path)) {
|
||||
return "", nil, ErrUnsafeSSLFile
|
||||
}
|
||||
if privateKey && info.Mode().Perm()&0077 != 0 && runtimeSupportsPermissionCheck() {
|
||||
return "", nil, ErrSSLPrivateKeyPermission
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", nil, ErrUnsafeSSLFile
|
||||
}
|
||||
return path, data, nil
|
||||
}
|
||||
|
||||
func rollbackManagedSSLActivation(activePath, backupPath string, previousMoved, activeInstalled bool) error {
|
||||
if activeInstalled {
|
||||
if err := removeManagedSSLDirectory(activePath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if previousMoved {
|
||||
return os.Rename(backupPath, activePath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeManagedSSLDirectory(directory string) error {
|
||||
info, err := os.Lstat(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
entries, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.Name() != "private.key" && entry.Name() != "public.crt" && entry.Name() != "issuer.crt" {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
entryPath := filepath.Join(directory, entry.Name())
|
||||
entryInfo, statErr := os.Lstat(entryPath)
|
||||
if statErr != nil || entryInfo.Mode()&os.ModeSymlink != 0 || !entryInfo.Mode().IsRegular() {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
if err := os.Remove(entryPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return os.Remove(directory)
|
||||
}
|
||||
|
||||
func ensureManagedDirectory(directory string) error {
|
||||
rootPath, err := filepath.Abs(filepath.Clean(ROOT_PATH))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !pathWithin(rootPath, directory) {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
relative, err := filepath.Rel(rootPath, directory)
|
||||
if err != nil || filepath.IsAbs(relative) {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
current := rootPath
|
||||
for _, part := range strings.Split(relative, string(os.PathSeparator)) {
|
||||
if part == "" || part == "." || part == ".." {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
current = filepath.Join(current, part)
|
||||
info, statErr := os.Lstat(current)
|
||||
if os.IsNotExist(statErr) {
|
||||
if err := os.Mkdir(current, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
info, statErr = os.Lstat(current)
|
||||
}
|
||||
if statErr != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return ErrUnsafeSSLFile
|
||||
}
|
||||
if err := os.Chmod(current, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func pathWithin(root, target string) bool {
|
||||
relative, err := filepath.Rel(root, target)
|
||||
return err == nil && !filepath.IsAbs(relative) && relative != ".." && !strings.HasPrefix(relative, ".."+string(os.PathSeparator))
|
||||
}
|
||||
|
||||
func randomStorageToken() (string, error) {
|
||||
buffer := make([]byte, 16)
|
||||
if _, err := rand.Read(buffer); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(buffer), nil
|
||||
}
|
||||
|
||||
func runtimeSupportsPermissionCheck() bool {
|
||||
return os.PathSeparator == '/'
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !windows
|
||||
|
||||
package config
|
||||
|
||||
import "syscall"
|
||||
|
||||
func hasMultipleFileLinks(path string) bool {
|
||||
var stat syscall.Stat_t
|
||||
if err := syscall.Lstat(path, &stat); err != nil {
|
||||
return true
|
||||
}
|
||||
return stat.Nlink > 1
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//go:build windows
|
||||
|
||||
package config
|
||||
|
||||
func hasMultipleFileLinks(path string) bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestImportManagedSSLUsesInternalReferencesAndAtomicCurrent(t *testing.T) {
|
||||
oldRoot := ROOT_PATH
|
||||
ROOT_PATH = t.TempDir()
|
||||
t.Cleanup(func() { ROOT_PATH = oldRoot })
|
||||
privateKeyPEM, certificatePEM := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
|
||||
first, err := ImportManagedSSL(ManagedSSLModeDefault, privateKeyPEM, certificatePEM, nil, []string{"mail.example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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" {
|
||||
t.Fatalf("内部引用不符合预期: %+v", first)
|
||||
}
|
||||
resolved, err := ResolveManagedSSLReferences(SSLConfig{Mode: ManagedSSLModeDefault, DefaultDirectory: "config/ssl", CertificateRef: first.CertificateRef, PrivateKeyRef: first.PrivateKeyRef})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resolved.CertificatePath != first.CertificatePath || resolved.PrivateKeyPath != first.PrivateKeyPath {
|
||||
t.Fatal("解析后的受控路径与导入结果不一致")
|
||||
}
|
||||
|
||||
secondKey, secondCertificate := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
second, err := ImportManagedSSL(ManagedSSLModeDefault, secondKey, secondCertificate, nil, []string{"mail.example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if second.CertificatePath != first.CertificatePath {
|
||||
t.Fatal("current 引用路径应保持稳定")
|
||||
}
|
||||
currentData, err := os.ReadFile(second.CertificatePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(currentData) == string(certificatePEM) {
|
||||
t.Fatal("current 目录没有切换到新证书")
|
||||
}
|
||||
previousMatches := false
|
||||
entries, err := os.ReadDir(filepath.Dir(filepath.Dir(second.CertificatePath)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !strings.HasPrefix(entry.Name(), ".previous-") {
|
||||
continue
|
||||
}
|
||||
previousData, readErr := os.ReadFile(filepath.Join(filepath.Dir(filepath.Dir(second.CertificatePath)), entry.Name(), "public.crt"))
|
||||
if readErr == nil && string(previousData) == string(certificatePEM) {
|
||||
previousMatches = true
|
||||
}
|
||||
}
|
||||
if !previousMatches {
|
||||
t.Fatal("更新后未保留旧证书目录")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveRuntimeSSLPathsUsesManagedReferences(t *testing.T) {
|
||||
oldRoot := ROOT_PATH
|
||||
ROOT_PATH = t.TempDir()
|
||||
t.Cleanup(func() { ROOT_PATH = oldRoot })
|
||||
privateKeyPEM, certificatePEM := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
reference, err := ImportManagedSSL(ManagedSSLModeDefault, privateKeyPEM, certificatePEM, nil, []string{"mail.example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &Config{SSL: SSLConfig{Mode: ManagedSSLModeDefault, CertificateRef: reference.CertificateRef, PrivateKeyRef: reference.PrivateKeyRef}}
|
||||
certificatePath, privateKeyPath, err := ResolveRuntimeSSLPaths(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if certificatePath != reference.CertificatePath || privateKeyPath != reference.PrivateKeyPath {
|
||||
t.Fatalf("resolved paths = %q, %q", certificatePath, privateKeyPath)
|
||||
}
|
||||
if _, err := LoadRuntimeTLSCertificate(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveRuntimeSSLPathsRejectsPartialManagedConfiguration(t *testing.T) {
|
||||
_, _, err := ResolveRuntimeSSLPaths(&Config{SSL: SSLConfig{Mode: ManagedSSLModeDefault, CertificateRef: "config/ssl/managed/default/current/public.crt"}})
|
||||
if !errors.Is(err, ErrInvalidSSLReference) {
|
||||
t.Fatalf("error = %v, want %v", err, ErrInvalidSSLReference)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveRuntimeSSLPathsValidatesLegacyFiles(t *testing.T) {
|
||||
oldRoot := ROOT_PATH
|
||||
ROOT_PATH = t.TempDir()
|
||||
t.Cleanup(func() { ROOT_PATH = oldRoot })
|
||||
privateKeyPEM, certificatePEM := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
directory := filepath.Join(t.TempDir(), "legacy-ssl")
|
||||
if err := os.MkdirAll(directory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
privateKeyPath := filepath.Join(directory, "private.key")
|
||||
certificatePath := filepath.Join(directory, "public.crt")
|
||||
if err := os.WriteFile(privateKeyPath, privateKeyPEM, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(certificatePath, certificatePEM, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolvedCertificate, resolvedPrivate, err := ResolveRuntimeSSLPaths(&Config{SSLPublicKeyPath: certificatePath, SSLPrivateKeyPath: privateKeyPath})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resolvedCertificate != certificatePath || resolvedPrivate != privateKeyPath {
|
||||
t.Fatalf("resolved paths = %q, %q", resolvedCertificate, resolvedPrivate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveManagedSSLReferencesRejectsUnsafeReferences(t *testing.T) {
|
||||
oldRoot := ROOT_PATH
|
||||
ROOT_PATH = t.TempDir()
|
||||
t.Cleanup(func() { ROOT_PATH = oldRoot })
|
||||
privateKeyPEM, certificatePEM := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
reference, err := ImportManagedSSL(ManagedSSLModeUpload, privateKeyPEM, certificatePEM, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unsafe := []SSLConfig{
|
||||
{Mode: ManagedSSLModeUpload, CertificateRef: filepath.Join(filepath.Dir(reference.CertificateRef), "..", "public.crt"), PrivateKeyRef: reference.PrivateKeyRef},
|
||||
{Mode: ManagedSSLModeUpload, CertificateRef: filepath.Join(t.TempDir(), "public.crt"), PrivateKeyRef: reference.PrivateKeyRef},
|
||||
{Mode: ManagedSSLModeUpload, CertificateRef: reference.CertificateRef, PrivateKeyRef: reference.CertificateRef},
|
||||
{Mode: ManagedSSLModeUpload, CertificateRef: `C:\outside\public.crt`, PrivateKeyRef: reference.PrivateKeyRef},
|
||||
{Mode: ManagedSSLModeUpload, CertificateRef: `\\server\share\public.crt`, PrivateKeyRef: reference.PrivateKeyRef},
|
||||
}
|
||||
for index, candidate := range unsafe {
|
||||
if _, err := ResolveManagedSSLReferences(candidate); !errors.Is(err, ErrInvalidSSLReference) {
|
||||
t.Fatalf("用例 %d error = %v, want %v", index, err, ErrInvalidSSLReference)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveManagedSSLReferencesRejectsSymlinkAndBroadPrivatePermissions(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("当前测试依赖 Unix 文件权限和符号链接")
|
||||
}
|
||||
oldRoot := ROOT_PATH
|
||||
ROOT_PATH = t.TempDir()
|
||||
t.Cleanup(func() { ROOT_PATH = oldRoot })
|
||||
privateKeyPEM, certificatePEM := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
reference, err := ImportManagedSSL(ManagedSSLModeDefault, privateKeyPEM, certificatePEM, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(reference.PrivateKeyPath, 0640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ResolveManagedSSLReferences(SSLConfig{Mode: ManagedSSLModeDefault, CertificateRef: reference.CertificateRef, PrivateKeyRef: reference.PrivateKeyRef}); !errors.Is(err, ErrSSLPrivateKeyPermission) {
|
||||
t.Fatalf("私钥权限 error = %v, want %v", err, ErrSSLPrivateKeyPermission)
|
||||
}
|
||||
if err := os.Chmod(reference.PrivateKeyPath, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outside := filepath.Join(t.TempDir(), "outside.crt")
|
||||
if err := os.WriteFile(outside, certificatePEM, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Remove(reference.CertificatePath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(outside, reference.CertificatePath); err != nil {
|
||||
t.Skipf("当前环境不支持符号链接测试: %v", err)
|
||||
}
|
||||
if _, err := ResolveManagedSSLReferences(SSLConfig{Mode: ManagedSSLModeDefault, CertificateRef: reference.CertificateRef, PrivateKeyRef: reference.PrivateKeyRef}); !errors.Is(err, ErrUnsafeSSLFile) {
|
||||
t.Fatalf("符号链接 error = %v, want %v", err, ErrUnsafeSSLFile)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateSSLMaterialRejectsDomainExpiryAndWeakKeys(t *testing.T) {
|
||||
privateKeyPEM, certificatePEM := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 2048)
|
||||
if err := ValidateSSLMaterial(privateKeyPEM, certificatePEM, nil, []string{"other.example.com"}); !errors.Is(err, ErrSSLCertificateDomain) {
|
||||
t.Fatalf("域名不匹配 error = %v, want %v", err, ErrSSLCertificateDomain)
|
||||
}
|
||||
expiredKey, expiredCertificate := testManagedCertificate(t, "mail.example.com", time.Now().Add(-48*time.Hour), time.Now().Add(-time.Hour), 2048)
|
||||
if err := ValidateSSLMaterial(expiredKey, expiredCertificate, nil, nil); !errors.Is(err, ErrSSLCertificateExpired) {
|
||||
t.Fatalf("过期证书 error = %v, want %v", err, ErrSSLCertificateExpired)
|
||||
}
|
||||
weakKey, weakCertificate := testManagedCertificate(t, "mail.example.com", time.Now().Add(-time.Hour), time.Now().Add(24*time.Hour), 1024)
|
||||
if err := ValidateSSLMaterial(weakKey, weakCertificate, nil, nil); !errors.Is(err, ErrInvalidSSLMaterial) {
|
||||
t.Fatalf("弱密钥 error = %v, want %v", err, ErrInvalidSSLMaterial)
|
||||
}
|
||||
}
|
||||
|
||||
func testManagedCertificate(t *testing.T, domain string, notBefore, notAfter time.Time, bits int) ([]byte, []byte) {
|
||||
t.Helper()
|
||||
key, err := rsa.GenerateKey(rand.Reader, bits)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
template := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(time.Now().UnixNano()),
|
||||
Subject: pkix.Name{CommonName: domain},
|
||||
DNSNames: []string{domain},
|
||||
NotBefore: notBefore,
|
||||
NotAfter: notAfter,
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}), pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
|
||||
}
|
||||
@@ -281,6 +281,22 @@ func persistRuntimeConfig(root string, current, candidate RuntimeConfig) error {
|
||||
}
|
||||
|
||||
func marshalRuntimeFiles(runtime RuntimeConfig) (map[string][]byte, error) {
|
||||
return marshalRuntimeFilesWithPluginSecrets(runtime, false)
|
||||
}
|
||||
|
||||
type persistedPluginInstance struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Executable string `json:"executable"`
|
||||
Permissions PluginPermissions `json:"permissions"`
|
||||
Settings json.RawMessage `json:"settings,omitempty"`
|
||||
}
|
||||
|
||||
type persistedPluginsSection struct {
|
||||
Meta SectionMeta `json:"meta"`
|
||||
Plugins map[string]persistedPluginInstance `json:"plugins"`
|
||||
}
|
||||
|
||||
func marshalRuntimeFilesWithPluginSecrets(runtime RuntimeConfig, keepPluginSecrets bool) (map[string][]byte, error) {
|
||||
runtime = cloneRuntimeConfig(runtime)
|
||||
runtime.Root.Generation = runtime.Generation
|
||||
runtime.Root.SetupState = runtime.SetupState
|
||||
@@ -299,7 +315,21 @@ func marshalRuntimeFiles(runtime RuntimeConfig) (map[string][]byte, error) {
|
||||
case SectionSSL:
|
||||
value = runtime.SSL
|
||||
case SectionPlugins:
|
||||
value = runtime.Plugins
|
||||
if keepPluginSecrets {
|
||||
value = runtime.Plugins
|
||||
break
|
||||
}
|
||||
plugins := persistedPluginsSection{
|
||||
Meta: runtime.Plugins.Meta,
|
||||
Plugins: make(map[string]persistedPluginInstance, len(runtime.Plugins.Plugins)),
|
||||
}
|
||||
for logicalID, plugin := range runtime.Plugins.Plugins {
|
||||
plugins.Plugins[logicalID] = persistedPluginInstance{
|
||||
Enabled: plugin.Enabled, Executable: plugin.Executable,
|
||||
Permissions: plugin.Permissions, Settings: plugin.Settings,
|
||||
}
|
||||
}
|
||||
value = plugins
|
||||
default:
|
||||
return nil, ErrInvalidSection
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package config
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -167,11 +167,11 @@ func validateSSL(ssl SSLConfig) error {
|
||||
}
|
||||
activeDirectory := ssl.DefaultDirectory
|
||||
switch ssl.Mode {
|
||||
case "default":
|
||||
if ssl.DefaultDirectory == "" {
|
||||
case ManagedSSLModeDefault, "default":
|
||||
if ssl.DefaultDirectory == "" || ssl.CustomDirectory != "" {
|
||||
return ErrInvalidSSL
|
||||
}
|
||||
case "custom":
|
||||
case ManagedSSLModeUpload, "custom":
|
||||
if !isControlledRelativePath(ssl.CustomDirectory) || ssl.CustomDirectory == "" {
|
||||
return ErrInvalidSSL
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user