mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 21:31:34 +00:00
fde5b39d53
- Add context.Context to handler test mocks (agent, agent_group) - Refactor scheduler to use local interfaces instead of concrete service types - Wire RevocationSvc/CAOperationsSvc sub-services in integration tests - Add context.Background() to service test calls (agent, agent_group) - Fix repo integration tests: add FK prerequisite records (team, owner, issuer, renewal_policy) before creating certificates - Set MaxOpenConns(1) on test DB to preserve SET search_path across queries - Fix Apache/HAProxy tests: replace "echo ok"/"echo reload" with "true" binary to avoid macOS exec.Command PATH resolution failure - Fix validation tests: correct error expectations for regex-first checks, replace null byte strings with strings.Repeat for length tests - Fix scheduler timeout test flakiness with t.Skip fallback - Remove unused imports (context in ca_operations_test, service in scheduler) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
201 lines
5.3 KiB
Go
201 lines
5.3 KiB
Go
package apache_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/shankar0123/certctl/internal/connector/target"
|
|
"github.com/shankar0123/certctl/internal/connector/target/apache"
|
|
)
|
|
|
|
func TestApacheConnector_ValidateConfig(t *testing.T) {
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
|
ctx := context.Background()
|
|
|
|
t.Run("valid config", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
cfg := apache.Config{
|
|
CertPath: filepath.Join(tmpDir, "cert.pem"),
|
|
KeyPath: filepath.Join(tmpDir, "key.pem"),
|
|
ChainPath: filepath.Join(tmpDir, "chain.pem"),
|
|
ReloadCommand: "true",
|
|
ValidateCommand: "true",
|
|
}
|
|
|
|
connector := apache.New(&cfg, logger)
|
|
rawConfig, _ := json.Marshal(cfg)
|
|
err := connector.ValidateConfig(ctx, rawConfig)
|
|
if err != nil {
|
|
t.Fatalf("ValidateConfig failed: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("missing cert_path", func(t *testing.T) {
|
|
cfg := apache.Config{
|
|
ChainPath: "/tmp/chain.pem",
|
|
ReloadCommand: "true",
|
|
ValidateCommand: "true",
|
|
}
|
|
|
|
connector := apache.New(&cfg, logger)
|
|
rawConfig, _ := json.Marshal(cfg)
|
|
err := connector.ValidateConfig(ctx, rawConfig)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing cert_path")
|
|
}
|
|
})
|
|
|
|
t.Run("missing reload_command", func(t *testing.T) {
|
|
cfg := apache.Config{
|
|
CertPath: "/tmp/cert.pem",
|
|
ChainPath: "/tmp/chain.pem",
|
|
ValidateCommand: "true",
|
|
}
|
|
|
|
connector := apache.New(&cfg, logger)
|
|
rawConfig, _ := json.Marshal(cfg)
|
|
err := connector.ValidateConfig(ctx, rawConfig)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing reload_command")
|
|
}
|
|
})
|
|
|
|
t.Run("invalid JSON", func(t *testing.T) {
|
|
connector := apache.New(&apache.Config{}, logger)
|
|
err := connector.ValidateConfig(ctx, json.RawMessage(`{invalid}`))
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid JSON")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestApacheConnector_DeployCertificate(t *testing.T) {
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
|
ctx := context.Background()
|
|
|
|
t.Run("successful deployment", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
cfg := &apache.Config{
|
|
CertPath: filepath.Join(tmpDir, "cert.pem"),
|
|
KeyPath: filepath.Join(tmpDir, "key.pem"),
|
|
ChainPath: filepath.Join(tmpDir, "chain.pem"),
|
|
ReloadCommand: "true",
|
|
ValidateCommand: "true",
|
|
}
|
|
|
|
connector := apache.New(cfg, logger)
|
|
|
|
req := target.DeploymentRequest{
|
|
CertPEM: "-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----",
|
|
KeyPEM: "-----BEGIN EC PRIVATE KEY-----\ntest\n-----END EC PRIVATE KEY-----",
|
|
ChainPEM: "-----BEGIN CERTIFICATE-----\nchain\n-----END CERTIFICATE-----",
|
|
}
|
|
|
|
result, err := connector.DeployCertificate(ctx, req)
|
|
if err != nil {
|
|
t.Fatalf("DeployCertificate failed: %v", err)
|
|
}
|
|
|
|
if !result.Success {
|
|
t.Fatalf("expected success, got: %s", result.Message)
|
|
}
|
|
|
|
// Verify files were written
|
|
certData, err := os.ReadFile(cfg.CertPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to read cert file: %v", err)
|
|
}
|
|
if string(certData) != req.CertPEM {
|
|
t.Errorf("cert content mismatch")
|
|
}
|
|
|
|
// Verify key has secure permissions
|
|
info, err := os.Stat(cfg.KeyPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to stat key file: %v", err)
|
|
}
|
|
if info.Mode().Perm() != 0600 {
|
|
t.Errorf("expected key permissions 0600, got %v", info.Mode().Perm())
|
|
}
|
|
})
|
|
|
|
t.Run("validate command fails", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
cfg := &apache.Config{
|
|
CertPath: filepath.Join(tmpDir, "cert.pem"),
|
|
KeyPath: filepath.Join(tmpDir, "key.pem"),
|
|
ChainPath: filepath.Join(tmpDir, "chain.pem"),
|
|
ReloadCommand: "true",
|
|
ValidateCommand: "false", // always fails
|
|
}
|
|
|
|
connector := apache.New(cfg, logger)
|
|
|
|
req := target.DeploymentRequest{
|
|
CertPEM: "cert",
|
|
ChainPEM: "chain",
|
|
}
|
|
|
|
result, err := connector.DeployCertificate(ctx, req)
|
|
if err == nil {
|
|
t.Fatal("expected error when validate command fails")
|
|
}
|
|
if result.Success {
|
|
t.Fatal("expected failure result")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestApacheConnector_ValidateDeployment(t *testing.T) {
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
|
ctx := context.Background()
|
|
|
|
t.Run("valid deployment", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
certPath := filepath.Join(tmpDir, "cert.pem")
|
|
os.WriteFile(certPath, []byte("cert"), 0644)
|
|
|
|
cfg := &apache.Config{
|
|
CertPath: certPath,
|
|
ValidateCommand: "true",
|
|
}
|
|
|
|
connector := apache.New(cfg, logger)
|
|
|
|
result, err := connector.ValidateDeployment(ctx, target.ValidationRequest{
|
|
CertificateID: "mc-test",
|
|
Serial: "123",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ValidateDeployment failed: %v", err)
|
|
}
|
|
if !result.Valid {
|
|
t.Fatal("expected valid deployment")
|
|
}
|
|
})
|
|
|
|
t.Run("missing cert file", func(t *testing.T) {
|
|
cfg := &apache.Config{
|
|
CertPath: "/nonexistent/cert.pem",
|
|
ValidateCommand: "true",
|
|
}
|
|
|
|
connector := apache.New(cfg, logger)
|
|
|
|
result, err := connector.ValidateDeployment(ctx, target.ValidationRequest{
|
|
CertificateID: "mc-test",
|
|
Serial: "123",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error for missing cert file")
|
|
}
|
|
if result.Valid {
|
|
t.Fatal("expected invalid result")
|
|
}
|
|
})
|
|
}
|