生产部署整改阶段检查点
This commit is contained in:
1 parent
f3348cf95d
commit
92d4b3d398
56 files changed
+4178
-240
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/releasearchive"
|
||||
)
|
||||
|
||||
type stringList []string
|
||||
|
||||
func (items *stringList) String() string {
|
||||
return strings.Join(*items, ",")
|
||||
}
|
||||
|
||||
func (items *stringList) Set(value string) error {
|
||||
*items = append(*items, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var executables stringList
|
||||
source := flag.String("source", "", "发布源目录")
|
||||
output := flag.String("output", "", "发布归档路径")
|
||||
format := flag.String("format", "", "归档格式:tar.gz 或 zip")
|
||||
flag.Var(&executables, "executable", "需要写入 0755 权限的相对路径,可重复指定")
|
||||
flag.Parse()
|
||||
|
||||
if err := releasearchive.Create(releasearchive.Options{
|
||||
Source: *source, Destination: *output, Format: releasearchive.Format(*format), Executables: executables,
|
||||
}); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "创建发布归档失败:"+err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
+13
-16
@@ -1,6 +1,6 @@
|
||||
module git.pcux.cn/xuans/Scert/backend
|
||||
|
||||
go 1.26.0
|
||||
go 1.26.6
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.10.1
|
||||
@@ -8,7 +8,10 @@ require (
|
||||
github.com/go-sql-driver/mysql v1.9.3
|
||||
github.com/pkg/sftp v1.13.11
|
||||
github.com/spf13/cobra v1.10.1
|
||||
golang.org/x/crypto v0.54.0
|
||||
golang.org/x/crypto v0.55.0
|
||||
golang.org/x/oauth2 v0.36.0
|
||||
golang.org/x/sys v0.47.0
|
||||
google.golang.org/api v0.288.0
|
||||
modernc.org/sqlite v1.37.0
|
||||
)
|
||||
|
||||
@@ -16,7 +19,7 @@ require (
|
||||
cloud.google.com/go/auth v0.20.0 // indirect
|
||||
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.9.0 // indirect
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
filippo.io/edwards25519 v1.1.1 // indirect
|
||||
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.5 // indirect
|
||||
github.com/alibabacloud-go/darabonba-openapi/v2 v2.2.3 // indirect
|
||||
github.com/alibabacloud-go/debug v1.0.1 // indirect
|
||||
@@ -69,7 +72,6 @@ require (
|
||||
github.com/json-iterator/go v1.1.13-0.20220915233716-71ac16282d12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/kr/fs v0.1.0 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.22 // indirect
|
||||
github.com/miekg/dns v1.1.72 // indirect
|
||||
@@ -78,7 +80,6 @@ require (
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.133 // indirect
|
||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||
@@ -87,23 +88,19 @@ require (
|
||||
go.mongodb.org/mongo-driver v1.17.9 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.67.0 // indirect
|
||||
go.opentelemetry.io/otel v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel v1.44.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.44.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.44.0 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect
|
||||
golang.org/x/mod v0.37.0 // indirect
|
||||
golang.org/x/mod v0.38.0 // indirect
|
||||
golang.org/x/net v0.57.0 // indirect
|
||||
golang.org/x/oauth2 v0.36.0 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
golang.org/x/text v0.40.0 // indirect
|
||||
golang.org/x/tools v0.47.0 // indirect
|
||||
google.golang.org/api v0.288.0 // indirect
|
||||
golang.org/x/text v0.41.0 // indirect
|
||||
golang.org/x/tools v0.48.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260630182238-925bb5da69e7 // indirect
|
||||
google.golang.org/grpc v1.82.0 // indirect
|
||||
google.golang.org/grpc v1.82.1 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
modernc.org/libc v1.62.1 // indirect
|
||||
|
||||
+43
-22
@@ -5,27 +5,36 @@ cloud.google.com/go/auth/oauth2adapt v0.2.8 h1:keo8NaayQZ6wimpNSmW5OPc283g65QNIi
|
||||
cloud.google.com/go/auth/oauth2adapt v0.2.8/go.mod h1:XQ9y31RkqZCcwJWNSx2Xvric3RrU88hAYYbjDWYDL+c=
|
||||
cloud.google.com/go/compute/metadata v0.9.0 h1:pDUj4QMoPejqq20dK0Pg2N4yG9zIkYGdBtwLoEkH9Zs=
|
||||
cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10=
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
filippo.io/edwards25519 v1.1.1 h1:YpjwWWlNmGIDyXOn8zLzqiD+9TyIlPhGFG96P39uBpw=
|
||||
filippo.io/edwards25519 v1.1.1/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/alibabacloud-go/alibabacloud-gateway-pop v0.0.6 h1:eIf+iGJxdU4U9ypaUfbtOWCsZSbTb8AUHvyPrxu6mAA=
|
||||
github.com/alibabacloud-go/alibabacloud-gateway-pop v0.0.6/go.mod h1:4EUIoxs/do24zMOGGqYVWgw0s9NtiylnJglOeEB5UJo=
|
||||
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4/go.mod h1:sCavSAvdzOjul4cEqeVtvlSaSScfNsTQ+46HwlTL1hc=
|
||||
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.5 h1:zE8vH9C7JiZLNJJQ5OwjU9mSi4T9ef9u3BURT6LCLC8=
|
||||
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.5/go.mod h1:tWnyE9AjF8J8qqLk645oUmVUnFybApTQWklQmi5tY6g=
|
||||
github.com/alibabacloud-go/darabonba-array v0.1.0 h1:vR8s7b1fWAQIjEjWnuF0JiKsCvclSRTfDzZHTYqfufY=
|
||||
github.com/alibabacloud-go/darabonba-array v0.1.0/go.mod h1:BLKxr0brnggqOJPqT09DFJ8g3fsDshapUD3C3aOEFaI=
|
||||
github.com/alibabacloud-go/darabonba-encode-util v0.0.2 h1:1uJGrbsGEVqWcWxrS9MyC2NG0Ax+GpOM5gtupki31XE=
|
||||
github.com/alibabacloud-go/darabonba-encode-util v0.0.2/go.mod h1:JiW9higWHYXm7F4PKuMgEUETNZasrDM6vqVr/Can7H8=
|
||||
github.com/alibabacloud-go/darabonba-map v0.0.2 h1:qvPnGB4+dJbJIxOOfawxzF3hzMnIpjmafa0qOTp6udc=
|
||||
github.com/alibabacloud-go/darabonba-map v0.0.2/go.mod h1:28AJaX8FOE/ym8OUFWga+MtEzBunJwQGceGQlvaPGPc=
|
||||
github.com/alibabacloud-go/darabonba-openapi/v2 v2.2.2/go.mod h1:CsqHkHPpAKL64jl/fChR249EL0abYEIDWmlwFYgo7/E=
|
||||
github.com/alibabacloud-go/darabonba-openapi/v2 v2.2.3 h1:rec08F9BzxIiJ03h2u6BT2QqLVa8ktnpvJ27j5PsjLM=
|
||||
github.com/alibabacloud-go/darabonba-openapi/v2 v2.2.3/go.mod h1:eHjVxrT9g8uVYN/nyAwOFQEfkVA154ChiqRc2XnNKYU=
|
||||
github.com/alibabacloud-go/darabonba-signature-util v0.0.7 h1:UzCnKvsjPFzApvODDNEYqBHMFt1w98wC7FOo0InLyxg=
|
||||
github.com/alibabacloud-go/darabonba-signature-util v0.0.7/go.mod h1:oUzCYV2fcCH797xKdL6BDH8ADIHlzrtKVjeRtunBNTQ=
|
||||
github.com/alibabacloud-go/darabonba-string v1.0.2 h1:E714wms5ibdzCqGeYJ9JCFywE5nDyvIXIIQbZVFkkqo=
|
||||
github.com/alibabacloud-go/darabonba-string v1.0.2/go.mod h1:93cTfV3vuPhhEwGGpKKqhVW4jLe7tDpo3LUM0i0g6mA=
|
||||
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68/go.mod h1:6pb/Qy8c+lqua8cFpEy7g39NRRqOWc3rOwAy8m5Y2BY=
|
||||
github.com/alibabacloud-go/debug v1.0.0/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
|
||||
github.com/alibabacloud-go/debug v1.0.1 h1:MsW9SmUtbb1Fnt3ieC6NNZi6aEwrXfDksD4QA6GSbPg=
|
||||
github.com/alibabacloud-go/debug v1.0.1/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
|
||||
github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q=
|
||||
github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE=
|
||||
github.com/alibabacloud-go/openapi-util v0.1.0/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws=
|
||||
github.com/alibabacloud-go/openapi-util v0.1.2 h1:aljdyAPotH4xHymo5wzARjcHb3Org0zKnLP4RxS0JGY=
|
||||
github.com/alibabacloud-go/openapi-util v0.1.2/go.mod h1:/UehBSE2cf1gYT43GV4E+RxTdLRzURImCYY0aRmlXpw=
|
||||
github.com/alibabacloud-go/tea v1.1.0/go.mod h1:IkGyUSX4Ba1V+k4pCtJUc6jDpZLFph9QMy2VUPTwukg=
|
||||
github.com/alibabacloud-go/tea v1.1.7/go.mod h1:/tmnEaQMyb4Ky1/5D+SE1BAsa5zj/KeGOFfwYm3N/p4=
|
||||
github.com/alibabacloud-go/tea v1.1.8/go.mod h1:/tmnEaQMyb4Ky1/5D+SE1BAsa5zj/KeGOFfwYm3N/p4=
|
||||
@@ -94,7 +103,6 @@ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
@@ -156,6 +164,8 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
|
||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
@@ -196,7 +206,6 @@ github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZY
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
|
||||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
@@ -226,7 +235,6 @@ github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJm
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pkg/sftp v1.13.11 h1:0N92SLTB8JqASJB14ZLHHzFnBV8mG9zw4K7jghEFWuE=
|
||||
github.com/pkg/sftp v1.13.11/go.mod h1:uNkH9roSXglNJqM+glJJi+TQXQUm0fXFWqCFmT8hsN0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
@@ -235,7 +243,6 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
@@ -287,14 +294,20 @@ go.mongodb.org/mongo-driver v1.17.9 h1:IexDdCuuNJ3BHrELgBlyaH9p60JXAvdzWR128q+U5
|
||||
go.mongodb.org/mongo-driver v1.17.9/go.mod h1:LlOhpH5NUEfhxcAwG0UEkMqwYcc4JU18gtCdGudk/tQ=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0 h1:yI1/OhfEPy7J9eoa6Sj051C7n5dvpj0QX8g4sRchg04=
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0/go.mod h1:NoUCKYWK+3ecatC4HjkRktREheMeEtrXoQxrqYFeHSc=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.67.0 h1:OyrsyzuttWTSur2qN/Lm0m2a8yqyIjUVBZcxFPuXq2o=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.67.0/go.mod h1:C2NGBr+kAB4bk3xtMXfZ94gqFDtg/GkI7e9zqGh5Beg=
|
||||
go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
|
||||
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
|
||||
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
|
||||
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
|
||||
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
|
||||
go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU=
|
||||
go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc=
|
||||
go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc=
|
||||
go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
|
||||
go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk=
|
||||
go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
@@ -313,8 +326,8 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf
|
||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
||||
golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
|
||||
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
|
||||
golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M=
|
||||
golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f h1:W3F4c+6OLc6H2lb//N1q4WpJkhzJCK5J6kUi1NTVXfM=
|
||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f/go.mod h1:J1xhfL/vlindoeF/aINzNzt2Bket5bjo9sdOYzOsU80=
|
||||
@@ -327,8 +340,8 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
|
||||
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
|
||||
golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk=
|
||||
golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
@@ -419,8 +432,10 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs=
|
||||
golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY=
|
||||
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
|
||||
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
|
||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
@@ -433,26 +448,32 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||
golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q=
|
||||
golang.org/x/tools v0.47.0/go.mod h1:dFHnyTvFWY212G+h7ZY4Vsp/K3U4/7W9TyVaAul8uCA=
|
||||
golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE=
|
||||
golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/api v0.288.0 h1:glhO/J88obKP5I269W3hB73dvBKrjU56ZfmNlNXpgTU=
|
||||
google.golang.org/api v0.288.0/go.mod h1:lM2kYRzYUCBY91P9h6VF1PYmvhxii3O5hji37qRvIcY=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/genproto v0.0.0-20260319201613-d00831a3d3e7 h1:XzmzkmB14QhVhgnawEVsOn6OFsnpyxNPRY9QV01dNB0=
|
||||
google.golang.org/genproto v0.0.0-20260319201613-d00831a3d3e7/go.mod h1:L43LFes82YgSonw6iTXTxXUX1OlULt4AQtkik4ULL/I=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260414002931-afd174a4e478 h1:yQugLulqltosq0B/f8l4w9VryjV+N/5gcW0jQ3N8Qec=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260414002931-afd174a4e478/go.mod h1:C6ADNqOxbgdUUeRTU+LCHDPB9ttAMCTff6auwCVa4uc=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260630182238-925bb5da69e7 h1:eM/YSd5bBFagF51o1E745Ta7RwzpW0h+z+QDNZOgmQ8=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260630182238-925bb5da69e7/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||
google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||
google.golang.org/grpc v1.82.0 h1:vguDnZUPjE26w09A63VoxZPnvPjB5Riyc0mkXPFmAIU=
|
||||
google.golang.org/grpc v1.82.0/go.mod h1:yzTZ1TB1Z3SG+LIYaI+WiE8D5+PZ3ArnrSp8zF3+/ZA=
|
||||
google.golang.org/grpc v1.82.1 h1:NnAxzGRA0677vCa4BUkOAnO5+FfQqVl9iUXeD0IqcGE=
|
||||
google.golang.org/grpc v1.82.1/go.mod h1:yzTZ1TB1Z3SG+LIYaI+WiE8D5+PZ3ArnrSp8zF3+/ZA=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
|
||||
@@ -38,6 +38,12 @@ func TestLinuxInstallerPolicy(t *testing.T) {
|
||||
`/scert|/scert/*`,
|
||||
`chown -R -h "$SERVICE_USER:$SERVICE_GROUP"`,
|
||||
`write_install_metadata`,
|
||||
`load_existing_certificate_dir`,
|
||||
`certificate_dir_explicit=true`,
|
||||
`file -b -- "$source_dir/$artifact"`,
|
||||
`systemctl is-active --quiet Scert.service`,
|
||||
`systemctl is-active --quiet Scert-admin.service`,
|
||||
`pwd -P`,
|
||||
`10-certificate-path.conf`,
|
||||
`"$INSTALL_DIR/conf"`,
|
||||
`"$INSTALL_DIR/data"`,
|
||||
|
||||
@@ -26,6 +26,23 @@ func TestScertSystemdUnitPolicy(t *testing.T) {
|
||||
"ExecStart=/scert/scert --config /scert/conf/scert.json serve",
|
||||
"Restart=on-failure",
|
||||
"UMask=0077",
|
||||
"CapabilityBoundingSet=",
|
||||
"AmbientCapabilities=",
|
||||
"PrivateDevices=yes",
|
||||
"ProtectSystem=strict",
|
||||
"ProtectHostname=yes",
|
||||
"ProtectClock=yes",
|
||||
"ProtectKernelTunables=yes",
|
||||
"ProtectKernelModules=yes",
|
||||
"ProtectControlGroups=yes",
|
||||
"ProtectKernelLogs=yes",
|
||||
"LockPersonality=yes",
|
||||
"MemoryDenyWriteExecute=yes",
|
||||
"RestrictNamespaces=yes",
|
||||
"RestrictRealtime=yes",
|
||||
"RestrictSUIDSGID=yes",
|
||||
"RemoveIPC=yes",
|
||||
"SystemCallArchitectures=native",
|
||||
"ReadWritePaths=/ssl /scert/conf /scert/data /scert/log /scert/backups",
|
||||
"[Install]",
|
||||
"WantedBy=multi-user.target",
|
||||
|
||||
@@ -39,8 +39,15 @@ type App struct {
|
||||
Workflow *CertificateWorkflow
|
||||
Renewal *certificate.RenewalScheduler
|
||||
HTTP *httpapi.Server
|
||||
maintenanceStop context.CancelFunc
|
||||
maintenanceDone chan struct{}
|
||||
}
|
||||
|
||||
const (
|
||||
auditLogRetentionDays = 30
|
||||
auditCleanupInterval = 24 * time.Hour
|
||||
)
|
||||
|
||||
func New(ctx context.Context, cfg config.Config) (*App, error) {
|
||||
return NewWithConfigPath(ctx, cfg, filepath.Join(cfg.Paths.Config, "scert.json"))
|
||||
}
|
||||
@@ -99,6 +106,11 @@ func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string
|
||||
logger.Warn("\u670d\u52a1\u91cd\u542f\u524d\u672a\u5b8c\u6210\u4efb\u52a1\u5df2\u6807\u8bb0\u4e3a\u5931\u8d25", "count", recovered)
|
||||
}
|
||||
auditRepo := repository.NewSQLAuditRepository(db.DB)
|
||||
if deleted, err := auditRepo.PruneExpiredAuditLogs(ctx, time.Now().UTC().AddDate(0, 0, -auditLogRetentionDays)); err != nil {
|
||||
logger.Warn("启动时清理过期审计日志失败", "error", err)
|
||||
} else if deleted > 0 {
|
||||
logger.Info("启动时已清理过期审计日志", "count", deleted)
|
||||
}
|
||||
sshKeys := sshkey.NewService(secretStore)
|
||||
accounts := certificate.NewAccountRepository(secretStore)
|
||||
certificates := certificate.NewService(cfg, repo, secretStore, dnsprovider.NewLegoFactory(), accounts)
|
||||
@@ -154,8 +166,9 @@ func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string
|
||||
ResetStatePath: admin.StatePath(cfg.Paths.Data),
|
||||
Audit: auditRepo,
|
||||
Tasks: taskRepo,
|
||||
ReadinessCheck: db.DB.PingContext,
|
||||
})
|
||||
return &App{
|
||||
application := &App{
|
||||
Config: cfg, Logger: logger, Database: db, Repository: repo,
|
||||
Servers: serverRepo,
|
||||
Users: userRepo,
|
||||
@@ -165,7 +178,36 @@ func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string
|
||||
Certificates: certificates,
|
||||
Workflow: workflow,
|
||||
Renewal: renewal,
|
||||
}, nil
|
||||
}
|
||||
application.startAuditCleanup(auditRepo)
|
||||
return application, nil
|
||||
}
|
||||
|
||||
func (a *App) startAuditCleanup(repo *repository.SQLAuditRepository) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
a.maintenanceStop = cancel
|
||||
a.maintenanceDone = done
|
||||
go func() {
|
||||
defer close(done)
|
||||
ticker := time.NewTicker(auditCleanupInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
deleted, err := repo.PruneExpiredAuditLogs(ctx, time.Now().UTC().AddDate(0, 0, -auditLogRetentionDays))
|
||||
if err != nil {
|
||||
a.Logger.Warn("定时清理过期审计日志失败", "error", err)
|
||||
continue
|
||||
}
|
||||
if deleted > 0 {
|
||||
a.Logger.Info("已定时清理过期审计日志", "count", deleted)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *App) Close() error {
|
||||
@@ -176,6 +218,12 @@ func (a *App) Close() error {
|
||||
if a.Renewal != nil {
|
||||
a.Renewal.Stop()
|
||||
}
|
||||
if a.maintenanceStop != nil {
|
||||
a.maintenanceStop()
|
||||
}
|
||||
if a.maintenanceDone != nil {
|
||||
<-a.maintenanceDone
|
||||
}
|
||||
if a.HTTP != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
if err := a.HTTP.Shutdown(ctx); err != nil && first == nil {
|
||||
|
||||
@@ -71,6 +71,7 @@ func (w *CertificateWorkflow) IssueWithReport(ctx context.Context, domainID stri
|
||||
return CertificateWorkflowResult{}, err
|
||||
}
|
||||
report, err := w.deploy(ctx, certificateResult)
|
||||
err = appendDeploymentResultError(err, report)
|
||||
if err != nil {
|
||||
w.markDeploymentError(certificateResult)
|
||||
}
|
||||
@@ -101,6 +102,7 @@ func (w *CertificateWorkflow) DeployCurrent(ctx context.Context, domainID string
|
||||
return CertificateWorkflowResult{}, err
|
||||
}
|
||||
report, err := w.deploy(ctx, result)
|
||||
err = appendDeploymentResultError(err, report)
|
||||
if err != nil {
|
||||
w.markDeploymentError(result)
|
||||
} else if updateErr := w.Domains.UpdateCertificateState(ctx, result.DomainID, "valid", &result.Saved.NotAfter); updateErr != nil {
|
||||
@@ -127,6 +129,7 @@ func (w *CertificateWorkflow) Import(ctx context.Context, request certificate.Im
|
||||
return CertificateWorkflowResult{}, err
|
||||
}
|
||||
report, err := w.deploy(ctx, certificateResult)
|
||||
err = appendDeploymentResultError(err, report)
|
||||
if err != nil {
|
||||
w.markDeploymentError(certificateResult)
|
||||
}
|
||||
@@ -158,6 +161,31 @@ func errorText(err error) string {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
func appendDeploymentResultError(err error, report deployment.Report) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
messages := make([]string, 0, len(report.Results))
|
||||
for _, result := range report.Results {
|
||||
message := strings.TrimSpace(result.Message)
|
||||
if result.Status == "success" || message == "" {
|
||||
continue
|
||||
}
|
||||
server := strings.TrimSpace(result.Server)
|
||||
if server == "" {
|
||||
server = strings.TrimSpace(result.ServerID)
|
||||
}
|
||||
if server != "" {
|
||||
message = server + ": " + message
|
||||
}
|
||||
messages = append(messages, message)
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("%w; %s", err, strings.Join(messages, "; "))
|
||||
}
|
||||
|
||||
func (w *CertificateWorkflow) deploy(ctx context.Context, result certificate.IssueResult) (deployment.Report, error) {
|
||||
domainItem, err := w.Domains.GetByID(ctx, result.DomainID)
|
||||
if err != nil {
|
||||
|
||||
@@ -112,7 +112,13 @@ func TestCertificateWorkflowDeploysIssuedCertificateToAssociatedServers(t *testi
|
||||
}
|
||||
|
||||
func TestCertificateWorkflowReturnsPartialDeploymentError(t *testing.T) {
|
||||
deployer := &workflowDeployer{report: deployment.Report{Total: 2, Success: 1, Failed: 1, Status: "partial"}}
|
||||
deployer := &workflowDeployer{report: deployment.Report{
|
||||
Total: 2, Success: 1, Failed: 1, Status: "partial",
|
||||
Results: []deployment.Result{
|
||||
{ServerID: "server-1", Server: "primary", Status: "success", Message: "deployed"},
|
||||
{ServerID: "server-2", Server: "secondary", Status: "failed", Message: "validation failed"},
|
||||
},
|
||||
}}
|
||||
state := &workflowDomainState{item: domain.Domain{ID: "domain-1", Name: "*.example.com", CertificateType: domain.CertificateWildcard}}
|
||||
workflow := &CertificateWorkflow{
|
||||
Certificates: &workflowCertificateService{result: certificate.IssueResult{DomainID: "domain-1"}},
|
||||
@@ -124,6 +130,9 @@ func TestCertificateWorkflowReturnsPartialDeploymentError(t *testing.T) {
|
||||
if err == nil || result.Deployment.Status != "partial" {
|
||||
t.Fatalf("部分部署应返回错误和报告:%+v %v", result, err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "secondary: validation failed") || strings.Contains(err.Error(), "primary: deployed") {
|
||||
t.Fatalf("deployment error details are incomplete: %v", err)
|
||||
}
|
||||
if result.Certificate.DomainID != "domain-1" {
|
||||
t.Fatalf("证书结果未保留:%+v", result.Certificate)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,675 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
const backupManifestVersion = 1
|
||||
|
||||
type backupManifest struct {
|
||||
Version int `json:"version"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Files []backupManifestFile `json:"files"`
|
||||
}
|
||||
|
||||
type backupManifestFile struct {
|
||||
Path string `json:"path"`
|
||||
Mode uint32 `json:"mode"`
|
||||
Size int64 `json:"size"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
func createBackup(ctx context.Context, cfg config.Config, configPath, output string) error {
|
||||
output = filepath.Clean(output)
|
||||
if err := ensureBackupDestination(output); err != nil {
|
||||
return err
|
||||
}
|
||||
parent := filepath.Dir(output)
|
||||
if err := os.MkdirAll(parent, 0o700); err != nil {
|
||||
return fmt.Errorf("创建备份父目录失败:%w", err)
|
||||
}
|
||||
parentInfo, err := os.Lstat(parent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查备份父目录失败:%w", err)
|
||||
}
|
||||
if parentInfo.Mode()&os.ModeSymlink != 0 || !parentInfo.IsDir() {
|
||||
return errors.New("备份父路径必须是普通目录")
|
||||
}
|
||||
staging, err := os.MkdirTemp(parent, ".scert-backup-")
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建备份暂存目录失败:%w", err)
|
||||
}
|
||||
completed := false
|
||||
defer func() {
|
||||
if !completed {
|
||||
_ = os.RemoveAll(staging)
|
||||
}
|
||||
}()
|
||||
if err := os.Chmod(staging, 0o700); err != nil {
|
||||
return fmt.Errorf("设置备份暂存目录权限失败:%w", err)
|
||||
}
|
||||
|
||||
if err := copyFileExclusive(configPath, filepath.Join(staging, "scert.json"), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateBackupSecretStore(ctx, cfg.Paths.Config, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range []string{"master.key", "secrets.json"} {
|
||||
source := filepath.Join(cfg.Paths.Config, name)
|
||||
info, statErr := os.Lstat(source)
|
||||
if statErr == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return fmt.Errorf("敏感配置文件不是普通文件:%s", name)
|
||||
}
|
||||
if err := copyFileExclusive(source, filepath.Join(staging, name), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !errors.Is(statErr, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查敏感配置文件失败:%w", statErr)
|
||||
}
|
||||
if cfg.SetupReady {
|
||||
return fmt.Errorf("已初始化配置缺少敏感配置文件:%s", name)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.Database.Driver == "sqlite" {
|
||||
_, memory, err := sqliteDatabasePath(cfg.Database.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !memory {
|
||||
if err := backupSQLiteDatabase(ctx, cfg, filepath.Join(staging, "scert.db")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
if err := backupMySQL(ctx, cfg, filepath.Join(staging, "scert.sql")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if info, err := os.Lstat(cfg.Paths.Certificate); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return errors.New("证书根目录必须是普通目录,不能使用符号链接")
|
||||
}
|
||||
if pathsOverlap(cfg.Paths.Certificate, output) {
|
||||
return errors.New("备份目录不能位于证书根目录内,也不能包含证书根目录")
|
||||
}
|
||||
if err := copyDirectoryExclusive(cfg.Paths.Certificate, filepath.Join(staging, "certificates")); err != nil {
|
||||
return fmt.Errorf("备份证书目录失败:%w", err)
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查证书根目录失败:%w", err)
|
||||
}
|
||||
|
||||
if err := writeBackupManifest(staging); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := syncDirectory(staging); err != nil {
|
||||
return fmt.Errorf("同步备份暂存目录失败:%w", err)
|
||||
}
|
||||
if err := os.Rename(staging, output); err != nil {
|
||||
return fmt.Errorf("发布备份目录失败:%w", err)
|
||||
}
|
||||
if err := syncDirectory(parent); err != nil {
|
||||
return fmt.Errorf("同步备份父目录失败:%w", err)
|
||||
}
|
||||
completed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func backupSQLiteDatabase(ctx context.Context, cfg config.Config, output string) error {
|
||||
if _, memory, err := sqliteDatabasePath(cfg.Database.DSN); err != nil {
|
||||
return err
|
||||
} else if memory {
|
||||
return errors.New("内存 SQLite 数据库不能创建文件备份")
|
||||
}
|
||||
db, err := database.Open(ctx, cfg.Database)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开 SQLite 数据库失败:%w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
absOutput, err := filepath.Abs(output)
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析 SQLite 备份路径失败:%w", err)
|
||||
}
|
||||
if _, err := os.Lstat(absOutput); err == nil {
|
||||
return errors.New("SQLite 备份文件已存在")
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查 SQLite 备份文件失败:%w", err)
|
||||
}
|
||||
statement := "VACUUM INTO '" + strings.ReplaceAll(filepath.ToSlash(absOutput), "'", "''") + "'"
|
||||
if _, err := db.DB.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("创建 SQLite 一致性快照失败:%w", err)
|
||||
}
|
||||
if err := os.Chmod(absOutput, 0o600); err != nil {
|
||||
return fmt.Errorf("设置 SQLite 备份权限失败:%w", err)
|
||||
}
|
||||
if err := validateSQLiteDatabase(ctx, absOutput); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSQLiteDatabase(ctx context.Context, path string) error {
|
||||
if !isRegularFile(path) {
|
||||
return errors.New("SQLite 备份不是普通文件")
|
||||
}
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开 SQLite 备份失败:%w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
var result string
|
||||
if err := db.QueryRowContext(ctx, "PRAGMA quick_check").Scan(&result); err != nil {
|
||||
return fmt.Errorf("检查 SQLite 备份完整性失败:%w", err)
|
||||
}
|
||||
if !strings.EqualFold(strings.TrimSpace(result), "ok") {
|
||||
return fmt.Errorf("SQLite 备份完整性检查失败:%s", result)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sqliteDatabasePath(dsn string) (string, bool, error) {
|
||||
dsn = strings.TrimSpace(dsn)
|
||||
if dsn == ":memory:" || strings.HasPrefix(dsn, "file::memory:") {
|
||||
return "", true, nil
|
||||
}
|
||||
if strings.HasPrefix(dsn, "file:") {
|
||||
dsn = strings.TrimPrefix(strings.SplitN(dsn, "?", 2)[0], "file:")
|
||||
}
|
||||
if dsn == "" {
|
||||
return "", false, errors.New("SQLite 数据库路径不能为空")
|
||||
}
|
||||
return filepath.Clean(filepath.FromSlash(dsn)), false, nil
|
||||
}
|
||||
|
||||
func validateRestoreCompatibility(current, backup config.Config) error {
|
||||
if current.Database.Driver != backup.Database.Driver {
|
||||
return fmt.Errorf("备份数据库类型(%s)与当前配置数据库类型(%s)不一致,拒绝恢复", backup.Database.Driver, current.Database.Driver)
|
||||
}
|
||||
for name, values := range map[string][2]string{
|
||||
"配置目录": {current.Paths.Config, backup.Paths.Config},
|
||||
"数据目录": {current.Paths.Data, backup.Paths.Data},
|
||||
"证书目录": {current.Paths.Certificate, backup.Paths.Certificate},
|
||||
"备份目录": {current.Paths.Backup, backup.Paths.Backup},
|
||||
} {
|
||||
if !samePath(values[0], values[1]) {
|
||||
return fmt.Errorf("备份%s与当前配置不一致,拒绝跨目录恢复", name)
|
||||
}
|
||||
}
|
||||
if backup.Database.Driver == "sqlite" {
|
||||
currentPath, currentMemory, err := sqliteDatabasePath(current.Database.DSN)
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析当前 SQLite 数据库路径失败:%w", err)
|
||||
}
|
||||
backupPath, backupMemory, err := sqliteDatabasePath(backup.Database.DSN)
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析备份 SQLite 数据库路径失败:%w", err)
|
||||
}
|
||||
if currentMemory != backupMemory || (!currentMemory && !samePath(currentPath, backupPath)) {
|
||||
return errors.New("备份 SQLite 数据库路径与当前配置不一致,拒绝跨数据库恢复")
|
||||
}
|
||||
}
|
||||
if backup.Database.Driver == "mysql" && strings.TrimSpace(current.Database.DSN) != strings.TrimSpace(backup.Database.DSN) {
|
||||
return errors.New("备份 MySQL 连接配置与当前配置不一致,拒绝跨数据库恢复")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBackupSecretStore(ctx context.Context, directory string, cfg config.Config) error {
|
||||
masterPath := filepath.Join(directory, "master.key")
|
||||
secretsPath := filepath.Join(directory, "secrets.json")
|
||||
masterExists := isRegularFile(masterPath)
|
||||
secretsExists := isRegularFile(secretsPath)
|
||||
if !masterExists {
|
||||
if secretsExists {
|
||||
return errors.New("敏感配置包含 secrets.json,但缺少 master.key")
|
||||
}
|
||||
if cfg.SetupReady {
|
||||
return errors.New("已初始化配置缺少 master.key")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
store, err := secretstore.Open(directory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("校验敏感配置主密钥失败:%w", err)
|
||||
}
|
||||
metadata, err := store.List(ctx, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("校验敏感配置文件失败:%w", err)
|
||||
}
|
||||
available := make(map[string]struct{}, len(metadata))
|
||||
for _, item := range metadata {
|
||||
if _, err := store.Get(ctx, item.Reference); err != nil {
|
||||
return fmt.Errorf("校验敏感配置引用失败:%s:%w", item.Reference, err)
|
||||
}
|
||||
available[item.Reference] = struct{}{}
|
||||
}
|
||||
for _, reference := range []string{cfg.DNS.CredentialRef, cfg.Notification.HTTPCredentialRef, cfg.Notification.SMTPCredentialRef} {
|
||||
reference = strings.TrimSpace(reference)
|
||||
if reference == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := available[reference]; !exists {
|
||||
return fmt.Errorf("敏感配置缺少配置引用:%s", reference)
|
||||
}
|
||||
}
|
||||
if cfg.SetupReady && !secretsExists {
|
||||
return errors.New("已初始化配置缺少 secrets.json")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func restoreAdditionalAssets(cfg config.Config, input string) error {
|
||||
timestamp := time.Now().UTC().Format("20060102-150405.000000000")
|
||||
rollbacks := make([]func() error, 0, 3)
|
||||
rollbackAll := func() error {
|
||||
var rollbackErr error
|
||||
for index := len(rollbacks) - 1; index >= 0; index-- {
|
||||
rollbackErr = errors.Join(rollbackErr, rollbacks[index]())
|
||||
}
|
||||
return rollbackErr
|
||||
}
|
||||
for _, name := range []string{"master.key", "secrets.json"} {
|
||||
source := filepath.Join(input, name)
|
||||
target := filepath.Join(cfg.Paths.Config, name)
|
||||
info, err := os.Lstat(source)
|
||||
if err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return errors.Join(fmt.Errorf("备份敏感配置文件不是普通文件:%s", name), rollbackAll())
|
||||
}
|
||||
rollback, restoreErr := restoreFileWithRollback(source, target, 0o600, timestamp)
|
||||
if restoreErr != nil {
|
||||
return errors.Join(restoreErr, rollbackAll())
|
||||
}
|
||||
rollbacks = append(rollbacks, rollback)
|
||||
continue
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return errors.Join(fmt.Errorf("检查备份敏感配置文件失败:%w", err), rollbackAll())
|
||||
}
|
||||
rollback, removeErr := removeFileWithRollback(target, 0o600, timestamp)
|
||||
if removeErr != nil {
|
||||
return errors.Join(removeErr, rollbackAll())
|
||||
}
|
||||
rollbacks = append(rollbacks, rollback)
|
||||
}
|
||||
certificateSource := filepath.Join(input, "certificates")
|
||||
if info, err := os.Lstat(certificateSource); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return errors.Join(errors.New("备份证书路径必须是普通目录"), rollbackAll())
|
||||
}
|
||||
rollback, err := restoreDirectoryWithRollback(certificateSource, cfg.Paths.Certificate, timestamp)
|
||||
if err != nil {
|
||||
return errors.Join(err, rollbackAll())
|
||||
}
|
||||
rollbacks = append(rollbacks, rollback)
|
||||
} else if errors.Is(err, os.ErrNotExist) {
|
||||
rollback, removeErr := removeDirectoryWithRollback(cfg.Paths.Certificate, timestamp)
|
||||
if removeErr != nil {
|
||||
return errors.Join(removeErr, rollbackAll())
|
||||
}
|
||||
rollbacks = append(rollbacks, rollback)
|
||||
} else {
|
||||
return errors.Join(fmt.Errorf("检查备份证书目录失败:%w", err), rollbackAll())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func restoreFileWithRollback(source, target string, mode os.FileMode, timestamp string) (func() error, error) {
|
||||
if !isRegularFile(source) {
|
||||
return nil, errors.New("恢复源文件必须是普通文件")
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o700); err != nil {
|
||||
return nil, fmt.Errorf("创建恢复目标目录失败:%w", err)
|
||||
}
|
||||
backupPath := ""
|
||||
if info, err := os.Lstat(target); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return nil, errors.New("恢复目标文件不是普通文件")
|
||||
}
|
||||
backupPath = target + ".before-restore-" + timestamp
|
||||
if err := copyFileExclusive(target, backupPath, mode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("检查恢复目标文件失败:%w", err)
|
||||
}
|
||||
if err := copyFile(source, target, mode); err != nil {
|
||||
if backupPath != "" {
|
||||
_ = copyFile(backupPath, target, mode)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return func() error {
|
||||
if backupPath != "" {
|
||||
return copyFile(backupPath, target, mode)
|
||||
}
|
||||
return os.Remove(target)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func removeFileWithRollback(target string, mode os.FileMode, timestamp string) (func() error, error) {
|
||||
info, err := os.Lstat(target)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return func() error { return nil }, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("检查恢复目标文件失败:%w", err)
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return nil, errors.New("恢复目标文件不是普通文件")
|
||||
}
|
||||
backupPath := target + ".before-restore-" + timestamp
|
||||
if err := copyFileExclusive(target, backupPath, mode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Remove(target); err != nil {
|
||||
return nil, fmt.Errorf("移除备份中不存在的当前文件失败:%w", err)
|
||||
}
|
||||
return func() error { return copyFile(backupPath, target, mode) }, nil
|
||||
}
|
||||
|
||||
func removeDirectoryWithRollback(target, timestamp string) (func() error, error) {
|
||||
info, err := os.Lstat(target)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return func() error { return nil }, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("检查证书恢复目标失败:%w", err)
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return nil, errors.New("证书恢复目标必须是普通目录")
|
||||
}
|
||||
backupPath := target + ".before-restore-" + timestamp
|
||||
if err := os.Rename(target, backupPath); err != nil {
|
||||
return nil, fmt.Errorf("保留现有证书目录失败:%w", err)
|
||||
}
|
||||
return func() error { return os.Rename(backupPath, target) }, nil
|
||||
}
|
||||
|
||||
func restoreDirectoryWithRollback(source, target, timestamp string) (func() error, error) {
|
||||
if pathsOverlap(source, target) {
|
||||
return nil, errors.New("证书恢复源目录和目标目录不能互相包含")
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o750); err != nil {
|
||||
return nil, fmt.Errorf("创建证书恢复父目录失败:%w", err)
|
||||
}
|
||||
backupPath := ""
|
||||
if info, err := os.Lstat(target); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return nil, errors.New("证书恢复目标必须是普通目录")
|
||||
}
|
||||
backupPath = target + ".before-restore-" + timestamp
|
||||
if err := os.Rename(target, backupPath); err != nil {
|
||||
return nil, fmt.Errorf("保留现有证书目录失败:%w", err)
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("检查证书恢复目标失败:%w", err)
|
||||
}
|
||||
if err := copyDirectoryExclusive(source, target); err != nil {
|
||||
_ = os.RemoveAll(target)
|
||||
if backupPath != "" {
|
||||
_ = os.Rename(backupPath, target)
|
||||
}
|
||||
return nil, fmt.Errorf("恢复证书目录失败:%w", err)
|
||||
}
|
||||
return func() error {
|
||||
if err := os.RemoveAll(target); err != nil {
|
||||
return err
|
||||
}
|
||||
if backupPath != "" {
|
||||
return os.Rename(backupPath, target)
|
||||
}
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func copyDirectoryExclusive(source, target string) error {
|
||||
if pathsOverlap(source, target) {
|
||||
return errors.New("复制源目录和目标目录不能互相包含")
|
||||
}
|
||||
rootInfo, err := os.Lstat(source)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查源目录失败:%w", err)
|
||||
}
|
||||
if rootInfo.Mode()&os.ModeSymlink != 0 || !rootInfo.IsDir() {
|
||||
return errors.New("复制源路径必须是普通目录")
|
||||
}
|
||||
if _, err := os.Lstat(target); err == nil {
|
||||
return errors.New("复制目标目录已存在")
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查复制目标目录失败:%w", err)
|
||||
}
|
||||
return filepath.WalkDir(source, func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("目录中不允许包含符号链接:%s", path)
|
||||
}
|
||||
relative, err := filepath.Rel(source, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
destination := filepath.Join(target, relative)
|
||||
if entry.IsDir() {
|
||||
mode := info.Mode().Perm()
|
||||
if mode == 0 {
|
||||
mode = 0o750
|
||||
}
|
||||
return os.Mkdir(destination, mode)
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return fmt.Errorf("目录中包含不支持的文件类型:%s", path)
|
||||
}
|
||||
return copyFileExclusive(path, destination, info.Mode().Perm())
|
||||
})
|
||||
}
|
||||
|
||||
func pathsOverlap(left, right string) bool {
|
||||
leftAbs, leftErr := filepath.Abs(filepath.Clean(left))
|
||||
rightAbs, rightErr := filepath.Abs(filepath.Clean(right))
|
||||
if leftErr != nil || rightErr != nil {
|
||||
return true
|
||||
}
|
||||
return pathContains(leftAbs, rightAbs) || pathContains(rightAbs, leftAbs)
|
||||
}
|
||||
|
||||
func pathContains(parent, child string) bool {
|
||||
relative, err := filepath.Rel(parent, child)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return relative == "." || (relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)))
|
||||
}
|
||||
|
||||
func writeBackupManifest(directory string) error {
|
||||
manifest, err := buildBackupManifest(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(manifest, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("生成备份清单失败:%w", err)
|
||||
}
|
||||
data = append(data, '\n')
|
||||
path := filepath.Join(directory, "manifest.json")
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建备份清单失败:%w", err)
|
||||
}
|
||||
if _, err := file.Write(data); err != nil {
|
||||
_ = file.Close()
|
||||
return fmt.Errorf("写入备份清单失败:%w", err)
|
||||
}
|
||||
if err := file.Sync(); err != nil {
|
||||
_ = file.Close()
|
||||
return fmt.Errorf("同步备份清单失败:%w", err)
|
||||
}
|
||||
if err := file.Close(); err != nil {
|
||||
return fmt.Errorf("关闭备份清单失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func syncDirectory(path string) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
return nil
|
||||
}
|
||||
directory, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer directory.Close()
|
||||
return directory.Sync()
|
||||
}
|
||||
|
||||
func buildBackupManifest(directory string) (backupManifest, error) {
|
||||
manifest := backupManifest{Version: backupManifestVersion, CreatedAt: time.Now().UTC()}
|
||||
err := filepath.WalkDir(directory, func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if path == directory || entry.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("备份中不允许包含符号链接:%s", path)
|
||||
}
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return fmt.Errorf("备份中包含不支持的文件类型:%s", path)
|
||||
}
|
||||
relative, err := filepath.Rel(directory, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
digest, err := fileSHA256(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
manifest.Files = append(manifest.Files, backupManifestFile{
|
||||
Path: filepath.ToSlash(relative), Mode: uint32(info.Mode().Perm()), Size: info.Size(), SHA256: digest,
|
||||
})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return backupManifest{}, fmt.Errorf("生成备份文件清单失败:%w", err)
|
||||
}
|
||||
sort.Slice(manifest.Files, func(i, j int) bool { return manifest.Files[i].Path < manifest.Files[j].Path })
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
func validateBackupManifest(directory string) error {
|
||||
path := filepath.Join(directory, "manifest.json")
|
||||
info, err := os.Lstat(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return errors.New("备份目录缺少 manifest.json,拒绝恢复未校验的备份")
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查备份清单失败:%w", err)
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return errors.New("备份清单必须是普通文件")
|
||||
}
|
||||
if info.Size() > 8<<20 {
|
||||
return errors.New("备份清单超过 8 MiB 安全上限")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取备份清单失败:%w", err)
|
||||
}
|
||||
var expected backupManifest
|
||||
if err := json.Unmarshal(data, &expected); err != nil {
|
||||
return fmt.Errorf("解析备份清单失败:%w", err)
|
||||
}
|
||||
if expected.Version != backupManifestVersion {
|
||||
return fmt.Errorf("不支持的备份清单版本:%d", expected.Version)
|
||||
}
|
||||
if expected.CreatedAt.IsZero() {
|
||||
return errors.New("备份清单缺少创建时间")
|
||||
}
|
||||
if len(expected.Files) == 0 {
|
||||
return errors.New("备份清单不包含任何文件")
|
||||
}
|
||||
actual, err := buildBackupManifestExcludingManifest(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(actual.Files) != len(expected.Files) {
|
||||
return errors.New("备份文件数量与清单不一致")
|
||||
}
|
||||
for index := range expected.Files {
|
||||
left, right := expected.Files[index], actual.Files[index]
|
||||
if left.Path != right.Path || left.Mode != right.Mode || left.Size != right.Size || !strings.EqualFold(left.SHA256, right.SHA256) {
|
||||
return fmt.Errorf("备份文件校验失败:%s", left.Path)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildBackupManifestExcludingManifest(directory string) (backupManifest, error) {
|
||||
manifest, err := buildBackupManifest(directory)
|
||||
if err != nil {
|
||||
return backupManifest{}, err
|
||||
}
|
||||
filtered := manifest.Files[:0]
|
||||
for _, item := range manifest.Files {
|
||||
if item.Path != "manifest.json" {
|
||||
filtered = append(filtered, item)
|
||||
}
|
||||
}
|
||||
manifest.Files = filtered
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
func fileSHA256(path string) (string, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
hash := sha256.New()
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func removeIfExists(path string) error {
|
||||
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,493 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
func TestCreateBackupIncludesSecretsCertificatesAndSQLiteSnapshot(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(base)
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data, cfg.Paths.Certificate, cfg.Paths.Backup} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatalf("创建测试目录失败:%v", err)
|
||||
}
|
||||
}
|
||||
configPath := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
if err := createTestSecretStore(cfg.Paths.Config); err != nil {
|
||||
t.Fatalf("创建敏感配置失败:%v", err)
|
||||
}
|
||||
versionDirectory := filepath.Join(cfg.Paths.Certificate, "example.com", "20260831-V1.0")
|
||||
if err := os.MkdirAll(versionDirectory, 0o750); err != nil {
|
||||
t.Fatalf("创建证书版本目录失败:%v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(versionDirectory, "fullchain.pem"), []byte("certificate"), 0o644); err != nil {
|
||||
t.Fatalf("写入证书失败:%v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(versionDirectory, "privkey.pem"), []byte("private-key"), 0o600); err != nil {
|
||||
t.Fatalf("写入私钥失败:%v", err)
|
||||
}
|
||||
|
||||
db, err := database.Open(context.Background(), cfg.Database)
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试数据库失败:%v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := database.Migrate(context.Background(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatalf("迁移测试数据库失败:%v", err)
|
||||
}
|
||||
if _, err := db.DB.Exec(`INSERT INTO audit_logs (action, resource_type, resource_id, status, message, created_at) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`, "backup_test", "test", "1", "success", "备份测试"); err != nil {
|
||||
t.Fatalf("写入测试数据失败:%v", err)
|
||||
}
|
||||
|
||||
output := filepath.Join(base, "backup-output")
|
||||
if err := createBackup(context.Background(), cfg, configPath, output); err != nil {
|
||||
t.Fatalf("创建完整备份失败:%v", err)
|
||||
}
|
||||
if err := validateBackupManifest(output); err != nil {
|
||||
t.Fatalf("备份清单校验失败:%v", err)
|
||||
}
|
||||
for _, relative := range []string{
|
||||
"scert.json",
|
||||
"master.key",
|
||||
"secrets.json",
|
||||
"scert.db",
|
||||
"manifest.json",
|
||||
filepath.Join("certificates", "example.com", "20260831-V1.0", "fullchain.pem"),
|
||||
filepath.Join("certificates", "example.com", "20260831-V1.0", "privkey.pem"),
|
||||
} {
|
||||
if _, err := os.Stat(filepath.Join(output, relative)); err != nil {
|
||||
t.Fatalf("完整备份缺少文件 %s:%v", relative, err)
|
||||
}
|
||||
}
|
||||
|
||||
snapshot, err := sql.Open("sqlite", filepath.Join(output, "scert.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("打开 SQLite 快照失败:%v", err)
|
||||
}
|
||||
defer snapshot.Close()
|
||||
var count int
|
||||
if err := snapshot.QueryRow(`SELECT COUNT(*) FROM audit_logs WHERE action = 'backup_test'`).Scan(&count); err != nil {
|
||||
t.Fatalf("读取 SQLite 快照失败:%v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("SQLite 快照缺少备份时数据:%d", count)
|
||||
}
|
||||
if runtime.GOOS != "windows" {
|
||||
for _, relative := range []string{"master.key", "secrets.json", "scert.db", "manifest.json"} {
|
||||
info, err := os.Stat(filepath.Join(output, relative))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Fatalf("敏感备份文件权限不正确 %s:%o", relative, info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupManifestRejectsTampering(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(directory, "scert.json"), []byte("original"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeBackupManifest(directory); err != nil {
|
||||
t.Fatalf("写入备份清单失败:%v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(directory, "scert.json"), []byte("tampered"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := validateBackupManifest(directory); err == nil || !strings.Contains(err.Error(), "校验失败") {
|
||||
t.Fatalf("被篡改的备份未被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupManifestIsRequired(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(directory, "scert.json"), []byte("{}"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := validateBackupManifest(directory); err == nil || !strings.Contains(err.Error(), "缺少 manifest.json") {
|
||||
t.Fatalf("缺少清单的备份未被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateBackupDoesNotChangeExistingParentPermissions(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Windows 不提供 POSIX 目录权限语义")
|
||||
}
|
||||
base := t.TempDir()
|
||||
parent := filepath.Join(base, "shared")
|
||||
if err := os.Mkdir(parent, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := config.Default(filepath.Join(base, "scert"))
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
configPath := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := createSQLiteTestDatabase(cfg.Database.DSN, "parent_mode"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := createBackup(context.Background(), cfg, configPath, filepath.Join(parent, "backup")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := os.Stat(parent)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o755 {
|
||||
t.Fatalf("备份操作修改了既有父目录权限:%o", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateBackupRequiresSecretsWhenInitialized(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(base)
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
configPath := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := createSQLiteTestDatabase(cfg.Database.DSN, "missing_secrets"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.SetupReady = true
|
||||
if err := createBackup(context.Background(), cfg, configPath, filepath.Join(base, "backup")); err == nil || !strings.Contains(err.Error(), "缺少 master.key") {
|
||||
t.Fatalf("已初始化系统缺少敏感文件时仍创建了备份:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreAdditionalAssetsKeepsRollbackCopies(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(filepath.Join(base, "target"))
|
||||
input := filepath.Join(base, "backup")
|
||||
if err := os.MkdirAll(filepath.Join(input, "certificates", "example.com"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(cfg.Paths.Config, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(cfg.Paths.Certificate, "old.example.com"), 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for path, value := range map[string]string{
|
||||
filepath.Join(input, "master.key"): "new-master",
|
||||
filepath.Join(input, "secrets.json"): "new-secrets",
|
||||
filepath.Join(input, "certificates", "example.com", "privkey.pem"): "new-private-key",
|
||||
filepath.Join(cfg.Paths.Config, "master.key"): "old-master",
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json"): "old-secrets",
|
||||
filepath.Join(cfg.Paths.Certificate, "old.example.com", "cert.pem"): "old-certificate",
|
||||
} {
|
||||
if err := os.WriteFile(path, []byte(value), 0o600); err != nil {
|
||||
t.Fatalf("写入测试恢复文件失败:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := restoreAdditionalAssets(cfg, input); err != nil {
|
||||
t.Fatalf("恢复敏感配置和证书失败:%v", err)
|
||||
}
|
||||
for path, expected := range map[string]string{
|
||||
filepath.Join(cfg.Paths.Config, "master.key"): "new-master",
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json"): "new-secrets",
|
||||
filepath.Join(cfg.Paths.Certificate, "example.com", "privkey.pem"): "new-private-key",
|
||||
} {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil || string(data) != expected {
|
||||
t.Fatalf("恢复结果不正确 %s:data=%q err=%v", path, data, err)
|
||||
}
|
||||
}
|
||||
for _, pattern := range []string{
|
||||
filepath.Join(cfg.Paths.Config, "master.key.before-restore-*"),
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json.before-restore-*"),
|
||||
cfg.Paths.Certificate + ".before-restore-*",
|
||||
} {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil || len(matches) != 1 {
|
||||
t.Fatalf("恢复前副本不正确 %s:matches=%v err=%v", pattern, matches, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreAdditionalAssetsRestoresAbsentState(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(filepath.Join(base, "target"))
|
||||
input := filepath.Join(base, "backup")
|
||||
if err := os.MkdirAll(input, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(cfg.Paths.Config, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(cfg.Paths.Certificate, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range []string{
|
||||
filepath.Join(cfg.Paths.Config, "master.key"),
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json"),
|
||||
filepath.Join(cfg.Paths.Certificate, "current.pem"),
|
||||
} {
|
||||
if err := os.WriteFile(path, []byte("current"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := restoreAdditionalAssets(cfg, input); err != nil {
|
||||
t.Fatalf("恢复缺失状态失败:%v", err)
|
||||
}
|
||||
for _, path := range []string{
|
||||
filepath.Join(cfg.Paths.Config, "master.key"),
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json"),
|
||||
cfg.Paths.Certificate,
|
||||
} {
|
||||
if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("备份中不存在的资产仍保留在当前路径:%s err=%v", path, err)
|
||||
}
|
||||
}
|
||||
for _, pattern := range []string{
|
||||
filepath.Join(cfg.Paths.Config, "master.key.before-restore-*"),
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json.before-restore-*"),
|
||||
cfg.Paths.Certificate + ".before-restore-*",
|
||||
} {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil || len(matches) != 1 {
|
||||
t.Fatalf("缺失状态恢复未保留恢复前副本 %s:matches=%v err=%v", pattern, matches, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRestoreCompatibilityRejectsCrossDatabase(t *testing.T) {
|
||||
current := config.Default(t.TempDir())
|
||||
backup := current
|
||||
backup.Database.DSN = filepath.Join(t.TempDir(), "other.db")
|
||||
if err := validateRestoreCompatibility(current, backup); err == nil || !strings.Contains(err.Error(), "跨数据库") {
|
||||
t.Fatalf("跨数据库恢复未被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreCommandRestoresCompleteSQLiteBackup(t *testing.T) {
|
||||
cfg, currentPath, input := prepareRestoreCommandFixture(t, false)
|
||||
expectedMaster, err := os.ReadFile(filepath.Join(input, "master.key"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedSecrets, err := os.ReadFile(filepath.Join(input, "secrets.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedConfig, err := os.ReadFile(filepath.Join(input, "scert.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previousConfigPath := configPath
|
||||
configPath = currentPath
|
||||
defer func() { configPath = previousConfigPath }()
|
||||
|
||||
command := newRestoreCommand()
|
||||
var output strings.Builder
|
||||
command.SetOut(&output)
|
||||
command.SetErr(&output)
|
||||
command.SetArgs([]string{"--input", input})
|
||||
if err := command.Execute(); err != nil {
|
||||
t.Fatalf("执行完整恢复失败:%v\n%s", err, output.String())
|
||||
}
|
||||
restored, err := config.Load(currentPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if restored.Server.Port != 9005 {
|
||||
t.Fatalf("配置未恢复:%d", restored.Server.Port)
|
||||
}
|
||||
actualConfig, err := os.ReadFile(currentPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(actualConfig) != string(expectedConfig) {
|
||||
t.Fatal("恢复后的配置文件与备份内容不一致")
|
||||
}
|
||||
for path, expected := range map[string][]byte{
|
||||
filepath.Join(cfg.Paths.Config, "master.key"): expectedMaster,
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json"): expectedSecrets,
|
||||
filepath.Join(cfg.Paths.Certificate, "example.com", "cert.pem"): []byte("new-certificate"),
|
||||
} {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil || string(data) != string(expected) {
|
||||
t.Fatalf("恢复文件不正确 %s:data=%q err=%v", path, data, err)
|
||||
}
|
||||
}
|
||||
if count, err := auditActionCount(cfg.Database.DSN, "restored"); err != nil || count != 1 {
|
||||
t.Fatalf("SQLite 数据未恢复:count=%d err=%v", count, err)
|
||||
}
|
||||
if count, err := auditActionCount(cfg.Database.DSN, "current"); err != nil || count != 0 {
|
||||
t.Fatalf("恢复后仍存在当前数据:count=%d err=%v", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreCommandRollsBackCoreAndSecretsOnCertificateFailure(t *testing.T) {
|
||||
cfg, currentPath, input := prepareRestoreCommandFixture(t, true)
|
||||
previousConfigPath := configPath
|
||||
configPath = currentPath
|
||||
defer func() { configPath = previousConfigPath }()
|
||||
|
||||
command := newRestoreCommand()
|
||||
var output strings.Builder
|
||||
command.SetOut(&output)
|
||||
command.SetErr(&output)
|
||||
command.SetArgs([]string{"--input", input})
|
||||
err := command.Execute()
|
||||
if err == nil || !strings.Contains(err.Error(), "证书恢复目标必须是普通目录") {
|
||||
t.Fatalf("证书恢复失败未触发:err=%v output=%s", err, output.String())
|
||||
}
|
||||
restored, loadErr := config.Load(currentPath)
|
||||
if loadErr != nil {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
if restored.Server.Port != 9004 {
|
||||
t.Fatalf("失败后配置未回滚:%d", restored.Server.Port)
|
||||
}
|
||||
master, readErr := os.ReadFile(filepath.Join(cfg.Paths.Config, "master.key"))
|
||||
if readErr != nil || string(master) != "old-master" {
|
||||
t.Fatalf("失败后主密钥未回滚:data=%q err=%v", master, readErr)
|
||||
}
|
||||
certificateTarget, readErr := os.ReadFile(cfg.Paths.Certificate)
|
||||
if readErr != nil || string(certificateTarget) != "blocked-certificate-target" {
|
||||
t.Fatalf("失败后证书目标被破坏:data=%q err=%v", certificateTarget, readErr)
|
||||
}
|
||||
if count, countErr := auditActionCount(cfg.Database.DSN, "current"); countErr != nil || count != 1 {
|
||||
t.Fatalf("失败后 SQLite 数据未回滚:count=%d err=%v", count, countErr)
|
||||
}
|
||||
if count, countErr := auditActionCount(cfg.Database.DSN, "restored"); countErr != nil || count != 0 {
|
||||
t.Fatalf("失败后仍残留恢复数据:count=%d err=%v", count, countErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteDatabasePathSupportsFileURI(t *testing.T) {
|
||||
path, memory, err := sqliteDatabasePath("file:/var/lib/scert/scert.db?_pragma=busy_timeout(5000)")
|
||||
if err != nil || memory {
|
||||
t.Fatalf("SQLite 文件 URI 解析失败:path=%s memory=%t err=%v", path, memory, err)
|
||||
}
|
||||
if filepath.ToSlash(path) != "/var/lib/scert/scert.db" {
|
||||
t.Fatalf("SQLite 文件 URI 路径不正确:%s", path)
|
||||
}
|
||||
if _, memory, err := sqliteDatabasePath("file::memory:?cache=shared"); err != nil || !memory {
|
||||
t.Fatalf("SQLite 内存 URI 识别失败:memory=%t err=%v", memory, err)
|
||||
}
|
||||
}
|
||||
|
||||
func createSQLiteTestDatabase(path, action string) error {
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
if err := database.Migrate(context.Background(), db, "sqlite"); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO audit_logs (action, resource_type, resource_id, status, message, created_at) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`, action, "test", "1", "success", action)
|
||||
return err
|
||||
}
|
||||
|
||||
func prepareRestoreCommandFixture(t *testing.T, blockedCertificate bool) (config.Config, string, string) {
|
||||
t.Helper()
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(filepath.Join(base, "target"))
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data, cfg.Paths.Backup} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if blockedCertificate {
|
||||
if err := os.MkdirAll(filepath.Dir(cfg.Paths.Certificate), 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(cfg.Paths.Certificate, []byte("blocked-certificate-target"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := os.MkdirAll(filepath.Join(cfg.Paths.Certificate, "old.example.com"), 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(cfg.Paths.Certificate, "old.example.com", "cert.pem"), []byte("old-certificate"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
cfg.Server.Port = 9004
|
||||
currentPath := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(currentPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := createSQLiteTestDatabase(cfg.Database.DSN, "current"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for path, value := range map[string]string{
|
||||
filepath.Join(cfg.Paths.Config, "master.key"): "old-master",
|
||||
filepath.Join(cfg.Paths.Config, "secrets.json"): "old-secrets",
|
||||
} {
|
||||
if err := os.WriteFile(path, []byte(value), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
input := filepath.Join(base, "restore-input")
|
||||
if err := os.MkdirAll(filepath.Join(input, "certificates", "example.com"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupConfig := cfg
|
||||
backupConfig.Server.Port = 9005
|
||||
if err := backupConfig.Save(filepath.Join(input, "scert.json")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := createSQLiteTestDatabase(filepath.Join(input, "scert.db"), "restored"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := createTestSecretStore(input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(input, "certificates", "example.com", "cert.pem"), []byte("new-certificate"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeBackupManifest(input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cfg, currentPath, input
|
||||
}
|
||||
|
||||
func auditActionCount(path, action string) (int, error) {
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
var count int
|
||||
err = db.QueryRow(`SELECT COUNT(*) FROM audit_logs WHERE action = ?`, action).Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
func createTestSecretStore(directory string) error {
|
||||
store, err := secretstore.Open(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return store.Put(context.Background(), "secret://test/backup", map[string]string{"token": "encrypted-test-value"})
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/processlock"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
@@ -330,29 +331,18 @@ func newBackupCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lock, err := acquireProcessLock(cfg, "服务正在运行或另一个备份恢复任务正在执行;请先停止 Scert 服务")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Close()
|
||||
if strings.TrimSpace(output) == "" {
|
||||
output = filepath.Join(cfg.Paths.Backup, "scert-"+time.Now().UTC().Format("20060102-150405.000000000"))
|
||||
}
|
||||
output = filepath.Clean(output)
|
||||
if err := ensureBackupDestination(output); err != nil {
|
||||
if err := createBackup(cmd.Context(), cfg, resolveConfigPath(), output); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(output, 0o700); err != nil {
|
||||
return fmt.Errorf("创建备份目录失败:%w", err)
|
||||
}
|
||||
if err := copyFileExclusive(resolveConfigPath(), filepath.Join(output, "scert.json"), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg.Database.Driver == "sqlite" && cfg.Database.DSN != ":memory:" {
|
||||
if err := copyFileExclusive(cfg.Database.DSN, filepath.Join(output, "scert.db"), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
if err := backupMySQL(cmd.Context(), cfg, filepath.Join(output, "scert.sql")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err = fmt.Fprintf(cmd.OutOrStdout(), "备份已创建:%s\n", output)
|
||||
return err
|
||||
},
|
||||
@@ -371,21 +361,42 @@ func newRestoreCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateBackupManifest(input); err != nil {
|
||||
return err
|
||||
}
|
||||
configFile := filepath.Join(input, "scert.json")
|
||||
cfg, err := config.Load(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取备份配置失败:%w", err)
|
||||
}
|
||||
if err := validateBackupSecretStore(cmd.Context(), input, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
currentPath := filepath.Clean(resolveConfigPath())
|
||||
expectedConfigPath := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if !samePath(currentPath, expectedConfigPath) {
|
||||
return fmt.Errorf("恢复目标配置路径必须与备份配置目录一致:%s", expectedConfigPath)
|
||||
}
|
||||
currentConfig, currentConfigErr := config.Load(currentPath)
|
||||
if currentConfigErr != nil && !errors.Is(currentConfigErr, os.ErrNotExist) {
|
||||
return fmt.Errorf("读取当前配置失败:%w", currentConfigErr)
|
||||
}
|
||||
if currentConfigErr == nil && currentConfig.Database.Driver != cfg.Database.Driver {
|
||||
return fmt.Errorf("备份数据库类型(%s)与当前配置数据库类型(%s)不一致,拒绝恢复", cfg.Database.Driver, currentConfig.Database.Driver)
|
||||
if currentConfigErr == nil {
|
||||
if err := validateRestoreCompatibility(currentConfig, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if info, statErr := os.Stat(currentPath); statErr == nil && !info.Mode().IsRegular() {
|
||||
lockConfig := cfg
|
||||
if currentConfigErr == nil {
|
||||
lockConfig = currentConfig
|
||||
}
|
||||
lock, err := acquireProcessLock(lockConfig, "服务正在运行或另一个备份恢复任务正在执行;请先停止 Scert 服务")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Close()
|
||||
if info, statErr := os.Lstat(currentPath); statErr == nil && (info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular()) {
|
||||
return errors.New("当前配置路径不是普通文件")
|
||||
} else if statErr != nil && !errors.Is(statErr, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查当前配置失败:%w", statErr)
|
||||
@@ -397,25 +408,36 @@ func newRestoreCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
databaseSource := filepath.Join(input, "scert.db")
|
||||
databaseSourceInfo, databaseSourceErr := os.Stat(databaseSource)
|
||||
databaseSourceExists := databaseSourceErr == nil
|
||||
databaseSourceInfo, databaseSourceErr := os.Lstat(databaseSource)
|
||||
databaseSourceExists := databaseSourceErr == nil && databaseSourceInfo.Mode().IsRegular()
|
||||
if databaseSourceErr != nil && !errors.Is(databaseSourceErr, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查备份数据库失败:%w", databaseSourceErr)
|
||||
}
|
||||
if databaseSourceExists && !databaseSourceInfo.Mode().IsRegular() {
|
||||
if databaseSourceErr == nil && !databaseSourceInfo.Mode().IsRegular() {
|
||||
return errors.New("备份数据库不是普通文件")
|
||||
}
|
||||
if cfg.Database.Driver == "sqlite" && cfg.Database.DSN != ":memory:" && !databaseSourceExists {
|
||||
databaseTarget := ""
|
||||
sqliteMemory := false
|
||||
if cfg.Database.Driver == "sqlite" {
|
||||
databaseTarget, sqliteMemory, err = sqliteDatabasePath(cfg.Database.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if cfg.Database.Driver == "sqlite" && !sqliteMemory && !databaseSourceExists {
|
||||
return errors.New("SQLite 备份目录缺少有效的 scert.db 文件")
|
||||
}
|
||||
if cfg.Database.Driver == "sqlite" && cfg.Database.DSN != ":memory:" && databaseSourceExists {
|
||||
if samePath(databaseSource, cfg.Database.DSN) {
|
||||
if cfg.Database.Driver == "sqlite" && !sqliteMemory && databaseSourceExists {
|
||||
if err := validateSQLiteDatabase(cmd.Context(), databaseSource); err != nil {
|
||||
return err
|
||||
}
|
||||
if samePath(databaseSource, databaseTarget) {
|
||||
return errors.New("备份数据库与当前数据库路径相同")
|
||||
}
|
||||
if err := ensureParentDirectory(cfg.Database.DSN); err != nil {
|
||||
if err := ensureParentDirectory(databaseTarget); err != nil {
|
||||
return fmt.Errorf("创建当前数据库父目录失败:%w", err)
|
||||
}
|
||||
if databaseTargetInfo, statErr := os.Stat(cfg.Database.DSN); statErr == nil && !databaseTargetInfo.Mode().IsRegular() {
|
||||
if databaseTargetInfo, statErr := os.Lstat(databaseTarget); statErr == nil && (databaseTargetInfo.Mode()&os.ModeSymlink != 0 || !databaseTargetInfo.Mode().IsRegular()) {
|
||||
return errors.New("当前数据库不是普通文件")
|
||||
} else if statErr != nil && !errors.Is(statErr, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查当前数据库失败:%w", statErr)
|
||||
@@ -431,9 +453,9 @@ func newRestoreCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
databaseBackupPath := ""
|
||||
if cfg.Database.Driver == "sqlite" && cfg.Database.DSN != ":memory:" && databaseSourceExists && isRegularFile(cfg.Database.DSN) {
|
||||
databaseBackupPath = cfg.Database.DSN + ".before-restore-" + time.Now().UTC().Format("20060102-150405.000000000")
|
||||
if err := copyFileExclusive(cfg.Database.DSN, databaseBackupPath, 0o600); err != nil {
|
||||
if cfg.Database.Driver == "sqlite" && !sqliteMemory && databaseSourceExists && isRegularFile(databaseTarget) {
|
||||
databaseBackupPath = databaseTarget + ".before-restore-" + time.Now().UTC().Format("20060102-150405.000000000")
|
||||
if err := copyFileExclusive(databaseTarget, databaseBackupPath, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -446,34 +468,50 @@ func newRestoreCommand() *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
if err := cfg.Save(currentPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg.Database.Driver == "sqlite" && cfg.Database.DSN != ":memory:" && databaseSourceExists {
|
||||
if err := copyFile(databaseSource, cfg.Database.DSN, 0o600); err != nil {
|
||||
if configBackupPath != "" {
|
||||
_ = copyFile(configBackupPath, currentPath, 0o600)
|
||||
}
|
||||
configRestoreAttempted := false
|
||||
sqliteRestoreAttempted := false
|
||||
mysqlRestoreAttempted := false
|
||||
rollbackCore := func() error {
|
||||
var rollbackErr error
|
||||
if mysqlRestoreAttempted && mysqlBackupPath != "" {
|
||||
rollbackErr = errors.Join(rollbackErr, restoreMySQL(cmd.Context(), cfg, mysqlBackupPath))
|
||||
}
|
||||
if sqliteRestoreAttempted {
|
||||
if databaseBackupPath != "" {
|
||||
_ = copyFile(databaseBackupPath, cfg.Database.DSN, 0o600)
|
||||
rollbackErr = errors.Join(rollbackErr, copyFileAtomic(databaseBackupPath, databaseTarget, 0o600))
|
||||
} else {
|
||||
rollbackErr = errors.Join(rollbackErr, removeIfExists(databaseTarget))
|
||||
}
|
||||
return fmt.Errorf("恢复 SQLite 数据库失败:%w", err)
|
||||
}
|
||||
if configRestoreAttempted {
|
||||
if configBackupPath != "" {
|
||||
rollbackErr = errors.Join(rollbackErr, copyFileAtomic(configBackupPath, currentPath, 0o600))
|
||||
} else {
|
||||
rollbackErr = errors.Join(rollbackErr, removeIfExists(currentPath))
|
||||
}
|
||||
}
|
||||
return rollbackErr
|
||||
}
|
||||
|
||||
configRestoreAttempted = true
|
||||
if err := copyFileAtomic(configFile, currentPath, 0o600); err != nil {
|
||||
return errors.Join(err, rollbackCore())
|
||||
}
|
||||
if cfg.Database.Driver == "sqlite" && !sqliteMemory && databaseSourceExists {
|
||||
sqliteRestoreAttempted = true
|
||||
if err := copyFileAtomic(databaseSource, databaseTarget, 0o600); err != nil {
|
||||
return errors.Join(fmt.Errorf("恢复 SQLite 数据库失败:%w", err), rollbackCore())
|
||||
}
|
||||
}
|
||||
if cfg.Database.Driver == "mysql" {
|
||||
mysqlRestoreAttempted = true
|
||||
if err := restoreMySQL(cmd.Context(), cfg, mysqlSource); err != nil {
|
||||
restoreFailure := fmt.Errorf("恢复 MySQL 数据库失败:%w", err)
|
||||
if mysqlBackupPath != "" {
|
||||
if rollbackErr := restoreMySQL(cmd.Context(), cfg, mysqlBackupPath); rollbackErr != nil {
|
||||
restoreFailure = fmt.Errorf("%w;MySQL 数据库回滚失败:%v", restoreFailure, rollbackErr)
|
||||
}
|
||||
}
|
||||
if configBackupPath != "" {
|
||||
_ = copyFile(configBackupPath, currentPath, 0o600)
|
||||
}
|
||||
return restoreFailure
|
||||
return errors.Join(fmt.Errorf("恢复 MySQL 数据库失败:%w", err), rollbackCore())
|
||||
}
|
||||
}
|
||||
if err := restoreAdditionalAssets(cfg, input); err != nil {
|
||||
return errors.Join(fmt.Errorf("恢复敏感配置或证书失败:%w", err), rollbackCore())
|
||||
}
|
||||
_, err = fmt.Fprintf(cmd.OutOrStdout(), "恢复已完成:%s\n", input)
|
||||
return err
|
||||
},
|
||||
@@ -491,6 +529,17 @@ func openApp(ctx context.Context) (*app.App, error) {
|
||||
return app.NewWithConfigPath(ctx, cfg, resolveConfigPath())
|
||||
}
|
||||
|
||||
func acquireProcessLock(cfg config.Config, busyMessage string) (*processlock.Lock, error) {
|
||||
lock, err := processlock.Acquire(filepath.Join(cfg.Paths.Data, "scert.lock"))
|
||||
if errors.Is(err, processlock.ErrLocked) {
|
||||
return nil, errors.New(busyMessage)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return lock, nil
|
||||
}
|
||||
|
||||
func tailLines(reader io.Reader, count int) ([]string, error) {
|
||||
values := make([]string, 0, count)
|
||||
scanner := bufio.NewScanner(reader)
|
||||
@@ -687,12 +736,59 @@ func copyFile(source, target string, mode os.FileMode) error {
|
||||
_ = output.Close()
|
||||
return fmt.Errorf("复制文件失败:%w", err)
|
||||
}
|
||||
if err := output.Sync(); err != nil {
|
||||
_ = output.Close()
|
||||
return fmt.Errorf("同步目标文件失败:%w", err)
|
||||
}
|
||||
if err := output.Close(); err != nil {
|
||||
return fmt.Errorf("关闭目标文件失败:%w", err)
|
||||
}
|
||||
return os.Chmod(target, mode)
|
||||
}
|
||||
|
||||
func copyFileAtomic(source, target string, mode os.FileMode) error {
|
||||
input, err := os.Open(source)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开文件失败:%w", err)
|
||||
}
|
||||
defer input.Close()
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o700); err != nil {
|
||||
return fmt.Errorf("创建目标目录失败:%w", err)
|
||||
}
|
||||
output, err := os.CreateTemp(filepath.Dir(target), ".scert-restore-")
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建恢复暂存文件失败:%w", err)
|
||||
}
|
||||
temporaryPath := output.Name()
|
||||
completed := false
|
||||
defer func() {
|
||||
if !completed {
|
||||
_ = output.Close()
|
||||
_ = os.Remove(temporaryPath)
|
||||
}
|
||||
}()
|
||||
if err := output.Chmod(mode); err != nil {
|
||||
return fmt.Errorf("设置恢复暂存文件权限失败:%w", err)
|
||||
}
|
||||
if _, err := io.Copy(output, input); err != nil {
|
||||
return fmt.Errorf("复制文件失败:%w", err)
|
||||
}
|
||||
if err := output.Sync(); err != nil {
|
||||
return fmt.Errorf("同步恢复暂存文件失败:%w", err)
|
||||
}
|
||||
if err := output.Close(); err != nil {
|
||||
return fmt.Errorf("关闭恢复暂存文件失败:%w", err)
|
||||
}
|
||||
if err := os.Rename(temporaryPath, target); err != nil {
|
||||
return fmt.Errorf("原子替换恢复文件失败:%w", err)
|
||||
}
|
||||
if err := syncDirectory(filepath.Dir(target)); err != nil {
|
||||
return fmt.Errorf("同步恢复目标目录失败:%w", err)
|
||||
}
|
||||
completed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyFileExclusive(source, target string, mode os.FileMode) error {
|
||||
input, err := os.Open(source)
|
||||
if err != nil {
|
||||
@@ -706,14 +802,27 @@ func copyFileExclusive(source, target string, mode os.FileMode) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建目标文件失败:%w", err)
|
||||
}
|
||||
completed := false
|
||||
defer func() {
|
||||
if !completed {
|
||||
_ = output.Close()
|
||||
_ = os.Remove(target)
|
||||
}
|
||||
}()
|
||||
if _, err := io.Copy(output, input); err != nil {
|
||||
_ = output.Close()
|
||||
return fmt.Errorf("复制文件失败:%w", err)
|
||||
}
|
||||
if err := output.Sync(); err != nil {
|
||||
return fmt.Errorf("同步目标文件失败:%w", err)
|
||||
}
|
||||
if err := output.Close(); err != nil {
|
||||
return fmt.Errorf("关闭目标文件失败:%w", err)
|
||||
}
|
||||
return os.Chmod(target, mode)
|
||||
if err := os.Chmod(target, mode); err != nil {
|
||||
return err
|
||||
}
|
||||
completed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureBackupDestination(path string) error {
|
||||
@@ -733,12 +842,12 @@ func validateRestoreInput(path string) (string, error) {
|
||||
if path == "." || path == "" {
|
||||
return "", errors.New("备份目录不能为空")
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("检查备份目录失败:%w", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return "", errors.New("备份路径必须是目录")
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", errors.New("备份路径必须是普通目录,不能使用符号链接")
|
||||
}
|
||||
configFile := filepath.Join(path, "scert.json")
|
||||
if !isRegularFile(configFile) {
|
||||
|
||||
@@ -56,6 +56,17 @@ func TestCopyFileExclusiveRefusesOverwrite(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFileExclusiveRemovesPartialTargetOnCopyFailure(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
target := filepath.Join(dir, "target")
|
||||
if err := copyFileExclusive(dir, target, 0o600); err == nil {
|
||||
t.Fatal("复制目录时没有返回错误")
|
||||
}
|
||||
if _, err := os.Lstat(target); !os.IsNotExist(err) {
|
||||
t.Fatalf("复制失败后残留了不完整目标文件:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamePathNormalizesAbsolutePaths(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "file")
|
||||
|
||||
@@ -653,6 +653,11 @@ func newServeCommand() *cobra.Command {
|
||||
if cfg.SetupReady && !cfg.Web.Enabled {
|
||||
return fmt.Errorf("管理页面已关闭")
|
||||
}
|
||||
lock, err := acquireProcessLock(cfg, "Scert 服务已在运行或备份恢复任务正在执行")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Close()
|
||||
service, err := app.NewWithConfigPath(cmd.Context(), cfg, resolveConfigPath())
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -60,6 +61,9 @@ func newServerAddCommand() *cobra.Command {
|
||||
}
|
||||
defer service.Close()
|
||||
item := value.server()
|
||||
now := time.Now().UTC()
|
||||
item.CreatedAt = now
|
||||
item.UpdatedAt = now
|
||||
item.ID = domain.NewID()
|
||||
if _, err := service.SSHKeys.ValidateReference(cmd.Context(), item.PrivateKeyRef); err != nil {
|
||||
return fmt.Errorf("SSH 私钥引用无效:%w", err)
|
||||
@@ -89,6 +93,7 @@ func newServerEditCommand() *cobra.Command {
|
||||
item := value.server()
|
||||
item.ID = existing.ID
|
||||
item.CreatedAt = existing.CreatedAt
|
||||
item.UpdatedAt = time.Now().UTC()
|
||||
if _, err := service.SSHKeys.ValidateReference(cmd.Context(), item.PrivateKeyRef); err != nil {
|
||||
return fmt.Errorf("SSH 私钥引用无效:%w", err)
|
||||
}
|
||||
@@ -180,7 +185,7 @@ func newDomainServersSetCommand() *cobra.Command {
|
||||
type serverFlags struct {
|
||||
name, address, username, privateKeyRef, fingerprint string
|
||||
certificatePath, privateKeyPath, chainPath, webServer string
|
||||
reloadCommand []string
|
||||
validationCommand, reloadCommand []string
|
||||
port int
|
||||
}
|
||||
|
||||
@@ -195,6 +200,7 @@ func (f *serverFlags) bind(command *cobra.Command) {
|
||||
command.Flags().StringVar(&f.privateKeyPath, "private-key-path", deployment.DefaultPrivateKeyPath, "远程私钥文件路径模板")
|
||||
command.Flags().StringVar(&f.chainPath, "chain-path", deployment.DefaultCertificateChainPath, "远程完整证书链路径模板")
|
||||
command.Flags().StringVar(&f.webServer, "web-server", string(deployment.WebServerNginx), "Web 服务类型:nginx、caddy、apache、custom")
|
||||
command.Flags().StringSliceVar(&f.validationCommand, "validation-command", nil, "自定义配置校验命令及参数")
|
||||
command.Flags().StringSliceVar(&f.reloadCommand, "reload-command", nil, "自定义 reload 命令及参数")
|
||||
for _, name := range []string{"name", "address", "username", "private-key-ref", "host-key-fingerprint"} {
|
||||
_ = command.MarkFlagRequired(name)
|
||||
@@ -202,20 +208,24 @@ func (f *serverFlags) bind(command *cobra.Command) {
|
||||
}
|
||||
|
||||
func (f serverFlags) server() deployment.Server {
|
||||
command := make([]string, 0, len(f.reloadCommand))
|
||||
for _, value := range f.reloadCommand {
|
||||
if value = strings.TrimSpace(value); value != "" {
|
||||
command = append(command, value)
|
||||
}
|
||||
}
|
||||
return deployment.Server{
|
||||
Name: f.name, Address: f.address, Port: f.port, Username: f.username,
|
||||
PrivateKeyRef: f.privateKeyRef, HostKeyFingerprint: f.fingerprint,
|
||||
CertificatePath: f.certificatePath, PrivateKeyPath: f.privateKeyPath,
|
||||
CertificateChainPath: f.chainPath, WebServer: deployment.WebServer(f.webServer), ReloadCommand: command,
|
||||
CertificateChainPath: f.chainPath, WebServer: deployment.WebServer(f.webServer),
|
||||
ValidationCommand: normalizeCommand(f.validationCommand), ReloadCommand: normalizeCommand(f.reloadCommand),
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeCommand(values []string) []string {
|
||||
command := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
if value = strings.TrimSpace(value); value != "" {
|
||||
command = append(command, value)
|
||||
}
|
||||
}
|
||||
return command
|
||||
}
|
||||
func openApplication(cmd *cobra.Command) (*app.App, error) {
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
|
||||
@@ -61,3 +61,39 @@ func TestMySQLMigrationStatementsRecognizeRecoverableChanges(t *testing.T) {
|
||||
t.Errorf("可恢复的 MySQL 迁移语句未被识别:%q", item.statement)
|
||||
}
|
||||
}
|
||||
func TestSQLiteUpgradeAddsServerValidationCommand(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := config.Default(dir).Database
|
||||
cfg.DSN = filepath.Join(dir, "data", "upgrade.db")
|
||||
db, err := Open(t.Context(), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("打开升级测试数据库失败:%v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
if _, err := db.DB.ExecContext(t.Context(), migrationTableSQL("sqlite")); err != nil {
|
||||
t.Fatalf("创建迁移表失败:%v", err)
|
||||
}
|
||||
for version := 1; version <= 5; version++ {
|
||||
if err := applyMigration(t.Context(), db.DB, "sqlite", version); err != nil {
|
||||
t.Fatalf("应用第 %d 版迁移失败:%v", version, err)
|
||||
}
|
||||
}
|
||||
_, err = db.DB.ExecContext(t.Context(), `INSERT INTO servers
|
||||
(id, name, address, port, username, private_key_ref, host_key_fingerprint, certificate_path, private_key_path, certificate_chain_path, web_server, reload_command_json)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
"server-upgrade", "升级测试服务器", "192.0.2.80", 22, "deploy", "secret://ssh/upgrade", "SHA256:test",
|
||||
"/ssl/cert.pem", "/ssl/key.pem", "/ssl/fullchain.pem", "nginx", `["nginx","-s","reload"]`)
|
||||
if err != nil {
|
||||
t.Fatalf("写入旧版服务器记录失败:%v", err)
|
||||
}
|
||||
if err := Migrate(t.Context(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatalf("升级到最新数据库版本失败:%v", err)
|
||||
}
|
||||
var validationCommand string
|
||||
if err := db.DB.QueryRowContext(t.Context(), "SELECT validation_command_json FROM servers WHERE id = ?", "server-upgrade").Scan(&validationCommand); err != nil {
|
||||
t.Fatalf("读取升级后的配置校验命令失败:%v", err)
|
||||
}
|
||||
if validationCommand != "[]" {
|
||||
t.Fatalf("旧服务器记录的配置校验命令默认值错误:%s", validationCommand)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/sqlite/0002_domain_options.sql migrations/sqlite/0003_deployment.sql migrations/sqlite/0004_task_attempts.sql migrations/sqlite/0005_users.sql migrations/mysql/0001_initial.sql migrations/mysql/0002_domain_options.sql migrations/mysql/0003_deployment.sql migrations/mysql/0004_task_attempts.sql migrations/mysql/0005_users.sql
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/sqlite/0002_domain_options.sql migrations/sqlite/0003_deployment.sql migrations/sqlite/0004_task_attempts.sql migrations/sqlite/0005_users.sql migrations/sqlite/0006_server_validation.sql migrations/mysql/0001_initial.sql migrations/mysql/0002_domain_options.sql migrations/mysql/0003_deployment.sql migrations/mysql/0004_task_attempts.sql migrations/mysql/0005_users.sql migrations/mysql/0006_server_validation.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
@@ -38,7 +38,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const latestMigrationVersion = 5
|
||||
const latestMigrationVersion = 6
|
||||
|
||||
func applyMigration(ctx context.Context, db *sql.DB, driver string, version int) error {
|
||||
path := fmt.Sprintf("migrations/%s/%04d_%s.sql", driver, version, migrationName(version))
|
||||
@@ -125,6 +125,8 @@ func migrationName(version int) string {
|
||||
return "task_attempts"
|
||||
case 5:
|
||||
return "users"
|
||||
case 6:
|
||||
return "server_validation"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE servers ADD COLUMN validation_command_json TEXT NULL;
|
||||
UPDATE servers SET validation_command_json = '[]' WHERE validation_command_json IS NULL OR validation_command_json = '';
|
||||
ALTER TABLE servers MODIFY COLUMN validation_command_json TEXT NOT NULL;
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE servers ADD COLUMN validation_command_json TEXT NOT NULL DEFAULT '[]';
|
||||
@@ -39,6 +39,7 @@ type Server struct {
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
CertificateChainPath string `json:"certificate_chain_path"`
|
||||
WebServer WebServer `json:"web_server"`
|
||||
ValidationCommand []string `json:"validation_command,omitempty"`
|
||||
ReloadCommand []string `json:"reload_command,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
@@ -70,6 +71,12 @@ func (s Server) Normalize() Server {
|
||||
if len(s.ReloadCommand) == 0 {
|
||||
s.ReloadCommand = DefaultReloadCommand(s.WebServer)
|
||||
}
|
||||
if len(s.ValidationCommand) == 0 {
|
||||
s.ValidationCommand = DefaultValidationCommand(s.WebServer)
|
||||
}
|
||||
for index := range s.ValidationCommand {
|
||||
s.ValidationCommand[index] = strings.TrimSpace(s.ValidationCommand[index])
|
||||
}
|
||||
for index := range s.ReloadCommand {
|
||||
s.ReloadCommand[index] = strings.TrimSpace(s.ReloadCommand[index])
|
||||
}
|
||||
@@ -172,17 +179,28 @@ func (s Server) Validate() error {
|
||||
if s.WebServer != WebServerNginx && s.WebServer != WebServerCaddy && s.WebServer != WebServerApache && s.WebServer != WebServerCustom {
|
||||
return fmt.Errorf("网站服务类型无效:%s", s.WebServer)
|
||||
}
|
||||
if len(s.ReloadCommand) == 0 || s.ReloadCommand[0] == "" {
|
||||
return errors.New("reload 命令不能为空")
|
||||
if err := validateServerCommand("配置校验", s.ValidationCommand); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, argument := range s.ReloadCommand {
|
||||
if argument == "" || strings.ContainsAny(argument, "\x00\r\n") {
|
||||
return errors.New("reload 命令参数无效")
|
||||
}
|
||||
if err := validateServerCommand("reload", s.ReloadCommand); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DefaultValidationCommand(webServer WebServer) []string {
|
||||
switch WebServer(strings.ToLower(strings.TrimSpace(string(webServer)))) {
|
||||
case WebServerNginx:
|
||||
return []string{"nginx", "-t"}
|
||||
case WebServerCaddy:
|
||||
return []string{"caddy", "validate", "--config", "/etc/caddy/Caddyfile"}
|
||||
case WebServerApache:
|
||||
return []string{"apachectl", "configtest"}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultReloadCommand(webServer WebServer) []string {
|
||||
switch WebServer(strings.ToLower(strings.TrimSpace(string(webServer)))) {
|
||||
case WebServerNginx:
|
||||
@@ -196,6 +214,18 @@ func DefaultReloadCommand(webServer WebServer) []string {
|
||||
}
|
||||
}
|
||||
|
||||
func validateServerCommand(label string, command []string) error {
|
||||
if len(command) == 0 || command[0] == "" {
|
||||
return fmt.Errorf("%s命令不能为空", label)
|
||||
}
|
||||
for _, argument := range command {
|
||||
if argument == "" || strings.ContainsAny(argument, "\x00\r\n") {
|
||||
return fmt.Errorf("%s命令参数无效", label)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemotePath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if strings.ContainsAny(value, "\x00\r\n") {
|
||||
|
||||
@@ -10,6 +10,23 @@ func TestServerUsesDefaultReloadCommand(t *testing.T) {
|
||||
if len(server.ReloadCommand) != 3 || server.ReloadCommand[0] != "nginx" {
|
||||
t.Fatalf("Nginx 默认 reload 命令错误:%v", server.ReloadCommand)
|
||||
}
|
||||
if len(server.ValidationCommand) != 2 || server.ValidationCommand[0] != "nginx" || server.ValidationCommand[1] != "-t" {
|
||||
t.Fatalf("Nginx 默认配置校验命令错误:%v", server.ValidationCommand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerRequiresCustomValidationCommand(t *testing.T) {
|
||||
server := testServer("one")
|
||||
server.WebServer = WebServerCustom
|
||||
server.ValidationCommand = nil
|
||||
server.ReloadCommand = []string{"service-control", "reload"}
|
||||
if err := server.Validate(); err == nil {
|
||||
t.Fatal("自定义网站服务未配置校验命令时应被拒绝")
|
||||
}
|
||||
server.ValidationCommand = []string{"service-control", "validate"}
|
||||
if err := server.Validate(); err != nil {
|
||||
t.Fatalf("自定义网站服务配置有效命令后应通过校验:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerUsesDefaultCertificatePathTemplates(t *testing.T) {
|
||||
@@ -108,4 +125,9 @@ func TestServerRejectsPasswordStyleOrUnsafeConfiguration(t *testing.T) {
|
||||
if err := server.Validate(); err == nil {
|
||||
t.Fatal("reload 参数包含换行时应拒绝服务器配置")
|
||||
}
|
||||
server = testServer("one")
|
||||
server.ValidationCommand = []string{"nginx", "-t\nrm"}
|
||||
if err := server.Validate(); err == nil {
|
||||
t.Fatal("配置校验参数包含换行时应拒绝服务器配置")
|
||||
}
|
||||
}
|
||||
@@ -196,6 +196,13 @@ func (s *Service) deployOne(ctx context.Context, server Server, artifact Artifac
|
||||
temporary = temporary[:len(temporary)-1]
|
||||
installed = append(installed, file.path)
|
||||
}
|
||||
validationResult, err := client.Run(ctx, server.ValidationCommand)
|
||||
if err != nil || validationResult.ExitCode != 0 {
|
||||
if err == nil {
|
||||
err = fmt.Errorf("退出码 %d", validationResult.ExitCode)
|
||||
}
|
||||
return failed(fmt.Sprintf("配置校验失败:%v", err))
|
||||
}
|
||||
commandResult, err := client.Run(ctx, server.ReloadCommand)
|
||||
if err != nil || commandResult.ExitCode != 0 {
|
||||
if err == nil {
|
||||
@@ -204,7 +211,7 @@ func (s *Service) deployOne(ctx context.Context, server Server, artifact Artifac
|
||||
return failed(fmt.Sprintf("reload 失败:%v", err))
|
||||
}
|
||||
result.Status = "success"
|
||||
result.Message = "证书部署并 reload 成功"
|
||||
result.Message = "证书部署、配置校验并 reload 成功"
|
||||
result.BackupID = backupID
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -15,9 +15,10 @@ type memoryFile struct {
|
||||
|
||||
type memoryClient struct {
|
||||
files map[string]memoryFile
|
||||
command []string
|
||||
runResult CommandResult
|
||||
runErr error
|
||||
commands [][]string
|
||||
runResults []CommandResult
|
||||
runErrors []error
|
||||
runIndex int
|
||||
uploadError string
|
||||
removeError string
|
||||
renameError string
|
||||
@@ -25,7 +26,7 @@ type memoryClient struct {
|
||||
}
|
||||
|
||||
func newMemoryClient() *memoryClient {
|
||||
return &memoryClient{files: map[string]memoryFile{}, runResult: CommandResult{ExitCode: 0}}
|
||||
return &memoryClient{files: map[string]memoryFile{}}
|
||||
}
|
||||
|
||||
func (c *memoryClient) MkdirAll(context.Context, string) error { return nil }
|
||||
@@ -68,8 +69,18 @@ func (c *memoryClient) Remove(_ context.Context, path string) error {
|
||||
}
|
||||
|
||||
func (c *memoryClient) Run(_ context.Context, command []string) (CommandResult, error) {
|
||||
c.command = append([]string(nil), command...)
|
||||
return c.runResult, c.runErr
|
||||
c.commands = append(c.commands, append([]string(nil), command...))
|
||||
index := c.runIndex
|
||||
c.runIndex++
|
||||
var result CommandResult
|
||||
var err error
|
||||
if index < len(c.runResults) {
|
||||
result = c.runResults[index]
|
||||
}
|
||||
if index < len(c.runErrors) {
|
||||
err = c.runErrors[index]
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (c *memoryClient) Close() error { return nil }
|
||||
@@ -101,8 +112,8 @@ func TestDeployReplacesFilesAndKeepsBackup(t *testing.T) {
|
||||
if string(client.files[server.CertificatePath].data) != "new-cert" || client.files[server.PrivateKeyPath].mode != 0o600 {
|
||||
t.Fatalf("远程证书内容或权限错误:%+v", client.files)
|
||||
}
|
||||
if len(client.command) != 3 || client.command[0] != "nginx" {
|
||||
t.Fatalf("reload 命令错误:%v", client.command)
|
||||
if len(client.commands) != 2 || len(client.commands[0]) != 2 || client.commands[0][1] != "-t" || len(client.commands[1]) != 3 || client.commands[1][0] != "nginx" {
|
||||
t.Fatalf("配置校验或 reload 命令错误:%v", client.commands)
|
||||
}
|
||||
backupCount := 0
|
||||
for path := range client.files {
|
||||
@@ -117,7 +128,7 @@ func TestDeployReplacesFilesAndKeepsBackup(t *testing.T) {
|
||||
|
||||
func TestDeployRollsBackWhenReloadFails(t *testing.T) {
|
||||
client := newMemoryClient()
|
||||
client.runResult.ExitCode = 1
|
||||
client.runResults = []CommandResult{{ExitCode: 0}, {ExitCode: 1}}
|
||||
server := testServer("server-1")
|
||||
client.files[server.CertificatePath] = memoryFile{data: []byte("old-cert"), mode: 0o644}
|
||||
service := NewService(memoryFactory{clients: map[string]*memoryClient{server.ID: client}})
|
||||
@@ -138,9 +149,34 @@ func TestDeployRollsBackWhenReloadFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployRollsBackWhenValidationFailsWithoutReload(t *testing.T) {
|
||||
client := newMemoryClient()
|
||||
client.runResults = []CommandResult{{ExitCode: 1}}
|
||||
server := testServer("server-1")
|
||||
client.files[server.CertificatePath] = memoryFile{data: []byte("old-cert"), mode: 0o644}
|
||||
client.files[server.CertificateChainPath] = memoryFile{data: []byte("old-chain"), mode: 0o644}
|
||||
client.files[server.PrivateKeyPath] = memoryFile{data: []byte("old-key"), mode: 0o600}
|
||||
service := NewService(memoryFactory{clients: map[string]*memoryClient{server.ID: client}})
|
||||
report := service.Deploy(context.Background(), []Target{{Server: server}}, testArtifact())
|
||||
if report.Status != "failed" || report.Failed != 1 || !strings.Contains(report.Results[0].Message, "配置校验失败") {
|
||||
t.Fatalf("配置校验失败结果错误:%+v", report)
|
||||
}
|
||||
if len(client.commands) != 1 || client.commands[0][1] != "-t" {
|
||||
t.Fatalf("配置校验失败后不应执行 reload:%v", client.commands)
|
||||
}
|
||||
if string(client.files[server.CertificatePath].data) != "old-cert" || string(client.files[server.CertificateChainPath].data) != "old-chain" || string(client.files[server.PrivateKeyPath].data) != "old-key" {
|
||||
t.Fatalf("配置校验失败后旧证书未完整恢复:%+v", client.files)
|
||||
}
|
||||
for path := range client.files {
|
||||
if strings.Contains(path, ".scert-tmp-") || strings.Contains(path, ".scert-backup-") {
|
||||
t.Fatalf("配置校验回滚后不应残留临时文件或备份文件:%s", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployReportsRollbackFailure(t *testing.T) {
|
||||
client := newMemoryClient()
|
||||
client.runResult.ExitCode = 1
|
||||
client.runResults = []CommandResult{{ExitCode: 0}, {ExitCode: 1}}
|
||||
client.restoreError = true
|
||||
server := testServer("server-1")
|
||||
client.files[server.CertificatePath] = memoryFile{data: []byte("old-cert"), mode: 0o644}
|
||||
|
||||
@@ -19,8 +19,9 @@ type SecretReader interface {
|
||||
}
|
||||
|
||||
type SSHFactory struct {
|
||||
Secrets SecretReader
|
||||
Timeout time.Duration
|
||||
Secrets SecretReader
|
||||
Timeout time.Duration
|
||||
HostKeyAlgorithms []string
|
||||
}
|
||||
|
||||
func (f SSHFactory) Connect(ctx context.Context, server Server) (Client, error) {
|
||||
@@ -53,29 +54,16 @@ func (f SSHFactory) Connect(ctx context.Context, server Server) (Client, error)
|
||||
if timeout <= 0 {
|
||||
timeout = 15 * time.Second
|
||||
}
|
||||
address := net.JoinHostPort(server.Address, fmt.Sprintf("%d", server.Port))
|
||||
dialer := net.Dialer{Timeout: timeout}
|
||||
connection, err := dialer.DialContext(ctx, "tcp", address)
|
||||
algorithms, err := f.secureHostKeyAlgorithms()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("连接 SSH 服务器失败:%w", err)
|
||||
return nil, err
|
||||
}
|
||||
hostKeyCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||
fingerprint := ssh.FingerprintSHA256(key)
|
||||
if fingerprint != server.HostKeyFingerprint {
|
||||
return fmt.Errorf("SSH 主机指纹不匹配:服务器 %s 返回 %s", server.Name, fingerprint)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
config := &ssh.ClientConfig{
|
||||
User: server.Username, Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||
HostKeyCallback: hostKeyCallback, Timeout: timeout,
|
||||
}
|
||||
clientConn, channels, requests, err := ssh.NewClientConn(connection, address, config)
|
||||
timeoutContext, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
sshClient, err := connectSSHClient(timeoutContext, server, signer, algorithms)
|
||||
if err != nil {
|
||||
_ = connection.Close()
|
||||
return nil, fmt.Errorf("SSH 握手失败:%w", err)
|
||||
return nil, err
|
||||
}
|
||||
sshClient := ssh.NewClient(clientConn, channels, requests)
|
||||
sftpClient, err := sftp.NewClient(sshClient)
|
||||
if err != nil {
|
||||
_ = sshClient.Close()
|
||||
@@ -84,6 +72,107 @@ func (f SSHFactory) Connect(ctx context.Context, server Server) (Client, error)
|
||||
return &sshClientAdapter{ssh: sshClient, sftp: sftpClient}, nil
|
||||
}
|
||||
|
||||
func (f SSHFactory) secureHostKeyAlgorithms() ([]string, error) {
|
||||
supported := ssh.SupportedAlgorithms().HostKeys
|
||||
if len(f.HostKeyAlgorithms) == 0 {
|
||||
return append([]string(nil), supported...), nil
|
||||
}
|
||||
supportedSet := make(map[string]struct{}, len(supported))
|
||||
for _, algorithm := range supported {
|
||||
supportedSet[algorithm] = struct{}{}
|
||||
}
|
||||
algorithms := make([]string, 0, len(f.HostKeyAlgorithms))
|
||||
seen := make(map[string]struct{}, len(f.HostKeyAlgorithms))
|
||||
for _, algorithm := range f.HostKeyAlgorithms {
|
||||
algorithm = strings.TrimSpace(algorithm)
|
||||
if _, ok := supportedSet[algorithm]; !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[algorithm]; ok {
|
||||
continue
|
||||
}
|
||||
seen[algorithm] = struct{}{}
|
||||
algorithms = append(algorithms, algorithm)
|
||||
}
|
||||
if len(algorithms) == 0 {
|
||||
return nil, errors.New("SSH 主机密钥算法列表不包含受支持的安全算法")
|
||||
}
|
||||
return algorithms, nil
|
||||
}
|
||||
|
||||
func connectSSHClient(
|
||||
ctx context.Context,
|
||||
server Server,
|
||||
signer ssh.Signer,
|
||||
algorithms []string,
|
||||
) (*ssh.Client, error) {
|
||||
address := net.JoinHostPort(server.Address, fmt.Sprintf("%d", server.Port))
|
||||
var mismatchErrors []string
|
||||
for _, algorithm := range algorithms {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("连接 SSH 服务器超时:%w", err)
|
||||
}
|
||||
connection, err := (&net.Dialer{}).DialContext(ctx, "tcp", address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("连接 SSH 服务器失败:%w", err)
|
||||
}
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
if err := connection.SetDeadline(deadline); err != nil {
|
||||
_ = connection.Close()
|
||||
return nil, fmt.Errorf("设置 SSH 连接超时失败:%w", err)
|
||||
}
|
||||
}
|
||||
hostKeyMatched := false
|
||||
hostKeyMismatch := ""
|
||||
config := &ssh.ClientConfig{
|
||||
User: server.Username,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||
HostKeyAlgorithms: []string{algorithm},
|
||||
HostKeyCallback: func(_ string, _ net.Addr, key ssh.PublicKey) error {
|
||||
fingerprint := ssh.FingerprintSHA256(key)
|
||||
if fingerprint != server.HostKeyFingerprint {
|
||||
hostKeyMismatch = fmt.Sprintf("%s 返回 %s", algorithm, fingerprint)
|
||||
return fmt.Errorf("SSH 主机指纹不匹配:服务器 %s %s", server.Name, hostKeyMismatch)
|
||||
}
|
||||
hostKeyMatched = true
|
||||
return nil
|
||||
},
|
||||
}
|
||||
clientConn, channels, requests, handshakeErr := ssh.NewClientConn(connection, address, config)
|
||||
if handshakeErr == nil {
|
||||
if err := connection.SetDeadline(time.Time{}); err != nil {
|
||||
_ = clientConn.Close()
|
||||
return nil, fmt.Errorf("清除 SSH 连接超时失败:%w", err)
|
||||
}
|
||||
return ssh.NewClient(clientConn, channels, requests), nil
|
||||
}
|
||||
_ = connection.Close()
|
||||
if hostKeyMatched {
|
||||
return nil, fmt.Errorf("SSH 握手失败:%w", handshakeErr)
|
||||
}
|
||||
if hostKeyMismatch != "" {
|
||||
mismatchErrors = append(mismatchErrors, hostKeyMismatch)
|
||||
continue
|
||||
}
|
||||
var negotiationError *ssh.AlgorithmNegotiationError
|
||||
if errors.As(handshakeErr, &negotiationError) && negotiationError.What == "host key" {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("SSH 握手失败:%w", handshakeErr)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("连接 SSH 服务器超时:%w", err)
|
||||
}
|
||||
if len(mismatchErrors) == 0 {
|
||||
return nil, fmt.Errorf("SSH 主机指纹不匹配:服务器 %s 未提供已登记的主机密钥", server.Name)
|
||||
}
|
||||
return nil, fmt.Errorf(
|
||||
"SSH 主机指纹不匹配:服务器 %s 未提供已登记的主机密钥;已校验 %s",
|
||||
server.Name,
|
||||
strings.Join(mismatchErrors, ";"),
|
||||
)
|
||||
}
|
||||
|
||||
type sshClientAdapter struct {
|
||||
ssh *ssh.Client
|
||||
sftp *sftp.Client
|
||||
|
||||
@@ -0,0 +1,335 @@
|
||||
package deployment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func TestSSHFactoryConnectsUsingRegisteredHostKeyAcrossAlgorithms(t *testing.T) {
|
||||
clientSigner, privateKey := newTestSSHSigner(t)
|
||||
ecdsaHostSigner := newTestECDSAHostSigner(t)
|
||||
ed25519HostSigner, _ := newTestSSHSigner(t)
|
||||
server := startTestSSHServer(t, func(key ssh.PublicKey) error {
|
||||
if !bytes.Equal(key.Marshal(), clientSigner.PublicKey().Marshal()) {
|
||||
return errors.New("测试客户端公钥不匹配")
|
||||
}
|
||||
return nil
|
||||
}, ecdsaHostSigner, ed25519HostSigner)
|
||||
|
||||
factory := SSHFactory{
|
||||
Secrets: staticSecretReader{
|
||||
"secret://ssh/hostkey-fallback": {"private_key": privateKey},
|
||||
},
|
||||
Timeout: 2 * time.Second,
|
||||
HostKeyAlgorithms: []string{
|
||||
ssh.KeyAlgoECDSA256,
|
||||
ssh.KeyAlgoED25519,
|
||||
},
|
||||
}
|
||||
client, err := factory.Connect(t.Context(), testSSHServerModel(
|
||||
server,
|
||||
"secret://ssh/hostkey-fallback",
|
||||
ssh.FingerprintSHA256(ed25519HostSigner.PublicKey()),
|
||||
))
|
||||
if err != nil {
|
||||
t.Fatalf("应在 ECDSA 指纹不匹配后使用 ED25519 主机密钥连接:%v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
if server.accepted.Load() != 2 {
|
||||
t.Fatalf("应分别尝试两个主机密钥算法,实际连接次数:%d", server.accepted.Load())
|
||||
}
|
||||
if exists, err := client.Exists(t.Context(), "."); err != nil || !exists {
|
||||
t.Fatalf("SFTP 连接不可用:exists=%t err=%v", exists, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSHFactorySkipsUnavailableSafeHostKeyAlgorithm(t *testing.T) {
|
||||
clientSigner, privateKey := newTestSSHSigner(t)
|
||||
ed25519HostSigner, _ := newTestSSHSigner(t)
|
||||
server := startTestSSHServer(t, func(key ssh.PublicKey) error {
|
||||
if !bytes.Equal(key.Marshal(), clientSigner.PublicKey().Marshal()) {
|
||||
return errors.New("测试客户端公钥不匹配")
|
||||
}
|
||||
return nil
|
||||
}, ed25519HostSigner)
|
||||
|
||||
factory := SSHFactory{
|
||||
Secrets: staticSecretReader{
|
||||
"secret://ssh/hostkey-unavailable": {"private_key": privateKey},
|
||||
},
|
||||
Timeout: 2 * time.Second,
|
||||
HostKeyAlgorithms: []string{
|
||||
ssh.KeyAlgoECDSA256,
|
||||
ssh.KeyAlgoED25519,
|
||||
},
|
||||
}
|
||||
client, err := factory.Connect(t.Context(), testSSHServerModel(
|
||||
server,
|
||||
"secret://ssh/hostkey-unavailable",
|
||||
ssh.FingerprintSHA256(ed25519HostSigner.PublicKey()),
|
||||
))
|
||||
if err != nil {
|
||||
t.Fatalf("不可用的安全主机密钥算法应被跳过:%v", err)
|
||||
}
|
||||
client.Close()
|
||||
if server.accepted.Load() != 2 {
|
||||
t.Fatalf("应在算法协商失败后尝试下一种安全算法,实际连接次数:%d", server.accepted.Load())
|
||||
}
|
||||
}
|
||||
func TestSSHFactoryRejectsUnregisteredHostKeysAcrossAlgorithms(t *testing.T) {
|
||||
clientSigner, privateKey := newTestSSHSigner(t)
|
||||
ecdsaHostSigner := newTestECDSAHostSigner(t)
|
||||
ed25519HostSigner, _ := newTestSSHSigner(t)
|
||||
server := startTestSSHServer(t, func(key ssh.PublicKey) error {
|
||||
if !bytes.Equal(key.Marshal(), clientSigner.PublicKey().Marshal()) {
|
||||
return errors.New("测试客户端公钥不匹配")
|
||||
}
|
||||
return nil
|
||||
}, ecdsaHostSigner, ed25519HostSigner)
|
||||
|
||||
factory := SSHFactory{
|
||||
Secrets: staticSecretReader{
|
||||
"secret://ssh/hostkey-reject": {"private_key": privateKey},
|
||||
},
|
||||
Timeout: 2 * time.Second,
|
||||
HostKeyAlgorithms: []string{
|
||||
ssh.KeyAlgoECDSA256,
|
||||
ssh.KeyAlgoED25519,
|
||||
},
|
||||
}
|
||||
_, err := factory.Connect(t.Context(), testSSHServerModel(
|
||||
server,
|
||||
"secret://ssh/hostkey-reject",
|
||||
"SHA256:unregistered-test-host-key",
|
||||
))
|
||||
if err == nil || !strings.Contains(err.Error(), "SSH 主机指纹不匹配") {
|
||||
t.Fatalf("未登记的主机指纹必须被拒绝:%v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), ssh.KeyAlgoECDSA256) || !strings.Contains(err.Error(), ssh.KeyAlgoED25519) {
|
||||
t.Fatalf("错误应包含已经安全校验的主机密钥算法:%v", err)
|
||||
}
|
||||
if server.accepted.Load() != 2 {
|
||||
t.Fatalf("错误指纹应校验两个可用算法,实际连接次数:%d", server.accepted.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSHFactoryStopsAfterMatchedHostKeyAuthenticationFailure(t *testing.T) {
|
||||
_, privateKey := newTestSSHSigner(t)
|
||||
ecdsaHostSigner := newTestECDSAHostSigner(t)
|
||||
ed25519HostSigner, _ := newTestSSHSigner(t)
|
||||
server := startTestSSHServer(t, func(ssh.PublicKey) error {
|
||||
return errors.New("测试认证拒绝")
|
||||
}, ecdsaHostSigner, ed25519HostSigner)
|
||||
|
||||
factory := SSHFactory{
|
||||
Secrets: staticSecretReader{
|
||||
"secret://ssh/auth-reject": {"private_key": privateKey},
|
||||
},
|
||||
Timeout: 2 * time.Second,
|
||||
HostKeyAlgorithms: []string{
|
||||
ssh.KeyAlgoECDSA256,
|
||||
ssh.KeyAlgoED25519,
|
||||
},
|
||||
}
|
||||
_, err := factory.Connect(t.Context(), testSSHServerModel(
|
||||
server,
|
||||
"secret://ssh/auth-reject",
|
||||
ssh.FingerprintSHA256(ecdsaHostSigner.PublicKey()),
|
||||
))
|
||||
if err == nil || !strings.Contains(err.Error(), "SSH 握手失败") {
|
||||
t.Fatalf("主机指纹匹配后的认证失败必须直接返回:%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "SSH 主机指纹不匹配") {
|
||||
t.Fatalf("认证失败不得继续尝试其他主机密钥算法:%v", err)
|
||||
}
|
||||
if server.accepted.Load() != 1 {
|
||||
t.Fatalf("认证失败后不得继续重连,实际连接次数:%d", server.accepted.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSHFactoryFiltersUnsupportedAndDuplicateHostKeyAlgorithms(t *testing.T) {
|
||||
factory := SSHFactory{HostKeyAlgorithms: []string{
|
||||
"ssh-dss",
|
||||
ssh.KeyAlgoED25519,
|
||||
ssh.KeyAlgoED25519,
|
||||
}}
|
||||
algorithms, err := factory.secureHostKeyAlgorithms()
|
||||
if err != nil {
|
||||
t.Fatalf("安全算法过滤失败:%v", err)
|
||||
}
|
||||
if len(algorithms) != 1 || algorithms[0] != ssh.KeyAlgoED25519 {
|
||||
t.Fatalf("算法过滤结果不正确:%v", algorithms)
|
||||
}
|
||||
factory.HostKeyAlgorithms = []string{"ssh-dss"}
|
||||
if _, err := factory.secureHostKeyAlgorithms(); err == nil {
|
||||
t.Fatal("仅包含不安全或不支持算法时必须拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
type testSSHServer struct {
|
||||
listener net.Listener
|
||||
accepted atomic.Int32
|
||||
host string
|
||||
port int
|
||||
config *ssh.ServerConfig
|
||||
}
|
||||
|
||||
func startTestSSHServer(
|
||||
t *testing.T,
|
||||
authorize func(ssh.PublicKey) error,
|
||||
hostSigners ...ssh.Signer,
|
||||
) *testSSHServer {
|
||||
t.Helper()
|
||||
config := &ssh.ServerConfig{
|
||||
PublicKeyCallback: func(_ ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
|
||||
return nil, authorize(key)
|
||||
},
|
||||
}
|
||||
for _, signer := range hostSigners {
|
||||
config.AddHostKey(signer)
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("启动测试 SSH 服务失败:%v", err)
|
||||
}
|
||||
host, portText, err := net.SplitHostPort(listener.Addr().String())
|
||||
if err != nil {
|
||||
listener.Close()
|
||||
t.Fatalf("解析测试 SSH 地址失败:%v", err)
|
||||
}
|
||||
port, err := strconv.Atoi(portText)
|
||||
if err != nil {
|
||||
listener.Close()
|
||||
t.Fatalf("解析测试 SSH 端口失败:%v", err)
|
||||
}
|
||||
server := &testSSHServer{
|
||||
listener: listener,
|
||||
host: host,
|
||||
port: port,
|
||||
config: config,
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
listener.Close()
|
||||
})
|
||||
go server.serve()
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *testSSHServer) serve() {
|
||||
for {
|
||||
connection, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.accepted.Add(1)
|
||||
go s.serveConnection(connection)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *testSSHServer) serveConnection(connection net.Conn) {
|
||||
defer connection.Close()
|
||||
_, channels, requests, err := ssh.NewServerConn(connection, s.config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go ssh.DiscardRequests(requests)
|
||||
for channelRequest := range channels {
|
||||
if channelRequest.ChannelType() != "session" {
|
||||
channelRequest.Reject(ssh.UnknownChannelType, "session channel required")
|
||||
continue
|
||||
}
|
||||
channel, requests, err := channelRequest.Accept()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
go serveTestSFTP(channel, requests)
|
||||
}
|
||||
}
|
||||
|
||||
func serveTestSFTP(channel ssh.Channel, requests <-chan *ssh.Request) {
|
||||
defer channel.Close()
|
||||
for request := range requests {
|
||||
if request.Type != "subsystem" {
|
||||
request.Reply(false, nil)
|
||||
continue
|
||||
}
|
||||
var subsystem struct {
|
||||
Name string
|
||||
}
|
||||
if err := ssh.Unmarshal(request.Payload, &subsystem); err != nil || subsystem.Name != "sftp" {
|
||||
request.Reply(false, nil)
|
||||
continue
|
||||
}
|
||||
if err := request.Reply(true, nil); err != nil {
|
||||
return
|
||||
}
|
||||
server, err := sftp.NewServer(channel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = server.Serve()
|
||||
_ = server.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func newTestSSHSigner(t *testing.T) (ssh.Signer, string) {
|
||||
t.Helper()
|
||||
_, privateKey, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("生成测试 ED25519 密钥失败:%v", err)
|
||||
}
|
||||
signer, err := ssh.NewSignerFromKey(privateKey)
|
||||
if err != nil {
|
||||
t.Fatalf("创建测试 SSH 签名器失败:%v", err)
|
||||
}
|
||||
block, err := ssh.MarshalPrivateKey(privateKey, "scert-ssh-test")
|
||||
if err != nil {
|
||||
t.Fatalf("编码测试 SSH 私钥失败:%v", err)
|
||||
}
|
||||
return signer, string(pem.EncodeToMemory(block))
|
||||
}
|
||||
|
||||
func newTestECDSAHostSigner(t *testing.T) ssh.Signer {
|
||||
t.Helper()
|
||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("生成测试 ECDSA 主机密钥失败:%v", err)
|
||||
}
|
||||
signer, err := ssh.NewSignerFromKey(privateKey)
|
||||
if err != nil {
|
||||
t.Fatalf("创建测试 ECDSA 主机签名器失败:%v", err)
|
||||
}
|
||||
return signer
|
||||
}
|
||||
|
||||
func testSSHServerModel(server *testSSHServer, privateKeyRef, fingerprint string) Server {
|
||||
return Server{
|
||||
ID: fmt.Sprintf("server-%d", server.port),
|
||||
Name: "多主机密钥测试服务器",
|
||||
Address: server.host,
|
||||
Port: server.port,
|
||||
Username: "deploy",
|
||||
PrivateKeyRef: privateKeyRef,
|
||||
HostKeyFingerprint: fingerprint,
|
||||
CertificatePath: "/tmp/scert-test-cert.pem",
|
||||
PrivateKeyPath: "/tmp/scert-test-key.pem",
|
||||
CertificateChainPath: "/tmp/scert-test-fullchain.pem",
|
||||
WebServer: WebServerNginx,
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,7 @@ func isCoreRequestPath(path string) bool {
|
||||
return path == "/api" ||
|
||||
strings.HasPrefix(path, "/api/") ||
|
||||
path == "/healthz" ||
|
||||
path == "/readyz" ||
|
||||
path == "/reset" ||
|
||||
strings.HasPrefix(path, "/reset/")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
)
|
||||
|
||||
const readinessDNSReference = "secret://dns/cloudflare/readiness-test"
|
||||
|
||||
func TestReadinessRejectsIncompleteSetup(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
response := requestReadiness(New(Dependencies{Config: cfg}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "setup_incomplete")
|
||||
}
|
||||
|
||||
func TestReadinessRejectsMissingDNSCredentialReference(t *testing.T) {
|
||||
cfg := readinessConfig(t)
|
||||
cfg.DNS.CredentialRef = ""
|
||||
response := requestReadiness(New(Dependencies{Config: cfg}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "dns_credential_missing")
|
||||
}
|
||||
|
||||
func TestReadinessRejectsUnavailableCredentialWithoutLeakingReference(t *testing.T) {
|
||||
cfg := readinessConfig(t)
|
||||
store, err := secretstore.Open(filepath.Join(t.TempDir(), "secrets"))
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试敏感信息存储失败:%v", err)
|
||||
}
|
||||
response := requestReadiness(New(Dependencies{Config: cfg, Secrets: store}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "credential_unavailable")
|
||||
if strings.Contains(response.Body.String(), readinessDNSReference) || strings.Contains(response.Body.String(), "secret://") {
|
||||
t.Fatalf("生产就绪响应泄漏敏感信息引用:%s", response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessChecksConfiguredNotificationCredentials(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configure func(*config.Config)
|
||||
}{
|
||||
{
|
||||
name: "HTTP notification",
|
||||
configure: func(cfg *config.Config) {
|
||||
cfg.Notification.HTTPCredentialRef = "secret://notification/http/readiness-test"
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "SMTP notification",
|
||||
configure: func(cfg *config.Config) {
|
||||
cfg.Notification.SMTPCredentialRef = "secret://notification/smtp/readiness-test"
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cfg := readinessConfig(t)
|
||||
test.configure(&cfg)
|
||||
store := readinessStore(t)
|
||||
response := requestReadiness(New(Dependencies{Config: cfg, Secrets: store}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "credential_unavailable")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessRejectsInvalidTLSMaterial(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configure func(*testing.T, *config.Config)
|
||||
}{
|
||||
{
|
||||
name: "missing file",
|
||||
configure: func(t *testing.T, cfg *config.Config) {
|
||||
cfg.Server.TLSCert = filepath.Join(t.TempDir(), "missing.pem")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "directory",
|
||||
configure: func(t *testing.T, cfg *config.Config) {
|
||||
cfg.Server.TLSKey = t.TempDir()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "symbolic link",
|
||||
configure: func(t *testing.T, cfg *config.Config) {
|
||||
target := filepath.Join(t.TempDir(), "target.pem")
|
||||
if err := os.WriteFile(target, []byte("test"), 0o600); err != nil {
|
||||
t.Fatalf("写入符号链接目标失败:%v", err)
|
||||
}
|
||||
link := filepath.Join(t.TempDir(), "linked.pem")
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Skipf("当前环境无法创建符号链接:%v", err)
|
||||
}
|
||||
cfg.Server.TLSCert = link
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cfg := readinessConfig(t)
|
||||
test.configure(t, &cfg)
|
||||
response := requestReadiness(New(Dependencies{Config: cfg, Secrets: readinessStore(t)}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "tls_material_unavailable")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessRejectsUnavailableDatabase(t *testing.T) {
|
||||
cfg := readinessConfig(t)
|
||||
response := requestReadiness(New(Dependencies{
|
||||
Config: cfg,
|
||||
Secrets: readinessStore(t),
|
||||
ReadinessCheck: func(_ context.Context) error {
|
||||
return errors.New("database unavailable")
|
||||
},
|
||||
}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "database_unavailable")
|
||||
}
|
||||
|
||||
func TestReadinessSucceedsWhenDependenciesAreAvailable(t *testing.T) {
|
||||
cfg := readinessConfig(t)
|
||||
response := requestReadiness(New(Dependencies{
|
||||
Config: cfg,
|
||||
Secrets: readinessStore(t),
|
||||
ReadinessCheck: func(_ context.Context) error {
|
||||
return nil
|
||||
},
|
||||
}))
|
||||
assertReadinessResponse(t, response, http.StatusOK, "ready")
|
||||
}
|
||||
|
||||
func TestReadinessIsNotHandledByFrontendFallback(t *testing.T) {
|
||||
staticDir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(staticDir, "index.html"), []byte("frontend fallback"), 0o644); err != nil {
|
||||
t.Fatalf("写入前端入口失败:%v", err)
|
||||
}
|
||||
cfg := config.Default(t.TempDir())
|
||||
response := requestReadiness(New(Dependencies{Config: cfg, FrontendDir: staticDir}))
|
||||
assertReadinessResponse(t, response, http.StatusServiceUnavailable, "setup_incomplete")
|
||||
if strings.Contains(response.Body.String(), "frontend fallback") {
|
||||
t.Fatalf("生产就绪路径被前端回退接管:%s", response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func readinessConfig(t *testing.T) config.Config {
|
||||
t.Helper()
|
||||
directory := t.TempDir()
|
||||
cfg := config.Default(directory)
|
||||
cfg.SetupReady = true
|
||||
cfg.DNS.CredentialRef = readinessDNSReference
|
||||
cfg.Server.TLSCert = filepath.Join(directory, "server.crt")
|
||||
cfg.Server.TLSKey = filepath.Join(directory, "server.key")
|
||||
if err := os.WriteFile(cfg.Server.TLSCert, []byte("certificate"), 0o644); err != nil {
|
||||
t.Fatalf("写入测试证书失败:%v", err)
|
||||
}
|
||||
if err := os.WriteFile(cfg.Server.TLSKey, []byte("private key"), 0o600); err != nil {
|
||||
t.Fatalf("写入测试私钥失败:%v", err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func readinessStore(t *testing.T) *secretstore.Store {
|
||||
t.Helper()
|
||||
store, err := secretstore.Open(filepath.Join(t.TempDir(), "secrets"))
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试敏感信息存储失败:%v", err)
|
||||
}
|
||||
if err := store.Put(t.Context(), readinessDNSReference, map[string]string{"api_token": "readiness-test-token"}); err != nil {
|
||||
t.Fatalf("写入测试 DNS 凭据失败:%v", err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func requestReadiness(server *Server) *httptest.ResponseRecorder {
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/readyz", nil))
|
||||
return response
|
||||
}
|
||||
|
||||
func assertReadinessResponse(t *testing.T, response *httptest.ResponseRecorder, wantStatus int, wantState string) {
|
||||
t.Helper()
|
||||
if response.Code != wantStatus {
|
||||
t.Fatalf("生产就绪状态码 = %d,期望 %d,响应:%s", response.Code, wantStatus, response.Body.String())
|
||||
}
|
||||
if !strings.Contains(response.Body.String(), `"status":"`+wantState+`"`) &&
|
||||
!strings.Contains(response.Body.String(), `"reason":"`+wantState+`"`) {
|
||||
t.Fatalf("生产就绪响应缺少状态 %q:%s", wantState, response.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -83,6 +84,37 @@ func TestStatusReturnsApplicationAndConfigVersions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicStatusDoesNotExposeInternalConfiguration(t *testing.T) {
|
||||
cfg := readyTestConfig(t.TempDir())
|
||||
cfg.Database.Driver = "mysql"
|
||||
cfg.Server.HTTPSMode = "proxy"
|
||||
server := New(Dependencies{Config: cfg, Repository: &stubDomainRepository{}})
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/status", nil))
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("公开状态接口响应错误:%d", response.Code)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("解析公开状态响应失败:%v", err)
|
||||
}
|
||||
for _, key := range []string{"database_driver", "https_mode", "reset_url"} {
|
||||
if _, exists := payload[key]; exists {
|
||||
t.Fatalf("公开状态接口不应返回内部字段 %s:%s", key, response.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPServerUsesProductionTimeoutsAndTLSMinimum(t *testing.T) {
|
||||
server := newHTTPServer("127.0.0.1:0", http.NewServeMux())
|
||||
if server.ReadHeaderTimeout != 10*time.Second || server.ReadTimeout != 30*time.Second || server.WriteTimeout != 30*time.Second || server.IdleTimeout != 60*time.Second {
|
||||
t.Fatalf("HTTP 服务超时配置不正确:%+v", server)
|
||||
}
|
||||
if server.TLSConfig == nil || server.TLSConfig.MinVersion != tls.VersionTLS12 {
|
||||
t.Fatalf("TLS 最低版本应为 TLS 1.2:%+v", server.TLSConfig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainAPIValidatesSANAndHidesInternalErrors(t *testing.T) {
|
||||
cfg := readyTestConfig(t.TempDir())
|
||||
repo := &stubDomainRepository{}
|
||||
|
||||
@@ -2,6 +2,7 @@ package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -33,6 +34,10 @@ type setupSecretWriter interface {
|
||||
Delete(context.Context, string) error
|
||||
}
|
||||
|
||||
type readinessSecretReader interface {
|
||||
Get(context.Context, string) (map[string]string, error)
|
||||
}
|
||||
|
||||
type Dependencies struct {
|
||||
Config config.Config
|
||||
ConfigPath string
|
||||
@@ -58,6 +63,7 @@ type Dependencies struct {
|
||||
ResetStatePath string
|
||||
Audit repository.AuditRepository
|
||||
Tasks repository.TaskRepository
|
||||
ReadinessCheck func(context.Context) error
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
@@ -86,6 +92,7 @@ type Server struct {
|
||||
resetStatePath string
|
||||
audit repository.AuditRepository
|
||||
tasks repository.TaskRepository
|
||||
readinessCheck func(context.Context) error
|
||||
taskMu sync.Mutex
|
||||
taskCancels map[string]context.CancelFunc
|
||||
issueTaskMu sync.Mutex
|
||||
@@ -127,6 +134,7 @@ func New(deps Dependencies) *Server {
|
||||
resetStatePath: deps.ResetStatePath,
|
||||
audit: deps.Audit,
|
||||
tasks: deps.Tasks,
|
||||
readinessCheck: deps.ReadinessCheck,
|
||||
taskCancels: make(map[string]context.CancelFunc),
|
||||
resetLimiter: newResetRateLimiter(),
|
||||
authSessions: make(map[string]authSession),
|
||||
@@ -142,6 +150,7 @@ func New(deps Dependencies) *Server {
|
||||
func (s *Server) registerRoutes() {
|
||||
// 主路由保持独立,初始化路由由专用模块注册。
|
||||
s.router.GET("/healthz", s.health)
|
||||
s.router.GET("/readyz", s.ready)
|
||||
s.router.GET(admin.ResetPath, s.resetPage)
|
||||
s.router.POST(admin.ResetPath, s.resetSubmit)
|
||||
s.router.GET("/api/v1/status", s.status)
|
||||
@@ -204,14 +213,7 @@ func (s *Server) ListenAndServe() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.httpServer = &http.Server{
|
||||
Addr: address,
|
||||
Handler: s.router,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
s.httpServer = newHTTPServer(address, s.router)
|
||||
if !cfg.SetupReady {
|
||||
if s.logger != nil {
|
||||
s.logger.Info("HTTP 服务启动", "address", s.httpServer.Addr, "https_mode", "初始化明文 HTTP")
|
||||
@@ -235,6 +237,20 @@ func (s *Server) ListenAndServe() error {
|
||||
return s.httpServer.ListenAndServeTLS(certPath, keyPath)
|
||||
}
|
||||
|
||||
func newHTTPServer(address string, handler http.Handler) *http.Server {
|
||||
return &http.Server{
|
||||
Addr: address,
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
TLSConfig: &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// listenAddress 返回当前 HTTPS 模式实际使用的监听地址。
|
||||
// 高级模式的核心服务只允许通过本机回环地址提供内部 HTTPS,避免误绑定到公网网卡。
|
||||
func listenAddress(cfg config.Config) (string, error) {
|
||||
@@ -291,6 +307,7 @@ type serverRequest struct {
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
CertificateChainPath string `json:"certificate_chain_path"`
|
||||
WebServer string `json:"web_server" binding:"required"`
|
||||
ValidationCommand []string `json:"validation_command"`
|
||||
ReloadCommand []string `json:"reload_command"`
|
||||
}
|
||||
|
||||
@@ -302,6 +319,71 @@ func (s *Server) health(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "service": "scert"})
|
||||
}
|
||||
|
||||
func (s *Server) ready(c *gin.Context) {
|
||||
s.configMu.RLock()
|
||||
cfg := s.config
|
||||
s.configMu.RUnlock()
|
||||
if !cfg.SetupReady {
|
||||
s.writeReadinessFailure(c, "setup_incomplete")
|
||||
return
|
||||
}
|
||||
|
||||
dnsReference := strings.TrimSpace(cfg.DNS.CredentialRef)
|
||||
if dnsReference == "" {
|
||||
s.writeReadinessFailure(c, "dns_credential_missing")
|
||||
return
|
||||
}
|
||||
references := []string{
|
||||
dnsReference,
|
||||
strings.TrimSpace(cfg.Notification.HTTPCredentialRef),
|
||||
strings.TrimSpace(cfg.Notification.SMTPCredentialRef),
|
||||
}
|
||||
reader, ok := s.secrets.(readinessSecretReader)
|
||||
if !ok {
|
||||
s.writeReadinessFailure(c, "secret_store_unavailable")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
checked := make(map[string]struct{}, len(references))
|
||||
for _, reference := range references {
|
||||
if reference == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := checked[reference]; exists {
|
||||
continue
|
||||
}
|
||||
checked[reference] = struct{}{}
|
||||
if _, err := reader.Get(ctx, reference); err != nil {
|
||||
s.writeReadinessFailure(c, "credential_unavailable")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, path := range []string{cfg.Server.TLSCert, cfg.Server.TLSKey} {
|
||||
info, err := os.Lstat(filepath.Clean(strings.TrimSpace(path)))
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
s.writeReadinessFailure(c, "tls_material_unavailable")
|
||||
return
|
||||
}
|
||||
}
|
||||
if s.readinessCheck != nil {
|
||||
if err := s.readinessCheck(ctx); err != nil {
|
||||
s.writeReadinessFailure(c, "database_unavailable")
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ready", "service": "scert"})
|
||||
}
|
||||
|
||||
func (s *Server) writeReadinessFailure(c *gin.Context, reason string) {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
||||
"status": "not_ready",
|
||||
"service": "scert",
|
||||
"reason": reason,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) resetPage(c *gin.Context) {
|
||||
s.resetMu.Lock()
|
||||
defer s.resetMu.Unlock()
|
||||
@@ -323,15 +405,18 @@ func (s *Server) status(c *gin.Context) {
|
||||
s.configMu.RLock()
|
||||
cfg := s.config
|
||||
s.configMu.RUnlock()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"service": "scert",
|
||||
"version": buildinfo.Version,
|
||||
"config_version": cfg.Version,
|
||||
"setup_ready": cfg.SetupReady,
|
||||
"https_mode": cfg.Server.HTTPSMode,
|
||||
"database_driver": cfg.Database.Driver,
|
||||
"reset_url": cfg.ResetURL(),
|
||||
})
|
||||
payload := gin.H{
|
||||
"service": "scert",
|
||||
"version": buildinfo.Version,
|
||||
"config_version": cfg.Version,
|
||||
"setup_ready": cfg.SetupReady,
|
||||
}
|
||||
if _, authenticated := s.currentAuthSession(c); authenticated {
|
||||
payload["https_mode"] = cfg.Server.HTTPSMode
|
||||
payload["database_driver"] = cfg.Database.Driver
|
||||
payload["reset_url"] = cfg.ResetURL()
|
||||
}
|
||||
c.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
func (s *Server) requireDomainRepository(c *gin.Context) bool {
|
||||
@@ -521,6 +606,9 @@ func (s *Server) createServer(c *gin.Context) {
|
||||
}
|
||||
item := serverFromRequest(request)
|
||||
item = item.Normalize()
|
||||
now := time.Now().UTC()
|
||||
item.CreatedAt = now
|
||||
item.UpdatedAt = now
|
||||
if err := s.validateSSHKeyReference(c.Request.Context(), item.PrivateKeyRef); err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "SSH 私钥引用无效", err)
|
||||
return
|
||||
@@ -555,6 +643,7 @@ func (s *Server) updateServer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
item.CreatedAt = existing.CreatedAt
|
||||
item.UpdatedAt = time.Now().UTC()
|
||||
if err := s.validateSSHKeyReference(c.Request.Context(), item.PrivateKeyRef); err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "SSH 私钥引用无效", err)
|
||||
return
|
||||
@@ -653,7 +742,7 @@ func serverFromRequest(request serverRequest) deployment.Server {
|
||||
Username: request.Username, PrivateKeyRef: request.PrivateKeyRef, HostKeyFingerprint: request.HostKeyFingerprint,
|
||||
CertificatePath: request.CertificatePath, PrivateKeyPath: request.PrivateKeyPath,
|
||||
CertificateChainPath: request.CertificateChainPath, WebServer: deployment.WebServer(request.WebServer),
|
||||
ReloadCommand: request.ReloadCommand,
|
||||
ValidationCommand: request.ValidationCommand, ReloadCommand: request.ReloadCommand,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -332,13 +332,31 @@ func TestServerAndDomainAssociationAPI(t *testing.T) {
|
||||
t.Fatalf("创建测试服务器失败:%d %s", serverResponse.Code, serverResponse.Body.String())
|
||||
}
|
||||
var created deployment.Server
|
||||
if err := json.Unmarshal(serverResponse.Body.Bytes(), &created); err != nil || created.ID == "" || len(created.ReloadCommand) != 3 {
|
||||
if err := json.Unmarshal(serverResponse.Body.Bytes(), &created); err != nil || created.ID == "" || len(created.ValidationCommand) != 2 || created.ValidationCommand[1] != "-t" || len(created.ReloadCommand) != 3 {
|
||||
t.Fatalf("测试服务器响应无效:%v %+v", err, created)
|
||||
}
|
||||
if created.CreatedAt.IsZero() || created.UpdatedAt.IsZero() {
|
||||
t.Fatalf("created server response has zero timestamps: %+v", created)
|
||||
}
|
||||
if created.CertificatePath != deployment.DefaultCertificatePath || created.CertificateChainPath != deployment.DefaultCertificateChainPath || created.PrivateKeyPath != deployment.DefaultPrivateKeyPath {
|
||||
t.Fatalf("服务器 API 未使用默认远程证书路径模板:%+v", created)
|
||||
}
|
||||
|
||||
updateRequest := httptest.NewRequest(http.MethodPut, "/api/v1/servers/"+created.ID, strings.NewReader(`{"name":"updated-server","address":"192.0.2.50","port":22,"username":"deploy","private_key_ref":"`+key.Reference+`","host_key_fingerprint":"SHA256:test","web_server":"nginx"}`))
|
||||
updateRequest.Header.Set("Content-Type", "application/json")
|
||||
updateResponse := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, updateResponse, updateRequest)
|
||||
if updateResponse.Code != http.StatusOK {
|
||||
t.Fatalf("update server failed: %d %s", updateResponse.Code, updateResponse.Body.String())
|
||||
}
|
||||
var updated deployment.Server
|
||||
if err := json.Unmarshal(updateResponse.Body.Bytes(), &updated); err != nil {
|
||||
t.Fatalf("decode updated server response: %v", err)
|
||||
}
|
||||
if updated.Name != "updated-server" || updated.CreatedAt.IsZero() || updated.UpdatedAt.IsZero() {
|
||||
t.Fatalf("updated server response is incomplete: %+v", updated)
|
||||
}
|
||||
|
||||
associationRequest := httptest.NewRequest(http.MethodPut, "/api/v1/domains/"+item.ID+"/servers", strings.NewReader(`{"server_ids":["`+created.ID+`"]}`))
|
||||
associationRequest.Header.Set("Content-Type", "application/json")
|
||||
associationResponse := httptest.NewRecorder()
|
||||
|
||||
@@ -102,6 +102,15 @@ func (s *Server) setupStatus(c *gin.Context) {
|
||||
blockers = append(blockers, "DNS API 尚未验证")
|
||||
}
|
||||
}
|
||||
if cfg.SetupReady {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"initialized": cfg.SetupReady,
|
||||
"phase": phase,
|
||||
"blockers": blockers,
|
||||
"web_enabled": cfg.Web.Enabled,
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"initialized": cfg.SetupReady,
|
||||
"phase": phase,
|
||||
|
||||
@@ -177,6 +177,38 @@ func TestSetupStatusOnlyAllowsLocalAccessBeforeInitialization(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupStatusHidesConfigurationAfterInitialization(t *testing.T) {
|
||||
cfg := readyTestConfig(t.TempDir())
|
||||
cfg.Server.Port = 9004
|
||||
cfg.Server.HTTPSMode = "proxy"
|
||||
cfg.Web.Hostname = "internal.example.test"
|
||||
cfg.Web.PublicIP = "192.0.2.20"
|
||||
cfg.Paths.Certificate = filepath.Join(t.TempDir(), "private-certificates")
|
||||
cfg.DNS.Provider = "cloudflare"
|
||||
cfg.DNS.VerificationState = "verified"
|
||||
server := New(Dependencies{Config: cfg, Repository: &stubDomainRepository{}})
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/setup/status", nil)
|
||||
request.RemoteAddr = "203.0.113.8:53000"
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("初始化完成后的状态接口响应错误:%d %s", response.Code, response.Body.String())
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("解析初始化状态失败:%v", err)
|
||||
}
|
||||
if payload["initialized"] != true {
|
||||
t.Fatalf("初始化状态不正确:%s", response.Body.String())
|
||||
}
|
||||
for _, key := range []string{"server_port", "reset_url", "https_mode", "web_hostname", "web_public_ip", "certificate_root", "dns_provider", "dns_verification_state"} {
|
||||
if _, exists := payload[key]; exists {
|
||||
t.Fatalf("初始化完成后不应公开内部字段 %s:%s", key, response.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupDNSProviderCatalogAndOnlineTest(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
verifier := &fakeSetupDNSVerifier{}
|
||||
|
||||
@@ -4,35 +4,26 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
*slog.Logger
|
||||
file *os.File
|
||||
closer io.Closer
|
||||
}
|
||||
|
||||
func New(logDir string, level slog.Level) (*Logger, error) {
|
||||
if err := os.MkdirAll(logDir, 0o750); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := filepath.Join(logDir, "scert.log")
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
|
||||
fileWriter, err := newDailyFileWriter(logDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := file.Chmod(0o600); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
writer := io.MultiWriter(os.Stdout, file)
|
||||
writer := io.MultiWriter(os.Stdout, fileWriter)
|
||||
handler := slog.NewJSONHandler(writer, &slog.HandlerOptions{Level: level})
|
||||
return &Logger{Logger: slog.New(handler), file: file}, nil
|
||||
return &Logger{Logger: slog.New(handler), closer: fileWriter}, nil
|
||||
}
|
||||
|
||||
func (l *Logger) Close() error {
|
||||
if l == nil || l.file == nil {
|
||||
if l == nil || l.closer == nil {
|
||||
return nil
|
||||
}
|
||||
return l.file.Close()
|
||||
return l.closer.Close()
|
||||
}
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoggerWritesJSONAndUsesRestrictedPermissions(t *testing.T) {
|
||||
@@ -38,3 +40,110 @@ func TestLoggerWritesJSONAndUsesRestrictedPermissions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDailyFileWriterRotatesCapsAndRemovesExpiredLogs(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
now := time.Date(2026, 8, 31, 8, 0, 0, 0, time.Local)
|
||||
oldPath := filepath.Join(directory, "scert-20260701.log")
|
||||
if err := os.WriteFile(oldPath, []byte("old"), 0o600); err != nil {
|
||||
t.Fatalf("写入过期日志失败:%v", err)
|
||||
}
|
||||
oldTime := now.AddDate(0, 0, -60)
|
||||
if err := os.Chtimes(oldPath, oldTime, oldTime); err != nil {
|
||||
t.Fatalf("设置过期日志时间失败:%v", err)
|
||||
}
|
||||
|
||||
writer, err := newDailyFileWriterWithOptions(directory, 32, 30, func() time.Time { return now })
|
||||
if err != nil {
|
||||
t.Fatalf("初始化日志轮转器失败:%v", err)
|
||||
}
|
||||
if _, err := writer.Write([]byte("first-day-entry\n")); err != nil {
|
||||
t.Fatalf("写入首日日志失败:%v", err)
|
||||
}
|
||||
if _, err := os.Stat(oldPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("过期日志未被清理:%v", err)
|
||||
}
|
||||
|
||||
now = now.Add(24 * time.Hour)
|
||||
if _, err := writer.Write([]byte("second-day-entry\n")); err != nil {
|
||||
t.Fatalf("写入次日日志失败:%v", err)
|
||||
}
|
||||
rotatedPath := filepath.Join(directory, "scert-20260831.log")
|
||||
if data, err := os.ReadFile(rotatedPath); err != nil || !strings.Contains(string(data), "first-day-entry") {
|
||||
t.Fatalf("首日日志未正确轮转:data=%q err=%v", data, err)
|
||||
}
|
||||
|
||||
activePath := filepath.Join(directory, "scert.log")
|
||||
before, err := os.Stat(activePath)
|
||||
if err != nil {
|
||||
t.Fatalf("读取活动日志状态失败:%v", err)
|
||||
}
|
||||
large := strings.Repeat("x", 64)
|
||||
if written, err := writer.Write([]byte(large)); err != nil || written != len(large) {
|
||||
t.Fatalf("超过容量的日志写入结果不正确:written=%d err=%v", written, err)
|
||||
}
|
||||
after, err := os.Stat(activePath)
|
||||
if err != nil {
|
||||
t.Fatalf("再次读取活动日志状态失败:%v", err)
|
||||
}
|
||||
if before.Size() != after.Size() || after.Size() > 32 {
|
||||
t.Fatalf("活动日志超过每日容量上限:before=%s after=%s", strconv.FormatInt(before.Size(), 10), strconv.FormatInt(after.Size(), 10))
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatalf("关闭日志轮转器失败:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDailyFileWriterRejectsSymlink(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Windows 普通用户环境通常无法创建符号链接")
|
||||
}
|
||||
directory := t.TempDir()
|
||||
target := filepath.Join(directory, "target.log")
|
||||
if err := os.WriteFile(target, []byte("target"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(target, filepath.Join(directory, "scert.log")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := newDailyFileWriter(directory); err == nil {
|
||||
t.Fatal("活动日志是符号链接时应拒绝启动")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDailyFileWriterKeepsDailyCapAfterRestart(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
now := time.Date(2026, 8, 31, 8, 0, 0, 0, time.Local)
|
||||
writer, err := newDailyFileWriterWithOptions(directory, 16, 30, func() time.Time { return now })
|
||||
if err != nil {
|
||||
t.Fatalf("初始化日志轮转器失败:%v", err)
|
||||
}
|
||||
if _, err := writer.Write([]byte("1234567890123456")); err != nil {
|
||||
t.Fatalf("写满每日日志容量失败:%v", err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatalf("关闭日志轮转器失败:%v", err)
|
||||
}
|
||||
|
||||
restarted, err := newDailyFileWriterWithOptions(directory, 16, 30, func() time.Time { return now })
|
||||
if err != nil {
|
||||
t.Fatalf("重启日志轮转器失败:%v", err)
|
||||
}
|
||||
if _, err := restarted.Write([]byte("should-not-be-written")); err != nil {
|
||||
t.Fatalf("容量用尽后写入结果错误:%v", err)
|
||||
}
|
||||
if err := restarted.Close(); err != nil {
|
||||
t.Fatalf("再次关闭日志轮转器失败:%v", err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(filepath.Join(directory, "scert.log"))
|
||||
if err != nil {
|
||||
t.Fatalf("读取活动日志状态失败:%v", err)
|
||||
}
|
||||
if info.Size() != 16 {
|
||||
t.Fatalf("服务重启后每日日志容量被绕过:%d", info.Size())
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(directory, "scert-20260831.log")); !os.IsNotExist(err) {
|
||||
t.Fatalf("同日重启不应轮转活动日志:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMaxDailyBytes int64 = 5 * 1024 * 1024
|
||||
defaultRetentionDays = 30
|
||||
)
|
||||
|
||||
var archivedLogPattern = regexp.MustCompile(`^scert-[0-9]{8}(?:-[0-9]{2})?\.log$`)
|
||||
|
||||
type dailyFileWriter struct {
|
||||
mu sync.Mutex
|
||||
directory string
|
||||
activePath string
|
||||
file *os.File
|
||||
day string
|
||||
size int64
|
||||
maxDailyBytes int64
|
||||
retentionDays int
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func newDailyFileWriter(directory string) (*dailyFileWriter, error) {
|
||||
return newDailyFileWriterWithOptions(directory, defaultMaxDailyBytes, defaultRetentionDays, time.Now)
|
||||
}
|
||||
|
||||
func newDailyFileWriterWithOptions(directory string, maxDailyBytes int64, retentionDays int, now func() time.Time) (*dailyFileWriter, error) {
|
||||
if maxDailyBytes < 1 {
|
||||
return nil, errors.New("日志每日容量上限必须大于 0")
|
||||
}
|
||||
if retentionDays < 1 {
|
||||
return nil, errors.New("日志保留天数必须大于 0")
|
||||
}
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
if err := os.MkdirAll(directory, 0o750); err != nil {
|
||||
return nil, fmt.Errorf("创建日志目录失败:%w", err)
|
||||
}
|
||||
if err := os.Chmod(directory, 0o750); err != nil {
|
||||
return nil, fmt.Errorf("设置日志目录权限失败:%w", err)
|
||||
}
|
||||
writer := &dailyFileWriter{
|
||||
directory: directory,
|
||||
activePath: filepath.Join(directory, "scert.log"),
|
||||
maxDailyBytes: maxDailyBytes,
|
||||
retentionDays: retentionDays,
|
||||
now: now,
|
||||
}
|
||||
if err := writer.open(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := writer.cleanupExpired(); err != nil {
|
||||
_ = writer.Close()
|
||||
return nil, err
|
||||
}
|
||||
return writer, nil
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) Write(value []byte) (int, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
|
||||
currentDay := logDay(w.now())
|
||||
if currentDay != w.day {
|
||||
if err := w.rotateLocked(w.day); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := w.openLocked(currentDay); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := w.cleanupExpiredLocked(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
if int64(len(value)) > w.maxDailyBytes-w.size {
|
||||
return len(value), nil
|
||||
}
|
||||
written, err := w.file.Write(value)
|
||||
w.size += int64(written)
|
||||
return written, err
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) Close() error {
|
||||
if w == nil {
|
||||
return nil
|
||||
}
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.file == nil {
|
||||
return nil
|
||||
}
|
||||
err := errors.Join(w.file.Sync(), w.file.Close())
|
||||
w.file = nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) open() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
currentDay := logDay(w.now())
|
||||
if info, err := os.Lstat(w.activePath); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return errors.New("活动日志路径必须是普通文件,不能使用符号链接")
|
||||
}
|
||||
fileDay := info.ModTime().Local().Format("20060102")
|
||||
if fileDay != currentDay {
|
||||
if err := w.rotateExistingLocked(fileDay); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("检查活动日志失败:%w", err)
|
||||
}
|
||||
return w.openLocked(currentDay)
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) openLocked(day string) error {
|
||||
file, err := os.OpenFile(w.activePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开活动日志失败:%w", err)
|
||||
}
|
||||
if err := file.Chmod(0o600); err != nil {
|
||||
_ = file.Close()
|
||||
return fmt.Errorf("设置活动日志权限失败:%w", err)
|
||||
}
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
_ = file.Close()
|
||||
return fmt.Errorf("读取活动日志状态失败:%w", err)
|
||||
}
|
||||
w.file = file
|
||||
w.day = day
|
||||
w.size = info.Size()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) rotateLocked(day string) error {
|
||||
if w.file != nil {
|
||||
if err := errors.Join(w.file.Sync(), w.file.Close()); err != nil {
|
||||
return fmt.Errorf("关闭活动日志失败:%w", err)
|
||||
}
|
||||
w.file = nil
|
||||
}
|
||||
return w.rotateExistingLocked(day)
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) rotateExistingLocked(day string) error {
|
||||
if _, err := os.Lstat(w.activePath); errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("检查待轮转日志失败:%w", err)
|
||||
}
|
||||
if day == "" {
|
||||
day = logDay(w.now())
|
||||
}
|
||||
for index := 0; index < 100; index++ {
|
||||
name := fmt.Sprintf("scert-%s.log", day)
|
||||
if index > 0 {
|
||||
name = fmt.Sprintf("scert-%s-%02d.log", day, index)
|
||||
}
|
||||
destination := filepath.Join(w.directory, name)
|
||||
if _, err := os.Lstat(destination); errors.Is(err, os.ErrNotExist) {
|
||||
if err := os.Rename(w.activePath, destination); err != nil {
|
||||
return fmt.Errorf("轮转活动日志失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("检查轮转日志失败:%w", err)
|
||||
}
|
||||
}
|
||||
return errors.New("同一天的日志轮转文件数量已达到上限")
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) cleanupExpired() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return w.cleanupExpiredLocked()
|
||||
}
|
||||
|
||||
func (w *dailyFileWriter) cleanupExpiredLocked() error {
|
||||
entries, err := os.ReadDir(w.directory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取日志目录失败:%w", err)
|
||||
}
|
||||
cutoff := w.now().AddDate(0, 0, -w.retentionDays)
|
||||
for _, entry := range entries {
|
||||
if !archivedLogPattern.MatchString(entry.Name()) {
|
||||
continue
|
||||
}
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取历史日志状态失败:%w", err)
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() || !info.ModTime().Before(cutoff) {
|
||||
continue
|
||||
}
|
||||
if err := os.Remove(filepath.Join(w.directory, entry.Name())); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return fmt.Errorf("删除过期日志失败:%w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func logDay(value time.Time) string {
|
||||
return value.Local().Format("20060102")
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var ErrLocked = errors.New("进程锁已被占用")
|
||||
|
||||
type Lock struct {
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func Acquire(path string) (*Lock, error) {
|
||||
path = filepath.Clean(path)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return nil, fmt.Errorf("创建进程锁目录失败:%w", err)
|
||||
}
|
||||
if info, err := os.Lstat(path); err == nil {
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return nil, errors.New("进程锁路径必须是普通文件")
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("检查进程锁失败:%w", err)
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("打开进程锁失败:%w", err)
|
||||
}
|
||||
if err := lockFile(file); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Chmod(path, 0o600); err != nil {
|
||||
_ = unlockFile(file)
|
||||
_ = file.Close()
|
||||
return nil, fmt.Errorf("设置进程锁权限失败:%w", err)
|
||||
}
|
||||
return &Lock{file: file}, nil
|
||||
}
|
||||
|
||||
func (lock *Lock) Close() error {
|
||||
if lock == nil || lock.file == nil {
|
||||
return nil
|
||||
}
|
||||
file := lock.file
|
||||
lock.file = nil
|
||||
return errors.Join(unlockFile(file), file.Close())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAcquirePreventsConcurrentProcessAccess(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "scert.lock")
|
||||
first, err := Acquire(path)
|
||||
if err != nil {
|
||||
t.Fatalf("获取第一个进程锁失败:%v", err)
|
||||
}
|
||||
defer first.Close()
|
||||
|
||||
if _, err := Acquire(path); !errors.Is(err, ErrLocked) {
|
||||
t.Fatalf("并发进程锁未被拒绝:%v", err)
|
||||
}
|
||||
if err := first.Close(); err != nil {
|
||||
t.Fatalf("释放第一个进程锁失败:%v", err)
|
||||
}
|
||||
second, err := Acquire(path)
|
||||
if err != nil {
|
||||
t.Fatalf("释放后无法重新获取进程锁:%v", err)
|
||||
}
|
||||
if err := second.Close(); err != nil {
|
||||
t.Fatalf("释放第二个进程锁失败:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//go:build !windows
|
||||
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func lockFile(file *os.File) error {
|
||||
err := unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB)
|
||||
if errors.Is(err, unix.EWOULDBLOCK) || errors.Is(err, unix.EAGAIN) {
|
||||
return ErrLocked
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unlockFile(file *os.File) error {
|
||||
if err := unix.Flock(int(file.Fd()), unix.LOCK_UN); err != nil {
|
||||
return fmt.Errorf("释放进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//go:build windows
|
||||
|
||||
package processlock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func lockFile(file *os.File) error {
|
||||
overlapped := new(windows.Overlapped)
|
||||
err := windows.LockFileEx(
|
||||
windows.Handle(file.Fd()),
|
||||
windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
overlapped,
|
||||
)
|
||||
if errors.Is(err, windows.ERROR_LOCK_VIOLATION) {
|
||||
return ErrLocked
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unlockFile(file *os.File) error {
|
||||
overlapped := new(windows.Overlapped)
|
||||
if err := windows.UnlockFileEx(windows.Handle(file.Fd()), 0, 1, 0, overlapped); err != nil {
|
||||
return fmt.Errorf("释放进程锁失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package releasearchive
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Format string
|
||||
|
||||
const (
|
||||
FormatTarGz Format = "tar.gz"
|
||||
FormatZip Format = "zip"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Source string
|
||||
Destination string
|
||||
Format Format
|
||||
Executables []string
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
path string
|
||||
relative string
|
||||
info fs.FileInfo
|
||||
}
|
||||
|
||||
var archiveTime = time.Unix(0, 0).UTC()
|
||||
|
||||
func Create(options Options) error {
|
||||
_, destination, executables, entries, err := prepare(options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureExecutableEntries(executables, entries); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(destination, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
return fmt.Errorf("发布归档已存在,拒绝覆盖:%s", destination)
|
||||
}
|
||||
return fmt.Errorf("创建发布归档失败:%w", err)
|
||||
}
|
||||
completed := false
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
if !completed {
|
||||
_ = os.Remove(destination)
|
||||
}
|
||||
}()
|
||||
|
||||
switch options.Format {
|
||||
case FormatTarGz:
|
||||
err = writeTarGz(file, entries, executables)
|
||||
case FormatZip:
|
||||
err = writeZip(file, entries, executables)
|
||||
default:
|
||||
err = fmt.Errorf("不支持的发布归档格式:%s", options.Format)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.Sync(); err != nil {
|
||||
return fmt.Errorf("同步发布归档失败:%w", err)
|
||||
}
|
||||
if err := file.Close(); err != nil {
|
||||
return fmt.Errorf("关闭发布归档失败:%w", err)
|
||||
}
|
||||
completed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepare(options Options) (string, string, map[string]struct{}, []entry, error) {
|
||||
if options.Source == "" || options.Destination == "" {
|
||||
return "", "", nil, nil, errors.New("发布源目录和归档路径不能为空")
|
||||
}
|
||||
source, err := filepath.Abs(options.Source)
|
||||
if err != nil {
|
||||
return "", "", nil, nil, fmt.Errorf("解析发布源目录失败:%w", err)
|
||||
}
|
||||
destination, err := filepath.Abs(options.Destination)
|
||||
if err != nil {
|
||||
return "", "", nil, nil, fmt.Errorf("解析发布归档路径失败:%w", err)
|
||||
}
|
||||
info, err := os.Lstat(source)
|
||||
if err != nil {
|
||||
return "", "", nil, nil, fmt.Errorf("检查发布源目录失败:%w", err)
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", "", nil, nil, errors.New("发布源路径必须是普通目录")
|
||||
}
|
||||
if inside(source, destination) {
|
||||
return "", "", nil, nil, errors.New("发布归档不能位于发布源目录内")
|
||||
}
|
||||
if _, err := os.Lstat(destination); err == nil {
|
||||
return "", "", nil, nil, fmt.Errorf("发布归档已存在,拒绝覆盖:%s", destination)
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return "", "", nil, nil, fmt.Errorf("检查发布归档失败:%w", err)
|
||||
}
|
||||
|
||||
executables := make(map[string]struct{}, len(options.Executables))
|
||||
for _, name := range options.Executables {
|
||||
normalized, err := normalizeRelative(name)
|
||||
if err != nil {
|
||||
return "", "", nil, nil, fmt.Errorf("可执行文件路径无效:%w", err)
|
||||
}
|
||||
executables[normalized] = struct{}{}
|
||||
}
|
||||
|
||||
var entries []entry
|
||||
err = filepath.WalkDir(source, func(path string, item fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if path == source {
|
||||
return nil
|
||||
}
|
||||
if item.Type()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("发布目录不能包含符号链接:%s", path)
|
||||
}
|
||||
info, err := item.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() && !info.Mode().IsRegular() {
|
||||
return fmt.Errorf("发布目录包含不支持的文件类型:%s", path)
|
||||
}
|
||||
relative, err := filepath.Rel(source, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
entries = append(entries, entry{path: path, relative: filepath.ToSlash(relative), info: info})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", nil, nil, fmt.Errorf("检查发布目录失败:%w", err)
|
||||
}
|
||||
sort.Slice(entries, func(i, j int) bool { return entries[i].relative < entries[j].relative })
|
||||
return source, destination, executables, entries, nil
|
||||
}
|
||||
|
||||
func ensureExecutableEntries(executables map[string]struct{}, entries []entry) error {
|
||||
for executable := range executables {
|
||||
found := false
|
||||
for _, item := range entries {
|
||||
if item.relative == executable && item.info.Mode().IsRegular() {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("发布目录缺少可执行文件:%s", executable)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeTarGz(output io.Writer, entries []entry, executables map[string]struct{}) error {
|
||||
gzipWriter := gzip.NewWriter(output)
|
||||
gzipWriter.Header.ModTime = archiveTime
|
||||
gzipWriter.Header.OS = 255
|
||||
tarWriter := tar.NewWriter(gzipWriter)
|
||||
|
||||
for _, item := range entries {
|
||||
header, err := tar.FileInfoHeader(item.info, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建 tar 文件头失败:%w", err)
|
||||
}
|
||||
header.Name = item.relative
|
||||
header.Uid = 0
|
||||
header.Gid = 0
|
||||
header.Uname = "root"
|
||||
header.Gname = "root"
|
||||
header.ModTime = archiveTime
|
||||
header.AccessTime = archiveTime
|
||||
header.ChangeTime = archiveTime
|
||||
header.Mode = archiveMode(item, executables)
|
||||
if item.info.IsDir() && !strings.HasSuffix(header.Name, "/") {
|
||||
header.Name += "/"
|
||||
}
|
||||
if err := tarWriter.WriteHeader(header); err != nil {
|
||||
return fmt.Errorf("写入 tar 文件头失败:%w", err)
|
||||
}
|
||||
if item.info.Mode().IsRegular() {
|
||||
if err := copyEntry(tarWriter, item.path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := tarWriter.Close(); err != nil {
|
||||
return fmt.Errorf("关闭 tar 归档失败:%w", err)
|
||||
}
|
||||
if err := gzipWriter.Close(); err != nil {
|
||||
return fmt.Errorf("关闭 gzip 归档失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeZip(output io.Writer, entries []entry, executables map[string]struct{}) error {
|
||||
zipWriter := zip.NewWriter(output)
|
||||
for _, item := range entries {
|
||||
header, err := zip.FileInfoHeader(item.info)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建 zip 文件头失败:%w", err)
|
||||
}
|
||||
header.Name = item.relative
|
||||
header.Modified = archiveTime
|
||||
header.SetMode(fs.FileMode(archiveMode(item, executables)))
|
||||
if item.info.IsDir() && !strings.HasSuffix(header.Name, "/") {
|
||||
header.Name += "/"
|
||||
}
|
||||
if item.info.Mode().IsRegular() {
|
||||
header.Method = zip.Deflate
|
||||
}
|
||||
writer, err := zipWriter.CreateHeader(header)
|
||||
if err != nil {
|
||||
return fmt.Errorf("写入 zip 文件头失败:%w", err)
|
||||
}
|
||||
if item.info.Mode().IsRegular() {
|
||||
if err := copyEntry(writer, item.path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := zipWriter.Close(); err != nil {
|
||||
return fmt.Errorf("关闭 zip 归档失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyEntry(destination io.Writer, path string) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开发布文件失败:%w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
if _, err := io.Copy(destination, file); err != nil {
|
||||
return fmt.Errorf("写入发布文件失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func archiveMode(item entry, executables map[string]struct{}) int64 {
|
||||
if item.info.IsDir() {
|
||||
return 0o755
|
||||
}
|
||||
if _, ok := executables[item.relative]; ok {
|
||||
return 0o755
|
||||
}
|
||||
return 0o644
|
||||
}
|
||||
|
||||
func normalizeRelative(path string) (string, error) {
|
||||
path = filepath.ToSlash(filepath.Clean(path))
|
||||
if path == "." || path == "" || strings.HasPrefix(path, "/") || path == ".." || strings.HasPrefix(path, "../") {
|
||||
return "", errors.New("路径必须位于发布目录内")
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func inside(parent, child string) bool {
|
||||
relative, err := filepath.Rel(parent, child)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return relative == "." || (relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)))
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package releasearchive
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateTarGzPreservesReleaseModes(t *testing.T) {
|
||||
source := createFixture(t)
|
||||
destination := filepath.Join(t.TempDir(), "Scert-test.tar.gz")
|
||||
if err := Create(Options{
|
||||
Source: source, Destination: destination, Format: FormatTarGz,
|
||||
Executables: []string{"install-linux.sh", "scert", "scert-admin"},
|
||||
}); err != nil {
|
||||
t.Fatalf("创建 Linux 发布归档失败:%v", err)
|
||||
}
|
||||
|
||||
file, err := os.Open(destination)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
gzipReader, err := gzip.NewReader(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
tarReader := tar.NewReader(gzipReader)
|
||||
modes := map[string]int64{}
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
modes[strings.TrimSuffix(header.Name, "/")] = header.Mode
|
||||
}
|
||||
assertMode(t, modes, "install-linux.sh", 0o755)
|
||||
assertMode(t, modes, "scert", 0o755)
|
||||
assertMode(t, modes, "scert-admin", 0o755)
|
||||
assertMode(t, modes, "web/index.html", 0o644)
|
||||
assertMode(t, modes, "web", 0o755)
|
||||
}
|
||||
|
||||
func TestCreateZipContainsReleaseFiles(t *testing.T) {
|
||||
source := createFixture(t)
|
||||
destination := filepath.Join(t.TempDir(), "Scert-test.zip")
|
||||
if err := Create(Options{Source: source, Destination: destination, Format: FormatZip}); err != nil {
|
||||
t.Fatalf("创建 Windows 发布归档失败:%v", err)
|
||||
}
|
||||
reader, err := zip.OpenReader(destination)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reader.Close()
|
||||
found := false
|
||||
for _, item := range reader.File {
|
||||
if item.Name == "web/index.html" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("Windows 发布归档缺少 Web 入口")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRejectsExistingArchive(t *testing.T) {
|
||||
source := createFixture(t)
|
||||
destination := filepath.Join(t.TempDir(), "existing.tar.gz")
|
||||
if err := os.WriteFile(destination, []byte("keep"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := Create(Options{Source: source, Destination: destination, Format: FormatTarGz})
|
||||
if err == nil || !strings.Contains(err.Error(), "拒绝覆盖") {
|
||||
t.Fatalf("已有发布归档未被拒绝:%v", err)
|
||||
}
|
||||
data, readErr := os.ReadFile(destination)
|
||||
if readErr != nil || string(data) != "keep" {
|
||||
t.Fatalf("已有发布归档被修改:data=%q err=%v", data, readErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRejectsMissingExecutable(t *testing.T) {
|
||||
source := createFixture(t)
|
||||
err := Create(Options{
|
||||
Source: source, Destination: filepath.Join(t.TempDir(), "missing.tar.gz"),
|
||||
Format: FormatTarGz, Executables: []string{"missing"},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "缺少可执行文件") {
|
||||
t.Fatalf("缺少可执行文件未被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRejectsSymlink(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Windows 测试环境通常不允许普通用户创建符号链接")
|
||||
}
|
||||
source := createFixture(t)
|
||||
if err := os.Symlink(filepath.Join(source, "scert"), filepath.Join(source, "linked")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := Create(Options{
|
||||
Source: source, Destination: filepath.Join(t.TempDir(), "linked.tar.gz"), Format: FormatTarGz,
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "符号链接") {
|
||||
t.Fatalf("发布目录符号链接未被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func createFixture(t *testing.T) string {
|
||||
t.Helper()
|
||||
source := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(source, "web"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"install-linux.sh", "scert", "scert-admin"} {
|
||||
if err := os.WriteFile(filepath.Join(source, name), []byte(name), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(source, "web", "index.html"), []byte("index"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
func assertMode(t *testing.T, modes map[string]int64, name string, expected int64) {
|
||||
t.Helper()
|
||||
if actual, ok := modes[name]; !ok || actual != expected {
|
||||
t.Fatalf("归档权限不正确 %s:actual=%o expected=%o", name, actual, expected)
|
||||
}
|
||||
}
|
||||
@@ -52,3 +52,21 @@ func (r *SQLAuditRepository) CreateAuditLog(ctx context.Context, item AuditLog)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SQLAuditRepository) PruneExpiredAuditLogs(ctx context.Context, before time.Time) (int64, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return 0, errors.New("数据库连接不能为空")
|
||||
}
|
||||
if before.IsZero() {
|
||||
return 0, errors.New("审计日志清理时间不能为空")
|
||||
}
|
||||
result, err := r.db.ExecContext(ctx, `DELETE FROM audit_logs WHERE created_at < ?`, before.UTC())
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("清理过期审计日志失败:%w", err)
|
||||
}
|
||||
count, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("读取审计日志清理数量失败:%w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
@@ -33,3 +34,53 @@ func TestSQLAuditRepositoryCreatesLog(t *testing.T) {
|
||||
t.Fatalf("审计日志数量不正确:%d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLAuditRepositoryPrunesOnlyExpiredLogs(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbConfig := config.Default(dir).Database
|
||||
dbConfig.DSN = filepath.Join(dir, "data", "audit-prune.db")
|
||||
db, err := database.Open(context.Background(), dbConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("打开数据库失败:%v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := database.Migrate(context.Background(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatalf("迁移数据库失败:%v", err)
|
||||
}
|
||||
|
||||
repo := NewSQLAuditRepository(db.DB)
|
||||
cutoff := time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC)
|
||||
items := []AuditLog{
|
||||
{Action: "expired", ResourceType: "test", Status: "success", CreatedAt: cutoff.Add(-time.Second)},
|
||||
{Action: "boundary", ResourceType: "test", Status: "success", CreatedAt: cutoff},
|
||||
{Action: "retained", ResourceType: "test", Status: "success", CreatedAt: cutoff.Add(time.Second)},
|
||||
}
|
||||
for _, item := range items {
|
||||
if err := repo.CreateAuditLog(context.Background(), item); err != nil {
|
||||
t.Fatalf("写入审计日志失败:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
deleted, err := repo.PruneExpiredAuditLogs(context.Background(), cutoff)
|
||||
if err != nil {
|
||||
t.Fatalf("清理过期审计日志失败:%v", err)
|
||||
}
|
||||
if deleted != 1 {
|
||||
t.Fatalf("清理数量不正确:%d", deleted)
|
||||
}
|
||||
var count int
|
||||
if err := db.DB.QueryRow(`SELECT COUNT(*) FROM audit_logs`).Scan(&count); err != nil {
|
||||
t.Fatalf("统计保留日志失败:%v", err)
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("保留日志数量不正确:%d", count)
|
||||
}
|
||||
for _, action := range []string{"boundary", "retained"} {
|
||||
if err := db.DB.QueryRow(`SELECT COUNT(*) FROM audit_logs WHERE action = ?`, action).Scan(&count); err != nil {
|
||||
t.Fatalf("检查保留日志失败:%v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("应保留审计日志 %s", action)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,10 @@ func (r *SQLServerRepository) CreateServer(ctx context.Context, server deploymen
|
||||
if err != nil {
|
||||
return fmt.Errorf("编码 reload 命令失败:%w", err)
|
||||
}
|
||||
validationCommand, err := json.Marshal(server.ValidationCommand)
|
||||
if err != nil {
|
||||
return fmt.Errorf("编码配置校验命令失败:%w", err)
|
||||
}
|
||||
createdAt := server.CreatedAt
|
||||
if createdAt.IsZero() {
|
||||
createdAt = time.Now().UTC()
|
||||
@@ -49,9 +53,9 @@ func (r *SQLServerRepository) CreateServer(ctx context.Context, server deploymen
|
||||
updatedAt = createdAt
|
||||
}
|
||||
_, err = r.db.ExecContext(ctx, `INSERT INTO servers
|
||||
(id, name, address, port, username, private_key_ref, host_key_fingerprint, certificate_path, private_key_path, certificate_chain_path, web_server, reload_command_json, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, server.ID, server.Name, server.Address, server.Port, server.Username, server.PrivateKeyRef,
|
||||
server.HostKeyFingerprint, server.CertificatePath, server.PrivateKeyPath, server.CertificateChainPath, server.WebServer, string(command), createdAt, updatedAt)
|
||||
(id, name, address, port, username, private_key_ref, host_key_fingerprint, certificate_path, private_key_path, certificate_chain_path, web_server, validation_command_json, reload_command_json, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, server.ID, server.Name, server.Address, server.Port, server.Username, server.PrivateKeyRef,
|
||||
server.HostKeyFingerprint, server.CertificatePath, server.PrivateKeyPath, server.CertificateChainPath, server.WebServer, string(validationCommand), string(command), createdAt, updatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("保存服务器失败:%w", err)
|
||||
}
|
||||
@@ -67,8 +71,16 @@ func (r *SQLServerRepository) UpdateServer(ctx context.Context, server deploymen
|
||||
if err != nil {
|
||||
return fmt.Errorf("编码 reload 命令失败:%w", err)
|
||||
}
|
||||
result, err := r.db.ExecContext(ctx, `UPDATE servers SET name = ?, address = ?, port = ?, username = ?, private_key_ref = ?, host_key_fingerprint = ?, certificate_path = ?, private_key_path = ?, certificate_chain_path = ?, web_server = ?, reload_command_json = ?, updated_at = ? WHERE id = ?`,
|
||||
server.Name, server.Address, server.Port, server.Username, server.PrivateKeyRef, server.HostKeyFingerprint, server.CertificatePath, server.PrivateKeyPath, server.CertificateChainPath, server.WebServer, string(command), time.Now().UTC(), server.ID)
|
||||
validationCommand, err := json.Marshal(server.ValidationCommand)
|
||||
if err != nil {
|
||||
return fmt.Errorf("编码配置校验命令失败:%w", err)
|
||||
}
|
||||
updatedAt := server.UpdatedAt
|
||||
if updatedAt.IsZero() {
|
||||
updatedAt = time.Now().UTC()
|
||||
}
|
||||
result, err := r.db.ExecContext(ctx, `UPDATE servers SET name = ?, address = ?, port = ?, username = ?, private_key_ref = ?, host_key_fingerprint = ?, certificate_path = ?, private_key_path = ?, certificate_chain_path = ?, web_server = ?, validation_command_json = ?, reload_command_json = ?, updated_at = ? WHERE id = ?`,
|
||||
server.Name, server.Address, server.Port, server.Username, server.PrivateKeyRef, server.HostKeyFingerprint, server.CertificatePath, server.PrivateKeyPath, server.CertificateChainPath, server.WebServer, string(validationCommand), string(command), updatedAt, server.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("更新服务器失败:%w", err)
|
||||
}
|
||||
@@ -218,8 +230,8 @@ func (r *SQLServerRepository) ReplaceDomainServers(ctx context.Context, domainID
|
||||
return nil
|
||||
}
|
||||
|
||||
const serverSelect = `SELECT id, name, address, port, username, private_key_ref, host_key_fingerprint, certificate_path, private_key_path, certificate_chain_path, web_server, reload_command_json, created_at, updated_at FROM servers`
|
||||
const serverSelectQualified = `SELECT servers.id, servers.name, servers.address, servers.port, servers.username, servers.private_key_ref, servers.host_key_fingerprint, servers.certificate_path, servers.private_key_path, servers.certificate_chain_path, servers.web_server, servers.reload_command_json, servers.created_at, servers.updated_at FROM servers`
|
||||
const serverSelect = `SELECT id, name, address, port, username, private_key_ref, host_key_fingerprint, certificate_path, private_key_path, certificate_chain_path, web_server, validation_command_json, reload_command_json, created_at, updated_at FROM servers`
|
||||
const serverSelectQualified = `SELECT servers.id, servers.name, servers.address, servers.port, servers.username, servers.private_key_ref, servers.host_key_fingerprint, servers.certificate_path, servers.private_key_path, servers.certificate_chain_path, servers.web_server, servers.validation_command_json, servers.reload_command_json, servers.created_at, servers.updated_at FROM servers`
|
||||
|
||||
type rowScanner interface {
|
||||
Scan(dest ...any) error
|
||||
@@ -228,14 +240,18 @@ type rowScanner interface {
|
||||
func scanServer(row rowScanner) (deployment.Server, error) {
|
||||
var server deployment.Server
|
||||
var webServer string
|
||||
var validationCommandJSON string
|
||||
var commandJSON string
|
||||
if err := row.Scan(&server.ID, &server.Name, &server.Address, &server.Port, &server.Username, &server.PrivateKeyRef, &server.HostKeyFingerprint, &server.CertificatePath, &server.PrivateKeyPath, &server.CertificateChainPath, &webServer, &commandJSON, &server.CreatedAt, &server.UpdatedAt); err != nil {
|
||||
if err := row.Scan(&server.ID, &server.Name, &server.Address, &server.Port, &server.Username, &server.PrivateKeyRef, &server.HostKeyFingerprint, &server.CertificatePath, &server.PrivateKeyPath, &server.CertificateChainPath, &webServer, &validationCommandJSON, &commandJSON, &server.CreatedAt, &server.UpdatedAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return deployment.Server{}, sql.ErrNoRows
|
||||
}
|
||||
return deployment.Server{}, fmt.Errorf("读取服务器失败:%w", err)
|
||||
}
|
||||
server.WebServer = deployment.WebServer(webServer)
|
||||
if err := json.Unmarshal([]byte(validationCommandJSON), &server.ValidationCommand); err != nil {
|
||||
return deployment.Server{}, fmt.Errorf("解析配置校验命令失败:%w", err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(commandJSON), &server.ReloadCommand); err != nil {
|
||||
return deployment.Server{}, fmt.Errorf("解析 reload 命令失败:%w", err)
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestServerRepositoryCRUDAndDomainAssociations(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("读取服务器失败:%v", err)
|
||||
}
|
||||
if loaded.Name != server.Name || loaded.ReloadCommand[0] != "nginx" || loaded.CreatedAt.IsZero() {
|
||||
if loaded.Name != server.Name || loaded.ValidationCommand[0] != "nginx" || loaded.ValidationCommand[1] != "-t" || loaded.ReloadCommand[0] != "nginx" || loaded.CreatedAt.IsZero() {
|
||||
t.Fatalf("服务器内容错误:%+v", loaded)
|
||||
}
|
||||
server.Name = "生产服务器-更新"
|
||||
@@ -45,6 +45,13 @@ func TestServerRepositoryCRUDAndDomainAssociations(t *testing.T) {
|
||||
if err := repo.UpdateServer(context.Background(), server); err != nil {
|
||||
t.Fatalf("更新服务器失败:%v", err)
|
||||
}
|
||||
updated, err := repo.GetServerByID(context.Background(), server.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("load updated server: %v", err)
|
||||
}
|
||||
if updated.CreatedAt.IsZero() || updated.UpdatedAt.IsZero() || updated.UpdatedAt.Before(updated.CreatedAt) {
|
||||
t.Fatalf("updated server timestamps are invalid: %+v", updated)
|
||||
}
|
||||
if err := repo.ReplaceDomainServers(context.Background(), "domain-1", []string{server.ID, server.ID}); err != nil {
|
||||
t.Fatalf("保存域名服务器关联失败:%v", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user