mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 20:31: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.
293 lines
10 KiB
Go
293 lines
10 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/api/middleware"
|
|
"github.com/certctl-io/certctl/internal/service"
|
|
)
|
|
|
|
// EST RFC 7030 hardening master bundle Phase 7.4 — admin handler tests.
|
|
// Mirrors admin_scep_intune_test.go's structure verbatim:
|
|
// - M-008 admin-gate triplet for both endpoints (non-admin / admin=false / admin=true).
|
|
// - Method-not-allowed gates.
|
|
// - Error mapping (404 unknown PathID / 409 mTLS-disabled / 500 underlying parse error).
|
|
|
|
// fakeAdminESTService is the test stub. Records call observations so the
|
|
// M-008 admin-gate triplet can pin "service was never invoked" when the
|
|
// gate rejects the caller.
|
|
type fakeAdminESTService struct {
|
|
profilesCalled bool
|
|
reloadCalled bool
|
|
rows []service.ESTStatsSnapshot
|
|
profilesErr error
|
|
reloadPathID string
|
|
reloadErr error
|
|
}
|
|
|
|
func (f *fakeAdminESTService) Profiles(_ context.Context, _ time.Time) ([]service.ESTStatsSnapshot, error) {
|
|
f.profilesCalled = true
|
|
return f.rows, f.profilesErr
|
|
}
|
|
|
|
func (f *fakeAdminESTService) ReloadTrust(_ context.Context, pathID string) error {
|
|
f.reloadCalled = true
|
|
f.reloadPathID = pathID
|
|
return f.reloadErr
|
|
}
|
|
|
|
// ----- M-008 admin-gate triplet for Profiles (GET) -----
|
|
|
|
func TestAdminEST_Profiles_NonAdmin_Returns403(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/est/profiles", nil)
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
h.Profiles(w, req)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("non-admin status = %d, want 403", w.Code)
|
|
}
|
|
if svc.profilesCalled {
|
|
t.Errorf("service was invoked despite non-admin caller — gate failed open")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_Profiles_AdminExplicitFalse_Returns403(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/est/profiles", nil)
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, false)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.Profiles(w, req)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("admin=false status = %d, want 403", w.Code)
|
|
}
|
|
if svc.profilesCalled {
|
|
t.Errorf("service was invoked despite admin=false — gate failed open")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_Profiles_AdminTrue_Returns200(t *testing.T) {
|
|
svc := &fakeAdminESTService{
|
|
rows: []service.ESTStatsSnapshot{
|
|
{PathID: "corp", IssuerID: "iss-corp"},
|
|
},
|
|
}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/est/profiles", nil)
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.Profiles(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("admin status = %d, want 200; body = %q", w.Code, w.Body.String())
|
|
}
|
|
var resp map[string]any
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if pc, _ := resp["profile_count"].(float64); int(pc) != 1 {
|
|
t.Errorf("profile_count = %v, want 1", resp["profile_count"])
|
|
}
|
|
if !svc.profilesCalled {
|
|
t.Error("service should have been called")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_Profiles_MethodNotAllowed(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/profiles", nil)
|
|
w := httptest.NewRecorder()
|
|
h.Profiles(w, req)
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("POST against GET-only endpoint status = %d, want 405", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_Profiles_NilRowsSerializedAsEmptyArray(t *testing.T) {
|
|
svc := &fakeAdminESTService{rows: nil}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/est/profiles", nil)
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.Profiles(w, req)
|
|
body := w.Body.String()
|
|
if strings.Contains(body, `"profiles":null`) {
|
|
t.Errorf("profiles serialised as null; want []. body=%q", body)
|
|
}
|
|
}
|
|
|
|
// ----- M-008 admin-gate triplet for ReloadTrust (POST) -----
|
|
|
|
func TestAdminEST_ReloadTrust_NonAdmin_Returns403(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(`{"path_id":"corp"}`))
|
|
req.ContentLength = int64(len(`{"path_id":"corp"}`))
|
|
req = req.WithContext(contextWithRequestID())
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("non-admin status = %d, want 403", w.Code)
|
|
}
|
|
if svc.reloadCalled {
|
|
t.Errorf("service was invoked despite non-admin caller — gate failed open")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_AdminExplicitFalse_Returns403(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(`{"path_id":"corp"}`))
|
|
req.ContentLength = int64(len(`{"path_id":"corp"}`))
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, false)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("admin=false status = %d, want 403", w.Code)
|
|
}
|
|
if svc.reloadCalled {
|
|
t.Errorf("service was invoked despite admin=false — gate failed open")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_HappyPath(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
body := `{"path_id":"corp"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(body))
|
|
req.ContentLength = int64(len(body))
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body = %q", w.Code, w.Body.String())
|
|
}
|
|
if svc.reloadPathID != "corp" {
|
|
t.Errorf("reloadPathID = %q, want %q", svc.reloadPathID, "corp")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_UnknownPathID_Returns404(t *testing.T) {
|
|
svc := &fakeAdminESTService{reloadErr: ErrAdminESTProfileNotFound}
|
|
h := NewAdminESTHandler(svc)
|
|
body := `{"path_id":"nope"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(body))
|
|
req.ContentLength = int64(len(body))
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("unknown path_id status = %d, want 404", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_MTLSDisabled_Returns409(t *testing.T) {
|
|
svc := &fakeAdminESTService{reloadErr: service.ErrESTMTLSDisabled}
|
|
h := NewAdminESTHandler(svc)
|
|
body := `{"path_id":"static-only"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(body))
|
|
req.ContentLength = int64(len(body))
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusConflict {
|
|
t.Errorf("mTLS-disabled status = %d, want 409", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_ParseError_Returns500(t *testing.T) {
|
|
svc := &fakeAdminESTService{reloadErr: errors.New("trustanchor: cert in /etc/est-corp.pem expired at 2020-01-01")}
|
|
h := NewAdminESTHandler(svc)
|
|
body := `{"path_id":"corp"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(body))
|
|
req.ContentLength = int64(len(body))
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Errorf("parse-error status = %d, want 500", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_MalformedJSON_Returns400(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
body := `not-json`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/est/reload-trust",
|
|
strings.NewReader(body))
|
|
req.ContentLength = int64(len(body))
|
|
ctx := context.WithValue(context.Background(), middleware.RequestIDKey{}, "test-request-id")
|
|
ctx = context.WithValue(ctx, middleware.AdminKey{}, true)
|
|
req = req.WithContext(ctx)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("malformed-JSON status = %d, want 400", w.Code)
|
|
}
|
|
if svc.reloadCalled {
|
|
t.Errorf("service called despite malformed body")
|
|
}
|
|
}
|
|
|
|
func TestAdminEST_ReloadTrust_MethodNotAllowed(t *testing.T) {
|
|
svc := &fakeAdminESTService{}
|
|
h := NewAdminESTHandler(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/est/reload-trust", nil)
|
|
w := httptest.NewRecorder()
|
|
h.ReloadTrust(w, req)
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("GET against POST-only endpoint status = %d, want 405", w.Code)
|
|
}
|
|
}
|
|
|
|
// ----- AdminESTServiceImpl plumbing -----
|
|
|
|
func TestAdminESTServiceImpl_NilMapAccepted(t *testing.T) {
|
|
svc := NewAdminESTServiceImpl(nil)
|
|
rows, err := svc.Profiles(context.Background(), time.Now())
|
|
if err != nil {
|
|
t.Fatalf("Profiles: %v", err)
|
|
}
|
|
if len(rows) != 0 {
|
|
t.Errorf("nil-map should produce empty profile list; got %d", len(rows))
|
|
}
|
|
}
|
|
|
|
func TestAdminESTServiceImpl_ReloadTrust_UnknownPath_NotFound(t *testing.T) {
|
|
svc := NewAdminESTServiceImpl(map[string]*service.ESTService{})
|
|
if err := svc.ReloadTrust(context.Background(), "nonexistent"); !errors.Is(err, ErrAdminESTProfileNotFound) {
|
|
t.Errorf("unknown path_id err = %v, want ErrAdminESTProfileNotFound", err)
|
|
}
|
|
}
|