mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-10 10:08:52 +00:00
e146b00f0e
First slice of the RFC 8555 ACME server endpoint (master plan at cowork/acme-server-endpoint-prompt.md, per-phase prompts at cowork/acme-server-prompts/). This commit lands the smallest viable end-to-end deployable slice: an ACME client running curl -sk https://certctl/acme/profile/<id>/directory curl -sk -I https://certctl/acme/profile/<id>/new-nonce successfully fetches the directory document and a Replay-Nonce. Account creation, JWS verification, orders, challenges, and revocation are all out of scope for this phase and arrive in Phases 1b–4. Closes the Rank 1 LHF from the 2026-05-03 Infisical deep-research (cowork/infisical-deep-research-results.md). Pre-fix, certctl was an ACME consumer only — no /acme/directory endpoint, no JWS verifier, no challenge validators. K8s customers running cert-manager could not point at certctl as an ACME issuer; they had to deploy a certctl agent on every node. What ships: - internal/api/acme/{directory,nonce,errors}.go (+ tests). - internal/api/handler/acme.go + acme_handler_test.go. - internal/repository/postgres/acme.go (nonce ops only — Phase 1b extends with account CRUD; Phases 2-4 extend with order / authz / challenge CRUD). - internal/service/acme.go (BuildDirectory + IssueNonce stubs; Phase 1b adds VerifyJWS / NewAccount / etc.). - migrations/000025_acme_server.{up,down}.sql ships the full 5-table ACME schema (acme_accounts / acme_orders / acme_authorizations / acme_challenges / acme_nonces) PLUS the per-profile certificate_profiles.acme_auth_mode column. Phase 1a actively uses only acme_nonces; remaining tables are empty until Phases 1b-4 plug in. - internal/config/config.go: ACMEServerConfig struct + ACMEServer field on Config. Env vars use CERTCTL_ACME_SERVER_* prefix to avoid colliding with the existing consumer-side ACMEConfig at config.go:1746 (CERTCTL_ACME_DIRECTORY_URL / PROFILE / CHALLENGE_TYPE etc.). Phase 1a wires Enabled + DefaultAuthMode + DefaultProfileID + NonceTTL + DirectoryMeta; Order/Authz TTLs + per-challenge-type concurrency caps + DNS01 resolver are reserved fields parsed in 1a so operators can set them ahead of Phases 2/3. - cmd/server/main.go: wire ACMEHandler into the HandlerRegistry literal alongside the existing certificate / EST / SCEP / etc. handlers. - internal/api/router/router.go: HandlerRegistry.ACME field + 6 Register calls (3 per-profile + 3 shorthand). - internal/api/router/openapi_parity_test.go: 6 new entries in SpecParityExceptions. ACME is a wire-protocol surface (JWS-signed JSON over HTTPS per RFC 7515) whose semantics are dictated by RFC 8555 + RFC 9773 rather than by an OpenAPI document, same precedent as SCEP/EST. The canonical reference is docs/acme-server.md. - docs/acme-server.md: Phase-1a-shaped reference. Configuration table for every CERTCTL_ACME_SERVER_* env var. Per-profile auth-mode decision tree skeleton. TLS trust bootstrap section flagging cert-manager's ClusterIssuer.spec.acme.caBundle requirement (the single biggest first-time-deploy footgun; the full cert-manager walkthrough lands in Phase 6 but the requirement is documented up front). Architecture decisions baked in: - URL family is /acme/profile/<id>/* (per-profile, canonical) with /acme/* shorthand active when CERTCTL_ACME_SERVER_DEFAULT_PROFILE_ID is set. Path matches existing per-profile precedent in EST + SCEP. - Auth mode is per-profile (acme_auth_mode column on certificate_profiles), NOT server-wide. One certctl-server can serve trust_authenticated for an internal-PKI profile and challenge for a public-trust-style profile simultaneously. The column is read at request time, not cached at server start — operators flipping a profile's mode via SQL take effect on the next order without restart. - Nonces are DB-backed (acme_nonces table). Survive server restart. The RFC 8555 §6.5 replay defense requires the store to outlast the client's nonce caching window; an in-memory-only nonce store would lose every in-flight order on restart. - Per-op atomic counters on service.ACMEService.Metrics() — certctl_acme_directory_total, certctl_acme_directory_failures_total, certctl_acme_new_nonce_total, certctl_acme_new_nonce_failures_total. Naming follows certctl frozen decision 0.10 cardinality discipline. Phase 1b will extend with new_account counters; Phase 2 with order / finalize / cert; Phase 3 with per-challenge-type counters. Audit fixes #11 + #12 (cowork/acme-server-prompts/audit-additions.md) applied: - #11: CERTCTL_ACME_SERVER_* prefix avoids the consumer-side CERTCTL_ACME_* namespace collision. - #12: prior-attempt WIP from two failed Phase-1 dispatches was discarded at phase start; this commit starts from a clean tree. Tests: - 14 unit tests in internal/api/acme/ (directory, nonce, errors). - 7 handler-level tests via httptest.NewServer + mockACMEService (mirrors the mockSCEPService pattern at scep_handler_test.go). - 7 service-layer tests with mocked repo + injected profileLookup. - All pass under -race -count=1 -short. Deferred to Phase 1b: - JWS verification (go-jose v4 — see master-prompt §8a for the API surface and audit doc for the speculation pitfalls). - new-account / account/<id> endpoints + AccountService. - Nonce *consumption* path (issue path is in this commit; consume is only invoked by JWS-verified POSTs which Phase 1b adds). Engineering history: cowork/WORKSPACE-CHANGELOG.md "ACME-Server-1a". Per-phase implementation plan: cowork/acme-server-prompts/. Master plan + audit fixes: cowork/acme-server-endpoint-prompt.md + cowork/acme-server-prompt-audit.md + cowork/acme-server-prompts/audit-additions.md.
182 lines
5.5 KiB
Go
182 lines
5.5 KiB
Go
// Copyright (c) certctl
|
|
// SPDX-License-Identifier: BSL-1.1
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/shankar0123/certctl/internal/config"
|
|
"github.com/shankar0123/certctl/internal/domain"
|
|
"github.com/shankar0123/certctl/internal/repository"
|
|
)
|
|
|
|
// fakeACMERepo is an in-memory ACMERepo for tests. It tracks issued
|
|
// nonces in a map; Consume removes the entry to model one-shot use.
|
|
type fakeACMERepo struct {
|
|
issued map[string]time.Time // nonce → expires_at
|
|
issueErr error
|
|
}
|
|
|
|
func newFakeACMERepo() *fakeACMERepo {
|
|
return &fakeACMERepo{issued: make(map[string]time.Time)}
|
|
}
|
|
|
|
func (f *fakeACMERepo) IssueNonce(ctx context.Context, nonce string, ttl time.Duration) error {
|
|
if f.issueErr != nil {
|
|
return f.issueErr
|
|
}
|
|
f.issued[nonce] = time.Now().Add(ttl)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeACMERepo) ConsumeNonce(ctx context.Context, nonce string) error {
|
|
exp, ok := f.issued[nonce]
|
|
if !ok {
|
|
return errors.New("not found")
|
|
}
|
|
if time.Now().After(exp) {
|
|
return errors.New("expired")
|
|
}
|
|
delete(f.issued, nonce)
|
|
return nil
|
|
}
|
|
|
|
// fakeProfileLookup is an in-memory profileLookup that returns the
|
|
// profile by ID. Unknown IDs return repository.ErrNotFound (the
|
|
// canonical sentinel ACMEService maps to ErrACMEProfileNotFound).
|
|
type fakeProfileLookup struct {
|
|
profiles map[string]*domain.CertificateProfile
|
|
}
|
|
|
|
func (f *fakeProfileLookup) Get(ctx context.Context, id string) (*domain.CertificateProfile, error) {
|
|
p, ok := f.profiles[id]
|
|
if !ok {
|
|
return nil, repository.ErrNotFound
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func newSvc(t *testing.T, cfg config.ACMEServerConfig, profiles map[string]*domain.CertificateProfile) (*ACMEService, *fakeACMERepo) {
|
|
t.Helper()
|
|
repo := newFakeACMERepo()
|
|
pl := &fakeProfileLookup{profiles: profiles}
|
|
return NewACMEService(repo, pl, cfg), repo
|
|
}
|
|
|
|
func TestBuildDirectory_HappyPath(t *testing.T) {
|
|
cfg := config.ACMEServerConfig{
|
|
NonceTTL: 5 * time.Minute,
|
|
}
|
|
cfg.DirectoryMeta.TermsOfService = "https://example.com/tos"
|
|
cfg.DirectoryMeta.Website = "https://example.com"
|
|
svc, _ := newSvc(t, cfg, map[string]*domain.CertificateProfile{
|
|
"prof-corp": {ID: "prof-corp", Name: "corp"},
|
|
})
|
|
dir, err := svc.BuildDirectory(context.Background(), "prof-corp", "https://server/acme/profile/prof-corp")
|
|
if err != nil {
|
|
t.Fatalf("BuildDirectory: %v", err)
|
|
}
|
|
if dir == nil {
|
|
t.Fatal("dir is nil")
|
|
}
|
|
if dir.NewNonce != "https://server/acme/profile/prof-corp/new-nonce" {
|
|
t.Errorf("NewNonce = %q", dir.NewNonce)
|
|
}
|
|
if dir.Meta == nil || dir.Meta.TermsOfService != "https://example.com/tos" {
|
|
t.Errorf("meta tos = %+v", dir.Meta)
|
|
}
|
|
if got := svc.Metrics().DirectoryTotal.Load(); got != 1 {
|
|
t.Errorf("DirectoryTotal = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestBuildDirectory_UnknownProfile(t *testing.T) {
|
|
cfg := config.ACMEServerConfig{NonceTTL: 5 * time.Minute}
|
|
svc, _ := newSvc(t, cfg, nil)
|
|
_, err := svc.BuildDirectory(context.Background(), "prof-missing", "https://server/acme/profile/prof-missing")
|
|
if !errors.Is(err, ErrACMEProfileNotFound) {
|
|
t.Errorf("err = %v, want ErrACMEProfileNotFound", err)
|
|
}
|
|
if got := svc.Metrics().DirectoryFailureTotal.Load(); got != 1 {
|
|
t.Errorf("DirectoryFailureTotal = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestBuildDirectory_EmptyProfileNoDefault(t *testing.T) {
|
|
cfg := config.ACMEServerConfig{NonceTTL: 5 * time.Minute}
|
|
svc, _ := newSvc(t, cfg, nil)
|
|
_, err := svc.BuildDirectory(context.Background(), "", "https://server/acme")
|
|
if !errors.Is(err, ErrACMEUserActionRequired) {
|
|
t.Errorf("err = %v, want ErrACMEUserActionRequired", err)
|
|
}
|
|
}
|
|
|
|
func TestBuildDirectory_EmptyProfileWithDefault(t *testing.T) {
|
|
cfg := config.ACMEServerConfig{
|
|
NonceTTL: 5 * time.Minute,
|
|
DefaultProfileID: "prof-default",
|
|
}
|
|
svc, _ := newSvc(t, cfg, map[string]*domain.CertificateProfile{
|
|
"prof-default": {ID: "prof-default", Name: "default"},
|
|
})
|
|
dir, err := svc.BuildDirectory(context.Background(), "", "https://server/acme")
|
|
if err != nil {
|
|
t.Fatalf("BuildDirectory: %v", err)
|
|
}
|
|
if dir.NewNonce != "https://server/acme/new-nonce" {
|
|
t.Errorf("NewNonce = %q (shorthand path)", dir.NewNonce)
|
|
}
|
|
}
|
|
|
|
func TestIssueNonce_HappyPath(t *testing.T) {
|
|
cfg := config.ACMEServerConfig{NonceTTL: 5 * time.Minute}
|
|
svc, repo := newSvc(t, cfg, nil)
|
|
n, err := svc.IssueNonce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("IssueNonce: %v", err)
|
|
}
|
|
if len(n) != 43 {
|
|
t.Errorf("nonce length = %d, want 43 (base64url-no-pad of 32 bytes)", len(n))
|
|
}
|
|
if _, ok := repo.issued[n]; !ok {
|
|
t.Errorf("issued nonce was not persisted")
|
|
}
|
|
if got := svc.Metrics().NewNonceTotal.Load(); got != 1 {
|
|
t.Errorf("NewNonceTotal = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestIssueNonce_RepoFailure(t *testing.T) {
|
|
cfg := config.ACMEServerConfig{NonceTTL: 5 * time.Minute}
|
|
svc, repo := newSvc(t, cfg, nil)
|
|
repo.issueErr = errors.New("disk full")
|
|
_, err := svc.IssueNonce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("expected error from IssueNonce when repo fails")
|
|
}
|
|
if got := svc.Metrics().NewNonceFailureTotal.Load(); got != 1 {
|
|
t.Errorf("NewNonceFailureTotal = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestACMEMetrics_Snapshot(t *testing.T) {
|
|
m := NewACMEMetrics()
|
|
m.DirectoryTotal.Store(7)
|
|
m.NewNonceTotal.Store(11)
|
|
m.NewNonceFailureTotal.Store(2)
|
|
snap := m.Snapshot()
|
|
if snap["certctl_acme_directory_total"] != 7 {
|
|
t.Errorf("directory_total = %d", snap["certctl_acme_directory_total"])
|
|
}
|
|
if snap["certctl_acme_new_nonce_total"] != 11 {
|
|
t.Errorf("new_nonce_total = %d", snap["certctl_acme_new_nonce_total"])
|
|
}
|
|
if snap["certctl_acme_new_nonce_failures_total"] != 2 {
|
|
t.Errorf("new_nonce_failures_total = %d", snap["certctl_acme_new_nonce_failures_total"])
|
|
}
|
|
}
|