检查点:保存双层证书协调进展并准备新主机验收
This commit is contained in:
1 parent
48cee12c4a
commit
2c0d0e8eda
15 files changed
+1190
-9
No files matched your search
@@ -41,6 +41,8 @@ type App struct {
|
||||
HTTP *httpapi.Server
|
||||
maintenanceStop context.CancelFunc
|
||||
maintenanceDone chan struct{}
|
||||
managementStop context.CancelFunc
|
||||
managementDone chan struct{}
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -180,7 +182,13 @@ func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string
|
||||
Tasks: taskRepo,
|
||||
ReadinessCheck: db.DB.PingContext,
|
||||
})
|
||||
management.activator = directManagementActivator{server: server}
|
||||
activator, recoverManagement, err := newManagementActivator(cfg, server)
|
||||
if err != nil {
|
||||
_ = db.Close()
|
||||
_ = logger.Close()
|
||||
return nil, fmt.Errorf("初始化管理证书协调器失败:%w", err)
|
||||
}
|
||||
management.activator = activator
|
||||
application := &App{
|
||||
Config: cfg, Logger: logger, Database: db, Repository: repo,
|
||||
Servers: serverRepo,
|
||||
@@ -193,6 +201,7 @@ func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string
|
||||
Renewal: renewal,
|
||||
}
|
||||
application.startAuditCleanup(auditRepo)
|
||||
application.startManagementRecovery(recoverManagement)
|
||||
return application, nil
|
||||
}
|
||||
|
||||
@@ -228,6 +237,9 @@ func (a *App) Close() error {
|
||||
return nil
|
||||
}
|
||||
var first error
|
||||
if a.managementStop != nil {
|
||||
a.managementStop()
|
||||
}
|
||||
if a.Renewal != nil {
|
||||
a.Renewal.Stop()
|
||||
}
|
||||
@@ -237,6 +249,9 @@ func (a *App) Close() error {
|
||||
if a.maintenanceDone != nil {
|
||||
<-a.maintenanceDone
|
||||
}
|
||||
if a.managementDone != nil {
|
||||
<-a.managementDone
|
||||
}
|
||||
if a.HTTP != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
if err := a.HTTP.Shutdown(ctx); err != nil && first == nil {
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/httpapi"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/nginxactivation"
|
||||
)
|
||||
|
||||
const managementActivationSocket = "/run/scert-nginx/activation.sock"
|
||||
|
||||
type coordinatedManagementActivator struct {
|
||||
server *httpapi.Server
|
||||
directory string
|
||||
proxy nginxactivation.ProxyClient
|
||||
}
|
||||
|
||||
func newManagementActivator(cfg config.Config, server *httpapi.Server) (managementActivator, func(context.Context) error, error) {
|
||||
directory := filepath.Join(cfg.Paths.Data, "management-activation")
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
activator := coordinatedManagementActivator{
|
||||
server: server, directory: directory,
|
||||
proxy: nginxactivation.Client{SocketPath: managementActivationSocket, ServiceUID: 0},
|
||||
}
|
||||
return activator, activator.coordinator(nil).Recover, nil
|
||||
}
|
||||
|
||||
func (a coordinatedManagementActivator) coordinator(expected *httpapi.ManagementCertificateState) nginxactivation.Coordinator {
|
||||
return nginxactivation.Coordinator{
|
||||
Directory: a.directory, Proxy: a.proxy,
|
||||
Upstream: managementUpstream{server: a.server, expected: expected},
|
||||
}
|
||||
}
|
||||
|
||||
func (a coordinatedManagementActivator) Ready(ctx context.Context, state httpapi.ManagementCertificateState) error {
|
||||
if state.HTTPSMode == "direct" {
|
||||
if err := a.coordinator(nil).Recover(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return (directManagementActivator{server: a.server}).Ready(ctx, state)
|
||||
}
|
||||
return a.coordinator(&state).Ready(ctx)
|
||||
}
|
||||
|
||||
func (a coordinatedManagementActivator) Activate(ctx context.Context, state httpapi.ManagementCertificateState, saved certificate.SavedResource) error {
|
||||
if state.HTTPSMode == "direct" {
|
||||
if err := a.Ready(ctx, state); err != nil {
|
||||
return err
|
||||
}
|
||||
return (directManagementActivator{server: a.server}).Activate(ctx, state, saved)
|
||||
}
|
||||
return a.coordinator(&state).Activate(ctx, nginxactivation.CertificateFiles{
|
||||
CertificatePath: saved.FullChainPath, PrivateKeyPath: saved.PrivateKeyPath,
|
||||
})
|
||||
}
|
||||
|
||||
type managementUpstream struct {
|
||||
server *httpapi.Server
|
||||
expected *httpapi.ManagementCertificateState
|
||||
}
|
||||
|
||||
func (u managementUpstream) WithCertificate(ctx context.Context, operation func(nginxactivation.ManagedSession) error) error {
|
||||
return u.server.WithManagementCertificate(ctx, func(session *httpapi.ManagementCertificateSession) error {
|
||||
return operation(&managementSession{session: session, expected: u.expected})
|
||||
})
|
||||
}
|
||||
|
||||
type managementSession struct {
|
||||
session *httpapi.ManagementCertificateSession
|
||||
expected *httpapi.ManagementCertificateState
|
||||
previous *httpapi.ManagementSnapshot
|
||||
}
|
||||
|
||||
func (s *managementSession) Snapshot() (nginxactivation.ManagedCertificate, error) {
|
||||
current, err := s.session.Snapshot()
|
||||
if err != nil {
|
||||
return nginxactivation.ManagedCertificate{}, err
|
||||
}
|
||||
if s.previous == nil && s.expected != nil {
|
||||
expected, actual := *s.expected, current.State
|
||||
expected.NotAfter, actual.NotAfter = time.Time{}, time.Time{}
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
return nginxactivation.ManagedCertificate{}, errors.New("签发期间管理配置已变化,拒绝开始双层激活")
|
||||
}
|
||||
}
|
||||
s.previous = ¤t
|
||||
return nginxactivation.ManagedCertificate{
|
||||
CertificateFiles: nginxactivation.CertificateFiles{CertificatePath: current.State.CertificatePath, PrivateKeyPath: current.State.PrivateKeyPath},
|
||||
Hostname: current.State.Hostname, Mode: current.State.HTTPSMode, LeafHash: current.LeafHash,
|
||||
ProxyAddress: current.ProxyAddress, UpstreamAddress: current.UpstreamAddress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *managementSession) Activate(files nginxactivation.CertificateFiles, hash string) error {
|
||||
if s.previous == nil {
|
||||
return errors.New("管理证书协调缺少切换前快照")
|
||||
}
|
||||
return s.session.Activate(*s.previous, certificate.SavedResource{FullChainPath: files.CertificatePath, PrivateKeyPath: files.PrivateKeyPath}, hash)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
//go:build !linux
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/httpapi"
|
||||
)
|
||||
|
||||
func newManagementActivator(_ config.Config, server *httpapi.Server) (managementActivator, func(context.Context) error, error) {
|
||||
return directManagementActivator{server: server}, nil, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (a *App) startManagementRecovery(recover func(context.Context) error) {
|
||||
if recover == nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
a.managementStop = cancel
|
||||
a.managementDone = make(chan struct{})
|
||||
go func() {
|
||||
defer close(a.managementDone)
|
||||
for {
|
||||
attempt, stop := context.WithTimeout(ctx, 100*time.Second)
|
||||
err := recover(attempt)
|
||||
stop()
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if err != nil && a.Logger != nil {
|
||||
a.Logger.Error("管理证书协调恢复尚未完成,保持续期保护", "error", err)
|
||||
}
|
||||
if err := waitForManagementRetry(ctx, time.Minute); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in new issue
Block a user