完善证书管理与邮件通知流程
This commit is contained in:
1 parent
c74372284a
commit
b2833b761d
60 files changed
+7340
-254
No files matched your search
@@ -16,6 +16,7 @@ import (
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/firewall"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/httpapi"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/logging"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/notification"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/setup"
|
||||
@@ -28,6 +29,7 @@ type App struct {
|
||||
Database *database.Database
|
||||
Repository *repository.SQLDomainRepository
|
||||
Servers *repository.SQLServerRepository
|
||||
Tasks *repository.SQLTaskRepository
|
||||
Deployment *deployment.Service
|
||||
SecretStore *secretstore.Store
|
||||
SSHKeys *sshkey.Service
|
||||
@@ -75,23 +77,53 @@ func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string
|
||||
dnsVerification := setup.NewDNSVerificationManager(dnsTester, secretStore)
|
||||
repo := repository.NewSQLDomainRepository(db.DB)
|
||||
serverRepo := repository.NewSQLServerRepository(db.DB)
|
||||
taskRepo := repository.NewSQLTaskRepository(db.DB)
|
||||
auditRepo := repository.NewSQLAuditRepository(db.DB)
|
||||
sshKeys := sshkey.NewService(secretStore)
|
||||
accounts := certificate.NewAccountRepository(secretStore)
|
||||
certificates := certificate.NewService(cfg, repo, secretStore, dnsprovider.NewLegoFactory(), accounts)
|
||||
deploymentService := deployment.NewService(deployment.SSHFactory{Secrets: secretStore})
|
||||
workflow := &CertificateWorkflow{Certificates: certificates, Domains: repo, Servers: serverRepo, Deployment: deploymentService}
|
||||
notifier := notification.New(cfg.Notification, secretStore)
|
||||
remoteValidator := certificate.RemoteValidator{}
|
||||
workflow := &CertificateWorkflow{
|
||||
Certificates: certificates,
|
||||
Domains: repo,
|
||||
Servers: serverRepo,
|
||||
Deployment: deploymentService,
|
||||
RemoteVerifier: func(ctx context.Context, endpoint string, expectedDomains []string) (certificate.ValidationResult, error) {
|
||||
return remoteValidator.ValidateHTTPS(ctx, endpoint, expectedDomains)
|
||||
},
|
||||
Notifier: notifier,
|
||||
NotifyError: func(err error) { logger.Error("发送邮件通知失败", "error", err) },
|
||||
CurrentCertificate: certificates.Current,
|
||||
}
|
||||
renewal := certificate.NewRenewalScheduler(repo, workflow, cfg.ACME.RenewalThresholdDays)
|
||||
server := httpapi.New(httpapi.Dependencies{
|
||||
Config: cfg, ConfigPath: configPath, Logger: logger, Repository: repo, Servers: serverRepo, SSHKeys: sshKeys,
|
||||
SetupDNS: dnsVerification, Firewall: firewall.NewManager(),
|
||||
IssueCertificate: workflow.IssueForHTTP,
|
||||
ResetStatePath: admin.StatePath(cfg.Paths.Data),
|
||||
Audit: auditRepo,
|
||||
IssueSetupCertificate: func(ctx context.Context, request certificate.SetupCertificateRequest) (certificate.SavedResource, error) {
|
||||
return certificates.IssueForSetup(ctx, request)
|
||||
},
|
||||
RollbackSetupCertificate: func(ctx context.Context, saved certificate.SavedResource) error {
|
||||
return certificate.RollbackSavedResource(ctx, saved)
|
||||
},
|
||||
ImportCertificate: func(ctx context.Context, request certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error) {
|
||||
result, err := workflow.Import(ctx, request)
|
||||
return result.Certificate, result.Deployment, err
|
||||
},
|
||||
DeployCertificate: func(ctx context.Context, domainID string) (certificate.IssueResult, deployment.Report, error) {
|
||||
result, err := workflow.DeployCurrent(ctx, domainID)
|
||||
return result.Certificate, result.Deployment, err
|
||||
},
|
||||
CurrentCertificate: certificates,
|
||||
ResetStatePath: admin.StatePath(cfg.Paths.Data),
|
||||
Audit: auditRepo,
|
||||
Tasks: taskRepo,
|
||||
})
|
||||
return &App{
|
||||
Config: cfg, Logger: logger, Database: db, Repository: repo,
|
||||
Servers: serverRepo, Deployment: deploymentService,
|
||||
Tasks: taskRepo,
|
||||
SecretStore: secretStore, SSHKeys: sshKeys, DNSVerification: dnsVerification, HTTP: server,
|
||||
Certificates: certificates,
|
||||
Workflow: workflow,
|
||||
|
||||
@@ -4,18 +4,24 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/notification"
|
||||
)
|
||||
|
||||
type CertificateWorkflow struct {
|
||||
Certificates certificateService
|
||||
Domains domainDeploymentRepository
|
||||
Servers serverLister
|
||||
Deployment savedDeployer
|
||||
Certificates certificateService
|
||||
Domains domainDeploymentRepository
|
||||
Servers serverLister
|
||||
Deployment savedDeployer
|
||||
Notifier notification.Notifier
|
||||
NotifyError func(error)
|
||||
RemoteVerifier func(context.Context, string, []string) (certificate.ValidationResult, error)
|
||||
CurrentCertificate func(context.Context, string) (certificate.IssueResult, error)
|
||||
}
|
||||
|
||||
type certificateService interface {
|
||||
@@ -50,14 +56,25 @@ func (w *CertificateWorkflow) IssueWithReport(ctx context.Context, domainID stri
|
||||
if w == nil || w.Certificates == nil || w.Domains == nil || w.Servers == nil || w.Deployment == nil {
|
||||
return CertificateWorkflowResult{}, errors.New("证书部署流程未初始化")
|
||||
}
|
||||
domainItem, _ := w.Domains.GetByID(ctx, domainID)
|
||||
certificateResult, err := w.Certificates.Issue(ctx, domainID)
|
||||
if err != nil {
|
||||
w.notify(ctx, notification.Event{Type: "certificate_renewal", Status: "failed", DomainID: domainID, Domain: domainItem.Name, Error: err.Error()})
|
||||
return CertificateWorkflowResult{}, err
|
||||
}
|
||||
report, err := w.deploy(ctx, certificateResult)
|
||||
if err != nil {
|
||||
w.markDeploymentError(certificateResult)
|
||||
}
|
||||
status := "success"
|
||||
if err != nil {
|
||||
status = "error"
|
||||
}
|
||||
w.notify(ctx, notification.Event{
|
||||
Type: "certificate_renewal", Status: status, DomainID: certificateResult.DomainID,
|
||||
Domain: domainItem.Name, Domains: certificateResult.Domains, ExpiresAt: &certificateResult.Saved.NotAfter,
|
||||
Deployment: report, Error: errorText(err),
|
||||
})
|
||||
return CertificateWorkflowResult{Certificate: certificateResult, Deployment: report}, err
|
||||
}
|
||||
|
||||
@@ -66,21 +83,71 @@ func (w *CertificateWorkflow) IssueForHTTP(ctx context.Context, domainID string)
|
||||
return result.Certificate, result.Deployment, err
|
||||
}
|
||||
|
||||
// DeployCurrent validates and deploys the active certificate without issuing a new one.
|
||||
func (w *CertificateWorkflow) DeployCurrent(ctx context.Context, domainID string) (CertificateWorkflowResult, error) {
|
||||
if w == nil || w.CurrentCertificate == nil || w.Domains == nil || w.Servers == nil || w.Deployment == nil {
|
||||
return CertificateWorkflowResult{}, errors.New("当前证书部署流程未初始化")
|
||||
}
|
||||
result, err := w.CurrentCertificate(ctx, domainID)
|
||||
if err != nil {
|
||||
return CertificateWorkflowResult{}, err
|
||||
}
|
||||
report, err := w.deploy(ctx, result)
|
||||
if err != nil {
|
||||
w.markDeploymentError(result)
|
||||
}
|
||||
status := "success"
|
||||
if err != nil {
|
||||
status = "error"
|
||||
}
|
||||
w.notify(ctx, notification.Event{
|
||||
Type: "certificate_deploy", Status: status, DomainID: result.DomainID,
|
||||
Domains: result.Domains, ExpiresAt: &result.Saved.NotAfter, Deployment: report, Error: errorText(err),
|
||||
})
|
||||
return CertificateWorkflowResult{Certificate: result, Deployment: report}, err
|
||||
}
|
||||
func (w *CertificateWorkflow) Import(ctx context.Context, request certificate.ImportRequest) (CertificateWorkflowResult, error) {
|
||||
if w == nil || w.Certificates == nil || w.Domains == nil || w.Servers == nil || w.Deployment == nil {
|
||||
return CertificateWorkflowResult{}, errors.New("证书部署流程未初始化")
|
||||
}
|
||||
domainItem, _ := w.Domains.GetByID(ctx, request.DomainID)
|
||||
certificateResult, err := w.Certificates.Import(ctx, request)
|
||||
if err != nil {
|
||||
w.notify(ctx, notification.Event{Type: "certificate_import", Status: "failed", DomainID: request.DomainID, Domain: domainItem.Name, Error: err.Error()})
|
||||
return CertificateWorkflowResult{}, err
|
||||
}
|
||||
report, err := w.deploy(ctx, certificateResult)
|
||||
if err != nil {
|
||||
w.markDeploymentError(certificateResult)
|
||||
}
|
||||
status := "success"
|
||||
if err != nil {
|
||||
status = "error"
|
||||
}
|
||||
w.notify(ctx, notification.Event{
|
||||
Type: "certificate_import", Status: status, DomainID: certificateResult.DomainID,
|
||||
Domain: domainItem.Name, Domains: certificateResult.Domains, ExpiresAt: &certificateResult.Saved.NotAfter,
|
||||
Deployment: report, Error: errorText(err),
|
||||
})
|
||||
return CertificateWorkflowResult{Certificate: certificateResult, Deployment: report}, err
|
||||
}
|
||||
|
||||
func (w *CertificateWorkflow) notify(ctx context.Context, event notification.Event) {
|
||||
if w == nil || w.Notifier == nil {
|
||||
return
|
||||
}
|
||||
if err := w.Notifier.Send(ctx, event); err != nil && w.NotifyError != nil {
|
||||
w.NotifyError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func errorText(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
func (w *CertificateWorkflow) deploy(ctx context.Context, result certificate.IssueResult) (deployment.Report, error) {
|
||||
domainItem, err := w.Domains.GetByID(ctx, result.DomainID)
|
||||
if err != nil {
|
||||
@@ -109,9 +176,28 @@ func (w *CertificateWorkflow) deploy(ctx context.Context, result certificate.Iss
|
||||
if report.Status != "success" {
|
||||
return report, fmt.Errorf("证书部署未全部成功:成功 %d 台,失败 %d 台", report.Success, report.Failed)
|
||||
}
|
||||
if w.RemoteVerifier != nil {
|
||||
verificationDomain := verificationName(domainItem, result.Domains)
|
||||
if verificationDomain == "" {
|
||||
return report, errors.New("无法确定部署后的 HTTPS 校验域名")
|
||||
}
|
||||
if _, err := w.RemoteVerifier(ctx, "https://"+verificationDomain, result.Domains); err != nil {
|
||||
return report, fmt.Errorf("部署后 HTTPS 校验失败:%w", err)
|
||||
}
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func verificationName(item domain.Domain, issuedDomains []string) string {
|
||||
for _, value := range issuedDomains {
|
||||
value = strings.TrimSpace(value)
|
||||
if value != "" && !strings.HasPrefix(value, "*.") {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return strings.TrimPrefix(strings.TrimSpace(item.Name), "*.")
|
||||
}
|
||||
|
||||
func (w *CertificateWorkflow) markDeploymentError(result certificate.IssueResult) {
|
||||
if w == nil || w.Domains == nil {
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user