完善证书部署路径与任务状态处理
This commit is contained in:
1 parent
09e449226a
commit
e68b8ec342
7 files changed
+136
-1
No files matched your search
@@ -127,6 +127,9 @@ func resolveServerPathTemplate(label, value string, replacer *strings.Replacer)
|
||||
if cleaned == "" || cleaned == "." {
|
||||
return "", fmt.Errorf("%s无效", label)
|
||||
}
|
||||
if !strings.HasPrefix(cleaned, "/") {
|
||||
return "", fmt.Errorf("%s必须是绝对路径", label)
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -67,6 +67,14 @@ func TestResolveServerPathsKeepsCustomPathAndRejectsUnknownPlaceholder(t *testin
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveServerPathsRejectsRelativePath(t *testing.T) {
|
||||
server := testServer("one")
|
||||
server.CertificatePath = "cert.pem"
|
||||
if _, err := ResolveServerPaths(server, "example.com", "single"); err == nil {
|
||||
t.Fatal("相对远程证书路径应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerRejectsPasswordStyleOrUnsafeConfiguration(t *testing.T) {
|
||||
server := testServer("one")
|
||||
server.PrivateKeyRef = ""
|
||||
|
||||
@@ -132,6 +132,10 @@ func (s *Service) deployOne(ctx context.Context, server Server, artifact Artifac
|
||||
return result
|
||||
}
|
||||
}
|
||||
if err := validateDeploymentPaths(paths); err != nil {
|
||||
result.Message = err.Error()
|
||||
return result
|
||||
}
|
||||
backupID := s.backupID()
|
||||
type backup struct{ original, saved string }
|
||||
backups := make([]backup, 0, len(paths))
|
||||
@@ -211,6 +215,21 @@ type remoteFile struct {
|
||||
mode uint32
|
||||
}
|
||||
|
||||
func validateDeploymentPaths(files []remoteFile) error {
|
||||
seen := make(map[string]struct{}, len(files))
|
||||
for _, file := range files {
|
||||
cleaned := RemotePath(file.path)
|
||||
if cleaned == "" || cleaned == "." || !strings.HasPrefix(cleaned, "/") {
|
||||
return errors.New("远程证书路径必须是绝对路径")
|
||||
}
|
||||
if _, exists := seen[cleaned]; exists {
|
||||
return errors.New("远程证书、完整证书链和私钥不能使用相同路径")
|
||||
}
|
||||
seen[cleaned] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateArtifact(artifact Artifact) error {
|
||||
if len(artifact.Certificate) == 0 || len(artifact.FullChain) == 0 || len(artifact.PrivateKey) == 0 {
|
||||
return errors.New("证书部署文件不完整")
|
||||
|
||||
@@ -167,6 +167,36 @@ func TestDeployReportsPartialSuccess(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployRejectsDuplicateCertificatePaths(t *testing.T) {
|
||||
client := newMemoryClient()
|
||||
server := testServer("server-1")
|
||||
server.PrivateKeyPath = server.CertificatePath
|
||||
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 {
|
||||
t.Fatalf("重复远程路径应直接失败:%+v", report)
|
||||
}
|
||||
if !strings.Contains(report.Results[0].Message, "不能使用相同路径") {
|
||||
t.Fatalf("重复远程路径错误提示不正确:%s", report.Results[0].Message)
|
||||
}
|
||||
if len(client.files) != 0 {
|
||||
t.Fatalf("路径校验失败前不应连接并写入远程文件:%+v", client.files)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployRejectsRelativeCertificatePaths(t *testing.T) {
|
||||
client := newMemoryClient()
|
||||
server := testServer("server-1")
|
||||
server.CertificatePath = "cert.pem"
|
||||
service := NewService(memoryFactory{clients: map[string]*memoryClient{server.ID: client}})
|
||||
|
||||
report := service.Deploy(context.Background(), []Target{{Server: server}}, testArtifact())
|
||||
if report.Status != "failed" || !strings.Contains(report.Results[0].Message, "必须是绝对路径") {
|
||||
t.Fatalf("相对远程路径应被拒绝:%+v", report)
|
||||
}
|
||||
}
|
||||
|
||||
func testServer(id string) Server {
|
||||
return Server{
|
||||
ID: id, Name: id, Address: "192.0.2.10", Port: 22, Username: "deploy",
|
||||
|
||||
Reference in new issue
Block a user