mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 17:31:30 +00:00
0e29c416b1
Closes one 2026-04-24 audit finding (P2):
- cat-s6-efc7f6f6bd50: 30 strings.Contains(err.Error(), ...) sites
in internal/api/handler/ — brittle to repository-layer message
changes, untyped against the actual failure mode.
Approach (Option B from prompt design notes):
- New typed sentinels in internal/repository/errors.go:
ErrNotFound, ErrForeignKeyConstraint
IsForeignKeyError(err) helper (the only place substring
matching at the lib/pq boundary is allowed; isolates the
DB-driver string knowledge to one function).
- New typed sentinel in internal/domain/errors.go:
ErrValidation (reserved for future per-entity validation
wrappers; not yet used by all handlers).
- 49 sites in internal/repository/postgres/*.go updated to wrap
sql.ErrNoRows-derived errors via fmt.Errorf("...: %w",
repository.ErrNotFound).
- 18 not-found handler sites + 2 FK-constraint handler sites
refactored to errors.Is(err, repository.ErrNotFound) /
repository.IsForeignKeyError(err).
- 23 inline `fmt.Errorf("X not found")` test fixtures across
handler tests rewrapped to wrap repository.ErrNotFound.
- test_utils.go::ErrMockNotFound rewrapped to wrap
repository.ErrNotFound; renewal_policy.go closure docblock
updated to reflect the new convention.
- integration test mockJobRepository.Get wraps repository.ErrNotFound.
CI regression guardrail:
- .github/workflows/ci.yml::"Forbidden strings.Contains(err.Error())
regression guard (S-2)" greps for the three patterns ("not found",
"violates foreign key", "RESTRICT") under internal/api/handler/
and fails the build on regression.
Verification:
- go build ./... — clean
- go vet ./... — clean
- go test ./... -short -count=1 — all packages pass (handler +
repository + service + integration)
- golangci-lint v2.11.4 run ./... — 0 issues
- S-2 guardrail dry-run on post-fix tree → empty (good)
- All sibling guardrails (S-1, G-3, D-1+D-2, B-1, L-1, H-1, C-1, F-1, P-1) pass
Audit findings closed:
- cat-s6-efc7f6f6bd50 (P2)
Deferred follow-ups:
- 6 domain-specific substring patterns still inline in handlers
("cannot approve", "cannot reject", "cannot be parsed",
"no certificates found", "challenge password", "invalid"/
"required" validation chains in profiles + agent_groups). Each
needs its own typed sentinel, scoped per service. Documented
by the S-2 CI guardrail's allowlist for closure-comments only.
- Per-entity not-found sentinels (Option A — ErrCertificateNotFound,
ErrAgentNotFound, etc.) deferred. Generic ErrNotFound covers the
current dispatch needs; per-entity precision would let handlers
return entity-aware error bodies without a domain.Type field,
but not blocking.
221 lines
6.0 KiB
Go
221 lines
6.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/shankar0123/certctl/internal/domain"
|
|
)
|
|
|
|
// mockNetworkScanService implements NetworkScanService for testing.
|
|
type mockNetworkScanService struct {
|
|
targets []*domain.NetworkScanTarget
|
|
}
|
|
|
|
func (m *mockNetworkScanService) ListTargets(ctx context.Context) ([]*domain.NetworkScanTarget, error) {
|
|
return m.targets, nil
|
|
}
|
|
|
|
func (m *mockNetworkScanService) GetTarget(ctx context.Context, id string) (*domain.NetworkScanTarget, error) {
|
|
for _, t := range m.targets {
|
|
if t.ID == id {
|
|
return t, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("not found: %w", ErrMockNotFound)
|
|
}
|
|
|
|
func (m *mockNetworkScanService) CreateTarget(ctx context.Context, target *domain.NetworkScanTarget) (*domain.NetworkScanTarget, error) {
|
|
if target.Name == "" {
|
|
return nil, fmt.Errorf("name is required")
|
|
}
|
|
target.ID = "nst-test-123"
|
|
m.targets = append(m.targets, target)
|
|
return target, nil
|
|
}
|
|
|
|
func (m *mockNetworkScanService) UpdateTarget(ctx context.Context, id string, target *domain.NetworkScanTarget) (*domain.NetworkScanTarget, error) {
|
|
for _, t := range m.targets {
|
|
if t.ID == id {
|
|
if target.Name != "" {
|
|
t.Name = target.Name
|
|
}
|
|
return t, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("not found: %w", ErrMockNotFound)
|
|
}
|
|
|
|
func (m *mockNetworkScanService) DeleteTarget(ctx context.Context, id string) error {
|
|
for i, t := range m.targets {
|
|
if t.ID == id {
|
|
m.targets = append(m.targets[:i], m.targets[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return fmt.Errorf("not found: %w", ErrMockNotFound)
|
|
}
|
|
|
|
func (m *mockNetworkScanService) TriggerScan(ctx context.Context, targetID string) (*domain.DiscoveryScan, error) {
|
|
for _, t := range m.targets {
|
|
if t.ID == targetID {
|
|
return &domain.DiscoveryScan{
|
|
ID: "dscan-test",
|
|
AgentID: "server-scanner",
|
|
CertificatesFound: 3,
|
|
}, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("not found: %w", ErrMockNotFound)
|
|
}
|
|
|
|
func TestListNetworkScanTargets(t *testing.T) {
|
|
svc := &mockNetworkScanService{
|
|
targets: []*domain.NetworkScanTarget{
|
|
{ID: "nst-1", Name: "target1", CIDRs: []string{"10.0.0.0/24"}, Ports: []int64{443}},
|
|
{ID: "nst-2", Name: "target2", CIDRs: []string{"192.168.0.0/16"}, Ports: []int64{443, 8443}},
|
|
},
|
|
}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/network-scan-targets", nil)
|
|
w := httptest.NewRecorder()
|
|
h.ListNetworkScanTargets(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
var resp PagedResponse
|
|
json.NewDecoder(w.Body).Decode(&resp)
|
|
if resp.Total != 2 {
|
|
t.Errorf("expected total 2, got %d", resp.Total)
|
|
}
|
|
}
|
|
|
|
func TestListNetworkScanTargets_Empty(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/network-scan-targets", nil)
|
|
w := httptest.NewRecorder()
|
|
h.ListNetworkScanTargets(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("expected 200, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateNetworkScanTarget(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
body, _ := json.Marshal(map[string]interface{}{
|
|
"name": "Production",
|
|
"cidrs": []string{"10.0.0.0/24"},
|
|
"ports": []int64{443},
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/network-scan-targets", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
h.CreateNetworkScanTarget(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Errorf("expected 201, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCreateNetworkScanTarget_InvalidJSON(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/network-scan-targets", bytes.NewReader([]byte("not json")))
|
|
w := httptest.NewRecorder()
|
|
h.CreateNetworkScanTarget(w, req)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateNetworkScanTarget_MissingName(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
body, _ := json.Marshal(map[string]interface{}{
|
|
"cidrs": []string{"10.0.0.0/24"},
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/network-scan-targets", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
h.CreateNetworkScanTarget(w, req)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteNetworkScanTarget_NotFound(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/network-scan-targets/nst-nonexistent", nil)
|
|
req.SetPathValue("id", "nst-nonexistent")
|
|
w := httptest.NewRecorder()
|
|
h.DeleteNetworkScanTarget(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("expected 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestTriggerNetworkScan(t *testing.T) {
|
|
svc := &mockNetworkScanService{
|
|
targets: []*domain.NetworkScanTarget{
|
|
{ID: "nst-1", Name: "target1"},
|
|
},
|
|
}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/network-scan-targets/nst-1/scan", nil)
|
|
req.SetPathValue("id", "nst-1")
|
|
w := httptest.NewRecorder()
|
|
h.TriggerNetworkScan(w, req)
|
|
|
|
if w.Code != http.StatusAccepted {
|
|
t.Errorf("expected 202, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestTriggerNetworkScan_NotFound(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/network-scan-targets/nst-nonexistent/scan", nil)
|
|
req.SetPathValue("id", "nst-nonexistent")
|
|
w := httptest.NewRecorder()
|
|
h.TriggerNetworkScan(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Errorf("expected 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestListNetworkScanTargets_MethodNotAllowed(t *testing.T) {
|
|
svc := &mockNetworkScanService{}
|
|
h := NewNetworkScanHandler(svc)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/network-scan-targets", nil)
|
|
w := httptest.NewRecorder()
|
|
h.ListNetworkScanTargets(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("expected 405, got %d", w.Code)
|
|
}
|
|
}
|