完善 Scert 前后端与部署支持
This commit is contained in:
1 parent
afdb947bea
commit
7f0af703ed
85 files changed
+7530
-800
No files matched your search
@@ -16,7 +16,37 @@ import (
|
||||
const dnsVerificationLifetime = 15 * time.Minute
|
||||
|
||||
type DNSCredentialTester interface {
|
||||
Verify(ctx context.Context, providerID, testDomain string, credentials map[string]string) error
|
||||
Verify(ctx context.Context, providerID, testDomain string, credentials map[string]string) (DNSVerificationReport, error)
|
||||
}
|
||||
|
||||
type DNSVerificationStage struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type DNSVerificationReport struct {
|
||||
RecordName string `json:"record_name"`
|
||||
Stages []DNSVerificationStage `json:"stages"`
|
||||
}
|
||||
|
||||
type DNSVerificationError struct {
|
||||
Report DNSVerificationReport
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *DNSVerificationError) Error() string {
|
||||
if e == nil || e.Err == nil {
|
||||
return "DNS API 在线验证失败"
|
||||
}
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
func (e *DNSVerificationError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.Err
|
||||
}
|
||||
|
||||
type SecretWriter interface {
|
||||
@@ -33,6 +63,7 @@ type DNSVerificationResult struct {
|
||||
CredentialRef string
|
||||
VerificationToken string
|
||||
ExpiresAt time.Time
|
||||
TestReport DNSVerificationReport
|
||||
}
|
||||
|
||||
type dnsVerificationSession struct {
|
||||
@@ -69,15 +100,20 @@ func (m *DNSVerificationManager) Verify(ctx context.Context, input DNSVerificati
|
||||
if !domain.IsValidName(testDomain) {
|
||||
return DNSVerificationResult{}, fmt.Errorf("DNS 测试域名格式无效:%s", testDomain)
|
||||
}
|
||||
if err := m.tester.Verify(ctx, providerID, testDomain, input.Credentials); err != nil {
|
||||
return DNSVerificationResult{}, err
|
||||
normalizedCredentials := normalizeCredentials(input.Credentials)
|
||||
testReport, err := m.tester.Verify(ctx, providerID, testDomain, normalizedCredentials)
|
||||
if err != nil {
|
||||
return DNSVerificationResult{TestReport: testReport}, &DNSVerificationError{
|
||||
Report: testReport,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
credentialID, err := secureToken(18)
|
||||
if err != nil {
|
||||
return DNSVerificationResult{}, fmt.Errorf("生成 DNS 凭据引用失败:%w", err)
|
||||
}
|
||||
credentialRef := fmt.Sprintf("secret://dns/%s/%s", providerID, credentialID)
|
||||
if err := m.store.Put(ctx, credentialRef, input.Credentials); err != nil {
|
||||
if err := m.store.Put(ctx, credentialRef, normalizedCredentials); err != nil {
|
||||
return DNSVerificationResult{}, fmt.Errorf("加密保存 DNS 凭据失败:%w", err)
|
||||
}
|
||||
verificationToken, err := secureToken(32)
|
||||
@@ -94,6 +130,7 @@ func (m *DNSVerificationManager) Verify(ctx context.Context, input DNSVerificati
|
||||
m.mu.Unlock()
|
||||
return DNSVerificationResult{
|
||||
CredentialRef: credentialRef, VerificationToken: verificationToken, ExpiresAt: expiresAt,
|
||||
TestReport: testReport,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -140,6 +177,17 @@ func (m *DNSVerificationManager) clearExpiredLocked(now time.Time) {
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeCredentials(credentials map[string]string) map[string]string {
|
||||
if credentials == nil {
|
||||
return nil
|
||||
}
|
||||
normalized := make(map[string]string, len(credentials))
|
||||
for key, value := range credentials {
|
||||
normalized[strings.TrimSpace(key)] = strings.TrimSpace(value)
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func normalizeTestDomain(value string) string {
|
||||
return strings.ToLower(strings.TrimSuffix(strings.TrimSpace(value), "."))
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ type fakeDNSTester struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (f fakeDNSTester) Verify(context.Context, string, string, map[string]string) error {
|
||||
return f.err
|
||||
func (f fakeDNSTester) Verify(context.Context, string, string, map[string]string) (DNSVerificationReport, error) {
|
||||
return DNSVerificationReport{}, f.err
|
||||
}
|
||||
|
||||
type memorySecretWriter struct {
|
||||
@@ -105,3 +105,38 @@ func TestDNSVerificationManagerDoesNotStoreFailedCredential(t *testing.T) {
|
||||
t.Fatal("DNS 测试失败时不应保存凭据")
|
||||
}
|
||||
}
|
||||
|
||||
type recordingDNSTester struct {
|
||||
credentials map[string]string
|
||||
}
|
||||
|
||||
func (f *recordingDNSTester) Verify(_ context.Context, _ string, _ string, credentials map[string]string) (DNSVerificationReport, error) {
|
||||
f.credentials = credentials
|
||||
return DNSVerificationReport{}, nil
|
||||
}
|
||||
|
||||
func TestDNSVerificationManagerNormalizesCredentialCopyBeforeTestingAndStoring(t *testing.T) {
|
||||
store := &memorySecretWriter{}
|
||||
tester := &recordingDNSTester{}
|
||||
manager := NewDNSVerificationManager(tester, store)
|
||||
inputCredentials := map[string]string{" api_token ": " secret-token "}
|
||||
result, err := manager.Verify(t.Context(), DNSVerificationInput{
|
||||
ProviderID: "cloudflare", TestDomain: "example.com", Credentials: inputCredentials,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("DNS 在线验证失败:%v", err)
|
||||
}
|
||||
if tester.credentials["api_token"] != "secret-token" {
|
||||
t.Fatalf("传给测试器的凭据未规范化:%v", tester.credentials)
|
||||
}
|
||||
if _, ok := tester.credentials[" api_token "]; ok {
|
||||
t.Fatal("测试器不应收到带首尾空格的凭据键")
|
||||
}
|
||||
stored := store.values[result.CredentialRef]
|
||||
if stored["api_token"] != "secret-token" {
|
||||
t.Fatalf("保存的凭据未规范化:%v", stored)
|
||||
}
|
||||
if inputCredentials[" api_token "] != " secret-token " {
|
||||
t.Fatal("规范化过程不应修改调用方的原始凭据 Map")
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,14 @@ type Input struct {
|
||||
AdminPassword string
|
||||
ServerPort int
|
||||
EmailHTTPEnabled bool
|
||||
EmailSMTPEnabled bool
|
||||
EmailSMTPHost string
|
||||
EmailSMTPPort int
|
||||
EmailSMTPUsername string
|
||||
EmailSMTPPassword string
|
||||
EmailSMTPCredentialRef string
|
||||
EmailSMTPMode string
|
||||
EmailSMTPAllowPlaintextAuth bool
|
||||
EmailProvider string
|
||||
EmailEndpoint string
|
||||
EmailHTTPPath string
|
||||
@@ -118,7 +126,17 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
cfg.DNS.CredentialRef = strings.TrimSpace(input.DNSCredentialRef)
|
||||
cfg.DNS.VerificationState = "verified"
|
||||
cfg.Notification.HTTPEnabled = input.EmailHTTPEnabled
|
||||
cfg.Notification.SMTPEnabled = false
|
||||
cfg.Notification.SMTPEnabled = input.EmailSMTPEnabled
|
||||
cfg.Notification.SMTPHost = strings.TrimSpace(input.EmailSMTPHost)
|
||||
if input.EmailSMTPPort != 0 {
|
||||
cfg.Notification.SMTPPort = input.EmailSMTPPort
|
||||
}
|
||||
cfg.Notification.SMTPUsername = strings.TrimSpace(input.EmailSMTPUsername)
|
||||
cfg.Notification.SMTPCredentialRef = strings.TrimSpace(input.EmailSMTPCredentialRef)
|
||||
if strings.TrimSpace(input.EmailSMTPMode) != "" {
|
||||
cfg.Notification.SMTPMode = strings.TrimSpace(input.EmailSMTPMode)
|
||||
}
|
||||
cfg.Notification.SMTPAllowPlaintextAuth = input.EmailSMTPAllowPlaintextAuth
|
||||
cfg.Notification.Provider = strings.TrimSpace(input.EmailProvider)
|
||||
cfg.Notification.Endpoint = strings.TrimSpace(input.EmailEndpoint)
|
||||
if strings.TrimSpace(input.EmailHTTPPath) != "" {
|
||||
@@ -162,6 +180,9 @@ func Apply(cfg *config.Config, input Input) error {
|
||||
cfg.Notification.Recipients = []string{adminEmail}
|
||||
}
|
||||
cfg.SetupReady = true
|
||||
if cfg.Notification.SMTPEnabled && strings.TrimSpace(cfg.Notification.SMTPCredentialRef) == "" {
|
||||
return fmt.Errorf("启用 SMTP 通知后必须配置加密凭据引用")
|
||||
}
|
||||
return cfg.Validate()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user