完善证书管理与初始化流程
This commit is contained in:
1 parent
07636abe44
commit
c74372284a
84 files changed
+10151
-3502
No files matched your search
@@ -3,18 +3,20 @@ package setup
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/mail"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/passwordpolicy"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
AdminUsername string
|
||||
AdminEmail string
|
||||
AdminPassword string
|
||||
ServerPort int
|
||||
ResetServicePort int
|
||||
EmailProvider string
|
||||
EmailEndpoint string
|
||||
EmailRecipients []string
|
||||
@@ -35,18 +37,16 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
if strings.TrimSpace(input.AdminUsername) == "" {
|
||||
return fmt.Errorf("超级管理员用户名不能为空")
|
||||
}
|
||||
if len(input.AdminPassword) < 8 {
|
||||
return fmt.Errorf("超级管理员密码至少需要 8 个字符")
|
||||
adminEmail, err := normalizeAdminEmail(input.AdminEmail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := passwordpolicy.Validate(input.AdminPassword, input.AdminUsername); err != nil {
|
||||
return err
|
||||
}
|
||||
if input.ServerPort < 1 || input.ServerPort > 65535 {
|
||||
return fmt.Errorf("主服务端口无效:%d", input.ServerPort)
|
||||
}
|
||||
if input.ResetServicePort < 1 || input.ResetServicePort > 65535 {
|
||||
return fmt.Errorf("密码重置服务端口无效:%d", input.ResetServicePort)
|
||||
}
|
||||
if input.ServerPort == input.ResetServicePort {
|
||||
return fmt.Errorf("主服务和密码重置服务不能使用相同端口")
|
||||
}
|
||||
if strings.TrimSpace(input.DNSProvider) == "" {
|
||||
return fmt.Errorf("DNS 服务商不能为空")
|
||||
}
|
||||
@@ -57,13 +57,13 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
return fmt.Errorf("DNS API 尚未通过验证测试")
|
||||
}
|
||||
if strings.TrimSpace(input.WebHostname) == "" && strings.TrimSpace(input.WebPublicIP) == "" {
|
||||
return fmt.Errorf("Web 页面必须配置域名或公网 IP")
|
||||
return fmt.Errorf("管理页面必须配置域名或公网 IP")
|
||||
}
|
||||
if input.WebHostname != "" && input.WebPublicIP != "" {
|
||||
return fmt.Errorf("Web 页面不能同时配置域名和公网 IP")
|
||||
return fmt.Errorf("管理页面不能同时配置域名和公网 IP")
|
||||
}
|
||||
if input.WebPublicIP != "" && net.ParseIP(input.WebPublicIP) == nil {
|
||||
return fmt.Errorf("Web 公网 IP 格式无效")
|
||||
return fmt.Errorf("管理页面公网 IP 格式无效")
|
||||
}
|
||||
if input.HTTPSMode == "" {
|
||||
input.HTTPSMode = "direct"
|
||||
@@ -79,8 +79,8 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
return fmt.Errorf("生成管理员密码摘要失败:%w", err)
|
||||
}
|
||||
cfg.Admin.Username = strings.TrimSpace(input.AdminUsername)
|
||||
cfg.Admin.Email = adminEmail
|
||||
cfg.Admin.PasswordHash = string(hash)
|
||||
cfg.Admin.ResetServicePort = input.ResetServicePort
|
||||
cfg.Server.Port = input.ServerPort
|
||||
cfg.Server.HTTPSMode = input.HTTPSMode
|
||||
cfg.Web.Enabled = input.WebEnabled
|
||||
@@ -116,3 +116,15 @@ func cleanRecipients(values []string) []string {
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func normalizeAdminEmail(value string) (string, error) {
|
||||
value = strings.ToLower(strings.TrimSpace(value))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("超级管理员绑定邮箱不能为空")
|
||||
}
|
||||
parsed, err := mail.ParseAddress(value)
|
||||
if err != nil || parsed.Address != value || parsed.Name != "" {
|
||||
return "", fmt.Errorf("超级管理员绑定邮箱格式无效")
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
func TestApplyRequiresVerifiedDNS(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
err := Apply(&cfg, Input{
|
||||
AdminUsername: "admin", AdminPassword: "password123", ServerPort: 8080, ResetServicePort: 8091,
|
||||
AdminUsername: "admin", AdminEmail: "admin@example.com", AdminPassword: "password123", ServerPort: 8080,
|
||||
DNSProvider: "cloudflare", DNSCredentialRef: "credential-1", CertificateRoot: cfg.Paths.Certificate,
|
||||
WebHostname: "panel.example.com", WebEnabled: true,
|
||||
})
|
||||
@@ -21,7 +21,7 @@ func TestApplyRequiresVerifiedDNS(t *testing.T) {
|
||||
func TestApplyBuildsReadyConfig(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
err := Apply(&cfg, Input{
|
||||
AdminUsername: "admin", AdminPassword: "password123", ServerPort: 8080, ResetServicePort: 8091,
|
||||
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,
|
||||
WebHostname: "panel.example.com", WebEnabled: true,
|
||||
@@ -38,12 +38,15 @@ func TestApplyBuildsReadyConfig(t *testing.T) {
|
||||
if cfg.Admin.PasswordHash == "password123" || cfg.Admin.PasswordHash == "" {
|
||||
t.Fatal("管理员密码不应明文保存")
|
||||
}
|
||||
if cfg.ResetURL() != "https://panel.example.com/reset" {
|
||||
t.Fatalf("密码重置地址生成错误:%s", cfg.ResetURL())
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRejectsWebIPAsHostnamePair(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
err := Apply(&cfg, Input{
|
||||
AdminUsername: "admin", AdminPassword: "password123", ServerPort: 8080, ResetServicePort: 8091,
|
||||
AdminUsername: "admin", AdminEmail: "admin@example.com", AdminPassword: "password123", ServerPort: 8080,
|
||||
DNSProvider: "cloudflare", DNSCredentialRef: "credential-1", DNSVerified: true,
|
||||
WebHostname: "panel.example.com", WebPublicIP: "203.0.113.10", WebEnabled: true,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user