迁移测试并禁止私钥认证
This commit is contained in:
1 parent
48fc5eb395
commit
e6478ab303
18 files changed
+160
-475
No files matched your search
@@ -150,7 +150,7 @@ func checkProjectLifecycle(t *testing.T, makeConfig func(*testing.T) config.Data
|
||||
}
|
||||
case "server":
|
||||
resource := deployment.Server{ID: "server-a", Name: "项目服务器", Address: "192.0.2.10", Port: 22, Username: "deploy",
|
||||
PrivateKeyRef: "secret://ssh/test", HostKeyFingerprint: "SHA256:test", CertificatePath: "/test/cert.pem",
|
||||
AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/test", HostKeyFingerprint: "SHA256:test", CertificatePath: "/test/cert.pem",
|
||||
PrivateKeyPath: "/test/key.pem", CertificateChainPath: "/test/chain.pem", WebServer: deployment.WebServerNginx}
|
||||
resource.ProjectID = item.ID
|
||||
if err := NewSQLServerRepository(db, driver).CreateServer(t.Context(), resource); err != nil {
|
||||
|
||||
@@ -61,7 +61,7 @@ func checkProjectResources(t *testing.T, cfg config.DatabaseConfig, admin *sql.D
|
||||
server := deployment.Server{
|
||||
ResourceScope: domain.ResourceScope{ProjectID: item.ProjectID},
|
||||
ID: "server-" + id, Name: "资源服务器", Address: "192.0.2.10", Port: 22, Username: "deploy",
|
||||
PrivateKeyRef: "secret://ssh/fixture", HostKeyFingerprint: "SHA256:test",
|
||||
AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/fixture", HostKeyFingerprint: "SHA256:test",
|
||||
CertificatePath: "/ssl/cert.pem", PrivateKeyPath: "/ssl/key.pem", WebServer: deployment.WebServerNginx,
|
||||
}
|
||||
if err := servers.CreateServer(t.Context(), server); err != nil {
|
||||
|
||||
@@ -55,7 +55,7 @@ func checkResourceProjects(t *testing.T, cfg config.DatabaseConfig) {
|
||||
server := deployment.Server{
|
||||
ResourceScope: scope,
|
||||
ID: "server-a", Name: "项目服务器", Address: "192.0.2.10", Port: 22, Username: "deploy",
|
||||
PrivateKeyRef: "secret://ssh/test", HostKeyFingerprint: "SHA256:test",
|
||||
AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/test", HostKeyFingerprint: "SHA256:test",
|
||||
CertificatePath: "/test/cert.pem", PrivateKeyPath: "/test/key.pem",
|
||||
CertificateChainPath: "/test/chain.pem", WebServer: deployment.WebServerNginx,
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestServerRepositoryCRUDAndDomainAssociations(t *testing.T) {
|
||||
repo := NewSQLServerRepository(db.DB)
|
||||
server := deployment.Server{
|
||||
ID: "server-1", Name: "生产服务器", Address: "192.0.2.10", Port: 22, Username: "deploy",
|
||||
PrivateKeyRef: "secret://ssh/server-1", HostKeyFingerprint: "SHA256:test",
|
||||
AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/server-1", HostKeyFingerprint: "SHA256:test",
|
||||
CertificatePath: "/etc/scert/cert.pem", PrivateKeyPath: "/etc/scert/key.pem",
|
||||
CertificateChainPath: "/etc/scert/fullchain.pem", WebServer: deployment.WebServerNginx,
|
||||
}
|
||||
@@ -100,23 +100,15 @@ func TestServerRepositoryRejectsInvalidServer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerRepositoryReportsPrivateKeyReferenceUsage(t *testing.T) {
|
||||
func TestServerRepositoryRejectsPrivateKeyReference(t *testing.T) {
|
||||
db := openServerTestDatabase(t)
|
||||
defer db.Close()
|
||||
repo := NewSQLServerRepository(db.DB)
|
||||
server := validTestServer()
|
||||
server.ID = "server-private-key-usage"
|
||||
server.PrivateKeyRef = "secret://ssh/used-key"
|
||||
if err := repo.CreateServer(t.Context(), server); err != nil {
|
||||
t.Fatalf("创建测试服务器失败:%v", err)
|
||||
}
|
||||
used, err := repo.IsPrivateKeyRefUsed(t.Context(), server.PrivateKeyRef)
|
||||
if err != nil || !used {
|
||||
t.Fatalf("未识别已占用的 SSH 私钥引用:used=%t err=%v", used, err)
|
||||
}
|
||||
used, err = repo.IsPrivateKeyRefUsed(t.Context(), "secret://ssh/not-used")
|
||||
if err != nil || used {
|
||||
t.Fatalf("错误识别未占用的 SSH 私钥引用:used=%t err=%v", used, err)
|
||||
server.ID = "server-private-key-rejected"
|
||||
server.PrivateKeyRef = "secret://ssh/private-key"
|
||||
if err := repo.CreateServer(t.Context(), server); err == nil {
|
||||
t.Fatal("server repository must reject SSH private key references")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +125,7 @@ func TestReplaceDomainServersRejectsUnknownServerWithoutChangingAssociations(t *
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewSQLServerRepository(db.DB)
|
||||
server := deployment.Server{ID: "server-1", Name: "服务器", Address: "192.0.2.20", Username: "deploy", PrivateKeyRef: "secret://ssh/1", HostKeyFingerprint: "SHA256:test", CertificatePath: "/etc/scert/cert.pem", PrivateKeyPath: "/etc/scert/key.pem", CertificateChainPath: "/etc/scert/fullchain.pem", WebServer: deployment.WebServerNginx}
|
||||
server := deployment.Server{ID: "server-1", Name: "服务器", Address: "192.0.2.20", Username: "deploy", AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/1", HostKeyFingerprint: "SHA256:test", CertificatePath: "/etc/scert/cert.pem", PrivateKeyPath: "/etc/scert/key.pem", CertificateChainPath: "/etc/scert/fullchain.pem", WebServer: deployment.WebServerNginx}
|
||||
if err := repo.CreateServer(context.Background(), server); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -176,7 +168,7 @@ func openServerTestDatabase(t *testing.T) *database.Database {
|
||||
func validTestServer() deployment.Server {
|
||||
return deployment.Server{
|
||||
Name: "测试服务器", Address: "192.0.2.70", Port: 22, Username: "deploy",
|
||||
PrivateKeyRef: "secret://ssh/test", HostKeyFingerprint: "SHA256:test",
|
||||
AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/test", HostKeyFingerprint: "SHA256:test",
|
||||
CertificatePath: "/etc/scert/cert.pem", PrivateKeyPath: "/etc/scert/key.pem",
|
||||
CertificateChainPath: "/etc/scert/fullchain.pem", WebServer: deployment.WebServerNginx,
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func checkManyToManyBindings(t *testing.T, db, admin *sql.DB, members *SQLProjec
|
||||
server := deployment.Server{
|
||||
ResourceScope: domain.ResourceScope{ProjectID: "project-a"},
|
||||
ID: "shared-server-" + suffix, Name: suffix, Address: "192.0.2.20", Port: 22, Username: "deploy",
|
||||
PrivateKeyRef: "secret://ssh/fixture", HostKeyFingerprint: "SHA256:test", WebServer: deployment.WebServerNginx,
|
||||
AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/fixture", HostKeyFingerprint: "SHA256:test", WebServer: deployment.WebServerNginx,
|
||||
}
|
||||
if err := servers.CreateServer(t.Context(), server); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestRepositoryCRUD(t *testing.T) {
|
||||
t.Fatalf("域名列表不正确:%v,%+v", err, items)
|
||||
}
|
||||
serverRepo := NewSQLServerRepository(db.DB)
|
||||
server := deployment.Server{ID: "server-1", Name: "服务器", Address: "192.0.2.30", Username: "deploy", PrivateKeyRef: "secret://ssh/1", HostKeyFingerprint: "SHA256:test", CertificatePath: "/etc/scert/cert.pem", PrivateKeyPath: "/etc/scert/key.pem", CertificateChainPath: "/etc/scert/fullchain.pem", WebServer: deployment.WebServerNginx}
|
||||
server := deployment.Server{ID: "server-1", Name: "服务器", Address: "192.0.2.30", Username: "deploy", AuthMethod: deployment.SSHAuthPassword, PasswordRef: "secret://ssh/password/1", HostKeyFingerprint: "SHA256:test", CertificatePath: "/etc/scert/cert.pem", PrivateKeyPath: "/etc/scert/key.pem", CertificateChainPath: "/etc/scert/fullchain.pem", WebServer: deployment.WebServerNginx}
|
||||
if err := serverRepo.CreateServer(context.Background(), server); err != nil {
|
||||
t.Fatalf("创建关联服务器失败:%v", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user