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.
290 lines
8.0 KiB
Go
290 lines
8.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
)
|
|
|
|
// mockVerificationJobRepo is a test double for JobRepository used by verification tests.
|
|
type mockVerificationJobRepo struct {
|
|
jobs map[string]*domain.Job
|
|
err error
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) Get(ctx context.Context, id string) (*domain.Job, error) {
|
|
if m.err != nil {
|
|
return nil, m.err
|
|
}
|
|
job, ok := m.jobs[id]
|
|
if !ok {
|
|
return nil, errors.New("job not found")
|
|
}
|
|
return job, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) Create(ctx context.Context, job *domain.Job) error {
|
|
m.jobs[job.ID] = job
|
|
return nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) Update(ctx context.Context, job *domain.Job) error {
|
|
if m.err != nil {
|
|
return m.err
|
|
}
|
|
m.jobs[job.ID] = job
|
|
return nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) List(ctx context.Context) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) Delete(ctx context.Context, id string) error {
|
|
delete(m.jobs, id)
|
|
return nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) ListByStatus(ctx context.Context, status domain.JobStatus) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) ListByCertificate(ctx context.Context, certID string) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) UpdateStatus(ctx context.Context, id string, status domain.JobStatus, errMsg string) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) GetPendingJobs(ctx context.Context, jobType domain.JobType) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) ListPendingByAgentID(ctx context.Context, agentID string) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) ClaimPendingJobs(ctx context.Context, jobType domain.JobType, limit int) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) ClaimPendingByAgentID(ctx context.Context, agentID string) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockVerificationJobRepo) ListTimedOutAwaitingJobs(ctx context.Context, csrCutoff, approvalCutoff time.Time) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// Bundle C / Audit M-016: stub for the new offline-agent reaper repo method.
|
|
func (m *mockVerificationJobRepo) ListJobsWithOfflineAgents(ctx context.Context, agentCutoff time.Time) ([]*domain.Job, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// newVerificationTestService creates a VerificationService wired with test doubles.
|
|
func newVerificationTestService(jobs map[string]*domain.Job, jobRepoErr error) (*VerificationService, *mockVerificationJobRepo, *mockAuditRepo) {
|
|
jobRepo := &mockVerificationJobRepo{jobs: jobs, err: jobRepoErr}
|
|
auditRepo := newMockAuditRepository()
|
|
auditService := NewAuditService(auditRepo)
|
|
svc := NewVerificationService(jobRepo, auditService, slog.Default())
|
|
return svc, jobRepo, auditRepo
|
|
}
|
|
|
|
func TestVerificationService_RecordVerificationResult_Success(t *testing.T) {
|
|
ctx := context.Background()
|
|
jobs := map[string]*domain.Job{
|
|
"j-test1": {
|
|
ID: "j-test1",
|
|
Status: domain.JobStatusCompleted,
|
|
},
|
|
}
|
|
svc, jobRepo, auditRepo := newVerificationTestService(jobs, nil)
|
|
|
|
result := &domain.VerificationResult{
|
|
JobID: "j-test1",
|
|
TargetID: "t-nginx1",
|
|
ExpectedFingerprint: "abc123",
|
|
ActualFingerprint: "abc123",
|
|
Verified: true,
|
|
VerifiedAt: time.Now().UTC(),
|
|
}
|
|
|
|
err := svc.RecordVerificationResult(ctx, result)
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
// Check job was updated
|
|
job, _ := jobRepo.Get(ctx, "j-test1")
|
|
if job.VerificationStatus != domain.VerificationSuccess {
|
|
t.Errorf("expected VerificationSuccess, got %s", job.VerificationStatus)
|
|
}
|
|
if job.VerifiedAt == nil {
|
|
t.Error("expected verified_at to be set")
|
|
}
|
|
|
|
// Check audit event was recorded
|
|
if len(auditRepo.Events) == 0 {
|
|
t.Error("expected at least 1 audit event")
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_RecordVerificationResult_Failed(t *testing.T) {
|
|
ctx := context.Background()
|
|
jobs := map[string]*domain.Job{
|
|
"j-test2": {
|
|
ID: "j-test2",
|
|
Status: domain.JobStatusCompleted,
|
|
},
|
|
}
|
|
svc, jobRepo, _ := newVerificationTestService(jobs, nil)
|
|
|
|
result := &domain.VerificationResult{
|
|
JobID: "j-test2",
|
|
TargetID: "t-apache1",
|
|
ExpectedFingerprint: "aaa111",
|
|
ActualFingerprint: "bbb222",
|
|
Verified: false,
|
|
VerifiedAt: time.Now().UTC(),
|
|
}
|
|
|
|
err := svc.RecordVerificationResult(ctx, result)
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
job, _ := jobRepo.Get(ctx, "j-test2")
|
|
if job.VerificationStatus != domain.VerificationFailed {
|
|
t.Errorf("expected VerificationFailed, got %s", job.VerificationStatus)
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_RecordVerificationResult_WithError(t *testing.T) {
|
|
ctx := context.Background()
|
|
jobs := map[string]*domain.Job{
|
|
"j-test3": {
|
|
ID: "j-test3",
|
|
Status: domain.JobStatusCompleted,
|
|
},
|
|
}
|
|
svc, jobRepo, _ := newVerificationTestService(jobs, nil)
|
|
|
|
result := &domain.VerificationResult{
|
|
JobID: "j-test3",
|
|
TargetID: "t-haproxy1",
|
|
VerifiedAt: time.Now().UTC(),
|
|
Error: "connection refused",
|
|
}
|
|
|
|
err := svc.RecordVerificationResult(ctx, result)
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
job, _ := jobRepo.Get(ctx, "j-test3")
|
|
if job.VerificationStatus != domain.VerificationFailed {
|
|
t.Errorf("expected VerificationFailed, got %s", job.VerificationStatus)
|
|
}
|
|
if job.VerificationError == nil || *job.VerificationError != "connection refused" {
|
|
t.Error("expected verification error to be set")
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_RecordVerificationResult_JobNotFound(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc, _, _ := newVerificationTestService(map[string]*domain.Job{}, nil)
|
|
|
|
result := &domain.VerificationResult{
|
|
JobID: "j-nonexistent",
|
|
TargetID: "t-nginx1",
|
|
VerifiedAt: time.Now().UTC(),
|
|
}
|
|
|
|
err := svc.RecordVerificationResult(ctx, result)
|
|
if err == nil {
|
|
t.Error("expected error for nonexistent job")
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_RecordVerificationResult_MissingJobID(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc, _, _ := newVerificationTestService(map[string]*domain.Job{}, nil)
|
|
|
|
result := &domain.VerificationResult{
|
|
TargetID: "t-nginx1",
|
|
VerifiedAt: time.Now().UTC(),
|
|
}
|
|
|
|
err := svc.RecordVerificationResult(ctx, result)
|
|
if err == nil {
|
|
t.Error("expected error for missing job ID")
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_RecordVerificationResult_NilResult(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc, _, _ := newVerificationTestService(map[string]*domain.Job{}, nil)
|
|
|
|
err := svc.RecordVerificationResult(ctx, nil)
|
|
if err == nil {
|
|
t.Error("expected error for nil result")
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_GetVerificationResult_Success(t *testing.T) {
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
targetID := "t-nginx1"
|
|
fp := "abc123"
|
|
jobs := map[string]*domain.Job{
|
|
"j-test1": {
|
|
ID: "j-test1",
|
|
TargetID: &targetID,
|
|
VerificationStatus: domain.VerificationSuccess,
|
|
VerifiedAt: &now,
|
|
VerificationFp: &fp,
|
|
},
|
|
}
|
|
svc, _, _ := newVerificationTestService(jobs, nil)
|
|
|
|
result, err := svc.GetVerificationResult(ctx, "j-test1")
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
if result.JobID != "j-test1" {
|
|
t.Errorf("expected job ID j-test1, got %s", result.JobID)
|
|
}
|
|
if !result.Verified {
|
|
t.Error("expected Verified to be true")
|
|
}
|
|
if result.ActualFingerprint != "abc123" {
|
|
t.Errorf("expected fingerprint abc123, got %s", result.ActualFingerprint)
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_GetVerificationResult_NotFound(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc, _, _ := newVerificationTestService(map[string]*domain.Job{}, nil)
|
|
|
|
_, err := svc.GetVerificationResult(ctx, "j-nonexistent")
|
|
if err == nil {
|
|
t.Error("expected error for nonexistent job")
|
|
}
|
|
}
|
|
|
|
func TestVerificationService_GetVerificationResult_EmptyJobID(t *testing.T) {
|
|
ctx := context.Background()
|
|
svc, _, _ := newVerificationTestService(map[string]*domain.Job{}, nil)
|
|
|
|
_, err := svc.GetVerificationResult(ctx, "")
|
|
if err == nil {
|
|
t.Error("expected error for empty job ID")
|
|
}
|
|
}
|