Files
certctl/internal/connector/target/wincertstore/wincertstore_test.go
T
shankar0123 5dc698307b chore: rename Go module path to github.com/certctl-io/certctl
Mechanical sed across the main go.mod's module declaration, the f5-mock-icontrol
sub-module's go.mod, every Go file's import path (361 files), and a rebuild of
the checked-in f5-mock-icontrol binary so its embedded build-info reflects the
new module path. No behavior change.

Choice B from cowork/transfer-certctl-to-org.md, executed 2026-05-04. Choice A
(keep module path declared as github.com/shankar0123/certctl regardless of
repo URL) shipped on the day of the org transfer (2026-05-03) since we had no
external Go consumers; this commit closes that deferral.

Backward-compat: GitHub HTTP redirects continue to forward
github.com/shankar0123/certctl → github.com/certctl-io/certctl at the URL
level, but Go's module proxy uses the path declared in go.mod as the
canonical name. Pre-fix, anyone trying `go get github.com/certctl-io/certctl/...`
hit a "module path mismatch" error because go.mod said
github.com/shankar0123/certctl and the URL they fetched it from said
certctl-io/certctl. Post-fix, the canonical name and the URL agree, so
go get / go install / external Go consumers / Go-tooling integrations
work cleanly via either the new path (preferred) or the old path (which
redirects and Go follows the redirect for source fetch).

Anyone still importing the old path inside their own code keeps working
provided they update their go.mod's `require` line to match — the module
path declared in their consumer's go.sum / go.mod is the authoritative
import name, so a mass sed across their import statements is the migration
on the consumer side. No external consumers exist today.

Diff shape:
  361 *.go files  — import path replacement only
    2 go.mod     — module declaration replacement only
    1 binary     — deploy/test/f5-mock-icontrol/f5-mock-icontrol rebuilt
                   so embedded build-info reflects the new path (8618965 vs
                   8618933 bytes; 32-byte diff is the build-info change)

  Total: 364 files, 730 insertions / 730 deletions, net-zero size, pure
  mechanical substitution.

Verification:
  gofmt: 17 files needed re-alignment after sed (the new path is one char
    shorter than the old, so column-aligned import groups drifted). Applied
    `gofmt -w` to fix.
  go mod tidy: clean exit on both modules.
  go vet ./...: clean exit.
  go build ./...: clean exit.
  go test -short -count=1 on representative packages: all green
    (internal/domain, internal/validation, internal/crypto, internal/crypto/signer,
    cmd/agent). Test output now reads `ok github.com/certctl-io/certctl/...`
    confirming the module path resolves correctly.
  binary: f5-mock-icontrol rebuilt; `strings | grep shankar0123` returns
    nothing; `strings | grep certctl-io/certctl` shows the new module path
    embedded in build-info.

Files intentionally NOT touched in this commit:
  README.md / CHANGELOG.md / docs/ / etc. — already swept to certctl-io
    URLs in commit bc6039a (the post-transfer URL refresh). This commit is
    purely the Go-tooling layer.
  Scarf pixels (`shankar0123.docker.scarf.sh/...`) — Scarf-account
    namespace, not a Go import or GitHub repo URL. Stays.

This is a non-blocking, non-customer-impacting change. Operators pulling
container images, running `make verify`, hitting the API, or installing the
agent see no functional difference. Only Go-tooling consumers (none today)
are affected, and they're enabled — not broken — by this commit.
2026-05-04 00:30:29 +00:00

913 lines
28 KiB
Go

