mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 16:01:30 +00:00
8b75e0311b
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 0729ee4 (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.
222 lines
8.1 KiB
Go
222 lines
8.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
)
|
|
|
|
// newBulkRenewalTestService spins up a BulkRenewalService wired against
|
|
// the in-memory mocks used by every other service test in this package.
|
|
// keygenMode defaults to "agent" — production-like routing where renewal
|
|
// jobs start as AwaitingCSR.
|
|
func newBulkRenewalTestService() (*BulkRenewalService, *mockCertRepo, *mockJobRepo, *mockAuditRepo) {
|
|
certRepo := newMockCertificateRepository()
|
|
jobRepo := &mockJobRepo{Jobs: map[string]*domain.Job{}}
|
|
auditRepo := newMockAuditRepository()
|
|
auditService := NewAuditService(auditRepo)
|
|
svc := NewBulkRenewalService(certRepo, jobRepo, auditService, slog.Default(), "agent")
|
|
return svc, certRepo, jobRepo, auditRepo
|
|
}
|
|
|
|
// addRenewableCert seeds a cert that is eligible for renewal (Active
|
|
// status, future expiry).
|
|
func addRenewableCert(repo *mockCertRepo, id string) {
|
|
cert := &domain.ManagedCertificate{
|
|
ID: id,
|
|
CommonName: id + ".example.com",
|
|
Status: domain.CertificateStatusActive,
|
|
ExpiresAt: time.Now().AddDate(0, 1, 0),
|
|
IssuerID: "iss-test",
|
|
}
|
|
repo.AddCert(cert)
|
|
}
|
|
|
|
// TestBulkRenew_ByExplicitIDs — happy path. N IDs in, N jobs enqueued,
|
|
// EnqueuedJobs slice carries the {certificate_id, job_id} pairs.
|
|
func TestBulkRenew_ByExplicitIDs(t *testing.T) {
|
|
svc, certRepo, jobRepo, _ := newBulkRenewalTestService()
|
|
addRenewableCert(certRepo, "mc-1")
|
|
addRenewableCert(certRepo, "mc-2")
|
|
addRenewableCert(certRepo, "mc-3")
|
|
|
|
res, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{CertificateIDs: []string{"mc-1", "mc-2", "mc-3"}},
|
|
"alice")
|
|
if err != nil {
|
|
t.Fatalf("BulkRenew failed: %v", err)
|
|
}
|
|
if res.TotalMatched != 3 || res.TotalEnqueued != 3 || res.TotalSkipped != 0 || res.TotalFailed != 0 {
|
|
t.Errorf("counts = matched:%d enqueued:%d skipped:%d failed:%d, want 3/3/0/0",
|
|
res.TotalMatched, res.TotalEnqueued, res.TotalSkipped, res.TotalFailed)
|
|
}
|
|
if len(res.EnqueuedJobs) != 3 {
|
|
t.Fatalf("EnqueuedJobs len = %d, want 3", len(res.EnqueuedJobs))
|
|
}
|
|
if len(jobRepo.Jobs) != 3 {
|
|
t.Errorf("jobRepo got %d jobs, want 3 (one per renewable cert)", len(jobRepo.Jobs))
|
|
}
|
|
for _, j := range res.EnqueuedJobs {
|
|
if j.JobID == "" {
|
|
t.Errorf("EnqueuedJob missing job_id for cert %s", j.CertificateID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBulkRenew_ByOwnerCriteria — criteria-mode resolution. The
|
|
// criteria-routing path must call resolveCertificates with the filter
|
|
// branch (not the explicit-IDs branch). Mocking convention in this
|
|
// package: mockCertRepo.List ignores the filter and returns all certs,
|
|
// so the test seeds only certs that should match (mirrors
|
|
// TestBulkRevoke_ByOwner shape in bulk_revocation_test.go).
|
|
func TestBulkRenew_ByOwnerCriteria(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRenewalTestService()
|
|
for _, id := range []string{"mc-a1", "mc-a2"} {
|
|
cert := &domain.ManagedCertificate{
|
|
ID: id, CommonName: id, Status: domain.CertificateStatusActive,
|
|
OwnerID: "o-alice", ExpiresAt: time.Now().AddDate(0, 1, 0),
|
|
}
|
|
certRepo.AddCert(cert)
|
|
}
|
|
|
|
res, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{OwnerID: "o-alice"}, "alice")
|
|
if err != nil {
|
|
t.Fatalf("BulkRenew failed: %v", err)
|
|
}
|
|
if res.TotalEnqueued != 2 {
|
|
t.Errorf("TotalEnqueued = %d, want 2 (alice's 2 certs)", res.TotalEnqueued)
|
|
}
|
|
}
|
|
|
|
// TestBulkRenew_SkipsRenewalInProgress — a cert already in the renewal
|
|
// flow must NOT get a second job. This is the no-double-enqueue
|
|
// contract: dispatch the bulk-renew button twice in quick succession
|
|
// and the second call cleanly skips.
|
|
func TestBulkRenew_SkipsRenewalInProgress(t *testing.T) {
|
|
svc, certRepo, jobRepo, _ := newBulkRenewalTestService()
|
|
cert := &domain.ManagedCertificate{
|
|
ID: "mc-rip", Status: domain.CertificateStatusRenewalInProgress,
|
|
ExpiresAt: time.Now().AddDate(0, 1, 0),
|
|
}
|
|
certRepo.AddCert(cert)
|
|
|
|
res, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{CertificateIDs: []string{"mc-rip"}}, "alice")
|
|
if err != nil {
|
|
t.Fatalf("BulkRenew failed: %v", err)
|
|
}
|
|
if res.TotalSkipped != 1 || res.TotalEnqueued != 0 {
|
|
t.Errorf("counts wrong: skipped=%d enqueued=%d, want 1/0",
|
|
res.TotalSkipped, res.TotalEnqueued)
|
|
}
|
|
if len(jobRepo.Jobs) != 0 {
|
|
t.Errorf("no job should be created for already-in-progress cert; got %d jobs", len(jobRepo.Jobs))
|
|
}
|
|
}
|
|
|
|
// TestBulkRenew_SkipsRevokedAndArchived — terminal states are silent
|
|
// no-ops, not errors. Operator selecting a mix of live and revoked certs
|
|
// shouldn't see "ERROR: revoked cert can't be renewed" 50 times.
|
|
func TestBulkRenew_SkipsRevokedAndArchived(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRenewalTestService()
|
|
addRenewableCert(certRepo, "mc-live")
|
|
for _, st := range []domain.CertificateStatus{
|
|
domain.CertificateStatusRevoked,
|
|
domain.CertificateStatusArchived,
|
|
domain.CertificateStatusExpired,
|
|
} {
|
|
cert := &domain.ManagedCertificate{
|
|
ID: "mc-" + string(st), Status: st, ExpiresAt: time.Now().AddDate(0, 1, 0),
|
|
}
|
|
certRepo.AddCert(cert)
|
|
}
|
|
|
|
res, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{CertificateIDs: []string{
|
|
"mc-live", "mc-Revoked", "mc-Archived", "mc-Expired",
|
|
}}, "alice")
|
|
if err != nil {
|
|
t.Fatalf("BulkRenew failed: %v", err)
|
|
}
|
|
if res.TotalEnqueued != 1 || res.TotalSkipped != 3 {
|
|
t.Errorf("counts = enqueued:%d skipped:%d, want 1/3 (only mc-live qualifies)",
|
|
res.TotalEnqueued, res.TotalSkipped)
|
|
}
|
|
if len(res.Errors) != 0 {
|
|
t.Errorf("status-skip should NOT populate Errors; got %v", res.Errors)
|
|
}
|
|
}
|
|
|
|
// TestBulkRenew_EmptyCriteria_Error — defensive contract. Mirrors
|
|
// BulkRevocationCriteria.IsEmpty rejection so a stray empty POST
|
|
// doesn't try to renew the entire fleet.
|
|
func TestBulkRenew_EmptyCriteria_Error(t *testing.T) {
|
|
svc, _, _, _ := newBulkRenewalTestService()
|
|
_, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{}, "alice")
|
|
if err == nil {
|
|
t.Fatal("expected error for empty criteria, got nil")
|
|
}
|
|
}
|
|
|
|
// TestBulkRenew_PartialFailure — N=3, jobRepo.Create injected to fail
|
|
// on one of them. Response carries 2 enqueued + 1 error; no panic, no
|
|
// abort.
|
|
func TestBulkRenew_PartialFailure(t *testing.T) {
|
|
svc, certRepo, jobRepo, _ := newBulkRenewalTestService()
|
|
addRenewableCert(certRepo, "mc-1")
|
|
addRenewableCert(certRepo, "mc-2")
|
|
addRenewableCert(certRepo, "mc-3")
|
|
|
|
// Make Create fail uniformly. Two of the three certs will still
|
|
// have their status flipped (because Update happened first), so
|
|
// the failure manifests as "I tried to enqueue, the job-create
|
|
// failed". Per-cert error string surfaced.
|
|
jobRepo.CreateErr = errors.New("simulated DB outage")
|
|
|
|
res, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{CertificateIDs: []string{"mc-1", "mc-2", "mc-3"}},
|
|
"alice")
|
|
if err != nil {
|
|
t.Fatalf("BulkRenew should not propagate per-cert errors as a top-level error; got: %v", err)
|
|
}
|
|
if res.TotalFailed != 3 || res.TotalEnqueued != 0 {
|
|
t.Errorf("counts = failed:%d enqueued:%d, want 3/0", res.TotalFailed, res.TotalEnqueued)
|
|
}
|
|
if len(res.Errors) != 3 {
|
|
t.Errorf("Errors len = %d, want 3", len(res.Errors))
|
|
}
|
|
}
|
|
|
|
// TestBulkRenew_AuditEventEmitted — exactly ONE bulk audit event for
|
|
// the operation, NOT N. This is the audit-volume contract that makes
|
|
// bulk endpoints scalable.
|
|
func TestBulkRenew_AuditEventEmitted(t *testing.T) {
|
|
svc, certRepo, _, auditRepo := newBulkRenewalTestService()
|
|
addRenewableCert(certRepo, "mc-1")
|
|
addRenewableCert(certRepo, "mc-2")
|
|
addRenewableCert(certRepo, "mc-3")
|
|
|
|
_, err := svc.BulkRenew(context.Background(),
|
|
domain.BulkRenewalCriteria{CertificateIDs: []string{"mc-1", "mc-2", "mc-3"}},
|
|
"alice")
|
|
if err != nil {
|
|
t.Fatalf("BulkRenew failed: %v", err)
|
|
}
|
|
|
|
// audit_events count must be exactly 1 — the bulk-renewal envelope.
|
|
// Per-cert renewal events come from CertificateService.TriggerRenewal,
|
|
// which the bulk path bypasses for exactly this reason.
|
|
if len(auditRepo.Events) != 1 {
|
|
t.Errorf("audit events count = %d, want exactly 1 (one bulk event, NOT N per-cert events)", len(auditRepo.Events))
|
|
}
|
|
if len(auditRepo.Events) > 0 && auditRepo.Events[0].Action != "bulk_renewal_initiated" {
|
|
t.Errorf("audit action = %q, want 'bulk_renewal_initiated'", auditRepo.Events[0].Action)
|
|
}
|
|
}
|