完善证书管理与邮件通知流程
This commit is contained in:
1 parent
c74372284a
commit
b2833b761d
60 files changed
+7340
-254
No files matched your search
@@ -13,21 +13,36 @@ import (
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
AdminUsername string
|
||||
AdminEmail string
|
||||
AdminPassword string
|
||||
ServerPort int
|
||||
EmailProvider string
|
||||
EmailEndpoint string
|
||||
EmailRecipients []string
|
||||
DNSProvider string
|
||||
DNSCredentialRef string
|
||||
DNSVerified bool
|
||||
CertificateRoot string
|
||||
WebHostname string
|
||||
WebPublicIP string
|
||||
WebEnabled bool
|
||||
HTTPSMode string
|
||||
AdminUsername string
|
||||
AdminEmail string
|
||||
AdminPassword string
|
||||
ServerPort int
|
||||
EmailHTTPEnabled bool
|
||||
EmailProvider string
|
||||
EmailEndpoint string
|
||||
EmailHTTPPath string
|
||||
EmailHTTPLoginPath string
|
||||
EmailHTTPAuthMode string
|
||||
EmailHTTPCredentialRef string
|
||||
EmailHTTPHeaders map[string]string
|
||||
EmailEventMode string
|
||||
EmailIncludeDeploymentDetails bool
|
||||
EmailIncludeCertificateSerial bool
|
||||
EmailIncludeManagementURL bool
|
||||
EmailSenderName string
|
||||
EmailSenderEmail string
|
||||
EmailRecipients []string
|
||||
EmailTimeoutSecs int
|
||||
EmailRetryCount int
|
||||
EmailRetryIntervalsSecs []int
|
||||
DNSProvider string
|
||||
DNSCredentialRef string
|
||||
DNSVerified bool
|
||||
CertificateRoot string
|
||||
WebHostname string
|
||||
WebPublicIP string
|
||||
WebEnabled bool
|
||||
HTTPSMode string
|
||||
}
|
||||
|
||||
func Apply(cfg *config.Config, input Input) error {
|
||||
@@ -81,6 +96,7 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
cfg.Admin.Username = strings.TrimSpace(input.AdminUsername)
|
||||
cfg.Admin.Email = adminEmail
|
||||
cfg.Admin.PasswordHash = string(hash)
|
||||
cfg.ACME.AccountEmail = adminEmail
|
||||
cfg.Server.Port = input.ServerPort
|
||||
cfg.Server.HTTPSMode = input.HTTPSMode
|
||||
cfg.Web.Enabled = input.WebEnabled
|
||||
@@ -91,13 +107,66 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
cfg.DNS.Provider = strings.TrimSpace(input.DNSProvider)
|
||||
cfg.DNS.CredentialRef = strings.TrimSpace(input.DNSCredentialRef)
|
||||
cfg.DNS.VerificationState = "verified"
|
||||
cfg.Notification.HTTPEnabled = input.EmailHTTPEnabled
|
||||
cfg.Notification.SMTPEnabled = false
|
||||
cfg.Notification.Provider = strings.TrimSpace(input.EmailProvider)
|
||||
cfg.Notification.Endpoint = strings.TrimSpace(input.EmailEndpoint)
|
||||
if strings.TrimSpace(input.EmailHTTPPath) != "" {
|
||||
cfg.Notification.HTTPPath = strings.TrimSpace(input.EmailHTTPPath)
|
||||
}
|
||||
if strings.TrimSpace(input.EmailHTTPLoginPath) != "" {
|
||||
cfg.Notification.HTTPLoginPath = strings.TrimSpace(input.EmailHTTPLoginPath)
|
||||
}
|
||||
if strings.TrimSpace(input.EmailHTTPAuthMode) != "" {
|
||||
cfg.Notification.HTTPAuthMode = strings.TrimSpace(input.EmailHTTPAuthMode)
|
||||
}
|
||||
cfg.Notification.HTTPCredentialRef = strings.TrimSpace(input.EmailHTTPCredentialRef)
|
||||
if input.EmailHTTPHeaders != nil {
|
||||
cfg.Notification.HTTPHeaders = copyHeaders(input.EmailHTTPHeaders)
|
||||
}
|
||||
if strings.TrimSpace(input.EmailEventMode) != "" {
|
||||
cfg.Notification.EventMode = strings.TrimSpace(input.EmailEventMode)
|
||||
}
|
||||
cfg.Notification.IncludeDeploymentDetails = input.EmailIncludeDeploymentDetails
|
||||
cfg.Notification.IncludeCertificateSerial = input.EmailIncludeCertificateSerial
|
||||
cfg.Notification.IncludeManagementURL = input.EmailIncludeManagementURL
|
||||
if strings.TrimSpace(input.EmailSenderName) != "" {
|
||||
cfg.Notification.SenderName = strings.TrimSpace(input.EmailSenderName)
|
||||
}
|
||||
if strings.TrimSpace(input.EmailSenderEmail) != "" {
|
||||
cfg.Notification.SenderEmail = strings.ToLower(strings.TrimSpace(input.EmailSenderEmail))
|
||||
} else {
|
||||
cfg.Notification.SenderEmail = adminEmail
|
||||
}
|
||||
if input.EmailTimeoutSecs > 0 {
|
||||
cfg.Notification.TimeoutSecs = input.EmailTimeoutSecs
|
||||
}
|
||||
if input.EmailRetryCount > 0 {
|
||||
cfg.Notification.RetryCount = input.EmailRetryCount
|
||||
}
|
||||
if input.EmailRetryIntervalsSecs != nil {
|
||||
cfg.Notification.RetryIntervalsSecs = append([]int(nil), input.EmailRetryIntervalsSecs...)
|
||||
}
|
||||
cfg.Notification.Recipients = cleanRecipients(input.EmailRecipients)
|
||||
if len(cfg.Notification.Recipients) == 0 {
|
||||
cfg.Notification.Recipients = []string{adminEmail}
|
||||
}
|
||||
cfg.SetupReady = true
|
||||
return cfg.Validate()
|
||||
}
|
||||
|
||||
func copyHeaders(values map[string]string) map[string]string {
|
||||
result := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
result[key] = strings.TrimSpace(value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func cleanRecipients(values []string) []string {
|
||||
items := make([]string, 0, len(values))
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
|
||||
@@ -54,3 +54,26 @@ func TestApplyRejectsWebIPAsHostnamePair(t *testing.T) {
|
||||
t.Fatal("域名和公网 IP 同时配置时应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyUsesAdminEmailAndStoresNotificationOptions(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
err := Apply(&cfg, Input{
|
||||
AdminUsername: "admin", AdminEmail: "admin@example.com", AdminPassword: "password123", ServerPort: 8080,
|
||||
EmailHTTPEnabled: true, EmailProvider: "smail", EmailEndpoint: "https://mail.example.test",
|
||||
EmailHTTPPath: "/api/email/send", EmailHTTPAuthMode: "bearer", EmailHTTPCredentialRef: "secret://mail/token",
|
||||
EmailHTTPHeaders: map[string]string{" X-Tenant ": " scert "}, EmailEventMode: "detailed",
|
||||
EmailIncludeDeploymentDetails: true, EmailIncludeCertificateSerial: true, EmailIncludeManagementURL: true,
|
||||
EmailSenderName: "证书管理", EmailTimeoutSecs: 15, EmailRetryCount: 2, EmailRetryIntervalsSecs: []int{10, 30},
|
||||
DNSProvider: "cloudflare", DNSCredentialRef: "credential-1", DNSVerified: true, CertificateRoot: cfg.Paths.Certificate,
|
||||
WebHostname: "panel.example.com", WebEnabled: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("保存邮件通知选项失败:%v", err)
|
||||
}
|
||||
if len(cfg.Notification.Recipients) != 1 || cfg.Notification.Recipients[0] != "admin@example.com" {
|
||||
t.Fatalf("未填写收件人时应使用管理员邮箱:%v", cfg.Notification.Recipients)
|
||||
}
|
||||
if cfg.Notification.HTTPCredentialRef != "secret://mail/token" || cfg.Notification.HTTPHeaders["X-Tenant"] != "scert" {
|
||||
t.Fatalf("HTTP 通知配置未正确保存:%+v", cfg.Notification)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user