package wincertstore
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"fmt"
"log/slog"
"math/big"
"os"
"strings"
"testing"
"time"
"github.com/certctl-io/certctl/internal/connector/target"
)
func testLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
}
// mockExecutor records PowerShell scripts and returns configurable responses.
type mockExecutor struct {
scripts []string
responses []string
errors []error
callIndex int
}
func (m *mockExecutor) Execute(ctx context.Context, script string) (string, error) {
m.scripts = append(m.scripts, script)
idx := m.callIndex
m.callIndex++
if idx < len(m.errors) && m.errors[idx] != nil {
resp := ""
if idx < len(m.responses) {
resp = m.responses[idx]
}
return resp, m.errors[idx]
}
if idx < len(m.responses) {
return m.responses[idx], nil
}
return "", nil
}
// generateTestCertAndKey creates a self-signed certificate and key for testing.
func generateTestCertAndKey() (string, string, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return "", "", err
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test.example.com"},
NotBefore: time.Now().Add(-1 * time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return "", "", err
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
keyDER, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return "", "", err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER})
return string(certPEM), string(keyPEM), nil
}
// --- ValidateConfig Tests ---
func TestValidateConfig_Success(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_name":"My","store_location":"LocalMachine"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success, got: %v", err)
}
}
func TestValidateConfig_Defaults(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success with defaults, got: %v", err)
}
if c.config.StoreName != "My" {
t.Errorf("expected default store_name 'My', got: %s", c.config.StoreName)
}
if c.config.StoreLocation != "LocalMachine" {
t.Errorf("expected default store_location 'LocalMachine', got: %s", c.config.StoreLocation)
}
if c.config.Mode != "local" {
t.Errorf("expected default mode 'local', got: %s", c.config.Mode)
}
}
func TestValidateConfig_InvalidJSON(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
err := c.ValidateConfig(context.Background(), json.RawMessage(`{bad`))
if err == nil {
t.Fatal("expected error for invalid JSON")
}
}
func TestValidateConfig_InvalidStoreName(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_name":"My; Drop-Database"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid store_name") {
t.Fatalf("expected invalid store_name error, got: %v", err)
}
}
func TestValidateConfig_InvalidStoreLocation(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_location":"InvalidLocation"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid store_location") {
t.Fatalf("expected invalid store_location error, got: %v", err)
}
}
func TestValidateConfig_CurrentUser(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_location":"CurrentUser"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success with CurrentUser, got: %v", err)
}
}
func TestValidateConfig_InvalidMode(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"mode":"ssh"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid mode") {
t.Fatalf("expected invalid mode error, got: %v", err)
}
}
func TestValidateConfig_WinRM_MissingHost(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"mode":"winrm","winrm_username":"admin","winrm_password":"pass"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "winrm_host") {
t.Fatalf("expected winrm_host error, got: %v", err)
}
}
func TestValidateConfig_WinRM_MissingUsername(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"mode":"winrm","winrm_host":"host","winrm_password":"pass"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "winrm_username") {
t.Fatalf("expected winrm_username error, got: %v", err)
}
}
func TestValidateConfig_InvalidFriendlyName(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"friendly_name":"cert; rm -rf /"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid friendly_name") {
t.Fatalf("expected invalid friendly_name error, got: %v", err)
}
}
func TestValidateConfig_WithFriendlyName(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"friendly_name":"My Production Cert"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success with friendly name, got: %v", err)
}
}
// --- DeployCertificate Tests ---
func TestDeployCertificate_Success(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Bundle 10: idempotency probe runs first (returns IDEM_MISS by default),
// then Bundle 7: success path runs 3 PowerShell scripts in order:
// probe → snapshot → import → cleanup. Seed responses for each.
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"TEMPDIR:/tmp/certctl-snapshot-abc",
"SUCCESS:AABBCCDD",
"CLEANUP_OK",
},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !result.Success {
t.Error("expected success=true")
}
if result.TargetAddress != "cert:\\LocalMachine\\My" {
t.Errorf("expected target address cert:\\LocalMachine\\My, got: %s", result.TargetAddress)
}
if result.Metadata["store_name"] != "My" {
t.Errorf("expected store_name metadata 'My', got: %s", result.Metadata["store_name"])
}
// Bundle 10: 4 scripts on success path (probe + snapshot + import + cleanup).
if len(mock.scripts) != 4 {
t.Fatalf("expected 4 script calls (probe + snapshot + import + cleanup), got %d", len(mock.scripts))
}
if !strings.Contains(mock.scripts[0], "# CERTCTL_IDEM_PROBE") {
t.Errorf("expected # CERTCTL_IDEM_PROBE in first script, got: %s", mock.scripts[0])
}
if !strings.Contains(mock.scripts[1], "# CERTCTL_SNAPSHOT") {
t.Errorf("expected # CERTCTL_SNAPSHOT in second script, got: %s", mock.scripts[1])
}
importScript := mock.scripts[2]
if !strings.Contains(importScript, "Import-PfxCertificate") {
t.Error("expected Import-PfxCertificate in third script")
}
if !strings.Contains(importScript, "Cert:\\LocalMachine\\My") {
t.Error("expected correct cert store path in third script")
}
if !strings.Contains(mock.scripts[3], "# CERTCTL_CLEANUP") {
t.Errorf("expected # CERTCTL_CLEANUP in fourth script, got: %s", mock.scripts[3])
}
}
func TestWinCertStore_Idempotent_SkipsImportWhenCertInStore(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Probe returns IDEM_MATCH
mock := &mockExecutor{
responses: []string{"IDEM_MATCH"},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !result.Success {
t.Error("expected success=true")
}
// Verify idempotent flag is set
if result.Metadata["idempotent"] != "true" {
t.Errorf("expected idempotent=true, got %s", result.Metadata["idempotent"])
}
// Only the probe should have run (1 script call)
if len(mock.scripts) != 1 {
t.Errorf("expected 1 script call (probe only), got %d", len(mock.scripts))
}
if !strings.Contains(mock.scripts[0], "# CERTCTL_IDEM_PROBE") {
t.Errorf("expected probe script, got: %s", mock.scripts[0])
}
// Verify no Import-PfxCertificate call
for i, script := range mock.scripts {
if strings.Contains(script, "Import-PfxCertificate") {
t.Errorf("script %d should not contain Import-PfxCertificate (idempotent short-circuit): %s", i, script)
}
}
}
func TestWinCertStore_Idempotent_NotInStore_FallsThroughToDeploy(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Probe returns IDEM_MISS, then standard snapshot + import + cleanup responses
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"TEMPDIR:/tmp/certctl-snapshot-def",
"SUCCESS:DDEEFFGG",
"CLEANUP_OK",
},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !result.Success {
t.Error("expected success=true")
}
// Verify idempotent flag is NOT set
if result.Metadata["idempotent"] != "" {
t.Errorf("expected no idempotent flag, got %s", result.Metadata["idempotent"])
}
// Full flow: probe + snapshot + import + cleanup = 4 scripts
if len(mock.scripts) != 4 {
t.Errorf("expected 4 script calls (probe, snapshot, import, cleanup), got %d", len(mock.scripts))
}
// Verify probe ran first
if !strings.Contains(mock.scripts[0], "# CERTCTL_IDEM_PROBE") {
t.Errorf("expected probe as first script, got: %s", mock.scripts[0])
}
// Verify import happened
hasImport := false
for _, script := range mock.scripts {
if strings.Contains(script, "Import-PfxCertificate") {
hasImport = true
break
}
}
if !hasImport {
t.Error("expected Import-PfxCertificate in scripts")
}
}
func TestDeployCertificate_MissingKey(t *testing.T) {
certPEM, _, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
})
if err == nil || !strings.Contains(err.Error(), "private key is required") {
t.Fatalf("expected missing key error, got: %v", err)
}
}
func TestDeployCertificate_InvalidCert(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
_, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: "not-a-cert",
KeyPEM: "not-a-key",
})
if err == nil {
t.Fatal("expected error for invalid cert")
}
}
func TestDeployCertificate_ImportFailed(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Bundle 10 / Top-10 fix #3 idempotency probe runs first → IDEM_MISS,
// fall through. Bundle 7: snapshot returns empty → import fails →
// rollback runs (and succeeds since snapshot was empty, only removes
// the new cert if it landed).
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"TEMPDIR:/tmp/certctl-snapshot-xyz",
"Access denied",
"ROLLBACK_OK",
},
errors: []error{nil, nil, fmt.Errorf("exit code 1"), nil},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil || !strings.Contains(err.Error(), "PowerShell import failed") {
t.Fatalf("expected import failure error, got: %v", err)
}
// Bundle 7: error message must reference rollback so operators know
// the deploy left the store in a known state.
if !strings.Contains(err.Error(), "rolled back") {
t.Errorf("expected error to mention 'rolled back', got: %v", err)
}
}
func TestDeployCertificate_WithFriendlyName(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"TEMPDIR:/tmp/certctl-snapshot-fn",
"SUCCESS:AABB",
"CLEANUP_OK",
},
}
c := NewWithExecutor(&Config{
StoreName: "My",
FriendlyName: "Production API Cert",
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
// Bundle 10: probe at [0], snapshot at [1], import at [2].
if len(mock.scripts) < 3 {
t.Fatalf("expected at least 3 scripts (probe + snapshot + import), got %d", len(mock.scripts))
}
if !strings.Contains(mock.scripts[2], "FriendlyName") {
t.Error("expected FriendlyName in import script")
}
}
func TestDeployCertificate_WithRemoveExpired(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"TEMPDIR:/tmp/certctl-snapshot-re",
"SUCCESS:AABB",
"CLEANUP_OK",
},
}
c := NewWithExecutor(&Config{
StoreName: "My",
RemoveExpired: true,
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
// Bundle 10: probe at [0], snapshot at [1], import at [2].
if len(mock.scripts) < 3 {
t.Fatalf("expected at least 3 scripts (probe + snapshot + import), got %d", len(mock.scripts))
}
if !strings.Contains(mock.scripts[2], "Remove-Item") {
t.Error("expected Remove-Item for expired cert cleanup in import script")
}
}
// --- Bundle 7: pre-deploy snapshot + on-import-failure rollback ---
//
// These four tests pin the load-bearing rollback contract added in
// Bundle 7 of the 2026-05-02 deployment-target audit:
// - happy rollback path: snapshot finds same-Subject cert → import
// fails → rollback removes new cert + re-imports snapshot;
// - first-time deploy: snapshot finds no same-Subject certs → import
// fails → rollback only removes the new cert (no re-import);
// - FriendlyName-step failure: import script fails on Set
// FriendlyName → same rollback path;
// - rollback-also-fails: operator-actionable wrapped error.
func TestWinCertStore_ImportFails_RemovesNewCert_RestoresOldFromSnapshot(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Probe → IDEM_MISS, fall through. Snapshot finds one same-Subject
// cert and exports it. Import fails. Rollback succeeds. Verify confirms.
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"SNAPSHOT:OLDTHUMB123:/tmp/certctl-snapshot-abc/OLDTHUMB123.pfx\nTEMPDIR:/tmp/certctl-snapshot-abc",
"PFX import error",
"ROLLBACK_OK",
"VERIFY_OK",
},
errors: []error{nil, nil, fmt.Errorf("exit code 1"), nil, nil},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil {
t.Fatal("expected error when import fails")
}
if result == nil {
t.Fatal("expected non-nil result on rollback path")
}
if result.Success {
t.Fatal("expected failure result")
}
if !strings.Contains(err.Error(), "PowerShell import failed") {
t.Errorf("expected error to mention import failure, got: %v", err)
}
if !strings.Contains(err.Error(), "rolled back") {
t.Errorf("expected error to mention 'rolled back', got: %v", err)
}
// 5 scripts: probe + snapshot + import + rollback + verify.
if len(mock.scripts) != 5 {
t.Fatalf("expected 5 scripts (probe + snapshot + import + rollback + verify), got %d", len(mock.scripts))
}
// Locate the rollback script and assert it contains BOTH a Remove-Item
// for the new thumbprint AND an Import-PfxCertificate for the
// snapshotted PFX.
var rollbackScript string
for _, s := range mock.scripts {
if strings.Contains(s, "# CERTCTL_ROLLBACK") {
rollbackScript = s
break
}
}
if rollbackScript == "" {
t.Fatal("expected rollback script to be executed")
}
if !strings.Contains(rollbackScript, "Remove-Item") {
t.Errorf("expected rollback to contain Remove-Item, got: %s", rollbackScript)
}
if !strings.Contains(rollbackScript, "Import-PfxCertificate") {
t.Errorf("expected rollback to Import-PfxCertificate the snapshot, got: %s", rollbackScript)
}
if !strings.Contains(rollbackScript, "OLDTHUMB123.pfx") {
t.Errorf("expected rollback to reference the snapshot pfx path, got: %s", rollbackScript)
}
if result.Metadata["rolled_back"] != "true" {
t.Errorf("expected rolled_back=true in metadata, got: %s", result.Metadata["rolled_back"])
}
}
func TestWinCertStore_ImportFails_NoExistingSameSubject_RemovesNewCertOnly(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Probe → IDEM_MISS, fall through. Snapshot returns THUMB lines
// (different-Subject certs in store) but NO SNAPSHOT lines — no
// same-Subject cert was exported. Rollback removes the new cert but
// does NOT call Import-PfxCertificate.
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"THUMB:UNRELATED1\nTHUMB:UNRELATED2\nTEMPDIR:/tmp/certctl-snapshot-noss",
"PFX import error",
"ROLLBACK_OK",
"VERIFY_OK",
},
errors: []error{nil, nil, fmt.Errorf("exit code 1"), nil, nil},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil {
t.Fatal("expected error when import fails")
}
var rollbackScript string
for _, s := range mock.scripts {
if strings.Contains(s, "# CERTCTL_ROLLBACK") {
rollbackScript = s
break
}
}
if rollbackScript == "" {
t.Fatal("expected rollback script to be executed")
}
if !strings.Contains(rollbackScript, "Remove-Item") {
t.Errorf("expected rollback to remove new cert via Remove-Item, got: %s", rollbackScript)
}
// No same-Subject snapshots → no Import-PfxCertificate during rollback.
if strings.Contains(rollbackScript, "Import-PfxCertificate") {
t.Errorf("expected no Import-PfxCertificate when snapshot has no same-Subject entries, got: %s", rollbackScript)
}
}
func TestWinCertStore_FriendlyNameFails_NewCertRemoved_OldCertsRestored(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// The connector cannot inspect WHICH step inside the import script
// failed — Execute returns a single (output, error). Operators see
// the FriendlyName failure surfaced via the error output. The
// rollback runs the same way regardless of which post-Import step
// failed (FriendlyName, Get-ChildItem verify, RemoveExpired).
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"SNAPSHOT:OLDTHUMB456:/tmp/certctl-snapshot-fn/OLDTHUMB456.pfx\nTEMPDIR:/tmp/certctl-snapshot-fn",
"Cannot set FriendlyName: invalid character in friendly name",
"ROLLBACK_OK",
"VERIFY_OK",
},
errors: []error{nil, nil, fmt.Errorf("Set-ItemProperty failed"), nil, nil},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
FriendlyName: "Production",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil {
t.Fatal("expected error when FriendlyName step fails")
}
if result.Success {
t.Fatal("expected failure result")
}
// Operator visibility: the import_error in metadata should preserve
// the PowerShell output so operators can tell what went wrong.
if !strings.Contains(result.Metadata["import_error"], "FriendlyName") {
t.Errorf("expected import_error to reference FriendlyName, got: %s", result.Metadata["import_error"])
}
var rollbackScript string
for _, s := range mock.scripts {
if strings.Contains(s, "# CERTCTL_ROLLBACK") {
rollbackScript = s
break
}
}
if rollbackScript == "" {
t.Fatal("expected rollback script to be executed")
}
if !strings.Contains(rollbackScript, "Remove-Item") {
t.Errorf("expected rollback to Remove-Item the new cert, got: %s", rollbackScript)
}
if !strings.Contains(rollbackScript, "OLDTHUMB456.pfx") {
t.Errorf("expected rollback to restore snapshotted cert, got: %s", rollbackScript)
}
if result.Metadata["rolled_back"] != "true" {
t.Errorf("expected rolled_back=true, got: %s", result.Metadata["rolled_back"])
}
}
func TestWinCertStore_ImportFails_RollbackAlsoFails_OperatorActionable(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
// Probe → IDEM_MISS, fall through. Snapshot succeeds; import fails;
// rollback ALSO fails. Operator-actionable case: both errors must be
// surfaced + metadata flags manual_action_required.
mock := &mockExecutor{
responses: []string{
"IDEM_MISS",
"SNAPSHOT:OLDTHUMB789:/tmp/certctl-snapshot-rbf/OLDTHUMB789.pfx\nTEMPDIR:/tmp/certctl-snapshot-rbf",
"Import error",
"Rollback step failed",
},
errors: []error{
nil,
nil,
fmt.Errorf("import-step exit code 1"),
fmt.Errorf("rollback-step exit code 2"),
},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil {
t.Fatal("expected error when both import and rollback fail")
}
if result.Success {
t.Fatal("expected failure result")
}
// Wrapped error must mention BOTH errors.
if !strings.Contains(err.Error(), "PowerShell import failed") {
t.Errorf("expected error to mention import failure, got: %v", err)
}
if !strings.Contains(err.Error(), "rollback also failed") {
t.Errorf("expected error to mention rollback failure, got: %v", err)
}
if !strings.Contains(err.Error(), "manual operator inspection required") {
t.Errorf("expected error to flag manual inspection, got: %v", err)
}
// Metadata flags manual action + surfaces both errors.
if result.Metadata["manual_action_required"] != "true" {
t.Errorf("expected manual_action_required=true, got: %s", result.Metadata["manual_action_required"])
}
if result.Metadata["rolled_back"] != "false" {
t.Errorf("expected rolled_back=false, got: %s", result.Metadata["rolled_back"])
}
if result.Metadata["rollback_error"] == "" {
t.Error("expected rollback_error in metadata")
}
if result.Metadata["import_error"] == "" {
t.Error("expected import_error in metadata")
}
}
// --- ValidateDeployment Tests ---
func TestValidateDeployment_Success(t *testing.T) {
mock := &mockExecutor{
responses: []string{"FOUND:AABBCCDD:2027-01-01T00:00:00"},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
Metadata: map[string]string{
"thumbprint": "AABBCCDD",
},
})
if err != nil {
t.Fatalf("validate failed: %v", err)
}
if !result.Valid {
t.Error("expected valid=true")
}
if result.Metadata["thumbprint"] != "AABBCCDD" {
t.Errorf("expected thumbprint AABBCCDD, got: %s", result.Metadata["thumbprint"])
}
}
func TestValidateDeployment_NotFound(t *testing.T) {
mock := &mockExecutor{
responses: []string{"NOT_FOUND"},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
})
if err == nil {
t.Fatal("expected error for not found cert")
}
if result.Valid {
t.Error("expected valid=false")
}
}
func TestValidateDeployment_QueryFailed(t *testing.T) {
mock := &mockExecutor{
responses: []string{"error"},
errors: []error{fmt.Errorf("powershell error")},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
})
if err == nil {
t.Fatal("expected error for query failure")
}
if result.Valid {
t.Error("expected valid=false")
}
}
func TestValidateDeployment_BySerial(t *testing.T) {
mock := &mockExecutor{
responses: []string{"FOUND:AABB:2027-01-01T00:00:00"},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
// No thumbprint in metadata — should query by serial
_, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "DEADBEEF",
})
if err != nil {
t.Fatalf("validate failed: %v", err)
}
if !strings.Contains(mock.scripts[0], "SerialNumber") {
t.Error("expected serial number query in script")
}
}
// --- Top-10 fix #4: default-deadline ctx wrapper for PowerShell exec calls ---
//
// These tests pin realExecutor's safety-net behavior. See the matching pair
// in iis_test.go for the rationale (subprocess fails fast on non-Windows
// runners / deadline cancels on Windows; either path must return fast).
func TestWinCertStore_RealExecutor_AttachesDefaultDeadlineWhenCallerHasNone(t *testing.T) {
e := &realExecutor{deadline: 100 * time.Millisecond}
start := time.Now()
_, err := e.Execute(context.Background(), "Start-Sleep -Seconds 5")
elapsed := time.Since(start)
if elapsed > 500*time.Millisecond {
t.Errorf("expected fast return (default deadline = 100ms), took %v: err=%v", elapsed, err)
}
if err == nil {
t.Error("expected an error (context.DeadlineExceeded on Windows / powershell.exe missing on Linux)")
}
}
func TestWinCertStore_RealExecutor_RespectsCallerDeadlineWhenSet(t *testing.T) {
e := &realExecutor{deadline: 10 * time.Second} // long fallback
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
_, _ = e.Execute(ctx, "Start-Sleep -Seconds 5")
elapsed := time.Since(start)
if elapsed > 500*time.Millisecond {
t.Errorf("expected caller's tight 50ms deadline to fire fast, took %v", elapsed)
}
}
func TestWinCertStore_RealExecutor_NoDeadlineWiredWhenZero(t *testing.T) {
// deadline=0 means "no fallback wrapper". Pass a tight caller deadline.
e := &realExecutor{deadline: 0}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
_, _ = e.Execute(ctx, "Start-Sleep -Seconds 5")
elapsed := time.Since(start)
if elapsed > 500*time.Millisecond {
t.Errorf("expected caller deadline to bound the call, took %v", elapsed)
}
}
func TestWinCertStore_New_DefaultsExecDeadlineTo60s(t *testing.T) {
c, err := New(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger())
if err != nil {
t.Fatalf("New failed: %v", err)
}
if c.config.ExecDeadline != 60*time.Second {
t.Errorf("expected default ExecDeadline=60s, got %v", c.config.ExecDeadline)
}
}
func TestWinCertStore_New_RespectsExplicitExecDeadline(t *testing.T) {
c, err := New(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
ExecDeadline: 10 * time.Minute,
}, testLogger())
if err != nil {
t.Fatalf("New failed: %v", err)
}
if c.config.ExecDeadline != 10*time.Minute {
t.Errorf("expected ExecDeadline=10m, got %v", c.config.ExecDeadline)
}
}