完善初始化向导、通知配置与证书流程
This commit is contained in:
1 parent
b2833b761d
commit
f85ef10527
41 files changed
+3645
-1697
No files matched your search
@@ -13,36 +13,40 @@ import (
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
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
|
||||
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
|
||||
DNSCredentials map[string]string
|
||||
DNSTestDomain string
|
||||
DNSVerified bool
|
||||
PropagationTimeoutSecs int
|
||||
PropagationPollingIntervalSecs int
|
||||
CertificateRoot string
|
||||
WebHostname string
|
||||
WebPublicIP string
|
||||
WebEnabled bool
|
||||
HTTPSMode string
|
||||
}
|
||||
|
||||
func Apply(cfg *config.Config, input Input) error {
|
||||
@@ -97,6 +101,12 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
cfg.Admin.Email = adminEmail
|
||||
cfg.Admin.PasswordHash = string(hash)
|
||||
cfg.ACME.AccountEmail = adminEmail
|
||||
if input.PropagationTimeoutSecs != 0 {
|
||||
cfg.ACME.PropagationTimeoutSecs = input.PropagationTimeoutSecs
|
||||
}
|
||||
if input.PropagationPollingIntervalSecs != 0 {
|
||||
cfg.ACME.PropagationPollingIntervalSecs = input.PropagationPollingIntervalSecs
|
||||
}
|
||||
cfg.Server.Port = input.ServerPort
|
||||
cfg.Server.HTTPSMode = input.HTTPSMode
|
||||
cfg.Web.Enabled = input.WebEnabled
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
@@ -24,6 +27,7 @@ func TestApplyBuildsReadyConfig(t *testing.T) {
|
||||
AdminUsername: "admin", AdminEmail: "admin@example.com", AdminPassword: "password123", ServerPort: 8080,
|
||||
EmailProvider: "mail-api", EmailEndpoint: "https://mail.example.test/send", EmailRecipients: []string{"a@example.test,b@example.test", "a@example.test"},
|
||||
DNSProvider: "cloudflare", DNSCredentialRef: "credential-1", DNSVerified: true, CertificateRoot: cfg.Paths.Certificate,
|
||||
PropagationTimeoutSecs: 1200, PropagationPollingIntervalSecs: 15,
|
||||
WebHostname: "panel.example.com", WebEnabled: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -41,8 +45,44 @@ func TestApplyBuildsReadyConfig(t *testing.T) {
|
||||
if cfg.ResetURL() != "https://panel.example.com/reset" {
|
||||
t.Fatalf("密码重置地址生成错误:%s", cfg.ResetURL())
|
||||
}
|
||||
if cfg.ACME.PropagationTimeoutSecs != 1200 || cfg.ACME.PropagationPollingIntervalSecs != 15 {
|
||||
t.Fatalf("DNS 传播等待配置保存错误:timeout=%d interval=%d", cfg.ACME.PropagationTimeoutSecs, cfg.ACME.PropagationPollingIntervalSecs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRejectsInvalidPropagationSettings(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
timeout int
|
||||
interval int
|
||||
}{
|
||||
{name: "等待时间小于最小值", timeout: 0 - 1, interval: 10},
|
||||
{name: "等待时间超过最大值", timeout: 86401, interval: 10},
|
||||
{name: "检测间隔超过等待时间", timeout: 900, interval: 901},
|
||||
}
|
||||
|
||||
for _, item := range cases {
|
||||
t.Run(item.name, func(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
err := Apply(&cfg, Input{
|
||||
AdminUsername: "admin",
|
||||
AdminEmail: "admin@example.com",
|
||||
AdminPassword: "password123",
|
||||
ServerPort: 8080,
|
||||
DNSProvider: "cloudflare",
|
||||
DNSCredentialRef: "credential-1",
|
||||
DNSVerified: true,
|
||||
PropagationTimeoutSecs: item.timeout,
|
||||
PropagationPollingIntervalSecs: item.interval,
|
||||
WebHostname: "panel.example.com",
|
||||
WebEnabled: true,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("非法 DNS 传播等待配置不应完成初始化")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestApplyRejectsWebIPAsHostnamePair(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
err := Apply(&cfg, Input{
|
||||
@@ -77,3 +117,48 @@ func TestApplyUsesAdminEmailAndStoresNotificationOptions(t *testing.T) {
|
||||
t.Fatalf("HTTP 通知配置未正确保存:%+v", cfg.Notification)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyAndSaveStoresOnlyCredentialReference(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := config.Default(root)
|
||||
credentialRef := "secret://dns/cloudflare/test"
|
||||
secretValue := "test-dns-token"
|
||||
if err := Apply(&cfg, Input{
|
||||
AdminUsername: "admin",
|
||||
AdminEmail: "admin@example.com",
|
||||
AdminPassword: "password123",
|
||||
ServerPort: 8080,
|
||||
DNSProvider: "cloudflare",
|
||||
DNSCredentialRef: credentialRef,
|
||||
DNSVerified: true,
|
||||
CertificateRoot: cfg.Paths.Certificate,
|
||||
WebHostname: "panel.example.com",
|
||||
WebEnabled: true,
|
||||
}); err != nil {
|
||||
t.Fatalf("构造初始化配置失败:%v", err)
|
||||
}
|
||||
|
||||
path := filepath.Join(root, "config", "scert.json")
|
||||
if err := cfg.Save(path); err != nil {
|
||||
t.Fatalf("保存初始化配置失败:%v", err)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("读取初始化配置失败:%v", err)
|
||||
}
|
||||
content := string(data)
|
||||
if !strings.Contains(content, credentialRef) {
|
||||
t.Fatalf("初始化配置应保存凭据引用:%s", content)
|
||||
}
|
||||
if strings.Contains(content, secretValue) || strings.Contains(content, "api_token") {
|
||||
t.Fatalf("初始化配置不应保存 DNS 明文凭据:%s", content)
|
||||
}
|
||||
|
||||
loaded, err := config.Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("重新读取初始化配置失败:%v", err)
|
||||
}
|
||||
if loaded.DNS.CredentialRef != credentialRef {
|
||||
t.Fatalf("重新读取后凭据引用不正确:%q", loaded.DNS.CredentialRef)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user