mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 18:31:37 +00:00
5a682db8e2
+ Cisco IOS quirk fixtures + ManagedCertificate.Source provenance + EST bulk-revoke endpoint + 13 typed audit action codes. Phase 10.1 — libest reference-client sidecar: - deploy/test/libest/Dockerfile: multi-stage Debian-bookworm-slim build of Cisco's libest v3.2.0-2 from source (autoconf/automake/ libtool + libcurl4-openssl-dev + libssl-dev). Runtime stage carries only estclient + bash + openssl + ca-certificates so the exec surface stays small + predictable. - docker-compose.test.yml libest-client entry (profiles: [est-e2e]) with bind mounts for /config/est (test workspace) + /config/certs (certctl CA bundle for TLS pinning); IP 10.30.50.9 (10.30.50.8 was already taken by certctl-agent). - deploy/test/est/.gitkeep keeps the bind-mount target tracked. Phase 10.2 — 5 integration tests (//go:build integration) in deploy/test/est_e2e_test.go: - TestEST_LibESTClient_Enrollment_Integration (cacerts → simpleenroll → cert-shape assertion) - TestEST_LibESTClient_MTLSEnrollment_Integration (mTLS sibling-route cert auth; skip when bootstrap cert absent) - TestEST_LibESTClient_ServerKeygen_Integration (RFC 7030 §4.4 multipart; skip when profile gate disabled) - TestEST_LibESTClient_RateLimited_Integration (4th enroll trips per-principal cap, asserts 429-shaped error) - TestEST_LibESTClient_ChannelBinding_Integration (libest --tls-exporter; skip when libest build lacks the flag). - requireESTSidecar guard skips the suite when the operator forgot --profile est-e2e; helpful error message includes the exact command to bring the sidecar up. Phase 10.3 — Cisco IOS quirk fixtures + 3 unit tests in internal/api/handler/cisco_ios_quirks_test.go: - testdata/cisco_ios_15x_pem_csr.txt: PEM body sent with Content-Type application/x-pem-file. Handler dispatches on body-prefix not Content-Type — accepts cleanly. - testdata/cisco_ios_16x_trailing_newline_csr.txt: extra trailing newlines after base64 body. strings.TrimSpace tolerates. - testdata/cisco_ios_crlf_b64_csr.txt: CRLF-wrapped base64. base64.StdEncoding handles CRLF + LF identically. Phase 11.1 — ManagedCertificate.Source provenance: - New domain.CertificateSource enum (Unspecified/EST/SCEP/API/Agent). - Migration 000023_managed_certificates_source.up.sql adds source TEXT NOT NULL DEFAULT '' so existing rows scan as CertificateSourceUnspecified — back-compat: bulk-revoke filter treats empty as "any source". - Postgres repo Insert/Update/scan paths all wire the new column. Phase 11.2 — EST bulk-revoke endpoint: - BulkRevocationCriteria.Source field (Source-only requests rejected as too broad — must accompany at least one narrower criterion). - service.bulk_revocation.resolveCertificates post-filter by Source (empty=any, no SQL change so existing CertificateFilter callers unaffected). - New BulkRevocationHandler.BulkRevokeEST method pins Source=EST + dispatches; new route POST /api/v1/est/certificates/bulk-revoke (M-008 admin-gated). openapi.yaml documented + parity-guard green. Phase 11.3 — 13 typed audit action codes in internal/service/est_audit_actions.go: - est_simple_enroll_success / _failed - est_simple_reenroll_success / _failed - est_server_keygen_success / _failed - est_auth_failed_basic / _mtls / _channel_binding - est_rate_limited - est_csr_policy_violation - est_bulk_revoke - est_trust_anchor_reloaded - ESTService.processEnrollment + SimpleServerKeygen + ReloadTrust split-emit BOTH the legacy bare action codes (back-compat for the GUI activity-tab chip filters that match by exact string + existing audit-log analysers) AND the new typed _success / _failed variants (operator grep target + per-failure-mode counter). Tests: - internal/api/handler/bulk_revocation_est_test.go — 5 cases (admin-true happy path pins Source=EST + non-admin 403 + empty-criteria 400 + invalid-reason 400 + method-not-allowed). - internal/service/est_audit_actions_test.go — 5 cases (SimpleEnroll legacy+typed emission / SimpleReEnroll typed / IssuerError typed-failed / PolicyViolation triple-emit / unique-string invariant). Pre-commit verification (sandbox): gofmt clean, go vet clean (excluding repository/postgres testcontainers limit), staticcheck clean across api/handler/api/router/domain/service/deploy/test, go test -short -count=1 green for every non-postgres Go package + integration build (`go build -tags integration ./deploy/test/...`) clean. G-3 docs-drift guard reproduced locally clean (Phases 10-11 added zero new env vars). Spec preserved at cowork/est-rfc7030-hardening-prompt.md. Phases 12-13 (docs/est.md + WiFi/802.1X / IoT bootstrap / FreeRADIUS recipes; release prep + tag) remain — post-2.1.0 work.
176 lines
6.3 KiB
Go
176 lines
6.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/shankar0123/certctl/internal/api/middleware"
|
|
"github.com/shankar0123/certctl/internal/domain"
|
|
)
|
|
|
|
// BulkRevocationService defines the service interface for bulk certificate revocation.
|
|
type BulkRevocationService interface {
|
|
BulkRevoke(ctx context.Context, criteria domain.BulkRevocationCriteria, reason string, actor string) (*domain.BulkRevocationResult, error)
|
|
}
|
|
|
|
// BulkRevocationHandler handles HTTP requests for bulk revocation operations.
|
|
type BulkRevocationHandler struct {
|
|
svc BulkRevocationService
|
|
}
|
|
|
|
// NewBulkRevocationHandler creates a new BulkRevocationHandler.
|
|
func NewBulkRevocationHandler(svc BulkRevocationService) BulkRevocationHandler {
|
|
return BulkRevocationHandler{svc: svc}
|
|
}
|
|
|
|
// bulkRevokeRequest represents the JSON request body for bulk revocation.
|
|
type bulkRevokeRequest struct {
|
|
Reason string `json:"reason"`
|
|
ProfileID string `json:"profile_id,omitempty"`
|
|
OwnerID string `json:"owner_id,omitempty"`
|
|
AgentID string `json:"agent_id,omitempty"`
|
|
IssuerID string `json:"issuer_id,omitempty"`
|
|
TeamID string `json:"team_id,omitempty"`
|
|
CertificateIDs []string `json:"certificate_ids,omitempty"`
|
|
}
|
|
|
|
// BulkRevoke handles bulk certificate revocation.
|
|
// POST /api/v1/certificates/bulk-revoke
|
|
//
|
|
// M-003: admin-only. Bulk revocation is a fleet-scale destructive operation —
|
|
// a non-admin caller must not be able to invalidate certificates across
|
|
// profiles/owners/agents. The gate is enforced here (before body parsing) so a
|
|
// non-admin never sees its request criteria evaluated.
|
|
func (h BulkRevocationHandler) BulkRevoke(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
Error(w, http.StatusMethodNotAllowed, "Method not allowed")
|
|
return
|
|
}
|
|
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
|
|
// M-003: admin-only gate. Non-admin callers are rejected before any
|
|
// criteria/body processing to avoid leaking validation behavior to
|
|
// unauthorized actors.
|
|
if !middleware.IsAdmin(r.Context()) {
|
|
ErrorWithRequestID(w, http.StatusForbidden,
|
|
"Bulk revocation requires admin privileges",
|
|
requestID)
|
|
return
|
|
}
|
|
|
|
var req bulkRevokeRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "Invalid request body", requestID)
|
|
return
|
|
}
|
|
|
|
// Validate reason is present
|
|
if req.Reason == "" {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "Revocation reason is required", requestID)
|
|
return
|
|
}
|
|
|
|
// Validate reason is a valid RFC 5280 code
|
|
if !domain.IsValidRevocationReason(req.Reason) {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "Invalid revocation reason: "+req.Reason, requestID)
|
|
return
|
|
}
|
|
|
|
criteria := domain.BulkRevocationCriteria{
|
|
ProfileID: req.ProfileID,
|
|
OwnerID: req.OwnerID,
|
|
AgentID: req.AgentID,
|
|
IssuerID: req.IssuerID,
|
|
TeamID: req.TeamID,
|
|
CertificateIDs: req.CertificateIDs,
|
|
}
|
|
|
|
// Safety guard: at least one criterion required
|
|
if criteria.IsEmpty() {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "At least one filter criterion is required (profile_id, owner_id, agent_id, issuer_id, team_id, or certificate_ids)", requestID)
|
|
return
|
|
}
|
|
|
|
// Extract actor from auth context (M-002: named-key identity → audit trail)
|
|
actor := resolveActor(r.Context())
|
|
|
|
result, err := h.svc.BulkRevoke(r.Context(), criteria, req.Reason, actor)
|
|
if err != nil {
|
|
ErrorWithRequestID(w, http.StatusInternalServerError, "Bulk revocation failed: "+err.Error(), requestID)
|
|
return
|
|
}
|
|
|
|
JSON(w, http.StatusOK, result)
|
|
}
|
|
|
|
// BulkRevokeEST handles EST-source-scoped bulk certificate revocation.
|
|
// POST /api/v1/est/certificates/bulk-revoke
|
|
//
|
|
// EST RFC 7030 hardening master bundle Phase 11.2.
|
|
//
|
|
// Identical to BulkRevoke above but the Source criterion is pinned to
|
|
// CertificateSourceEST so the operation only affects certs the EST
|
|
// service stamped at issuance time. Operators who want to revoke
|
|
// "every cert this device family ever issued through EST" hit this
|
|
// endpoint with a profile_id / owner_id / etc. criterion + the
|
|
// handler narrows the result set to EST-only.
|
|
//
|
|
// Same M-008 admin-gate as the generic BulkRevoke. Audit action
|
|
// emitted by the service is `est_bulk_revoke` (typed code from Phase
|
|
// 11.3) so operators grep on the action string distinguishes
|
|
// EST-bulk-revoke from the generic bulk-revoke.
|
|
func (h BulkRevocationHandler) BulkRevokeEST(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
Error(w, http.StatusMethodNotAllowed, "Method not allowed")
|
|
return
|
|
}
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
if !middleware.IsAdmin(r.Context()) {
|
|
ErrorWithRequestID(w, http.StatusForbidden,
|
|
"EST bulk revocation requires admin privileges", requestID)
|
|
return
|
|
}
|
|
var req bulkRevokeRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "Invalid request body", requestID)
|
|
return
|
|
}
|
|
if req.Reason == "" {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "Revocation reason is required", requestID)
|
|
return
|
|
}
|
|
if !domain.IsValidRevocationReason(req.Reason) {
|
|
ErrorWithRequestID(w, http.StatusBadRequest, "Invalid revocation reason: "+req.Reason, requestID)
|
|
return
|
|
}
|
|
criteria := domain.BulkRevocationCriteria{
|
|
ProfileID: req.ProfileID,
|
|
OwnerID: req.OwnerID,
|
|
AgentID: req.AgentID,
|
|
IssuerID: req.IssuerID,
|
|
TeamID: req.TeamID,
|
|
CertificateIDs: req.CertificateIDs,
|
|
// Pin Source to EST — operators MUST also supply at least one
|
|
// narrower criterion (criteria.IsEmpty intentionally excludes
|
|
// Source so a Source-only request is still rejected as too
|
|
// broad). This protects against "revoke every EST cert in the
|
|
// fleet" via a malformed body.
|
|
Source: domain.CertificateSourceEST,
|
|
}
|
|
if criteria.IsEmpty() {
|
|
ErrorWithRequestID(w, http.StatusBadRequest,
|
|
"At least one narrower criterion is required (profile_id, owner_id, agent_id, issuer_id, team_id, or certificate_ids); EST bulk-revoke is implicitly Source-scoped to EST",
|
|
requestID)
|
|
return
|
|
}
|
|
actor := resolveActor(r.Context())
|
|
result, err := h.svc.BulkRevoke(r.Context(), criteria, req.Reason, actor)
|
|
if err != nil {
|
|
ErrorWithRequestID(w, http.StatusInternalServerError, "EST bulk revocation failed: "+err.Error(), requestID)
|
|
return
|
|
}
|
|
JSON(w, http.StatusOK, result)
|
|
}
|