mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 20:51:30 +00:00
3287e174dc
Closes the remaining P1 gaps from coverage-gap-audit.md (M-001/M-002/M-003/M-006)
on top of the C-001/C-002 ownership + agent-FK contract fixes landed in
a53a4b8. The work lands as a single commit spanning server, docs, tests,
and the React client.
M-002 — Named API keys with per-key actor propagation
* Migration 000014 adds the 'api_keys' table (id, name, hash,
principal, role, created_at, last_used_at, disabled_at) so every
credential carries an identifiable principal instead of the
opaque 'anonymous'/'api-key' sentinel.
* Auth middleware now rotates through configured keys, performs
constant-time hash comparison, stamps 'last_used_at', and emits
an actor struct via contextWithActor(). The audit middleware,
bulk-revocation handler, approval handlers, and MCP tool layer
now read the principal off the context and persist it on every
audit_events row.
* Regression coverage:
- internal/api/middleware/audit_test.go — actor propagation,
principal redaction for disabled keys, anonymous fallback for
unauthenticated endpoints.
- internal/api/handler/bulk_revocation_handler_test.go,
job_handler_test.go — principal-on-audit assertions.
M-003 — Authorization gates (Phase B)
* Approval handler rejects self-approval / self-rejection with 403
when the actor principal equals the job's requested_by field.
* Bulk revocation is gated behind the 'admin' role; operators and
viewers receive 403.
* Regression coverage:
- internal/service/job_test.go — TestApproveJob_NotSelf,
TestRejectJob_NotSelf.
- internal/api/handler/bulk_revocation_handler_test.go —
TestBulkRevoke_RequiresAdmin, TestBulkRevoke_AdminSucceeds.
M-006 — RFC-compliant CRL/OCSP on the unauthenticated .well-known mux
* Per RFC 8615, relying parties cannot reasonably be asked to
authenticate against the issuing certctl instance to retrieve
revocation material. CRL and OCSP move off the authenticated
'/api/v1/crl*' and '/api/v1/ocsp/*' paths onto:
GET /.well-known/pki/crl/{issuer_id}
Content-Type: application/pkix-crl (RFC 5280 §5)
GET /.well-known/pki/ocsp/{issuer_id}/{serial}
Content-Type: application/ocsp-response (RFC 6960)
* Non-standard JSON CRL shape is removed; only DER is served.
* Short-lived certificate exemption (profile TTL < 1h → skip
CRL/OCSP) is preserved; the response simply omits the serial.
* Routes are registered on the unauthenticated 'finalHandler' mux
in cmd/server/main.go alongside EST ('/.well-known/est/*') and
SCEP ('/scep'). Legacy authenticated paths return 404.
* Regression coverage:
- internal/api/handler/certificate_handler_test.go — content
type, DER parseability, 404 for unknown issuer.
- internal/api/handler/adversarial_path_test.go — unauthenticated
access asserted for CRL, OCSP, EST, SCEP.
- internal/api/router/router_test.go — route-table assertion
that '.well-known/pki/*', '.well-known/est/*', and '/scep' are
mounted on the unauthenticated branch.
M-001 — Auto-closed by M-002
EST and SCEP were already registered on the unauthenticated
'finalHandler' mux; the router comment at
internal/api/router/router.go:247 now matches reality. The
adversarial-path tests above lock the behavior in.
Verification (all gates green):
* go vet ./... — clean
* go build ./... — ok
* go test -short ./... (55+ packages) — all pass
* web/ : npm test (225 Vitest tests) — all pass
* web/ : npx tsc --noEmit — clean
* grep sweep for '/api/v1/(crl|ocsp)' — 13 surviving hits,
all intentional M-006 tombstone/relocation comments.
Documentation:
* coverage-gap-audit.md — status flips M-001/M-002/M-003/M-006 →
Fixed, with per-finding resolution paragraphs citing regression
test IDs. (Audit file lives outside this repo; see cowork root.)
* CLAUDE.md Project Status line updated with the auth-unification
closure note.
* docs/features.md, docs/architecture.md, docs/quickstart.md,
docs/concepts.md, docs/connectors.md, docs/test-env.md,
docs/testing-guide.md, docs/compliance-*.md, docs/demo-advanced.md
— refreshed for the new '.well-known/pki/*' namespace and named
API keys.
* api/openapi.yaml — documents the new unauthenticated endpoints
and removes the legacy '/api/v1/crl*' + '/api/v1/ocsp/*' paths.
.gitignore: adds '/.gocache/' and '/.gomodcache/' for the session-
scoped Go caches so they never enter the tree.
107 lines
3.5 KiB
Go
107 lines
3.5 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)
|
|
}
|