mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
feature/acme: test retrying renewal order without ARI replaces
Adds a test that issueACMECert, against tstest/acmetest, retries an order naming "replaces" once without it when the CA rejects it, and stores the cert from the retried order. It also checks that rate limits and server errors are not retried, and that the error from the plain order is returned if that is rejected too. Updates #22042 Signed-off-by: chaosinthecrd <tom@tmlabs.co.uk>
This commit is contained in:
1 parent
4fad6e5a3b
commit
a785baba6e
1 file changed
+154
@@ -4,14 +4,23 @@
|
||||
package acme
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -110,3 +119,148 @@ func TestIssueACMECertValidAuthorization(t *testing.T) {
|
||||
t.Errorf("stored certificate does not read back: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssueACMECertRetriesWithoutReplaces checks that a renewal order the
|
||||
// CA rejects for its ARI "replaces" field is retried once without it, and
|
||||
// that the cert from the retried order is stored.
|
||||
// See tailscale/tailscale#22042.
|
||||
func TestIssueACMECertRetriesWithoutReplaces(t *testing.T) {
|
||||
const domain = "example.com"
|
||||
prev := &x509.Certificate{AuthorityKeyId: []byte{1, 2, 3}, SerialNumber: big.NewInt(42)}
|
||||
tests := []struct {
|
||||
name string
|
||||
replaces *x509.Certificate
|
||||
rejectCode int // if non-zero, status for orders naming "replaces"
|
||||
rejectType string // ACME problem type for rejected orders
|
||||
rejectPlain bool // also reject orders that don't name "replaces"
|
||||
wantErr string // "replaces=<bool>" detail of the rejected order whose error is returned, or "" for success
|
||||
wantReplaces []bool // whether each new order request named "replaces"
|
||||
}{
|
||||
{
|
||||
name: "no_previous_cert",
|
||||
wantReplaces: []bool{false},
|
||||
},
|
||||
{
|
||||
name: "replaces_accepted",
|
||||
replaces: prev,
|
||||
wantReplaces: []bool{true},
|
||||
},
|
||||
{
|
||||
name: "account_mismatch_retries_without_replaces",
|
||||
replaces: prev,
|
||||
rejectCode: http.StatusForbidden,
|
||||
rejectType: "unauthorized",
|
||||
wantReplaces: []bool{true, false},
|
||||
},
|
||||
{
|
||||
name: "already_replaced_retries_without_replaces",
|
||||
replaces: prev,
|
||||
rejectCode: http.StatusConflict,
|
||||
rejectType: "alreadyReplaced",
|
||||
wantReplaces: []bool{true, false},
|
||||
},
|
||||
{
|
||||
name: "plain_order_rejected_too",
|
||||
replaces: prev,
|
||||
rejectCode: http.StatusBadRequest,
|
||||
rejectType: "rejectedIdentifier",
|
||||
rejectPlain: true,
|
||||
wantErr: "replaces=false",
|
||||
wantReplaces: []bool{true, false},
|
||||
},
|
||||
{
|
||||
name: "rate_limited_not_retried",
|
||||
replaces: prev,
|
||||
rejectCode: http.StatusTooManyRequests,
|
||||
rejectType: "rateLimited",
|
||||
wantErr: "replaces=true",
|
||||
wantReplaces: []bool{true},
|
||||
},
|
||||
{
|
||||
name: "server_error_not_retried",
|
||||
replaces: prev,
|
||||
rejectCode: http.StatusInternalServerError,
|
||||
rejectType: "serverInternal",
|
||||
wantErr: "replaces=true",
|
||||
wantReplaces: []bool{true},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var (
|
||||
mu sync.Mutex
|
||||
gotReplaces []bool
|
||||
s *acmetest.Server
|
||||
)
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/new-order" {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Errorf("reading new-order request: %v", err)
|
||||
}
|
||||
r.Body = io.NopCloser(bytes.NewReader(body))
|
||||
var jws struct {
|
||||
Payload string `json:"payload"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &jws); err != nil {
|
||||
t.Errorf("decoding JWS: %v", err)
|
||||
}
|
||||
payload, err := base64.RawURLEncoding.DecodeString(jws.Payload)
|
||||
if err != nil {
|
||||
t.Errorf("decoding JWS payload: %v", err)
|
||||
}
|
||||
var req struct {
|
||||
Replaces string `json:"replaces"`
|
||||
}
|
||||
if err := json.Unmarshal(payload, &req); err != nil {
|
||||
t.Errorf("decoding order: %v", err)
|
||||
}
|
||||
named := req.Replaces != ""
|
||||
mu.Lock()
|
||||
gotReplaces = append(gotReplaces, named)
|
||||
mu.Unlock()
|
||||
if tt.rejectCode != 0 && (named || tt.rejectPlain) {
|
||||
w.Header().Set("Content-Type", "application/problem+json")
|
||||
w.WriteHeader(tt.rejectCode)
|
||||
fmt.Fprintf(w, `{"type":"urn:ietf:params:acme:error:%s","detail":"replaces=%v"}`, tt.rejectType, named)
|
||||
return
|
||||
}
|
||||
}
|
||||
s.ServeHTTP(w, r)
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
s = acmetest.New(ts.URL)
|
||||
s.ChallengeType = "tls-alpn-01"
|
||||
|
||||
ac := newTestACMEClient(t, s)
|
||||
// Return rate limits and server errors straight away.
|
||||
ac.RetryBackoff = func(int, *http.Request, *http.Response) time.Duration { return 0 }
|
||||
roots := x509.NewCertPool()
|
||||
roots.AddCert(s.Root())
|
||||
cs := certStateStore{StateStore: new(mem.Store), testRoots: roots}
|
||||
e := &extension{logf: t.Logf}
|
||||
_, err := e.issueACMECert(t.Context(), nil, ac, acmeCertIssueArgs{
|
||||
cs: cs,
|
||||
logf: t.Logf,
|
||||
domain: domain,
|
||||
replaces: tt.replaces,
|
||||
challengeType: acmeChallengeTLSALPN01,
|
||||
})
|
||||
if tt.wantErr == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("issueACMECert: %v", err)
|
||||
}
|
||||
if _, err := cs.Read(domain, time.Now()); err != nil {
|
||||
t.Errorf("stored certificate does not read back: %v", err)
|
||||
}
|
||||
} else if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("issueACMECert error = %v, want it to contain %q", err, tt.wantErr)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if !slices.Equal(gotReplaces, tt.wantReplaces) {
|
||||
t.Errorf("new order requests naming replaces = %v, want %v", gotReplaces, tt.wantReplaces)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user