mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 16:11:29 +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.
380 lines
12 KiB
Go
380 lines
12 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
)
|
|
|
|
// helper to create a test BulkRevocationService wired for bulk revocation tests
|
|
func newBulkRevocationTestService() (*BulkRevocationService, *mockCertRepo, *mockRevocationRepo, *mockAuditRepo) {
|
|
certRepo := newMockCertificateRepository()
|
|
auditRepo := newMockAuditRepository()
|
|
revocationRepo := newMockRevocationRepository()
|
|
|
|
auditService := NewAuditService(auditRepo)
|
|
|
|
// Create RevocationSvc (underlying single-cert revocation)
|
|
revSvc := NewRevocationSvc(certRepo, revocationRepo, auditService)
|
|
registry := NewIssuerRegistry(slog.Default())
|
|
registry.Set("iss-local", &mockIssuerConnector{})
|
|
revSvc.SetIssuerRegistry(registry)
|
|
|
|
bulkSvc := NewBulkRevocationService(revSvc, certRepo, auditService, slog.Default())
|
|
|
|
return bulkSvc, certRepo, revocationRepo, auditRepo
|
|
}
|
|
|
|
func addTestCert(repo *mockCertRepo, id, status, issuerID string) {
|
|
cert := &domain.ManagedCertificate{
|
|
ID: id,
|
|
CommonName: id + ".example.com",
|
|
Status: domain.CertificateStatus(status),
|
|
IssuerID: issuerID,
|
|
ExpiresAt: time.Now().AddDate(0, 6, 0),
|
|
}
|
|
repo.AddCert(cert)
|
|
// Add a version with serial number (needed by RevokeCertificateWithActor)
|
|
repo.Versions[id] = []*domain.CertificateVersion{
|
|
{
|
|
ID: "ver-" + id,
|
|
CertificateID: id,
|
|
SerialNumber: "serial-" + id,
|
|
NotBefore: time.Now(),
|
|
NotAfter: time.Now().AddDate(1, 0, 0),
|
|
CreatedAt: time.Now(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func addTestCertWithProfile(repo *mockCertRepo, id, status, issuerID, profileID, ownerID string) {
|
|
cert := &domain.ManagedCertificate{
|
|
ID: id,
|
|
CommonName: id + ".example.com",
|
|
Status: domain.CertificateStatus(status),
|
|
IssuerID: issuerID,
|
|
CertificateProfileID: profileID,
|
|
OwnerID: ownerID,
|
|
ExpiresAt: time.Now().AddDate(0, 6, 0),
|
|
}
|
|
repo.AddCert(cert)
|
|
repo.Versions[id] = []*domain.CertificateVersion{
|
|
{
|
|
ID: "ver-" + id,
|
|
CertificateID: id,
|
|
SerialNumber: "serial-" + id,
|
|
NotBefore: time.Now(),
|
|
NotAfter: time.Now().AddDate(1, 0, 0),
|
|
CreatedAt: time.Now(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_ByExplicitIDs(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
|
|
addTestCert(certRepo, "mc-1", "Active", "iss-local")
|
|
addTestCert(certRepo, "mc-2", "Active", "iss-local")
|
|
addTestCert(certRepo, "mc-3", "Active", "iss-local")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-1", "mc-2", "mc-3"},
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if result.TotalMatched != 3 {
|
|
t.Errorf("expected TotalMatched=3, got %d", result.TotalMatched)
|
|
}
|
|
if result.TotalRevoked != 3 {
|
|
t.Errorf("expected TotalRevoked=3, got %d", result.TotalRevoked)
|
|
}
|
|
if result.TotalSkipped != 0 {
|
|
t.Errorf("expected TotalSkipped=0, got %d", result.TotalSkipped)
|
|
}
|
|
if result.TotalFailed != 0 {
|
|
t.Errorf("expected TotalFailed=0, got %d", result.TotalFailed)
|
|
}
|
|
|
|
// Verify certs are revoked
|
|
for _, id := range []string{"mc-1", "mc-2", "mc-3"} {
|
|
cert, _ := certRepo.Get(context.Background(), id)
|
|
if cert.Status != domain.CertificateStatusRevoked {
|
|
t.Errorf("expected cert %s to be Revoked, got %s", id, cert.Status)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_ByProfile(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
|
|
// The mock List returns all certs regardless of filter (mock limitation).
|
|
// We test the code path — real repo would filter by profile.
|
|
addTestCert(certRepo, "mc-1", "Active", "iss-local")
|
|
addTestCert(certRepo, "mc-2", "Active", "iss-local")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
ProfileID: "prof-tls",
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if result.TotalMatched != 2 {
|
|
t.Errorf("expected TotalMatched=2, got %d", result.TotalMatched)
|
|
}
|
|
if result.TotalRevoked != 2 {
|
|
t.Errorf("expected TotalRevoked=2, got %d", result.TotalRevoked)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_ByOwner(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
|
|
addTestCertWithProfile(certRepo, "mc-1", "Active", "iss-local", "", "o-alice")
|
|
addTestCertWithProfile(certRepo, "mc-2", "Active", "iss-local", "", "o-alice")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
OwnerID: "o-alice",
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "cessationOfOperation", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if result.TotalRevoked != 2 {
|
|
t.Errorf("expected TotalRevoked=2, got %d", result.TotalRevoked)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_MultipleCriteria(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
|
|
addTestCertWithProfile(certRepo, "mc-1", "Active", "iss-local", "prof-tls", "o-alice")
|
|
addTestCertWithProfile(certRepo, "mc-2", "Active", "iss-local", "prof-tls", "o-bob")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
ProfileID: "prof-tls",
|
|
CertificateIDs: []string{"mc-1"}, // Intersect: only mc-1 from the filter results
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
// Both certs match the filter, but intersection with IDs gives 1
|
|
if result.TotalMatched != 1 {
|
|
t.Errorf("expected TotalMatched=1, got %d", result.TotalMatched)
|
|
}
|
|
if result.TotalRevoked != 1 {
|
|
t.Errorf("expected TotalRevoked=1, got %d", result.TotalRevoked)
|
|
}
|
|
|
|
// mc-1 should be revoked, mc-2 should not
|
|
cert1, _ := certRepo.Get(context.Background(), "mc-1")
|
|
if cert1.Status != domain.CertificateStatusRevoked {
|
|
t.Errorf("expected mc-1 to be Revoked, got %s", cert1.Status)
|
|
}
|
|
cert2, _ := certRepo.Get(context.Background(), "mc-2")
|
|
if cert2.Status == domain.CertificateStatusRevoked {
|
|
t.Error("expected mc-2 to NOT be revoked")
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_EmptyCriteria_Error(t *testing.T) {
|
|
svc, _, _, _ := newBulkRevocationTestService()
|
|
|
|
criteria := domain.BulkRevocationCriteria{}
|
|
_, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err == nil {
|
|
t.Fatal("expected error for empty criteria")
|
|
}
|
|
if !strings.Contains(err.Error(), "at least one filter criterion") {
|
|
t.Errorf("expected 'at least one filter criterion' error, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_InvalidReason_Error(t *testing.T) {
|
|
svc, _, _, _ := newBulkRevocationTestService()
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-1"},
|
|
}
|
|
|
|
_, err := svc.BulkRevoke(context.Background(), criteria, "totallyBogus", "admin")
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid reason")
|
|
}
|
|
if !strings.Contains(err.Error(), "invalid revocation reason") {
|
|
t.Errorf("expected 'invalid revocation reason' error, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_EmptyReason_Error(t *testing.T) {
|
|
svc, _, _, _ := newBulkRevocationTestService()
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-1"},
|
|
}
|
|
|
|
_, err := svc.BulkRevoke(context.Background(), criteria, "", "admin")
|
|
if err == nil {
|
|
t.Fatal("expected error for empty reason")
|
|
}
|
|
if !strings.Contains(err.Error(), "revocation reason is required") {
|
|
t.Errorf("expected 'revocation reason is required' error, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_SkipsRevokedAndArchived(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
|
|
addTestCert(certRepo, "mc-active", "Active", "iss-local")
|
|
addTestCert(certRepo, "mc-revoked", "Revoked", "iss-local")
|
|
addTestCert(certRepo, "mc-archived", "Archived", "iss-local")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-active", "mc-revoked", "mc-archived"},
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if result.TotalMatched != 3 {
|
|
t.Errorf("expected TotalMatched=3, got %d", result.TotalMatched)
|
|
}
|
|
if result.TotalRevoked != 1 {
|
|
t.Errorf("expected TotalRevoked=1, got %d", result.TotalRevoked)
|
|
}
|
|
if result.TotalSkipped != 2 {
|
|
t.Errorf("expected TotalSkipped=2, got %d", result.TotalSkipped)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_PartialFailure(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
|
|
// mc-1 is active with version — will succeed
|
|
addTestCert(certRepo, "mc-1", "Active", "iss-local")
|
|
// mc-2 is active but has NO version — RevokeCertificateWithActor will fail on GetLatestVersion
|
|
cert2 := &domain.ManagedCertificate{
|
|
ID: "mc-2",
|
|
CommonName: "mc-2.example.com",
|
|
Status: domain.CertificateStatusActive,
|
|
IssuerID: "iss-local",
|
|
ExpiresAt: time.Now().AddDate(0, 6, 0),
|
|
}
|
|
certRepo.AddCert(cert2)
|
|
// Don't add versions for mc-2 so GetLatestVersion returns errNotFound
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-1", "mc-2"},
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error (partial failure is ok), got: %v", err)
|
|
}
|
|
|
|
if result.TotalMatched != 2 {
|
|
t.Errorf("expected TotalMatched=2, got %d", result.TotalMatched)
|
|
}
|
|
if result.TotalRevoked != 1 {
|
|
t.Errorf("expected TotalRevoked=1, got %d", result.TotalRevoked)
|
|
}
|
|
if result.TotalFailed != 1 {
|
|
t.Errorf("expected TotalFailed=1, got %d", result.TotalFailed)
|
|
}
|
|
if len(result.Errors) != 1 {
|
|
t.Fatalf("expected 1 error entry, got %d", len(result.Errors))
|
|
}
|
|
if result.Errors[0].CertificateID != "mc-2" {
|
|
t.Errorf("expected error for mc-2, got %s", result.Errors[0].CertificateID)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_AuditEvent(t *testing.T) {
|
|
svc, certRepo, _, auditRepo := newBulkRevocationTestService()
|
|
|
|
addTestCert(certRepo, "mc-1", "Active", "iss-local")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-1"},
|
|
}
|
|
|
|
_, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
// Find the bulk_revocation_initiated audit event
|
|
var found bool
|
|
for _, event := range auditRepo.Events {
|
|
if event.Action == "bulk_revocation_initiated" {
|
|
found = true
|
|
if event.Actor != "admin" {
|
|
t.Errorf("expected actor 'admin', got '%s'", event.Actor)
|
|
}
|
|
if event.ResourceType != "certificate" {
|
|
t.Errorf("expected resource type 'certificate', got '%s'", event.ResourceType)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Error("expected bulk_revocation_initiated audit event")
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_NoMatches(t *testing.T) {
|
|
svc, _, _, _ := newBulkRevocationTestService()
|
|
|
|
// IDs that don't exist in the repo
|
|
criteria := domain.BulkRevocationCriteria{
|
|
CertificateIDs: []string{"mc-nonexistent-1", "mc-nonexistent-2"},
|
|
}
|
|
|
|
result, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if result.TotalMatched != 0 {
|
|
t.Errorf("expected TotalMatched=0, got %d", result.TotalMatched)
|
|
}
|
|
if result.TotalRevoked != 0 {
|
|
t.Errorf("expected TotalRevoked=0, got %d", result.TotalRevoked)
|
|
}
|
|
}
|
|
|
|
func TestBulkRevoke_ListError(t *testing.T) {
|
|
svc, certRepo, _, _ := newBulkRevocationTestService()
|
|
certRepo.ListErr = errors.New("database connection failed")
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
ProfileID: "prof-tls",
|
|
}
|
|
|
|
_, err := svc.BulkRevoke(context.Background(), criteria, "keyCompromise", "admin")
|
|
if err == nil {
|
|
t.Fatal("expected error from list failure")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to resolve certificates") {
|
|
t.Errorf("expected 'failed to resolve certificates' error, got: %v", err)
|
|
}
|
|
}
|