Files
certctl/internal/api/router/router_scep_profiles_test.go
T
shankar0123 8b75e0311b 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 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.
2026-05-04 00:30:29 +00:00

184 lines
7.2 KiB
Go

package router
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/certctl-io/certctl/internal/api/handler"
"github.com/certctl-io/certctl/internal/domain"
)
// SCEP RFC 8894 + Intune master bundle Phase 1.5: per-issuer profiles router
// registration. Pins:
//
// 1. Empty PathID maps to /scep root (legacy backward-compat).
// 2. Non-empty PathID maps to /scep/<pathID>.
// 3. Multi-profile registration produces 2N routes (GET + POST per profile).
// 4. Each registered route reaches the right handler instance — no
// cross-profile bleed-through (proven by the per-profile mock counters).
//
// The mock service is a minimal SCEPService implementation that records
// which profile served the request via the GetCACaps capability string —
// the test asserts it sees the right per-profile string echoed back, which
// would only happen if the right handler was wired to the right path.
// scepProfileMockService is a per-profile-tagged mock SCEPService for
// router-level tests. The CACaps string carries the profile tag so the
// caller can verify which profile's handler served a given request.
type scepProfileMockService struct {
tag string
}
func (s *scepProfileMockService) GetCACaps(_ context.Context) string {
return "POSTPKIOperation\nSHA-256\nPROFILE=" + s.tag + "\n"
}
func (s *scepProfileMockService) GetCACert(_ context.Context) (string, error) {
return "", nil
}
func (s *scepProfileMockService) PKCSReq(_ context.Context, _, _, _ string) (*domain.SCEPEnrollResult, error) {
return nil, nil
}
// PKCSReqWithEnvelope / RenewalReqWithEnvelope / GetCertInitialWithEnvelope
// were added to the SCEPService interface in SCEP RFC 8894 + Intune master
// bundle Phase 2.4 + Phase 4. The router-level tests don't drive the
// RFC 8894 path; these stubs satisfy the interface so the per-profile
// dispatch tests still compile.
func (s *scepProfileMockService) PKCSReqWithEnvelope(_ context.Context, _, _ string, env *domain.SCEPRequestEnvelope) *domain.SCEPResponseEnvelope {
return &domain.SCEPResponseEnvelope{Status: domain.SCEPStatusSuccess, TransactionID: env.TransactionID}
}
func (s *scepProfileMockService) RenewalReqWithEnvelope(_ context.Context, _, _ string, env *domain.SCEPRequestEnvelope) *domain.SCEPResponseEnvelope {
return &domain.SCEPResponseEnvelope{Status: domain.SCEPStatusSuccess, TransactionID: env.TransactionID}
}
func (s *scepProfileMockService) GetCertInitialWithEnvelope(_ context.Context, env *domain.SCEPRequestEnvelope) *domain.SCEPResponseEnvelope {
return &domain.SCEPResponseEnvelope{Status: domain.SCEPStatusFailure, FailInfo: domain.SCEPFailBadCertID, TransactionID: env.TransactionID}
}
func TestRouter_RegisterSCEPHandlers_LegacyEmptyPathIDMapsToRoot(t *testing.T) {
r := New()
svc := &scepProfileMockService{tag: "legacy"}
r.RegisterSCEPHandlers(map[string]handler.SCEPHandler{
"": handler.NewSCEPHandler(svc),
})
// GetCACaps is GET-only per RFC 8894 §3.5.2. The router registers BOTH
// GET and POST; the handler decides what each operation accepts. We
// exercise GET here (POST PKIOperation is exercised by the existing
// internal/api/handler tests and by the e2e suite).
req := httptest.NewRequest(http.MethodGet, "/scep?operation=GetCACaps", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("GET /scep — code %d, want 200 (body=%q)", w.Code, w.Body.String())
}
if got := w.Body.String(); !contains(got, "PROFILE=legacy") {
t.Errorf("GET /scep body = %q, want contains PROFILE=legacy", got)
}
// Confirm POST /scep IS registered at the router level (the handler
// will respond 405 for GetCACaps because it's GET-only, but the route
// has to exist or we'd get a 404 from the mux instead).
req = httptest.NewRequest(http.MethodPost, "/scep?operation=GetCACaps", nil)
w = httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("POST /scep?operation=GetCACaps — code %d, want 405 (route registered, handler rejects POST for GetCACaps)", w.Code)
}
}
func TestRouter_RegisterSCEPHandlers_NonEmptyPathIDMapsToSubpath(t *testing.T) {
r := New()
svc := &scepProfileMockService{tag: "corp"}
r.RegisterSCEPHandlers(map[string]handler.SCEPHandler{
"corp": handler.NewSCEPHandler(svc),
})
// GET /scep/corp?operation=GetCACaps reaches the corp handler.
req := httptest.NewRequest(http.MethodGet, "/scep/corp?operation=GetCACaps", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("GET /scep/corp — code %d, want 200 (body=%q)", w.Code, w.Body.String())
}
if got := w.Body.String(); !contains(got, "PROFILE=corp") {
t.Errorf("GET /scep/corp body = %q, want contains PROFILE=corp", got)
}
// POST /scep/corp must also be registered (the handler will reject
// GetCACaps as 405; we just confirm the route exists).
req = httptest.NewRequest(http.MethodPost, "/scep/corp?operation=GetCACaps", nil)
w = httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("POST /scep/corp?operation=GetCACaps — code %d, want 405 (route registered, handler rejects POST for GetCACaps)", w.Code)
}
// /scep root must NOT be registered when only non-empty PathIDs exist.
req = httptest.NewRequest(http.MethodGet, "/scep?operation=GetCACaps", nil)
w = httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusNotFound && w.Code != http.StatusMethodNotAllowed {
t.Errorf("/scep without legacy profile — code %d, want 404 or 405 (no handler should be registered)", w.Code)
}
}
func TestRouter_RegisterSCEPHandlers_MultipleProfilesNoCrossBleed(t *testing.T) {
r := New()
r.RegisterSCEPHandlers(map[string]handler.SCEPHandler{
"": handler.NewSCEPHandler(&scepProfileMockService{tag: "default"}),
"corp": handler.NewSCEPHandler(&scepProfileMockService{tag: "corp"}),
"iot": handler.NewSCEPHandler(&scepProfileMockService{tag: "iot"}),
})
cases := []struct {
path string
wantTag string
}{
{"/scep?operation=GetCACaps", "default"},
{"/scep/corp?operation=GetCACaps", "corp"},
{"/scep/iot?operation=GetCACaps", "iot"},
}
for _, tc := range cases {
t.Run(tc.path, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tc.path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("code %d, want 200", w.Code)
}
if got := w.Body.String(); !contains(got, "PROFILE="+tc.wantTag) {
t.Errorf("body = %q, want contains PROFILE=%s", got, tc.wantTag)
}
})
}
}
func TestRouter_RegisterSCEPHandlers_EmptyMapRegistersNoRoutes(t *testing.T) {
r := New()
r.RegisterSCEPHandlers(map[string]handler.SCEPHandler{})
req := httptest.NewRequest(http.MethodGet, "/scep?operation=GetCACaps", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusNotFound && w.Code != http.StatusMethodNotAllowed {
t.Errorf("/scep with no profiles registered — code %d, want 404 or 405", w.Code)
}
}
// Tiny helper local to this file to avoid importing strings just for one
// substring check; keeps the test file's import surface minimal.
func contains(haystack, needle string) bool {
if len(needle) == 0 {
return true
}
for i := 0; i+len(needle) <= len(haystack); i++ {
if haystack[i:i+len(needle)] == needle {
return true
}
}
return false
}