Files
probo/pkg/certmanager/acme_errors.go
Bryan Frimin 121d4dcf93 Fix certmanager provisioning retry and metrics
Address several provisioning defects that either stalled the retry
budget or crashed the process:

- Classify CAA resolver/transport failures apart from a real CAA policy
  denial. Both shared the "caa records" wording, so a transient resolver
  error was persisted as customer misconfiguration and retried forever
  without consuming the retry budget. A new ErrCAANotPermitted sentinel
  now marks the genuine misconfiguration; other CAA errors are treated
  as ordinary transient failures.

- Honor an explicit Retry-After: 0 (or a past date) as permission for an
  immediate retry instead of promoting it to the one-hour default
  cooldown. acme.RateLimit collapses zero, invalid, and absent headers
  to a zero duration, so the header is now parsed directly to tell an
  explicit zero apart from a missing one.

- Reuse already-registered Prometheus collectors when a second
  ACMEService shares a registerer. The fixed-name collectors were
  MustRegistered, so a duplicate registration panicked the process.

- Persist provisioning failures on a context detached from the process
  tick deadline. A timed-out attempt reached persistFailure with an
  expired context, so the write-back failed and the retry budget never
  advanced, leaving the certificate indefinitely retriable.

- Use pgx.StrictNamedArgs in the certificate FOR UPDATE loaders to match
  the coredata SQL contract.

Signed-off-by: Bryan Frimin <bryan@probo.com>
2026-07-22 13:20:52 +02:00

171 lines
4.1 KiB
Go

// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package certmanager
import (
"errors"
"fmt"
"net/http"
"strconv"
"time"
"golang.org/x/crypto/acme"
)
const defaultCooldown = time.Hour
var (
ErrACMERateLimited = errors.New("acme rate limited")
ErrOrderNotReady = errors.New("acme order not ready")
ErrOrderInvalid = errors.New("acme order invalid")
)
type ACMEError struct {
op string
err error
problemType string
detail string
rateLimited bool
retryAfter time.Duration
retryAfterSet bool
}
func (e *ACMEError) Error() string {
if e == nil {
return ""
}
if e.err != nil {
return fmt.Sprintf("%s: %v", e.op, e.err)
}
return e.op
}
func (e *ACMEError) Unwrap() error {
if e == nil {
return nil
}
return e.err
}
func (e *ACMEError) Is(target error) bool {
return e != nil && e.rateLimited && target == ErrACMERateLimited
}
// RetryAfter returns how long callers should wait before retrying.
// For rate-limited errors it honors the ACME Retry-After value whenever the
// header is present and parseable — including a zero (or past) value, which the
// CA uses to permit an immediate retry. It falls back to defaultCooldown only
// when the header is absent or invalid. Non-rate-limited errors return 0.
func (e *ACMEError) RetryAfter() time.Duration {
if e == nil || !e.rateLimited {
return 0
}
if e.retryAfterSet {
if e.retryAfter < 0 {
return 0
}
return e.retryAfter
}
return defaultCooldown
}
func (e *ACMEError) ProblemType() string {
if e == nil {
return ""
}
return e.problemType
}
func (e *ACMEError) Detail() string {
if e == nil {
return ""
}
return e.detail
}
func newACMEError(op string, err error) *ACMEError {
if err == nil {
return nil
}
out := &ACMEError{
op: op,
err: err,
}
acmeErr, ok := errors.AsType[*acme.Error](err)
if !ok {
out.detail = err.Error()
return out
}
out.problemType = acmeErr.ProblemType
out.detail = acmeErr.Detail
if _, ok := acme.RateLimit(acmeErr); ok {
out.rateLimited = true
// acme.RateLimit collapses "Retry-After: 0", an invalid header, and a
// missing header all to a zero duration, so inspect the header directly
// to tell an explicit zero (immediate retry) apart from an absent one
// (fall back to defaultCooldown in RetryAfter).
if retryAfter, ok := parseRetryAfter(acmeErr.Header); ok {
out.retryAfter = retryAfter
out.retryAfterSet = true
}
}
return out
}
// parseRetryAfter reports the Retry-After delay and whether the header was
// present and parseable. It mirrors the delta-seconds and HTTP-date forms the
// ACME client understands. An absent or unparseable value returns ok=false so
// callers can apply their own fallback.
func parseRetryAfter(header http.Header) (time.Duration, bool) {
if header == nil {
return 0, false
}
value := header.Get("Retry-After")
if value == "" {
return 0, false
}
if seconds, err := strconv.Atoi(value); err == nil {
return time.Duration(seconds) * time.Second, true
}
if date, err := http.ParseTime(value); err == nil {
return time.Until(date), true
}
return 0, false
}