1 parent
3af1fc4147
commit
29cfebd620
36 files changed
+2838
-118
No files matched your search
@@ -35,6 +35,8 @@ type Config struct {
|
||||
SpamFilterLevel int `json:"spamFilterLevel"` //垃圾邮件过滤级别,0不过滤、1 spf dkim 校验均失败时过滤,2 spf校验不通过时过滤 3,dkim 校验不过的时候过滤
|
||||
HttpPort int `json:"httpPort"` //http服务端口设置,默认80
|
||||
HttpsPort int `json:"httpsPort"` //https服务端口,默认443
|
||||
TLSMode string `json:"tlsMode,omitempty"`
|
||||
TrustedProxyCIDRs []string `json:"trustedProxyCidrs,omitempty"`
|
||||
WeChatPushAppId string `json:"weChatPushAppId"`
|
||||
WeChatPushSecret string `json:"weChatPushSecret"`
|
||||
WeChatPushTemplateId string `json:"weChatPushTemplateId"`
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrDatabaseMigrationTarget = errors.New("database migration target is invalid")
|
||||
|
||||
// SwitchDatabase commits only the database name or SQLite local reference.
|
||||
// It deliberately does not open a connection or restart the service; the
|
||||
// caller must complete the physical migration before invoking this method.
|
||||
func (m *ConfigManager) SwitchDatabase(ctx context.Context, expectedGeneration uint64, target DatabaseConfig, actor AuditActor) error {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if m == nil || expectedGeneration == 0 {
|
||||
return ErrManagerStorage
|
||||
}
|
||||
if actor.UserID == "" || actor.SessionID == "" {
|
||||
return ErrAuthorizationRequired
|
||||
}
|
||||
if err := m.authorize(ctx, actor, ConfigOperationMigration, SectionRoot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.writeMu.Lock()
|
||||
defer m.writeMu.Unlock()
|
||||
m.mu.RLock()
|
||||
current := cloneRuntimeConfig(m.current)
|
||||
pending := m.pending != nil
|
||||
m.mu.RUnlock()
|
||||
if pending || expectedGeneration != current.Generation {
|
||||
return ErrGenerationConflict
|
||||
}
|
||||
|
||||
target = NormalizeDatabaseConfig(target)
|
||||
if err := ValidateDatabaseConfig(target); err != nil {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
if !sameDatabaseConnection(current.Root.Database, target) {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
if target.Type == DatabaseTypeSQLite {
|
||||
if target.LocalRef == current.Root.Database.LocalRef {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
} else if target.Name == current.Root.Database.Name {
|
||||
return ErrDatabaseMigrationTarget
|
||||
}
|
||||
|
||||
candidate := cloneRuntimeConfig(current)
|
||||
candidate.Root.Database = target
|
||||
setCandidateGeneration(&candidate, current.Generation+1)
|
||||
if err := ValidateRuntimeConfig(candidate, m.options); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, candidate); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, _, err := m.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.transactions.Commit(current, candidate); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.snapshots.Publish(candidate); err != nil {
|
||||
return 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{ServiceDatabase},
|
||||
}
|
||||
m.mu.Unlock()
|
||||
changedField := "root.database.name"
|
||||
if target.Type == DatabaseTypeSQLite {
|
||||
changedField = "root.database.localRef"
|
||||
}
|
||||
return m.writeAudit(ctx, ConfigAuditEvent{
|
||||
Operation: ConfigOperationMigration,
|
||||
Section: SectionRoot,
|
||||
ActorID: actor.UserID,
|
||||
ActorRole: actor.Role,
|
||||
FromGeneration: current.Generation,
|
||||
ToGeneration: candidate.Generation,
|
||||
TargetGeneration: candidate.Generation,
|
||||
ChangedFields: []string{changedField},
|
||||
Result: "success",
|
||||
CreatedAt: now,
|
||||
})
|
||||
}
|
||||
|
||||
func sameDatabaseConnection(current, target DatabaseConfig) bool {
|
||||
if current.Type != target.Type ||
|
||||
current.Host != target.Host ||
|
||||
current.Port != target.Port ||
|
||||
current.Username != target.Username ||
|
||||
current.Password != target.Password {
|
||||
return false
|
||||
}
|
||||
if current.Type == DatabaseTypeSQLite {
|
||||
return true
|
||||
}
|
||||
return current.LocalRef == target.LocalRef
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigManagerSwitchDatabaseCommitsSQLiteReference(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
target := manager.Snapshot().Root.Database
|
||||
target.LocalRef = "smail-next.db"
|
||||
if err := manager.SwitchDatabase(context.Background(), 1, target, authorizedConfigActor()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := manager.Snapshot(); got.Generation != 2 || got.Root.Database.LocalRef != "smail-next.db" {
|
||||
t.Fatalf("switched snapshot = %+v", got)
|
||||
}
|
||||
status := manager.Status()
|
||||
if len(status.PendingRestart) != 1 || status.PendingRestart[0] != ServiceDatabase {
|
||||
t.Fatalf("pending restart services = %+v", status.PendingRestart)
|
||||
}
|
||||
audit := manager.security.Audit.(*FileAuditWriter)
|
||||
events, err := audit.Query(context.Background(), ConfigAuditQuery{Operation: ConfigOperationMigration, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(events) != 1 || events[0].ChangedFields[0] != "root.database.localRef" {
|
||||
t.Fatalf("migration audit events = %+v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerSwitchDatabaseRejectsImmutableChangesAndStaleGeneration(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := manager.Snapshot().Root.Database
|
||||
target.LocalRef = "next.db"
|
||||
target.Host = "127.0.0.1"
|
||||
if err := manager.SwitchDatabase(context.Background(), 1, target, authorizedConfigActor()); !errors.Is(err, ErrDatabaseMigrationTarget) {
|
||||
t.Fatalf("immutable target error = %v, want invalid migration target", err)
|
||||
}
|
||||
target = manager.Snapshot().Root.Database
|
||||
target.LocalRef = "next.db"
|
||||
if err := manager.SwitchDatabase(context.Background(), 2, target, authorizedConfigActor()); !errors.Is(err, ErrGenerationConflict) {
|
||||
t.Fatalf("stale generation error = %v, want generation conflict", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerSwitchDatabaseRequiresAuthorizedActor(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := manager.Snapshot().Root.Database
|
||||
target.LocalRef = "next.db"
|
||||
if err := manager.SwitchDatabase(context.Background(), 1, target, AuditActor{}); !errors.Is(err, ErrAuthorizationRequired) {
|
||||
t.Fatalf("missing actor error = %v, want authorization required", err)
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,7 @@ func cloneRootConfig(source RootConfig) RootConfig {
|
||||
clone.Security.WebAuthnRPOrigins = append([]string(nil), source.Security.WebAuthnRPOrigins...)
|
||||
clone.Domains.Accepted = append([]string(nil), source.Domains.Accepted...)
|
||||
clone.Domains.AccessHosts = append([]string(nil), source.Domains.AccessHosts...)
|
||||
clone.HTTP.TrustedProxyCIDRs = append([]string(nil), source.HTTP.TrustedProxyCIDRs...)
|
||||
if source.Sections != nil {
|
||||
clone.Sections = make(map[Section]SectionRef, len(source.Sections))
|
||||
for section, reference := range source.Sections {
|
||||
|
||||
@@ -59,6 +59,8 @@ type legacyMigrationConfig struct {
|
||||
SpamFilterLevel int `json:"spamFilterLevel"`
|
||||
HttpPort int `json:"httpPort"`
|
||||
HttpsPort int `json:"httpsPort"`
|
||||
TLSMode string `json:"tlsMode"`
|
||||
TrustedProxyCIDRs []string `json:"trustedProxyCidrs"`
|
||||
WeChatPushAppID string `json:"weChatPushAppId"`
|
||||
WeChatPushSecret string `json:"weChatPushSecret"`
|
||||
WeChatPushTemplateID string `json:"weChatPushTemplateId"`
|
||||
@@ -86,7 +88,7 @@ type legacyMigrationConfig struct {
|
||||
var legacyMigrationKnownFields = map[string]struct{}{
|
||||
"logLevel": {}, "domain": {}, "domains": {}, "webDomain": {}, "accessHosts": {},
|
||||
"dkimPrivateKeyPath": {}, "sslType": {}, "SSLPrivateKeyPath": {}, "SSLPublicKeyPath": {},
|
||||
"dbDSN": {}, "dbType": {}, "httpsEnabled": {}, "spamFilterLevel": {}, "httpPort": {}, "httpsPort": {},
|
||||
"dbDSN": {}, "dbType": {}, "httpsEnabled": {}, "spamFilterLevel": {}, "httpPort": {}, "httpsPort": {}, "tlsMode": {}, "trustedProxyCidrs": {},
|
||||
"weChatPushAppId": {}, "weChatPushSecret": {}, "weChatPushTemplateId": {}, "weChatPushUserId": {},
|
||||
"tgBotToken": {}, "tgChatId": {}, "isInit": {}, "setupState": {}, "webPushUrl": {}, "webPushToken": {},
|
||||
"smtpHost": {}, "smtpPort": {}, "smtpUsername": {}, "smtpPassword": {}, "smtpSecurity": {}, "smtpFrom": {},
|
||||
@@ -454,6 +456,11 @@ func legacyRuntimeConfig(root string, legacy legacyMigrationConfig) (RuntimeConf
|
||||
}
|
||||
httpsEnabled := legacy.HttpsEnabled != 2
|
||||
httpsPort := legacy.HttpsPort
|
||||
tlsMode := strings.ToLower(strings.TrimSpace(legacy.TLSMode))
|
||||
if tlsMode == HTTPSTLSModeProxy {
|
||||
httpsEnabled = false
|
||||
httpsPort = 0
|
||||
}
|
||||
if httpsEnabled && httpsPort == 0 {
|
||||
httpsPort = 443
|
||||
}
|
||||
@@ -485,7 +492,13 @@ func legacyRuntimeConfig(root string, legacy legacyMigrationConfig) (RuntimeConf
|
||||
Web: legacy.WebDomain,
|
||||
AccessHosts: append([]string(nil), legacy.AccessHosts...),
|
||||
},
|
||||
HTTP: HTTPConfig{Port: legacy.HttpPort, HTTPSPort: httpsPort, HTTPSEnabled: httpsEnabled},
|
||||
HTTP: HTTPConfig{
|
||||
Port: legacy.HttpPort,
|
||||
HTTPSPort: httpsPort,
|
||||
HTTPSEnabled: httpsEnabled,
|
||||
TLSMode: tlsMode,
|
||||
TrustedProxyCIDRs: append([]string(nil), legacy.TrustedProxyCIDRs...),
|
||||
},
|
||||
Security: SecurityConfig{PasswordVersion: "argon2id", EncryptionKeyRef: "credential.key"},
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package config
|
||||
|
||||
import "sync/atomic"
|
||||
import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// publishedConfig 是旧配置模型的只读兼容快照。
|
||||
// 新业务应通过 CurrentConfig 读取,禁止在请求处理中直接持有并修改 Instance。
|
||||
@@ -43,6 +46,45 @@ func CurrentConfig() *Config {
|
||||
return cloneConfig(Instance)
|
||||
}
|
||||
|
||||
// CurrentHTTPConfig 返回当前进程实际使用的 HTTP 配置。
|
||||
// 分区配置存在时优先读取分区配置,旧版配置仅作为兼容回退。
|
||||
func CurrentHTTPConfig() HTTPConfig {
|
||||
if manager := CurrentManager(); manager != nil {
|
||||
runtime := manager.Snapshot()
|
||||
if runtime.Generation != 0 {
|
||||
return cloneHTTPConfig(runtime.Root.HTTP)
|
||||
}
|
||||
}
|
||||
cfg := CurrentConfig()
|
||||
if cfg == nil {
|
||||
return HTTPConfig{}
|
||||
}
|
||||
mode := strings.ToLower(strings.TrimSpace(cfg.TLSMode))
|
||||
return HTTPConfig{
|
||||
Port: cfg.HttpPort,
|
||||
HTTPSPort: cfg.HttpsPort,
|
||||
HTTPSEnabled: cfg.HttpsEnabled != 2,
|
||||
TLSMode: mode,
|
||||
TrustedProxyCIDRs: append([]string(nil), cfg.TrustedProxyCIDRs...),
|
||||
}
|
||||
}
|
||||
|
||||
// CurrentWebDomain 返回当前进程实际使用的 Web 域名。
|
||||
// 分区配置存在时优先读取分区配置,旧版配置仅作为兼容回退。
|
||||
func CurrentWebDomain() string {
|
||||
if manager := CurrentManager(); manager != nil {
|
||||
runtime := manager.Snapshot()
|
||||
if runtime.Generation != 0 {
|
||||
return runtime.Root.Domains.Web
|
||||
}
|
||||
}
|
||||
cfg := CurrentConfig()
|
||||
if cfg == nil {
|
||||
return ""
|
||||
}
|
||||
return cfg.WebDomain
|
||||
}
|
||||
|
||||
// SetRuntimeSetupPort stores the temporary setup listener port outside the persisted configuration.
|
||||
func SetRuntimeSetupPort(port int) {
|
||||
setupPort.Store(int64(port))
|
||||
@@ -59,6 +101,7 @@ func cloneConfig(source *Config) *Config {
|
||||
clone := *source
|
||||
clone.Domains = append([]string(nil), source.Domains...)
|
||||
clone.AccessHosts = append([]string(nil), source.AccessHosts...)
|
||||
clone.TrustedProxyCIDRs = append([]string(nil), source.TrustedProxyCIDRs...)
|
||||
clone.PluginAllowList = append([]string(nil), source.PluginAllowList...)
|
||||
if source.Tables != nil {
|
||||
clone.Tables = make(map[string]string, len(source.Tables))
|
||||
@@ -74,3 +117,9 @@ func cloneConfig(source *Config) *Config {
|
||||
}
|
||||
return &clone
|
||||
}
|
||||
|
||||
func cloneHTTPConfig(source HTTPConfig) HTTPConfig {
|
||||
clone := source
|
||||
clone.TrustedProxyCIDRs = append([]string(nil), source.TrustedProxyCIDRs...)
|
||||
return clone
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func ValidateRuntimeConfig(runtime RuntimeConfig, options RuntimeValidationOptio
|
||||
return ErrInvalidRuntimeConfig
|
||||
}
|
||||
}
|
||||
if options.Production && runtime.SetupState == SetupStateDegraded && !runtime.Root.HTTP.HTTPSEnabled {
|
||||
if options.Production && runtime.SetupState == SetupStateDegraded && !runtime.Root.HTTP.HTTPSEnabled && runtime.Root.HTTP.TLSMode != HTTPSTLSModeProxy {
|
||||
return ErrInvalidRuntimeConfig
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -118,3 +118,35 @@ func TestProductionDegradedRuntimeRequiresHTTPS(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateHTTPProxyMode(t *testing.T) {
|
||||
runtime := validRuntimeConfig()
|
||||
runtime.Root.HTTP = HTTPConfig{
|
||||
Port: 15184,
|
||||
TLSMode: HTTPSTLSModeProxy,
|
||||
TrustedProxyCIDRs: []string{"127.0.0.1/32", "::1/128"},
|
||||
}
|
||||
if err := ValidateRuntimeConfig(runtime, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatalf("proxy mode was rejected: %v", err)
|
||||
}
|
||||
|
||||
invalid := runtime
|
||||
invalid.Root.HTTP.HTTPSPort = 443
|
||||
if err := ValidateRuntimeConfig(invalid, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrInvalidHTTP) {
|
||||
t.Fatalf("proxy mode with HTTPS port returned %v, want %v", err, ErrInvalidHTTP)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateHTTPRejectsInvalidProxySettings(t *testing.T) {
|
||||
cases := []HTTPConfig{
|
||||
{Port: 15184, TLSMode: "unsupported"},
|
||||
{Port: 15184, TLSMode: HTTPSTLSModeProxy, HTTPSEnabled: true},
|
||||
{Port: 15184, TLSMode: HTTPSTLSModeProxy, TrustedProxyCIDRs: []string{"0.0.0.0"}},
|
||||
{Port: 15184, TLSMode: HTTPSTLSModeProxy, TrustedProxyCIDRs: []string{" 127.0.0.1/32"}},
|
||||
}
|
||||
for index, candidate := range cases {
|
||||
if err := validateHTTP(candidate); !errors.Is(err, ErrInvalidHTTP) {
|
||||
t.Fatalf("case %d returned %v, want %v", index, err, ErrInvalidHTTP)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,11 +41,18 @@ type DomainConfig struct {
|
||||
}
|
||||
|
||||
type HTTPConfig struct {
|
||||
Port int `json:"port,omitempty"`
|
||||
HTTPSPort int `json:"httpsPort,omitempty"`
|
||||
HTTPSEnabled bool `json:"httpsEnabled"`
|
||||
Port int `json:"port,omitempty"`
|
||||
HTTPSPort int `json:"httpsPort,omitempty"`
|
||||
HTTPSEnabled bool `json:"httpsEnabled"`
|
||||
TLSMode string `json:"tlsMode,omitempty"`
|
||||
TrustedProxyCIDRs []string `json:"trustedProxyCidrs,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
HTTPSTLSModeDirect = "direct"
|
||||
HTTPSTLSModeProxy = "proxy"
|
||||
)
|
||||
|
||||
type SMTPSectionConfig struct {
|
||||
Meta SectionMeta `json:"meta"`
|
||||
Relay SMTPConfig `json:"relay"`
|
||||
|
||||
@@ -144,6 +144,8 @@ func DefaultConfigAuthorization(_ context.Context, actor AuditActor, operation C
|
||||
capability := access.CapabilityConfigWriteGlobal
|
||||
if operation == ConfigOperationRollback {
|
||||
capability = access.CapabilityConfigRollback
|
||||
} else if operation == ConfigOperationMigration {
|
||||
capability = access.CapabilityDatabaseMigrate
|
||||
}
|
||||
request := access.Request{
|
||||
ActorRole: access.Role(actor.Role),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package config
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -199,6 +199,20 @@ func validateHTTP(http HTTPConfig) error {
|
||||
if http.HTTPSPort != 0 && !validPort(http.HTTPSPort) {
|
||||
return ErrInvalidHTTP
|
||||
}
|
||||
if http.TLSMode != "" && http.TLSMode != HTTPSTLSModeDirect && http.TLSMode != HTTPSTLSModeProxy {
|
||||
return ErrInvalidHTTP
|
||||
}
|
||||
if http.TLSMode == HTTPSTLSModeProxy && (http.HTTPSEnabled || http.HTTPSPort != 0) {
|
||||
return ErrInvalidHTTP
|
||||
}
|
||||
for _, cidr := range http.TrustedProxyCIDRs {
|
||||
if strings.TrimSpace(cidr) != cidr {
|
||||
return ErrInvalidHTTP
|
||||
}
|
||||
if _, _, err := net.ParseCIDR(cidr); err != nil {
|
||||
return ErrInvalidHTTP
|
||||
}
|
||||
}
|
||||
if http.HTTPSEnabled && http.HTTPSPort == 0 {
|
||||
return ErrInvalidHTTP
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user