补齐Nginx辅助服务安全初始化与部署模板
This commit is contained in:
1 parent
aa5d669db1
commit
d8b4850916
13 files changed
+593
-4
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/nginxactivation"
|
||||
)
|
||||
@@ -57,12 +58,55 @@ func run(ctx context.Context, path string) error {
|
||||
return service.Serve(ctx, listener)
|
||||
}
|
||||
|
||||
func execute(ctx context.Context, arguments []string) error {
|
||||
flags := flag.NewFlagSet("scert-nginx", flag.ContinueOnError)
|
||||
flags.SetOutput(os.Stdout)
|
||||
flags.Usage = func() {
|
||||
fmt.Fprintln(flags.Output(), "用法:scert-nginx [参数];默认由 systemd socket 启动服务。")
|
||||
flags.PrintDefaults()
|
||||
}
|
||||
path := flags.String("policy", "/etc/scert-nginx/policy.json", "root 管理的独立激活策略文件")
|
||||
initialize := flags.Bool("initialize", false, "仅初始化专用证书材料,不重载 Nginx")
|
||||
chain := flags.String("certificate", "", "root 受保护的现有完整证书链文件")
|
||||
key := flags.String("private-key", "", "root 受保护的现有私钥文件")
|
||||
if err := flags.Parse(arguments); err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if flags.NArg() != 0 || *path == "" {
|
||||
return errors.New("不接受位置参数,策略路径不能为空")
|
||||
}
|
||||
if !*initialize {
|
||||
if *chain != "" || *key != "" {
|
||||
return errors.New("证书材料参数仅用于显式初始化")
|
||||
}
|
||||
return run(ctx, *path)
|
||||
}
|
||||
if *chain == "" || *key == "" {
|
||||
return errors.New("初始化必须同时提供证书和私钥路径")
|
||||
}
|
||||
if os.Geteuid() != 0 {
|
||||
return errors.New("Nginx 激活目录初始化必须由 root 执行")
|
||||
}
|
||||
policy, err := nginxactivation.LoadPolicy(*path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取受信激活策略失败:%w", err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
if _, err := policy.Rotation().Initialize(ctx, *chain, *key); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("专用证书材料已初始化;尚未修改或重载 Nginx,请继续核对站点接线。")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
path := flag.String("policy", "/etc/scert-nginx/policy.json", "root 管理的独立激活策略文件")
|
||||
flag.Parse()
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
if err := run(ctx, *path); err != nil {
|
||||
if err := execute(ctx, os.Args[1:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Nginx 本地激活服务启动或运行失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
@@ -11,6 +12,18 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExecuteRejectsInvalidInitializationArguments(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--initialize"}, {"--initialize", "--certificate", "/no-certificate"},
|
||||
{"--initialize", "--private-key", "/no-key"}, {"--certificate", "/no-certificate"},
|
||||
{"--private-key", "/no-key"}, {"--policy", ""}, {"unexpected"},
|
||||
} {
|
||||
if err := execute(context.Background(), args); err == nil {
|
||||
t.Fatalf("无效初始化参数被接受:%v", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInheritedListener(t *testing.T) {
|
||||
if mode := os.Getenv("SCERT_LISTENER_TEST_CHILD"); mode != "" {
|
||||
os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid()))
|
||||
|
||||
Reference in new issue
Block a user