forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/crypto/acme/http.go

591 lines
10 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2018 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package acme
import (
"bytes"
"context"
"crypto"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"strconv"
"strings"
"time"
)
// retryTimer encapsulates common logic for retrying unsuccessful requests.
2024-02-18 10:42:21 +00:00
// It is not safe for concurrent use.
2024-02-18 10:42:21 +00:00
type retryTimer struct {
2024-02-18 10:42:21 +00:00
// backoffFn provides backoff delay sequence for retries.
2024-02-18 10:42:21 +00:00
// See Client.RetryBackoff doc comment.
2024-02-18 10:42:21 +00:00
backoffFn func(n int, r *http.Request, res *http.Response) time.Duration
2024-02-18 10:42:21 +00:00
// n is the current retry attempt.
2024-02-18 10:42:21 +00:00
n int
}
func (t *retryTimer) inc() {
2024-02-18 10:42:21 +00:00
t.n++
2024-02-18 10:42:21 +00:00
}
// backoff pauses the current goroutine as described in Client.RetryBackoff.
2024-02-18 10:42:21 +00:00
func (t *retryTimer) backoff(ctx context.Context, r *http.Request, res *http.Response) error {
2024-02-18 10:42:21 +00:00
d := t.backoffFn(t.n, r, res)
2024-02-18 10:42:21 +00:00
if d <= 0 {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("acme: no more retries for %s; tried %d time(s)", r.URL, t.n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
wakeup := time.NewTimer(d)
2024-02-18 10:42:21 +00:00
defer wakeup.Stop()
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
return ctx.Err()
2024-02-18 10:42:21 +00:00
case <-wakeup.C:
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (c *Client) retryTimer() *retryTimer {
2024-02-18 10:42:21 +00:00
f := c.RetryBackoff
2024-02-18 10:42:21 +00:00
if f == nil {
2024-02-18 10:42:21 +00:00
f = defaultBackoff
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &retryTimer{backoffFn: f}
2024-02-18 10:42:21 +00:00
}
// defaultBackoff provides default Client.RetryBackoff implementation
2024-02-18 10:42:21 +00:00
// using a truncated exponential backoff algorithm,
2024-02-18 10:42:21 +00:00
// as described in Client.RetryBackoff.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The n argument is always bounded between 1 and 30.
2024-02-18 10:42:21 +00:00
// The returned value is always greater than 0.
2024-02-18 10:42:21 +00:00
func defaultBackoff(n int, r *http.Request, res *http.Response) time.Duration {
2024-02-18 10:42:21 +00:00
const max = 10 * time.Second
2024-02-18 10:42:21 +00:00
var jitter time.Duration
2024-02-18 10:42:21 +00:00
if x, err := rand.Int(rand.Reader, big.NewInt(1000)); err == nil {
2024-02-18 10:42:21 +00:00
// Set the minimum to 1ms to avoid a case where
2024-02-18 10:42:21 +00:00
// an invalid Retry-After value is parsed into 0 below,
2024-02-18 10:42:21 +00:00
// resulting in the 0 returned value which would unintentionally
2024-02-18 10:42:21 +00:00
// stop the retries.
2024-02-18 10:42:21 +00:00
jitter = (1 + time.Duration(x.Int64())) * time.Millisecond
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if v, ok := res.Header["Retry-After"]; ok {
2024-02-18 10:42:21 +00:00
return retryAfter(v[0]) + jitter
2024-02-18 10:42:21 +00:00
}
if n < 1 {
2024-02-18 10:42:21 +00:00
n = 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n > 30 {
2024-02-18 10:42:21 +00:00
n = 30
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d := time.Duration(1<<uint(n-1))*time.Second + jitter
2024-02-18 10:42:21 +00:00
if d > max {
2024-02-18 10:42:21 +00:00
return max
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return d
2024-02-18 10:42:21 +00:00
}
// retryAfter parses a Retry-After HTTP header value,
2024-02-18 10:42:21 +00:00
// trying to convert v into an int (seconds) or use http.ParseTime otherwise.
2024-02-18 10:42:21 +00:00
// It returns zero value if v cannot be parsed.
2024-02-18 10:42:21 +00:00
func retryAfter(v string) time.Duration {
2024-02-18 10:42:21 +00:00
if i, err := strconv.Atoi(v); err == nil {
2024-02-18 10:42:21 +00:00
return time.Duration(i) * time.Second
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
t, err := http.ParseTime(v)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return t.Sub(timeNow())
2024-02-18 10:42:21 +00:00
}
// resOkay is a function that reports whether the provided response is okay.
2024-02-18 10:42:21 +00:00
// It is expected to keep the response body unread.
2024-02-18 10:42:21 +00:00
type resOkay func(*http.Response) bool
// wantStatus returns a function which reports whether the code
2024-02-18 10:42:21 +00:00
// matches the status code of a response.
2024-02-18 10:42:21 +00:00
func wantStatus(codes ...int) resOkay {
2024-02-18 10:42:21 +00:00
return func(res *http.Response) bool {
2024-02-18 10:42:21 +00:00
for _, code := range codes {
2024-02-18 10:42:21 +00:00
if code == res.StatusCode {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// get issues an unsigned GET request to the specified URL.
2024-02-18 10:42:21 +00:00
// It returns a non-error value only when ok reports true.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// get retries unsuccessful attempts according to c.RetryBackoff
2024-02-18 10:42:21 +00:00
// until the context is done or a non-retriable error is received.
2024-02-18 10:42:21 +00:00
func (c *Client) get(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
2024-02-18 10:42:21 +00:00
retry := c.retryTimer()
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
req, err := http.NewRequest("GET", url, nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
res, err := c.doNoRetry(ctx, req)
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case err != nil:
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
case ok(res):
2024-02-18 10:42:21 +00:00
return res, nil
2024-02-18 10:42:21 +00:00
case isRetriable(res.StatusCode):
2024-02-18 10:42:21 +00:00
retry.inc()
2024-02-18 10:42:21 +00:00
resErr := responseError(res)
2024-02-18 10:42:21 +00:00
res.Body.Close()
2024-02-18 10:42:21 +00:00
// Ignore the error value from retry.backoff
2024-02-18 10:42:21 +00:00
// and return the one from last retry, as received from the CA.
2024-02-18 10:42:21 +00:00
if retry.backoff(ctx, req, res) != nil {
2024-02-18 10:42:21 +00:00
return nil, resErr
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
defer res.Body.Close()
2024-02-18 10:42:21 +00:00
return nil, responseError(res)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// postAsGet is POST-as-GET, a replacement for GET in RFC 8555
2024-02-18 10:42:21 +00:00
// as described in https://tools.ietf.org/html/rfc8555#section-6.3.
2024-02-18 10:42:21 +00:00
// It makes a POST request in KID form with zero JWS payload.
2024-02-18 10:42:21 +00:00
// See nopayload doc comments in jws.go.
2024-02-18 10:42:21 +00:00
func (c *Client) postAsGet(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
2024-02-18 10:42:21 +00:00
return c.post(ctx, nil, url, noPayload, ok)
2024-02-18 10:42:21 +00:00
}
// post issues a signed POST request in JWS format using the provided key
2024-02-18 10:42:21 +00:00
// to the specified URL. If key is nil, c.Key is used instead.
2024-02-18 10:42:21 +00:00
// It returns a non-error value only when ok reports true.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// post retries unsuccessful attempts according to c.RetryBackoff
2024-02-18 10:42:21 +00:00
// until the context is done or a non-retriable error is received.
2024-02-18 10:42:21 +00:00
// It uses postNoRetry to make individual requests.
2024-02-18 10:42:21 +00:00
func (c *Client) post(ctx context.Context, key crypto.Signer, url string, body interface{}, ok resOkay) (*http.Response, error) {
2024-02-18 10:42:21 +00:00
retry := c.retryTimer()
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
res, req, err := c.postNoRetry(ctx, key, url, body)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if ok(res) {
2024-02-18 10:42:21 +00:00
return res, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
resErr := responseError(res)
2024-02-18 10:42:21 +00:00
res.Body.Close()
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
// Check for bad nonce before isRetriable because it may have been returned
2024-02-18 10:42:21 +00:00
// with an unretriable response code such as 400 Bad Request.
2024-02-18 10:42:21 +00:00
case isBadNonce(resErr):
2024-02-18 10:42:21 +00:00
// Consider any previously stored nonce values to be invalid.
2024-02-18 10:42:21 +00:00
c.clearNonces()
2024-02-18 10:42:21 +00:00
case !isRetriable(res.StatusCode):
2024-02-18 10:42:21 +00:00
return nil, resErr
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
retry.inc()
2024-02-18 10:42:21 +00:00
// Ignore the error value from retry.backoff
2024-02-18 10:42:21 +00:00
// and return the one from last retry, as received from the CA.
2024-02-18 10:42:21 +00:00
if err := retry.backoff(ctx, req, res); err != nil {
2024-02-18 10:42:21 +00:00
return nil, resErr
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// postNoRetry signs the body with the given key and POSTs it to the provided url.
2024-02-18 10:42:21 +00:00
// It is used by c.post to retry unsuccessful attempts.
2024-02-18 10:42:21 +00:00
// The body argument must be JSON-serializable.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If key argument is nil, c.Key is used to sign the request.
2024-02-18 10:42:21 +00:00
// If key argument is nil and c.accountKID returns a non-zero keyID,
2024-02-18 10:42:21 +00:00
// the request is sent in KID form. Otherwise, JWK form is used.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// In practice, when interfacing with RFC-compliant CAs most requests are sent in KID form
2024-02-18 10:42:21 +00:00
// and JWK is used only when KID is unavailable: new account endpoint and certificate
2024-02-18 10:42:21 +00:00
// revocation requests authenticated by a cert key.
2024-02-18 10:42:21 +00:00
// See jwsEncodeJSON for other details.
2024-02-18 10:42:21 +00:00
func (c *Client) postNoRetry(ctx context.Context, key crypto.Signer, url string, body interface{}) (*http.Response, *http.Request, error) {
2024-02-18 10:42:21 +00:00
kid := noKeyID
2024-02-18 10:42:21 +00:00
if key == nil {
2024-02-18 10:42:21 +00:00
if c.Key == nil {
2024-02-18 10:42:21 +00:00
return nil, nil, errors.New("acme: Client.Key must be populated to make POST requests")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
key = c.Key
2024-02-18 10:42:21 +00:00
kid = c.accountKID(ctx)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
nonce, err := c.popNonce(ctx, url)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
b, err := jwsEncodeJSON(body, key, kid, nonce, url)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
req.Header.Set("Content-Type", "application/jose+json")
2024-02-18 10:42:21 +00:00
res, err := c.doNoRetry(ctx, req)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c.addNonce(res.Header)
2024-02-18 10:42:21 +00:00
return res, req, nil
2024-02-18 10:42:21 +00:00
}
// doNoRetry issues a request req, replacing its context (if any) with ctx.
2024-02-18 10:42:21 +00:00
func (c *Client) doNoRetry(ctx context.Context, req *http.Request) (*http.Response, error) {
2024-02-18 10:42:21 +00:00
req.Header.Set("User-Agent", c.userAgent())
2024-02-18 10:42:21 +00:00
res, err := c.httpClient().Do(req.WithContext(ctx))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
// Prefer the unadorned context error.
2024-02-18 10:42:21 +00:00
// (The acme package had tests assuming this, previously from ctxhttp's
2024-02-18 10:42:21 +00:00
// behavior, predating net/http supporting contexts natively)
2024-02-18 10:42:21 +00:00
// TODO(bradfitz): reconsider this in the future. But for now this
2024-02-18 10:42:21 +00:00
// requires no test updates.
2024-02-18 10:42:21 +00:00
return nil, ctx.Err()
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return res, nil
2024-02-18 10:42:21 +00:00
}
func (c *Client) httpClient() *http.Client {
2024-02-18 10:42:21 +00:00
if c.HTTPClient != nil {
2024-02-18 10:42:21 +00:00
return c.HTTPClient
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return http.DefaultClient
2024-02-18 10:42:21 +00:00
}
// packageVersion is the version of the module that contains this package, for
2024-02-18 10:42:21 +00:00
// sending as part of the User-Agent header. It's set in version_go112.go.
2024-02-18 10:42:21 +00:00
var packageVersion string
// userAgent returns the User-Agent header value. It includes the package name,
2024-02-18 10:42:21 +00:00
// the module version (if available), and the c.UserAgent value (if set).
2024-02-18 10:42:21 +00:00
func (c *Client) userAgent() string {
2024-02-18 10:42:21 +00:00
ua := "golang.org/x/crypto/acme"
2024-02-18 10:42:21 +00:00
if packageVersion != "" {
2024-02-18 10:42:21 +00:00
ua += "@" + packageVersion
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if c.UserAgent != "" {
2024-02-18 10:42:21 +00:00
ua = c.UserAgent + " " + ua
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ua
2024-02-18 10:42:21 +00:00
}
// isBadNonce reports whether err is an ACME "badnonce" error.
2024-02-18 10:42:21 +00:00
func isBadNonce(err error) bool {
2024-02-18 10:42:21 +00:00
// According to the spec badNonce is urn:ietf:params:acme:error:badNonce.
2024-02-18 10:42:21 +00:00
// However, ACME servers in the wild return their versions of the error.
2024-02-18 10:42:21 +00:00
// See https://tools.ietf.org/html/draft-ietf-acme-acme-02#section-5.4
2024-02-18 10:42:21 +00:00
// and https://github.com/letsencrypt/boulder/blob/0e07eacb/docs/acme-divergences.md#section-66.
2024-02-18 10:42:21 +00:00
ae, ok := err.(*Error)
2024-02-18 10:42:21 +00:00
return ok && strings.HasSuffix(strings.ToLower(ae.ProblemType), ":badnonce")
2024-02-18 10:42:21 +00:00
}
// isRetriable reports whether a request can be retried
2024-02-18 10:42:21 +00:00
// based on the response status code.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that a "bad nonce" error is returned with a non-retriable 400 Bad Request code.
2024-02-18 10:42:21 +00:00
// Callers should parse the response and check with isBadNonce.
2024-02-18 10:42:21 +00:00
func isRetriable(code int) bool {
2024-02-18 10:42:21 +00:00
return code <= 399 || code >= 500 || code == http.StatusTooManyRequests
2024-02-18 10:42:21 +00:00
}
// responseError creates an error of Error type from resp.
2024-02-18 10:42:21 +00:00
func responseError(resp *http.Response) error {
2024-02-18 10:42:21 +00:00
// don't care if ReadAll returns an error:
2024-02-18 10:42:21 +00:00
// json.Unmarshal will fail in that case anyway
2024-02-18 10:42:21 +00:00
b, _ := io.ReadAll(resp.Body)
2024-02-18 10:42:21 +00:00
e := &wireError{Status: resp.StatusCode}
2024-02-18 10:42:21 +00:00
if err := json.Unmarshal(b, e); err != nil {
2024-02-18 10:42:21 +00:00
// this is not a regular error response:
2024-02-18 10:42:21 +00:00
// populate detail with anything we received,
2024-02-18 10:42:21 +00:00
// e.Status will already contain HTTP response code value
2024-02-18 10:42:21 +00:00
e.Detail = string(b)
2024-02-18 10:42:21 +00:00
if e.Detail == "" {
2024-02-18 10:42:21 +00:00
e.Detail = resp.Status
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return e.error(resp.Header)
2024-02-18 10:42:21 +00:00
}