继续初始化测试前保存启动链路与页面迁移检查点

This commit is contained in:
xuans committed 2026-09-16 19:53:34 +08:00
1 parent 34d1fb88e5
commit 199d727ecc
20 files changed
+891 -67

No files matched your search

+3
View File
@@ -53,6 +53,9 @@ func New(ctx context.Context, cfg config.Config) (*App, error) {
}
func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string) (*App, error) {
if !cfg.SetupReady {
return nil, fmt.Errorf("系统尚未初始化,请先启动 scert serve 完成首次配置")
}
if err := cfg.Validate(); err != nil {
return nil, err
}
+38 -4
View File
@@ -2,6 +2,7 @@ package app
import (
"context"
"os"
"path/filepath"
"testing"
"time"
@@ -9,11 +10,11 @@ import (
"git.pcux.cn/xuans/Scert/backend/internal/config"
"git.pcux.cn/xuans/Scert/backend/internal/domain"
"git.pcux.cn/xuans/Scert/backend/internal/repository"
"git.pcux.cn/xuans/Scert/backend/internal/setup"
)
func TestNewWiresApplicationServices(t *testing.T) {
baseDir := t.TempDir()
cfg := config.Default(baseDir)
cfg := readyApplicationConfig(t)
application, err := NewWithConfigPath(context.Background(), cfg, filepath.Join(cfg.Paths.Config, "scert.json"))
if err != nil {
t.Fatalf("初始化应用失败:%v", err)
@@ -31,8 +32,7 @@ func TestNewWiresApplicationServices(t *testing.T) {
}
}
func TestNewWithConfigPathRecoversInterruptedTasks(t *testing.T) {
baseDir := t.TempDir()
cfg := config.Default(baseDir)
cfg := readyApplicationConfig(t)
configPath := filepath.Join(cfg.Paths.Config, "scert.json")
first, err := NewWithConfigPath(context.Background(), cfg, configPath)
@@ -108,3 +108,37 @@ func TestNewWithConfigPathRecoversInterruptedTasks(t *testing.T) {
t.Fatalf("已完成任务不应被重启恢复逻辑修改:%+v", completed)
}
}
func readyApplicationConfig(t *testing.T) config.Config {
t.Helper()
root := t.TempDir()
cfg := config.Default(root)
cfg.Paths = config.PathsConfig{
Install: root, Config: filepath.Join(root, "conf"), Data: filepath.Join(root, "data"),
Certificate: filepath.Join(root, "ssl"), Backup: filepath.Join(root, "backups"), Log: filepath.Join(root, "logs"),
}
cfg.Database.DSN = filepath.Join(cfg.Paths.Data, "scert.db")
if err := setup.Apply(&cfg, setup.Input{
AdminUsername: "admin", AdminEmail: "admin@example.test", AdminPassword: "Test1234!",
ServerPort: 8080, WebHostname: "scert.example.test", WebEnabled: true,
DNSProvider: "aliyun", DNSCredentialRef: "secret://dns/test", DNSVerified: true,
CertificateRoot: cfg.Paths.Certificate, HTTPSMode: "direct",
}); err != nil {
t.Fatal(err)
}
cfg.SetupReady = true
cfg.Web.InitReady = true
return cfg
}
func TestUninitializedApplicationHasNoSideEffects(t *testing.T) {
root := t.TempDir()
cfg := config.Default(root)
cfg.Paths.Install = filepath.Join(root, "must-not-exist")
if _, err := NewWithConfigPath(context.Background(), cfg, filepath.Join(root, "config.json")); err == nil {
t.Fatal("未初始化配置未被拒绝")
}
if _, err := os.Stat(cfg.Paths.Install); !os.IsNotExist(err) {
t.Fatalf("拒绝启动前产生了文件副作用:%v", err)
}
}
+12 -5
View File
@@ -9,6 +9,7 @@ import (
"mime"
"net/http"
"strings"
"sync"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/dnsprovider"
@@ -37,10 +38,12 @@ type bootstrapCompleter interface {
// Router 只承载首次初始化接口,不注册正式管理接口。
type Router struct {
sessions *SessionManager
database bootstrapDatabaseVerifier
dns bootstrapDNSVerifier
completer bootstrapCompleter
sessions *SessionManager
database bootstrapDatabaseVerifier
dns bootstrapDNSVerifier
completer bootstrapCompleter
completed chan struct{}
completeOnce sync.Once
}
type bootstrapSessionRequest struct {
@@ -55,9 +58,12 @@ type bootstrapDNSTestRequest struct {
// NewRouter 创建与正式管理服务完全分离的首次初始化路由。
func NewRouter(sessions *SessionManager, database bootstrapDatabaseVerifier, dns bootstrapDNSVerifier, completer bootstrapCompleter) *Router {
return &Router{sessions: sessions, database: database, dns: dns, completer: completer}
return &Router{sessions: sessions, database: database, dns: dns, completer: completer, completed: make(chan struct{})}
}
// Completed 在成功响应写入后通知临时服务开始优雅关闭。
func (router *Router) Completed() <-chan struct{} { return router.completed }
func (router *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
setBootstrapSecurityHeaders(writer)
if request == nil {
@@ -254,6 +260,7 @@ func (router *Router) complete(writer http.ResponseWriter, request *http.Request
router.sessions.RevokeAll()
http.SetCookie(writer, ClearBootstrapSessionCookie())
writeBootstrapJSON(writer, http.StatusOK, result)
router.completeOnce.Do(func() { close(router.completed) })
}
func writeBootstrapCompletionError(writer http.ResponseWriter, err error) {
+206
View File
@@ -0,0 +1,206 @@
package bootstrap
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
"git.pcux.cn/xuans/Scert/backend/internal/config"
"git.pcux.cn/xuans/Scert/backend/internal/dnsprovider"
"git.pcux.cn/xuans/Scert/backend/internal/firewall"
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
"git.pcux.cn/xuans/Scert/backend/internal/setup"
)
type ServerOptions struct {
Config config.Config
ConfigPath string
PublicIP string
URLPath string
Output io.Writer
}
// Run 只创建初始化所需组件;正式数据库、业务路由和后台任务由切换后的应用创建。
func Run(ctx context.Context, options ServerOptions) (bool, error) {
if options.Config.SetupReady {
return false, errors.New("已经初始化,不能启动临时初始化服务")
}
material, err := NewTemporaryTLSMaterial(options.PublicIP)
if err != nil {
return false, err
}
sessions, token, err := NewSessionManager()
if err != nil {
return false, err
}
defer sessions.RevokeAll()
ip, err := advertisedBootstrapIP(options.PublicIP, material.IPAddresses)
if err != nil {
return false, err
}
address, err := bootstrapURL(ip, options.Config.Server.Port, token)
if err != nil {
return false, err
}
store, err := secretstore.Open(options.Config.Paths.Config)
if err != nil {
return false, fmt.Errorf("打开初始化敏感信息存储失败:%w", err)
}
databases := NewDatabaseVerificationManager(options.Config, store)
dns := setup.NewDNSVerificationManager(dnsprovider.NewTester(dnsprovider.NewLegoFactory()), store)
// 服务完全停止后再清理未领取的凭据,避免在途验证重新留下临时凭据。
defer databases.DiscardAllExcept()
defer dns.DiscardAll()
issuer := certificate.NewService(options.Config, nil, store, dnsprovider.NewLegoFactory(), certificate.NewAccountRepository(store))
issuer.SetDNSAuthorizationChecker(dnsprovider.NewAliyunAuthorizationChecker())
completer := NewCompleter(CompleteDependencies{
BaseConfig: options.Config, ConfigPath: options.ConfigPath, BootstrapURLPath: options.URLPath,
DatabaseSessions: databases, DNSSessions: dns, Credentials: store, Sessions: sessions,
Firewall: firewall.NewManager(), Certificate: issuer,
Warn: func(message string) {
if options.Output != nil {
fmt.Fprintln(options.Output, message)
}
},
})
router := NewRouter(sessions, databases, dns, completer)
handler, closeFiles := bootstrapHandler(router, options.Config)
defer closeFiles()
listener, err := net.Listen("tcp", net.JoinHostPort(options.Config.Server.Host, strconv.Itoa(options.Config.Server.Port)))
if err != nil {
return false, fmt.Errorf("监听初始化端口失败:%w", err)
}
defer listener.Close()
if err := WriteBootstrapURL(options.URLPath, address); err != nil {
return false, err
}
defer func() {
if err := os.Remove(options.URLPath); err != nil && !errors.Is(err, os.ErrNotExist) && options.Output != nil {
fmt.Fprintln(options.Output, "初始化地址文件清理失败,请检查运行目录权限。")
}
}()
if options.Output != nil {
// 一次性令牌只能写入受保护文件,不进入终端日志或 systemd journal。
fmt.Fprintf(options.Output, "首次初始化 HTTPS 服务已启动,端口 %d。请读取 %s 获取一次性访问地址;临时自签名证书需人工核对。\n", options.Config.Server.Port, options.URLPath)
}
return serveTemporaryHTTPS(ctx, listener, material, handler, router.Completed())
}
func serveTemporaryHTTPS(ctx context.Context, listener net.Listener, material TLSMaterial, handler http.Handler, completed <-chan struct{}) (bool, error) {
requestContext, cancelRequests := context.WithCancel(context.Background())
defer cancelRequests()
var active sync.WaitGroup
server := &http.Server{
Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
active.Add(1)
defer active.Done()
handler.ServeHTTP(writer, request)
}),
ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 15 * time.Second, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 16 * 1024,
TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12, Certificates: []tls.Certificate{material.Certificate}},
BaseContext: func(net.Listener) context.Context { return requestContext },
}
errorsChannel := make(chan error, 1)
go func() { errorsChannel <- server.ServeTLS(listener, "", "") }()
success := false
var serveErr error
select {
case <-completed:
success = true
case <-ctx.Done():
cancelRequests()
case serveErr = <-errorsChannel:
cancelRequests()
}
shutdownContext, cancelShutdown := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelShutdown()
shutdownErr := server.Shutdown(shutdownContext)
if shutdownErr != nil {
cancelRequests()
_ = server.Close()
}
// Shutdown 已停止接收新请求;等待在途处理完成后才能清理其凭据。
active.Wait()
if serveErr != nil && !errors.Is(serveErr, http.ErrServerClosed) {
return false, serveErr
}
if shutdownErr != nil {
return false, fmt.Errorf("关闭初始化服务失败:%w", shutdownErr)
}
return success, nil
}
func bootstrapHandler(router *Router, cfg config.Config) (http.Handler, func()) {
root, _ := os.OpenRoot(filepath.Join(cfg.Paths.Install, "web"))
closeFiles := func() {
if root != nil {
_ = root.Close()
}
}
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
setBootstrapSecurityHeaders(writer)
if request.URL.Path == "/api/v1/bootstrap/settings" {
if !requireBootstrapMethod(writer, request, http.MethodGet) || !router.authenticate(writer, request) {
return
}
writeBootstrapJSON(writer, http.StatusOK, map[string]any{
"mode": "bootstrap", "initialized": false, "server_port": cfg.Server.Port,
"web_enabled": true, "web_hostname": cfg.Web.Hostname, "web_public_ip": cfg.Web.PublicIP,
"https_mode": cfg.Server.HTTPSMode, "certificate_root": cfg.Paths.Certificate,
"dns_provider": dnsprovider.Aliyun, "acme_environment": cfg.ACME.Environment,
"propagation_timeout_secs": cfg.ACME.PropagationTimeoutSecs,
"propagation_polling_interval_secs": cfg.ACME.PropagationPollingIntervalSecs,
})
return
}
path := request.URL.Path
if strings.HasPrefix(path, "/api/") {
router.ServeHTTP(writer, request)
return
}
if request.Method != http.MethodGet && request.Method != http.MethodHead {
http.NotFound(writer, request)
return
}
name := strings.TrimPrefix(path, "/")
if path == "/" {
name = "index.html"
} else if path != "/Scert.png" && path != "/favicon.ico" && !strings.HasPrefix(path, "/assets/") {
http.NotFound(writer, request)
return
}
if !fs.ValidPath(name) {
writeBootstrapError(writer, http.StatusBadRequest, "资源路径无效")
return
}
if root == nil {
http.Error(writer, "初始化页面资源尚未安装,请检查 web 目录。", http.StatusServiceUnavailable)
return
}
// os.Root 限制符号链接逃逸,不暴露配置目录或任意业务文件。
file, err := root.Open(name)
if err != nil {
writeBootstrapError(writer, http.StatusNotFound, "资源不存在")
return
}
defer file.Close()
info, err := file.Stat()
if err != nil || !info.Mode().IsRegular() {
writeBootstrapError(writer, http.StatusNotFound, "资源不存在")
return
}
http.ServeContent(writer, request, info.Name(), info.ModTime(), file)
}), closeFiles
}
+115
View File
@@ -0,0 +1,115 @@
package bootstrap
import (
"context"
"crypto/tls"
"crypto/x509"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/config"
)
func TestTemporaryHTTPSDrainsCompletionResponse(t *testing.T) {
material, err := NewTemporaryTLSMaterial("127.0.0.1")
if err != nil {
t.Fatal(err)
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
completed := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
result := make(chan error, 1)
body := strings.Repeat("response-data", 10000)
go func() {
ok, err := serveTemporaryHTTPS(ctx, listener, material, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(completed)
time.Sleep(30 * time.Millisecond)
_, _ = io.WriteString(w, body)
}), completed)
if !ok && err == nil {
err = context.Canceled
}
result <- err
}()
roots := x509.NewCertPool()
roots.AddCert(material.Leaf)
transport := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12}}
defer transport.CloseIdleConnections()
client := &http.Client{Transport: transport, Timeout: 5 * time.Second}
response, err := client.Get("https://" + listener.Addr().String() + "/")
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(response.Body)
response.Body.Close()
if err != nil || string(data) != body {
t.Fatalf("成功响应被关闭截断:%v,长度 %d", err, len(data))
}
select {
case err := <-result:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("临时服务没有关闭")
}
if conn, err := net.DialTimeout("tcp", listener.Addr().String(), 100*time.Millisecond); err == nil {
conn.Close()
t.Fatal("关闭后仍可连接")
}
}
func TestBootstrapHandlerIsolatesBusinessRoutesAndSettings(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "web"), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "web", "index.html"), []byte("bootstrap-page"), 0o600); err != nil {
t.Fatal(err)
}
cfg := config.Default(root)
cfg.Paths.Install = root
sessions, token, err := NewSessionManager()
if err != nil {
t.Fatal(err)
}
router := NewRouter(sessions, nil, nil, nil)
handler, closeFiles := bootstrapHandler(router, cfg)
defer closeFiles()
for path, status := range map[string]int{
"/": 200, "/api/v1/bootstrap/status": 200, "/api/v1/bootstrap/settings": 401,
"/api/v1/setup": 404, "/api/v1/domains": 404, "/reset": 404, "/scert.json": 404,
"/assets/../../config.json": 400,
} {
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, path, nil))
if response.Code != status {
t.Fatalf("%s 状态码 %d,期望 %d", path, response.Code, status)
}
if response.Header().Get("Cache-Control") != "no-store" {
t.Fatal("临时页面允许缓存")
}
}
session, err := sessions.Exchange(token, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
request := httptest.NewRequest(http.MethodGet, "/api/v1/bootstrap/settings", nil)
request.AddCookie(BootstrapSessionCookie(session.Token, session.ExpiresAt))
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
if response.Code != 200 || !strings.Contains(response.Body.String(), `"mode":"bootstrap"`) {
t.Fatal("认证后无法读取初始化设置")
}
}
+16 -10
View File
@@ -186,9 +186,15 @@ func WriteBootstrapURL(path, value string) error {
return errors.New("Bootstrap 地址不能为空")
}
directory := filepath.Dir(path)
if directory == filepath.VolumeName(directory)+string(filepath.Separator) {
return errors.New("Bootstrap 地址文件不能直接放在文件系统根目录")
}
if err := os.MkdirAll(directory, 0o700); err != nil {
return fmt.Errorf("创建 Bootstrap 运行目录失败:%w", err)
}
if info, err := os.Lstat(directory); err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return errors.New("Bootstrap 运行目录必须是普通目录")
}
if err := os.Chmod(directory, 0o700); err != nil {
return fmt.Errorf("设置 Bootstrap 运行目录权限失败:%w", err)
}
@@ -197,24 +203,24 @@ func WriteBootstrapURL(path, value string) error {
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("检查 Bootstrap 地址文件失败:%w", err)
}
temporaryPath := path + ".tmp"
if info, err := os.Lstat(temporaryPath); err == nil && info.Mode()&os.ModeSymlink != 0 {
return errors.New("Bootstrap 临时地址文件不能是符号链接")
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("检查 Bootstrap 临时地址文件失败:%w", err)
}
if err := os.WriteFile(temporaryPath, []byte(value+"\n"), 0o600); err != nil {
temporary, err := os.CreateTemp(directory, ".bootstrap-url-")
if err != nil { return fmt.Errorf("创建 Bootstrap 临时地址文件失败:%w", err) }
temporaryPath := temporary.Name()
defer os.Remove(temporaryPath)
defer temporary.Close()
if _, err := temporary.WriteString(value+"\n"); err != nil {
return fmt.Errorf("写入 Bootstrap 临时地址文件失败:%w", err)
}
if err := os.Chmod(temporaryPath, 0o600); err != nil {
_ = os.Remove(temporaryPath)
if err := temporary.Chmod(0o600); err != nil {
return fmt.Errorf("设置 Bootstrap 临时地址文件权限失败:%w", err)
}
if err := temporary.Sync(); err != nil { return fmt.Errorf("同步 Bootstrap 地址文件失败:%w", err) }
if err := temporary.Close(); err != nil { return fmt.Errorf("关闭 Bootstrap 地址文件失败:%w", err) }
if err := os.Rename(temporaryPath, path); err != nil {
_ = os.Remove(temporaryPath)
return fmt.Errorf("替换 Bootstrap 地址文件失败:%w", err)
}
return os.Chmod(path, 0o600)
return nil
}
func cloneIPs(values []net.IP) []net.IP {
+24 -24
View File
@@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"path/filepath"
@@ -21,6 +20,7 @@ import (
"github.com/spf13/cobra"
"git.pcux.cn/xuans/Scert/backend/internal/app"
"git.pcux.cn/xuans/Scert/backend/internal/bootstrap"
"git.pcux.cn/xuans/Scert/backend/internal/buildinfo"
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
"git.pcux.cn/xuans/Scert/backend/internal/config"
@@ -653,45 +653,45 @@ func commandStatus(cfg config.Config) map[string]any {
}
func newServeCommand() *cobra.Command {
return &cobra.Command{Use: "serve", Short: "启动网站服务", RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadConfig()
var publicIP string
command := &cobra.Command{Use: "serve", Short: "启动网站服务", RunE: func(cmd *cobra.Command, args []string) error {
path, err := filepath.Abs(resolveConfigPath())
if err != nil {
return err
}
if !cfg.SetupReady {
cfg.Server.Host = "127.0.0.1"
fmt.Fprintln(cmd.OutOrStdout(), "系统尚未初始化,临时服务仅监听本机;请完成初始化后重启服务。")
}
if cfg.SetupReady && !cfg.Web.Enabled {
return fmt.Errorf("管理页面已关闭")
reload := func() (config.Config, error) { return loadServeConfig(path, config.Default(baseDir)) }
cfg, err := reload()
if err != nil {
return err
}
lock, err := acquireProcessLock(cfg, "Scert 服务已在运行或备份恢复任务正在执行")
if err != nil {
return err
}
defer lock.Close()
service, err := app.NewWithConfigPath(cmd.Context(), cfg, resolveConfigPath())
lockedDataPath := cfg.Paths.Data
cfg, err = reload()
if err != nil {
return err
}
defer service.Close()
if filepath.Clean(cfg.Paths.Data) != filepath.Clean(lockedDataPath) {
return fmt.Errorf("配置的数据目录已经变更,请重新启动服务")
}
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
defer stop()
if service.Renewal != nil && cfg.SetupReady {
service.Renewal.Start(ctx)
}
errCh := make(chan error, 1)
go func() { errCh <- service.HTTP.ListenAndServe() }()
select {
case <-ctx.Done():
return nil
case err := <-errCh:
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
advertisedIP := publicIP
if !cmd.Flags().Changed("bootstrap-public-ip") {
advertisedIP = os.Getenv("SCERT_BOOTSTRAP_PUBLIC_IP")
}
return serveLifecycle(ctx, cfg, reload,
func(ctx context.Context, initial config.Config) (bool, error) {
return bootstrap.Run(ctx, bootstrap.ServerOptions{Config: initial, ConfigPath: path, PublicIP: advertisedIP, URLPath: bootstrapURLPath(initial), Output: cmd.OutOrStdout()})
},
func(ctx context.Context, ready config.Config) (bool, error) { return runApplication(ctx, ready, path) },
)
}}
command.Flags().StringVar(&publicIP, "bootstrap-public-ip", "", "首次初始化地址中的公网 IP;也可设置 SCERT_BOOTSTRAP_PUBLIC_IP")
return command
}
func newDomainCommand() *cobra.Command {
+99
View File
@@ -0,0 +1,99 @@
package cli
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"git.pcux.cn/xuans/Scert/backend/internal/app"
"git.pcux.cn/xuans/Scert/backend/internal/config"
)
type serveStage func(context.Context, config.Config) (bool, error)
// loadServeConfig 只允许真正不存在的文件进入新初始化;损坏、无权限或符号链接均失败关闭。
func loadServeConfig(path string, defaults config.Config) (config.Config, error) {
info, err := os.Lstat(path)
if err == nil {
if !info.Mode().IsRegular() {
return config.Config{}, errors.New("服务配置必须是普通文件")
}
return config.LoadWithEnv(path)
}
if !errors.Is(err, os.ErrNotExist) {
return config.Config{}, err
}
defaults.Paths.Config = filepath.Dir(path)
if err := defaults.ApplyEnvironment(); err != nil {
return config.Config{}, err
}
if err := defaults.Validate(); err != nil {
return config.Config{}, err
}
return defaults, nil
}
func serveLifecycle(ctx context.Context, cfg config.Config, reload func() (config.Config, error), bootstrapStage, applicationStage serveStage) error {
if !cfg.SetupReady {
completed, err := bootstrapStage(ctx, cfg)
if err != nil {
return err
}
if !completed || ctx.Err() != nil {
return nil
}
next, err := reload()
if err != nil {
return fmt.Errorf("重新读取正式配置失败:%w", err)
}
if !next.SetupReady {
return errors.New("初始化服务已退出,但正式配置尚未完成;拒绝启动业务服务")
}
if filepath.Clean(next.Paths.Data) != filepath.Clean(cfg.Paths.Data) {
return errors.New("初始化不能更改进程锁所在数据目录")
}
cfg = next
}
if ctx.Err() != nil {
return nil
}
if !cfg.Web.Enabled {
return errors.New("管理页面已关闭")
}
_, err := applicationStage(ctx, cfg)
return err
}
func runApplication(ctx context.Context, cfg config.Config, path string) (bool, error) {
service, err := app.NewWithConfigPath(ctx, cfg, path)
if err != nil {
return false, err
}
defer service.Close()
if service.Renewal != nil {
service.Renewal.Start(ctx)
}
errorsChannel := make(chan error, 1)
go func() { errorsChannel <- service.HTTP.ListenAndServe() }()
select {
case <-ctx.Done():
return false, nil
case err := <-errorsChannel:
if errors.Is(err, http.ErrServerClosed) {
return false, nil
}
return false, err
}
}
func bootstrapURLPath(cfg config.Config) string {
if runtime.GOOS == "linux" {
return "/run/scert/bootstrap-url"
}
path, _ := filepath.Abs(filepath.Join(cfg.Paths.Data, "run", "bootstrap-url"))
return path
}
+99
View File
@@ -0,0 +1,99 @@
package cli
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"git.pcux.cn/xuans/Scert/backend/internal/config"
)
func TestLoadServeConfigFailsClosed(t *testing.T) {
root := t.TempDir()
path := filepath.Join(root, "conf", "scert.json")
defaults := config.Default(root)
cfg, err := loadServeConfig(path, defaults)
if err != nil || cfg.SetupReady || cfg.Paths.Config != filepath.Dir(path) {
t.Fatalf("缺失配置应进入初始化:%v", err)
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatal("仅加载不应保存默认配置")
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
for _, data := range []string{"{", "null", "{}", `{"setup_ready":true}`, "[]", ""} {
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
t.Fatal(err)
}
if _, err := loadServeConfig(path, defaults); err == nil {
t.Fatalf("损坏配置被降级成初始化:%q", data)
}
}
if _, err := loadServeConfig(root, defaults); err == nil {
t.Fatal("目录不应被视为缺失配置")
}
}
func TestServeLifecycle(t *testing.T) {
for _, name := range []string{"missing", "ready", "cancel", "bootstrap-error", "reload-error", "not-ready", "path-changed", "web-disabled", "application-error"} {
t.Run(name, func(t *testing.T) {
cfg := config.Default(t.TempDir())
cfg.SetupReady = name == "ready" || name == "web-disabled" || name == "application-error"
cfg.Web.Enabled = name != "web-disabled"
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bootstrapCalls, reloadCalls, appCalls := 0, 0, 0
err := serveLifecycle(ctx, cfg, func() (config.Config, error) {
reloadCalls++
if name == "reload-error" {
return config.Config{}, errors.New("test")
}
next := cfg
next.SetupReady = name != "not-ready"
if name == "path-changed" {
next.Paths.Data += "-changed"
}
return next, nil
}, func(context.Context, config.Config) (bool, error) {
bootstrapCalls++
if name == "bootstrap-error" {
return false, errors.New("test")
}
if name == "cancel" {
cancel()
return false, nil
}
return true, nil
}, func(_ context.Context, ready config.Config) (bool, error) {
appCalls++
if !ready.SetupReady {
t.Fatal("未初始化时构造业务应用")
}
if name == "application-error" {
return false, errors.New("test")
}
return false, nil
})
wantError := name != "missing" && name != "ready" && name != "cancel"
if (err != nil) != wantError {
t.Fatalf("错误边界不符:%v", err)
}
wantApp := 0
if name == "missing" || name == "ready" || name == "application-error" {
wantApp = 1
}
if appCalls != wantApp {
t.Fatalf("业务启动次数:%d", appCalls)
}
if cfg.SetupReady && (bootstrapCalls != 0 || reloadCalls != 0) {
t.Fatal("已完成配置进入初始化")
}
if !cfg.SetupReady && bootstrapCalls != 1 {
t.Fatal("初始化未运行一次")
}
})
}
}
+2 -1
View File
@@ -165,7 +165,6 @@ func renderUnitFile(options Options) (string, error) {
"Description=" + normalized.Description,
"After=network-online.target",
"Wants=network-online.target",
"ConditionPathExists=" + quoteUnitValue(normalized.ConfigPath),
"",
"[Service]",
"Type=simple",
@@ -180,6 +179,8 @@ func renderUnitFile(options Options) (string, error) {
"TimeoutStopSec=30",
"Restart=on-failure",
"UMask=0077",
"RuntimeDirectory=scert",
"RuntimeDirectoryMode=0700",
"NoNewPrivileges=yes",
"CapabilityBoundingSet=",
"AmbientCapabilities=",
+2 -1
View File
@@ -26,7 +26,8 @@ func TestRenderUnitFileIncludesExecutableConfigurationAndWorkDir(t *testing.T) {
"[Unit]",
"Description=Scert SSL 证书管理服务",
"After=network-online.target",
"ConditionPathExists=\"/opt/Scert data/scert.json\"",
"RuntimeDirectory=scert",
"RuntimeDirectoryMode=0700",
"[Service]",
"Type=simple",
"User=Scert",