mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 13:31:36 +00:00
44a85d6f85
Layers JWS-authenticated POST machinery onto the Phase 1a foundation
(commit ec88a61). After this commit, an ACME client can run
POST /acme/profile/<id>/new-account
against certctl and successfully register an account. Account update
+ deactivation via POST /acme/profile/<id>/account/<acc-id> work.
Orders + challenges remain Phase 2 / 3.
Background:
Two prior dispatch attempts at the original Phase 1 ("skeleton +
directory + new-nonce + new-account" as a single commit) failed on
go-jose v4 API speculation (jws.GetPayload, sig.Algorithm,
jose.SHA256, etc. — none of those exist in v4). Splitting Phase 1
into 1a (foundation, no go-jose) and 1b (this commit, all go-jose
in one place) concentrated the JWS work where attention pays off.
The verifier reads the actual go-jose v4 surface — ParseSigned with
closed alg allow-list, Header struct fields (Algorithm, KeyID,
JSONWebKey, Nonce, ExtraHeaders[HeaderKey]), JWK.Thumbprint with
stdlib crypto.SHA256.
What ships:
- internal/api/acme/jws.go: 487-line verifier + sentinel error
family. Enforces RFC 8555 §6.2 + §6.4 + §6.5 invariants:
- alg in {RS256, ES256, EdDSA} (closed allow-list passed to
jose.ParseSigned — HS256 / none / etc. rejected at parse time)
- exactly one of `kid` / `jwk` in protected header (per
endpoint policy — new-account demands jwk, others demand kid)
- protected `url` matches request URL exactly
- protected `nonce` consumed against acme_nonces (badNonce on
miss/replay/expiry per RFC 8555 §6.5.1)
- kid round-trips against canonical AccountKID(accountID) URL
(catches cross-profile / cross-host replay)
- kid path: account exists + status=valid (deactivated /
revoked accounts cannot authenticate)
- signature verifies; post-Verify payload bytes equal
UnsafePayloadWithoutVerification (defense in depth)
+ JWK persistence helpers (JWKToPEM / ParseJWKFromPEM round-
trip a public-only JWK as a PEM-wrapped JSON envelope; stored
as TEXT in acme_accounts.jwk_pem for diff-friendliness) +
JWKThumbprint per RFC 7638.
- internal/api/acme/jws_test.go: 16 cases covering happy paths
(RS256 kid, ES256 jwk, EdDSA kid) + every named failure mode
(alg-not-allowed, bad-sig, missing-nonce, unknown-nonce,
replay, url-mismatch, mixed kid+jwk, deactivated-account,
cross-host kid). Uses real keypairs + real go-jose Signer to
build JWS objects.
- internal/api/acme/account.go: NewAccountRequest /
AccountUpdateRequest payload shapes (RFC 8555 §7.3 + §7.3.2 +
§7.3.6) + AccountResponseJSON wire shape + MarshalAccount
helper.
- internal/domain/acme.go: ACMEAccount struct + ACMEAccountStatus
closed enum (valid / deactivated / revoked).
- internal/repository/postgres/acme.go: full account CRUD path
(CreateAccountWithTx with 23505-unique-violation sentinel
translation, GetAccountByID, GetAccountByThumbprint,
UpdateAccountContactWithTx, UpdateAccountStatusWithTx) +
sql.ErrNoRows-wrapped repository.ErrNotFound on lookup misses.
- internal/service/acme.go: ACMERepo interface extended;
SetTransactor + SetAuditService wires; NewAccount (idempotent
re-registration per RFC 8555 §7.3.1 — same JWK returns existing
row without an update or new audit event); LookupAccount;
UpdateAccount; DeactivateAccount; VerifyJWS adapter that bridges
api/acme.VerifierConfig to the service-layer ACMERepo; per-op
metrics extended (new_account_total + _failures_total +
_idempotent_total + update_account_total + _failures_total +
deactivate_account_total).
- internal/service/acme_test.go: 8 new tests covering
new-account happy path / idempotent re-registration / only-
return-existing match + no-match / contact update / deactivate
/ lookup-not-found / requires-transactor.
- internal/api/handler/acme.go: NewAccount + Account handlers.
Account dispatches POST-as-GET (RFC 8555 §6.3 — empty body or
{} payload returns the account row), contact update, and
deactivation from the same endpoint. Defense-in-depth check
that the kid path-segment matches the URL path-segment (the
verifier already round-tripped the kid against canonical URL,
but the handler re-asserts to catch any future verifier
refactor).
- internal/api/handler/acme_handler_test.go: 7 new cases
covering happy-create, idempotent-200, only-return-existing-
no-match-400, malformed-JWS-400, kid-URL-mismatch-401,
deactivate, contact-update, POST-as-GET.
- internal/api/router/router.go: 4 new Register calls (per-
profile + shorthand for new-account and account/{acc_id}).
- internal/api/router/openapi_parity_test.go: SpecParityExceptions
extended with the 4 new routes (RFC 8555 wire-protocol surface,
not OpenAPI-shaped — same precedent as Phase 1a).
- cmd/server/main.go: SetTransactor + SetAuditService on
acmeService at startup so the WithinTx-based new-account /
update / deactivate paths run with the same transactor instance
shared across CertificateService / RevocationSvc / RenewalService.
- docs/acme-server.md: Phase status updated; endpoints table grows
new-account + account/<acc_id> rows; new "JWS verification
(Phase 1b)" section enumerates the 7 invariants the verifier
enforces; phases-cross-reference table marks 1b live.
- go.mod / go.sum: github.com/go-jose/go-jose/v4 v4.0.4 added.
Atomicity: every account-state mutation writes its acme_accounts row
+ its audit_events row inside one repository.Transactor.WithinTx
call — the canonical certctl atomicity contract (matches
CertificateService.Create at internal/service/certificate.go:131).
Idempotent re-registration explicitly does NOT write an audit row
(RFC 8555 §7.3.1 returns the existing row unmodified).
Tests: 16 jws_test.go cases + 11 service tests + 11 handler tests
all pass under -short. Bad-signature test uses a real registered
account whose stored JWK is a different keypair from the signer's,
so the JWS parses cleanly but jose.Verify rejects — exercises the
ErrJWSSignatureInvalid path directly.
Engineering history: cowork/WORKSPACE-CHANGELOG.md "ACME-Server-1b".
215 lines
8.2 KiB
Go
215 lines
8.2 KiB
Go
package router
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Bundle D / Audit M-027: pin the router ↔ OpenAPI spec parity.
|
|
//
|
|
// The audit reported "router 121 vs OpenAPI 125 — 4 op gap" by counting
|
|
// r.Register call sites with a regex. That methodology is incomplete: the
|
|
// router additionally registers 4 routes via direct r.mux.Handle calls
|
|
// (the Bundle B / M-002 AuthExemptRouterRoutes — health/ready/auth-info/
|
|
// version). When you count BOTH dispatch shapes the totals match exactly.
|
|
//
|
|
// This test:
|
|
// 1. Walks router.go's AST to enumerate every (method, path) tuple from
|
|
// both r.Register AND r.mux.Handle sites.
|
|
// 2. Walks api/openapi.yaml's path/method nesting to enumerate every
|
|
// documented operation.
|
|
// 3. Asserts the two sets are identical (modulo a tiny exception list
|
|
// for routes that legitimately don't appear in the spec).
|
|
//
|
|
// Adding a new route without updating openapi.yaml fails this test.
|
|
|
|
// SpecParityExceptions is the documented allowlist of (method, path)
|
|
// tuples that are intentionally NOT in api/openapi.yaml. Each entry must
|
|
// have a justification — typically "internal" or "non-stable surface".
|
|
//
|
|
// At Bundle D close time, this list is empty. Future entries should be
|
|
// rare — the OpenAPI spec is the source of truth for the public API
|
|
// surface.
|
|
var SpecParityExceptions = map[string]string{
|
|
// SCEP RFC 8894 + Intune master bundle Phase 6.5: the /scep-mtls
|
|
// sibling route is opt-in (gated on per-profile MTLSEnabled). It rides
|
|
// the same SCEP-PKIOperation contract as /scep but with an additional
|
|
// client-cert auth layer at the handler. The OpenAPI spec covers the
|
|
// canonical /scep endpoint; documenting /scep-mtls separately would
|
|
// duplicate every operation row with no information gain — the
|
|
// PKIMessage wire format, query params, and response shapes are
|
|
// identical. The route lives in router.go as literal r.Register calls
|
|
// for the openapi-parity scanner's benefit; it stays out of openapi.yaml
|
|
// by exception. See docs/legacy-est-scep.md::mTLS-sibling-route for the
|
|
// operator-facing description.
|
|
"GET /scep-mtls": "Phase 6.5 mTLS sibling route — same wire format as /scep with cert-required gate; documented in docs/legacy-est-scep.md",
|
|
"POST /scep-mtls": "Phase 6.5 mTLS sibling route — same wire format as /scep with cert-required gate; documented in docs/legacy-est-scep.md",
|
|
|
|
// ACME server (RFC 8555 + RFC 9773 ARI) — Phase 1a foundation.
|
|
// Like SCEP/EST, ACME is a wire-protocol surface (JWS-signed JSON
|
|
// over HTTPS per RFC 7515) whose semantics are dictated by the RFC
|
|
// rather than by an OpenAPI document. Documenting every endpoint
|
|
// in openapi.yaml would duplicate RFC 8555 §7.1 + §7.2 with no
|
|
// information gain. The canonical reference is docs/acme-server.md.
|
|
// Subsequent phases will extend this list with new-account,
|
|
// new-order, finalize, authz, challenge, cert, key-change,
|
|
// revoke-cert, renewal-info — each gets its own exception entry
|
|
// in the same commit that lands the route.
|
|
"GET /acme/profile/{id}/directory": "RFC 8555 §7.1.1 directory; documented in docs/acme-server.md",
|
|
"HEAD /acme/profile/{id}/new-nonce": "RFC 8555 §7.2 new-nonce; documented in docs/acme-server.md",
|
|
"GET /acme/profile/{id}/new-nonce": "RFC 8555 §7.2 new-nonce (GET form); documented in docs/acme-server.md",
|
|
"POST /acme/profile/{id}/new-account": "RFC 8555 §7.3 new-account; documented in docs/acme-server.md",
|
|
"POST /acme/profile/{id}/account/{acc_id}": "RFC 8555 §7.3.2 account update + §7.3.6 deactivation; documented in docs/acme-server.md",
|
|
"GET /acme/directory": "RFC 8555 §7.1.1 directory (default-profile shorthand); documented in docs/acme-server.md",
|
|
"HEAD /acme/new-nonce": "RFC 8555 §7.2 new-nonce (default-profile shorthand); documented in docs/acme-server.md",
|
|
"GET /acme/new-nonce": "RFC 8555 §7.2 new-nonce GET (default-profile shorthand); documented in docs/acme-server.md",
|
|
"POST /acme/new-account": "RFC 8555 §7.3 new-account (default-profile shorthand); documented in docs/acme-server.md",
|
|
"POST /acme/account/{acc_id}": "RFC 8555 §7.3.2 + §7.3.6 (default-profile shorthand); documented in docs/acme-server.md",
|
|
}
|
|
|
|
func TestRouter_OpenAPIParity(t *testing.T) {
|
|
routes, err := scanRouterRoutes("router.go")
|
|
if err != nil {
|
|
t.Fatalf("scan router.go: %v", err)
|
|
}
|
|
specOps, err := scanOpenAPIOperations("../../../api/openapi.yaml")
|
|
if err != nil {
|
|
t.Fatalf("scan openapi.yaml: %v", err)
|
|
}
|
|
|
|
routeSet := make(map[string]bool, len(routes))
|
|
for _, r := range routes {
|
|
routeSet[r] = true
|
|
}
|
|
specSet := make(map[string]bool, len(specOps))
|
|
for _, o := range specOps {
|
|
specSet[o] = true
|
|
}
|
|
|
|
var inRouterNotSpec, inSpecNotRouter []string
|
|
for r := range routeSet {
|
|
if !specSet[r] {
|
|
if _, allow := SpecParityExceptions[r]; !allow {
|
|
inRouterNotSpec = append(inRouterNotSpec, r)
|
|
}
|
|
}
|
|
}
|
|
for s := range specSet {
|
|
if !routeSet[s] {
|
|
inSpecNotRouter = append(inSpecNotRouter, s)
|
|
}
|
|
}
|
|
|
|
sort.Strings(inRouterNotSpec)
|
|
sort.Strings(inSpecNotRouter)
|
|
|
|
if len(inRouterNotSpec) > 0 {
|
|
t.Errorf("routes in router.go but missing from api/openapi.yaml (%d):\n %s\n\n"+
|
|
"Add the operation to openapi.yaml OR add an explicit exception to "+
|
|
"SpecParityExceptions with a justification.",
|
|
len(inRouterNotSpec), strings.Join(inRouterNotSpec, "\n "))
|
|
}
|
|
if len(inSpecNotRouter) > 0 {
|
|
t.Errorf("operations in api/openapi.yaml but missing from router.go (%d):\n %s\n\n"+
|
|
"Either implement the endpoint or remove it from openapi.yaml.",
|
|
len(inSpecNotRouter), strings.Join(inSpecNotRouter, "\n "))
|
|
}
|
|
}
|
|
|
|
// --- helpers --------------------------------------------------------------
|
|
|
|
func scanRouterRoutes(name string) ([]string, error) {
|
|
fset := token.NewFileSet()
|
|
src, err := parser.ParseFile(fset, name, nil, parser.SkipObjectResolution)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []string
|
|
ast.Inspect(src, func(n ast.Node) bool {
|
|
call, ok := n.(*ast.CallExpr)
|
|
if !ok || len(call.Args) == 0 {
|
|
return true
|
|
}
|
|
// We care about r.mux.Handle("METHOD /path", ...) and
|
|
// r.Register("METHOD /path", ...). Both have a string literal as
|
|
// arg[0].
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
isMuxHandle := false
|
|
isRegister := sel.Sel.Name == "Register"
|
|
if sel.Sel.Name == "Handle" {
|
|
if inner, ok := sel.X.(*ast.SelectorExpr); ok && inner.Sel.Name == "mux" {
|
|
isMuxHandle = true
|
|
}
|
|
}
|
|
if !isMuxHandle && !isRegister {
|
|
return true
|
|
}
|
|
lit, ok := call.Args[0].(*ast.BasicLit)
|
|
if !ok || lit.Kind != token.STRING {
|
|
return true
|
|
}
|
|
v := strings.Trim(lit.Value, "\"`")
|
|
// Skip the generic Register helper itself (line 38: r.mux.Handle(pattern,...)
|
|
// — pattern is a func arg, not a literal, so it would not be a BasicLit).
|
|
// Skip non-METHOD-prefixed strings (defensive).
|
|
if !looksLikeMethodPath(v) {
|
|
return true
|
|
}
|
|
out = append(out, v)
|
|
return true
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
var methodPathRe = regexp.MustCompile(`^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD) /`)
|
|
|
|
func looksLikeMethodPath(s string) bool {
|
|
return methodPathRe.MatchString(s)
|
|
}
|
|
|
|
// scanOpenAPIOperations walks openapi.yaml's paths block and returns
|
|
// every (METHOD, PATH) tuple in the same "METHOD /path" string shape the
|
|
// router uses. Naive but sufficient: the spec is hand-maintained YAML
|
|
// with consistent 2-space-then-4-space indentation.
|
|
func scanOpenAPIOperations(path string) ([]string, error) {
|
|
body, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []string
|
|
inPaths := false
|
|
currentPath := ""
|
|
pathRe := regexp.MustCompile(`^ (/[^:]+):\s*$`)
|
|
methodRe := regexp.MustCompile(`^ (get|post|put|delete|patch|options|head):\s*$`)
|
|
for _, line := range strings.Split(string(body), "\n") {
|
|
if strings.HasPrefix(line, "paths:") {
|
|
inPaths = true
|
|
continue
|
|
}
|
|
if inPaths && line != "" && !strings.HasPrefix(line, " ") {
|
|
inPaths = false
|
|
continue
|
|
}
|
|
if !inPaths {
|
|
continue
|
|
}
|
|
if m := pathRe.FindStringSubmatch(line); m != nil {
|
|
currentPath = m[1]
|
|
continue
|
|
}
|
|
if m := methodRe.FindStringSubmatch(line); m != nil && currentPath != "" {
|
|
out = append(out, strings.ToUpper(m[1])+" "+currentPath)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|