Просмотр исходного кода

Merge pull request #2325 from smallstep/mariano/httpclient

Memory improvements
Mariano Cano 1 год назад
Родитель
Сommit
312d1a2f92

+ 14 - 4
authority/authority.go

@@ -8,8 +8,8 @@ import (
 	"crypto/sha256"
 	"crypto/sha256"
 	"crypto/x509"
 	"crypto/x509"
 	"encoding/hex"
 	"encoding/hex"
+	"fmt"
 	"log"
 	"log"
-	"net/http"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
 	"time"
 	"time"
@@ -50,8 +50,8 @@ type Authority struct {
 	templates     *templates.Templates
 	templates     *templates.Templates
 	linkedCAToken string
 	linkedCAToken string
 	wrapTransport httptransport.Wrapper
 	wrapTransport httptransport.Wrapper
-	webhookClient *http.Client
-	httpClient    *http.Client
+	webhookClient provisioner.HTTPClient
+	httpClient    provisioner.HTTPClient
 
 
 	// X509 CA
 	// X509 CA
 	password              []byte
 	password              []byte
@@ -147,6 +147,11 @@ func New(cfg *config.Config, opts ...Option) (*Authority, error) {
 		a.keyManager = newInstrumentedKeyManager(a.keyManager, a.meter)
 		a.keyManager = newInstrumentedKeyManager(a.keyManager, a.meter)
 	}
 	}
 
 
+	// Initialize system cert pool
+	if err := initializeSystemCertPool(); err != nil {
+		return nil, fmt.Errorf("failed to initialize the system cert pool: %w", err)
+	}
+
 	if !a.skipInit {
 	if !a.skipInit {
 		// Initialize authority from options or configuration.
 		// Initialize authority from options or configuration.
 		if err := a.init(); err != nil {
 		if err := a.init(); err != nil {
@@ -177,6 +182,11 @@ func NewEmbedded(opts ...Option) (*Authority, error) {
 		a.keyManager = newInstrumentedKeyManager(a.keyManager, a.meter)
 		a.keyManager = newInstrumentedKeyManager(a.keyManager, a.meter)
 	}
 	}
 
 
+	// Initialize system cert pool
+	if err := initializeSystemCertPool(); err != nil {
+		return nil, fmt.Errorf("failed to initialize the system cert pool: %w", err)
+	}
+
 	// Validate required options
 	// Validate required options
 	switch {
 	switch {
 	case a.config == nil:
 	case a.config == nil:
@@ -500,7 +510,7 @@ func (a *Authority) init() error {
 	clientRoots := make([]*x509.Certificate, 0, len(a.rootX509Certs)+len(a.federatedX509Certs))
 	clientRoots := make([]*x509.Certificate, 0, len(a.rootX509Certs)+len(a.federatedX509Certs))
 	clientRoots = append(clientRoots, a.rootX509Certs...)
 	clientRoots = append(clientRoots, a.rootX509Certs...)
 	clientRoots = append(clientRoots, a.federatedX509Certs...)
 	clientRoots = append(clientRoots, a.federatedX509Certs...)
-	a.httpClient, err = newHTTPClient(a.wrapTransport, clientRoots...)
+	a.httpClient = newHTTPClient(a.wrapTransport, clientRoots...)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}

+ 11 - 0
authority/authority_test.go

@@ -8,6 +8,7 @@ import (
 	"crypto/x509"
 	"crypto/x509"
 	"encoding/hex"
 	"encoding/hex"
 	"encoding/pem"
 	"encoding/pem"
+	"fmt"
 	"net"
 	"net"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
@@ -25,6 +26,16 @@ import (
 	"go.step.sm/crypto/pemutil"
 	"go.step.sm/crypto/pemutil"
 )
 )
 
 
+func TestMain(m *testing.M) {
+	if err := initializeSystemCertPool(); err != nil {
+		fmt.Fprintf(os.Stderr, "failed to initialize system cert pool: %v\n", err)
+		fmt.Fprintln(os.Stderr, "See https://pkg.go.dev/crypto/x509#SystemCertPool")
+		os.Exit(2)
+	}
+
+	os.Exit(m.Run())
+}
+
 func testAuthority(t *testing.T, opts ...Option) *Authority {
 func testAuthority(t *testing.T, opts ...Option) *Authority {
 	maxjwk, err := jose.ReadKey("testdata/secrets/max_pub.jwk")
 	maxjwk, err := jose.ReadKey("testdata/secrets/max_pub.jwk")
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)

+ 40 - 22
authority/http_client.go

@@ -3,36 +3,54 @@ package authority
 import (
 import (
 	"crypto/tls"
 	"crypto/tls"
 	"crypto/x509"
 	"crypto/x509"
-	"fmt"
 	"net/http"
 	"net/http"
+	"sync/atomic"
 
 
+	"github.com/smallstep/certificates/authority/poolhttp"
+	"github.com/smallstep/certificates/authority/provisioner"
 	"github.com/smallstep/certificates/internal/httptransport"
 	"github.com/smallstep/certificates/internal/httptransport"
 )
 )
 
 
+// systemCertPool holds a copy of the system cert pool. This cert pool must be
+// initialized when the authority is created and we should always get a clone of
+// this pool.
+var systemCertPool atomic.Pointer[x509.CertPool]
+
+// initializeSystemCertPool initializes the system cert pool if necessary.
+func initializeSystemCertPool() error {
+	if systemCertPool.Load() == nil {
+		pool, err := x509.SystemCertPool()
+		if err != nil {
+			return err
+		}
+		systemCertPool.Store(pool)
+	}
+	return nil
+}
+
 // newHTTPClient will return an HTTP client that trusts the system cert pool and
 // newHTTPClient will return an HTTP client that trusts the system cert pool and
 // the given roots.
 // the given roots.
-func newHTTPClient(wt httptransport.Wrapper, roots ...*x509.Certificate) (*http.Client, error) {
-	pool, err := x509.SystemCertPool()
-	if err != nil {
-		return nil, fmt.Errorf("error initializing http client: %w", err)
-	}
-	for _, crt := range roots {
-		pool.AddCert(crt)
-	}
+func newHTTPClient(wt httptransport.Wrapper, roots ...*x509.Certificate) provisioner.HTTPClient {
+	return poolhttp.New(func() *http.Client {
+		pool := systemCertPool.Load().Clone()
+		for _, crt := range roots {
+			pool.AddCert(crt)
+		}
 
 
-	tr, ok := http.DefaultTransport.(*http.Transport)
-	if !ok {
-		tr = httptransport.New()
-	} else {
-		tr = tr.Clone()
-	}
+		tr, ok := http.DefaultTransport.(*http.Transport)
+		if !ok {
+			tr = httptransport.New()
+		} else {
+			tr = tr.Clone()
+		}
 
 
-	tr.TLSClientConfig = &tls.Config{
-		MinVersion: tls.VersionTLS12,
-		RootCAs:    pool,
-	}
+		tr.TLSClientConfig = &tls.Config{
+			MinVersion: tls.VersionTLS12,
+			RootCAs:    pool,
+		}
+
+		rr := wt(tr)
 
 
-	return &http.Client{
-		Transport: wt(tr),
-	}, nil
+		return &http.Client{Transport: rr}
+	})
 }
 }

+ 1 - 2
authority/http_client_test.go

@@ -114,8 +114,7 @@ func Test_newHTTPClient(t *testing.T) {
 		}{http.DefaultTransport}
 		}{http.DefaultTransport}
 		http.DefaultTransport = transport
 		http.DefaultTransport = transport
 
 
-		client, err := newHTTPClient(httptransport.NoopWrapper(), auth.rootX509Certs...)
-		assert.NoError(t, err)
+		client := newHTTPClient(httptransport.NoopWrapper(), auth.rootX509Certs...)
 		assert.NotNil(t, client)
 		assert.NotNil(t, client)
 	})
 	})
 }
 }

+ 1 - 2
authority/options.go

@@ -5,7 +5,6 @@ import (
 	"crypto"
 	"crypto"
 	"crypto/x509"
 	"crypto/x509"
 	"encoding/pem"
 	"encoding/pem"
-	"net/http"
 
 
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 	"golang.org/x/crypto/ssh"
 	"golang.org/x/crypto/ssh"
@@ -97,7 +96,7 @@ func WithQuietInit() Option {
 }
 }
 
 
 // WithWebhookClient sets the http.Client to be used for outbound requests.
 // WithWebhookClient sets the http.Client to be used for outbound requests.
-func WithWebhookClient(c *http.Client) Option {
+func WithWebhookClient(c provisioner.HTTPClient) Option {
 	return func(a *Authority) error {
 	return func(a *Authority) error {
 		a.webhookClient = c
 		a.webhookClient = c
 		return nil
 		return nil

+ 94 - 0
authority/poolhttp/poolhttp.go

@@ -0,0 +1,94 @@
+package poolhttp
+
+import (
+	"net/http"
+	"sync"
+
+	"github.com/smallstep/certificates/internal/httptransport"
+)
+
+// Transporter is implemented by custom HTTP clients with a method that
+// returns an [*http.Transport].
+type Transporter interface {
+	Transport() *http.Transport
+}
+
+// Client is an HTTP client that uses a [sync.Pool] to create new and reuse HTTP
+// clients. It implements the [provisioner.HTTPClient] and [Transporter]
+// interfaces. This is the HTTP client used by the provisioners.
+type Client struct {
+	rw   sync.RWMutex
+	pool sync.Pool
+}
+
+// New creates a new poolhttp [Client], the [sync.Pool] will initialize a new
+// [*http.Client] with the given function.
+func New(fn func() *http.Client) *Client {
+	return &Client{
+		pool: sync.Pool{
+			New: func() any { return fn() },
+		},
+	}
+}
+
+// SetNew replaces the inner pool with a new [sync.Pool] with the given New
+// function. This method can be use concurrently with other methods of this
+// package.
+func (c *Client) SetNew(fn func() *http.Client) {
+	c.rw.Lock()
+	c.pool = sync.Pool{
+		New: func() any { return fn() },
+	}
+	c.rw.Unlock()
+}
+
+// getClient gets a client from the pool.
+func (c *Client) getClient() *http.Client {
+	c.rw.RLock()
+	defer c.rw.RUnlock()
+	if hc, ok := c.pool.Get().(*http.Client); ok && hc != nil {
+		return hc
+	}
+	return nil
+}
+
+// Get issues a GET request to the specified URL. If the response is one of the
+// following redirect codes, Get follows the redirect after calling the
+// [Client.CheckRedirect] function:
+func (c *Client) Get(u string) (resp *http.Response, err error) {
+	if hc := c.getClient(); hc != nil {
+		resp, err = hc.Get(u)
+		c.pool.Put(hc)
+	} else {
+		resp, err = http.DefaultClient.Get(u)
+	}
+
+	return
+}
+
+// Do sends an HTTP request and returns an HTTP response, following policy (such
+// as redirects, cookies, auth) as configured on the client.
+func (c *Client) Do(req *http.Request) (resp *http.Response, err error) {
+	if hc := c.getClient(); hc != nil {
+		resp, err = hc.Do(req)
+		c.pool.Put(hc)
+	} else {
+		resp, err = http.DefaultClient.Do(req)
+	}
+
+	return
+}
+
+// Transport() returns a clone of the http.Client Transport or returns the
+// default transport.
+func (c *Client) Transport() *http.Transport {
+	if hc := c.getClient(); hc != nil {
+		tr, ok := hc.Transport.(*http.Transport)
+		c.pool.Put(hc)
+		if ok {
+			return tr.Clone()
+		}
+	}
+
+	return httptransport.New()
+}

+ 126 - 0
authority/poolhttp/poolhttp_test.go

@@ -0,0 +1,126 @@
+package poolhttp
+
+import (
+	"fmt"
+	"io"
+	"net/http"
+	"net/http/httptest"
+	"strconv"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func requireBody(t *testing.T, want string, r io.ReadCloser) {
+	t.Helper()
+	t.Cleanup(func() {
+		require.NoError(t, r.Close())
+	})
+
+	b, err := io.ReadAll(r)
+	require.NoError(t, err)
+	require.Equal(t, want, string(b))
+}
+
+func TestClient(t *testing.T) {
+	httpSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprintln(w, "Hello World")
+	}))
+	t.Cleanup(httpSrv.Close)
+	tlsSrv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprintln(w, "Hello World")
+	}))
+	t.Cleanup(tlsSrv.Close)
+
+	tests := []struct {
+		name   string
+		client *Client
+		srv    *httptest.Server
+	}{
+		{"http", New(func() *http.Client { return httpSrv.Client() }), httpSrv},
+		{"tls", New(func() *http.Client { return tlsSrv.Client() }), tlsSrv},
+		{"nil", New(func() *http.Client { return nil }), httpSrv},
+		{"empty", &Client{}, httpSrv},
+	}
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			resp, err := tc.client.Get(tc.srv.URL)
+			require.NoError(t, err)
+			requireBody(t, "Hello World\n", resp.Body)
+
+			req, err := http.NewRequest("GET", tc.srv.URL, http.NoBody)
+			require.NoError(t, err)
+
+			resp, err = tc.client.Do(req)
+			require.NoError(t, err)
+			requireBody(t, "Hello World\n", resp.Body)
+
+			client := &http.Client{
+				Transport: tc.client.Transport(),
+			}
+			resp, err = client.Get(tc.srv.URL)
+			require.NoError(t, err)
+			requireBody(t, "Hello World\n", resp.Body)
+		})
+	}
+}
+
+func TestClient_SetNew(t *testing.T) {
+	srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprintln(w, "Hello World")
+	}))
+	t.Cleanup(srv.Close)
+
+	c := New(func() *http.Client {
+		return srv.Client()
+	})
+
+	tests := []struct {
+		name      string
+		client    *http.Client
+		assertion assert.ErrorAssertionFunc
+	}{
+		{"ok", srv.Client(), assert.NoError},
+		{"fail", http.DefaultClient, assert.Error},
+		{"ok again", srv.Client(), assert.NoError},
+	}
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			c.SetNew(func() *http.Client {
+				return tc.client
+			})
+			_, err := c.Get(srv.URL)
+			tc.assertion(t, err)
+
+		})
+	}
+}
+
+func TestClient_parallel(t *testing.T) {
+	t.Parallel()
+
+	srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprintln(w, "Hello World")
+	}))
+	t.Cleanup(srv.Close)
+
+	c := New(func() *http.Client {
+		return srv.Client()
+	})
+	req, err := http.NewRequest("GET", srv.URL, http.NoBody)
+	require.NoError(t, err)
+
+	for i := range 10 {
+		t.Run(strconv.Itoa(i), func(t *testing.T) {
+			t.Parallel()
+			resp, err := c.Get(srv.URL)
+			require.NoError(t, err)
+			requireBody(t, "Hello World\n", resp.Body)
+
+			resp, err = c.Do(req)
+			require.NoError(t, err)
+			requireBody(t, "Hello World\n", resp.Body)
+		})
+	}
+}

+ 4 - 3
authority/provisioner/controller.go

@@ -28,9 +28,9 @@ type Controller struct {
 	AuthorizeRenewFunc    AuthorizeRenewFunc
 	AuthorizeRenewFunc    AuthorizeRenewFunc
 	AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
 	AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
 	policy                *policyEngine
 	policy                *policyEngine
-	webhookClient         *http.Client
+	httpClient            HTTPClient
+	webhookClient         HTTPClient
 	webhooks              []*Webhook
 	webhooks              []*Webhook
-	httpClient            *http.Client
 	wrapTransport         httptransport.Wrapper
 	wrapTransport         httptransport.Wrapper
 }
 }
 
 
@@ -48,6 +48,7 @@ func NewController(p Interface, claims *Claims, config Config, options *Options)
 	if wt == nil {
 	if wt == nil {
 		wt = httptransport.NoopWrapper()
 		wt = httptransport.NoopWrapper()
 	}
 	}
+
 	return &Controller{
 	return &Controller{
 		Interface:             p,
 		Interface:             p,
 		Audiences:             &config.Audiences,
 		Audiences:             &config.Audiences,
@@ -65,7 +66,7 @@ func NewController(p Interface, claims *Claims, config Config, options *Options)
 
 
 // GetHTTPClient returns the configured HTTP client or the default one if none
 // GetHTTPClient returns the configured HTTP client or the default one if none
 // is configured.
 // is configured.
-func (c *Controller) GetHTTPClient() *http.Client {
+func (c *Controller) GetHTTPClient() HTTPClient {
 	if c.httpClient != nil {
 	if c.httpClient != nil {
 		return c.httpClient
 		return c.httpClient
 	}
 	}

+ 16 - 38
authority/provisioner/keystore.go

@@ -3,7 +3,6 @@ package provisioner
 import (
 import (
 	"encoding/json"
 	"encoding/json"
 	"math/rand"
 	"math/rand"
-	"net/http"
 	"regexp"
 	"regexp"
 	"strconv"
 	"strconv"
 	"sync"
 	"sync"
@@ -22,33 +21,26 @@ var maxAgeRegex = regexp.MustCompile(`max-age=(\d+)`)
 
 
 type keyStore struct {
 type keyStore struct {
 	sync.RWMutex
 	sync.RWMutex
-	client *http.Client
+	client HTTPClient
 	uri    string
 	uri    string
 	keySet jose.JSONWebKeySet
 	keySet jose.JSONWebKeySet
-	timer  *time.Timer
 	expiry time.Time
 	expiry time.Time
 	jitter time.Duration
 	jitter time.Duration
 }
 }
 
 
-func newKeyStore(client *http.Client, uri string) (*keyStore, error) {
+func newKeyStore(client HTTPClient, uri string) (*keyStore, error) {
 	keys, age, err := getKeysFromJWKsURI(client, uri)
 	keys, age, err := getKeysFromJWKsURI(client, uri)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	ks := &keyStore{
+	jitter := getCacheJitter(age)
+	return &keyStore{
 		client: client,
 		client: client,
 		uri:    uri,
 		uri:    uri,
 		keySet: keys,
 		keySet: keys,
-		expiry: getExpirationTime(age),
-		jitter: getCacheJitter(age),
-	}
-	next := ks.nextReloadDuration(age)
-	ks.timer = time.AfterFunc(next, ks.reload)
-	return ks, nil
-}
-
-func (ks *keyStore) Close() {
-	ks.timer.Stop()
+		expiry: getExpirationTime(age, jitter),
+		jitter: jitter,
+	}, nil
 }
 }
 
 
 func (ks *keyStore) Get(kid string) (keys []jose.JSONWebKey) {
 func (ks *keyStore) Get(kid string) (keys []jose.JSONWebKey) {
@@ -65,34 +57,16 @@ func (ks *keyStore) Get(kid string) (keys []jose.JSONWebKey) {
 }
 }
 
 
 func (ks *keyStore) reload() {
 func (ks *keyStore) reload() {
-	var next time.Duration
-	keys, age, err := getKeysFromJWKsURI(ks.client, ks.uri)
-	if err != nil {
-		next = ks.nextReloadDuration(ks.jitter / 2)
-	} else {
+	if keys, age, err := getKeysFromJWKsURI(ks.client, ks.uri); err == nil {
 		ks.Lock()
 		ks.Lock()
 		ks.keySet = keys
 		ks.keySet = keys
-		ks.expiry = getExpirationTime(age)
 		ks.jitter = getCacheJitter(age)
 		ks.jitter = getCacheJitter(age)
-		next = ks.nextReloadDuration(age)
+		ks.expiry = getExpirationTime(age, ks.jitter)
 		ks.Unlock()
 		ks.Unlock()
 	}
 	}
-
-	ks.Lock()
-	ks.timer.Reset(next)
-	ks.Unlock()
-}
-
-// nextReloadDuration would return the duration for the next rotation. If age is
-// 0 it will randomly rotate between 0-12 hours, but every time we call to Get
-// it will automatically rotate.
-func (ks *keyStore) nextReloadDuration(age time.Duration) time.Duration {
-	n := rand.Int63n(int64(ks.jitter)) //nolint:gosec // not used for cryptographic security
-	age -= time.Duration(n)
-	return abs(age)
 }
 }
 
 
-func getKeysFromJWKsURI(client *http.Client, uri string) (jose.JSONWebKeySet, time.Duration, error) {
+func getKeysFromJWKsURI(client HTTPClient, uri string) (jose.JSONWebKeySet, time.Duration, error) {
 	var keys jose.JSONWebKeySet
 	var keys jose.JSONWebKeySet
 	resp, err := client.Get(uri)
 	resp, err := client.Get(uri)
 	if err != nil {
 	if err != nil {
@@ -136,8 +110,12 @@ func getCacheJitter(age time.Duration) time.Duration {
 	}
 	}
 }
 }
 
 
-func getExpirationTime(age time.Duration) time.Time {
-	return time.Now().Truncate(time.Second).Add(age)
+func getExpirationTime(age, jitter time.Duration) time.Time {
+	if age > 0 {
+		n := rand.Int63n(int64(jitter)) //nolint:gosec // not used for cryptographic security
+		age -= time.Duration(n)
+	}
+	return time.Now().Truncate(time.Second).Add(abs(age))
 }
 }
 
 
 // abs returns the absolute value of n.
 // abs returns the absolute value of n.

+ 1 - 19
authority/provisioner/keystore_test.go

@@ -22,7 +22,6 @@ func Test_newKeyStore(t *testing.T) {
 
 
 	ks, err := newKeyStore(srv.Client(), srv.URL)
 	ks, err := newKeyStore(srv.Client(), srv.URL)
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
-	defer ks.Close()
 
 
 	type args struct {
 	type args struct {
 		client *http.Client
 		client *http.Client
@@ -49,7 +48,6 @@ func Test_newKeyStore(t *testing.T) {
 				if !reflect.DeepEqual(got.keySet, tt.want) {
 				if !reflect.DeepEqual(got.keySet, tt.want) {
 					t.Errorf("newKeyStore() = %v, want %v", got, tt.want)
 					t.Errorf("newKeyStore() = %v, want %v", got, tt.want)
 				}
 				}
-				got.Close()
 			}
 			}
 		})
 		})
 	}
 	}
@@ -61,7 +59,6 @@ func Test_keyStore(t *testing.T) {
 
 
 	ks, err := newKeyStore(srv.Client(), srv.URL+"/random")
 	ks, err := newKeyStore(srv.Client(), srv.URL+"/random")
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
-	defer ks.Close()
 	ks.RLock()
 	ks.RLock()
 	keySet1 := ks.keySet
 	keySet1 := ks.keySet
 	ks.RUnlock()
 	ks.RUnlock()
@@ -73,6 +70,7 @@ func Test_keyStore(t *testing.T) {
 
 
 	// Wait for rotation
 	// Wait for rotation
 	time.Sleep(5 * time.Second)
 	time.Sleep(5 * time.Second)
+	assert.Len(t, 0, ks.Get("foobar")) // force refresh
 
 
 	ks.RLock()
 	ks.RLock()
 	keySet2 := ks.keySet
 	keySet2 := ks.keySet
@@ -105,7 +103,6 @@ func Test_keyStore_noCache(t *testing.T) {
 
 
 	ks, err := newKeyStore(srv.Client(), srv.URL+"/no-cache")
 	ks, err := newKeyStore(srv.Client(), srv.URL+"/no-cache")
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
-	defer ks.Close()
 	ks.RLock()
 	ks.RLock()
 	keySet1 := ks.keySet
 	keySet1 := ks.keySet
 	ks.RUnlock()
 	ks.RUnlock()
@@ -116,20 +113,6 @@ func Test_keyStore_noCache(t *testing.T) {
 	assert.Len(t, 0, ks.Get(keySet1.Keys[1].KeyID))
 	assert.Len(t, 0, ks.Get(keySet1.Keys[1].KeyID))
 	assert.Len(t, 0, ks.Get("foobar"))
 	assert.Len(t, 0, ks.Get("foobar"))
 
 
-	ks.RLock()
-	keySet2 := ks.keySet
-	ks.RUnlock()
-	if reflect.DeepEqual(keySet1, keySet2) {
-		t.Error("keyStore did not rotated")
-	}
-
-	// The keys will rotate on Get.
-	// So we won't be able to find the cached ones
-	assert.Len(t, 2, keySet2.Keys)
-	assert.Len(t, 0, ks.Get(keySet2.Keys[0].KeyID))
-	assert.Len(t, 0, ks.Get(keySet2.Keys[1].KeyID))
-	assert.Len(t, 0, ks.Get("foobar"))
-
 	// Check hits
 	// Check hits
 	resp, err := srv.Client().Get(srv.URL + "/hits")
 	resp, err := srv.Client().Get(srv.URL + "/hits")
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
@@ -147,7 +130,6 @@ func Test_keyStore_Get(t *testing.T) {
 	defer srv.Close()
 	defer srv.Close()
 	ks, err := newKeyStore(srv.Client(), srv.URL)
 	ks, err := newKeyStore(srv.Client(), srv.URL)
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
-	defer ks.Close()
 
 
 	type args struct {
 	type args struct {
 		kid string
 		kid string

+ 1 - 1
authority/provisioner/oidc.go

@@ -485,7 +485,7 @@ func (o *OIDC) AuthorizeSSHRevoke(_ context.Context, token string) error {
 	return errs.Unauthorized("oidc.AuthorizeSSHRevoke; cannot revoke with non-admin oidc token")
 	return errs.Unauthorized("oidc.AuthorizeSSHRevoke; cannot revoke with non-admin oidc token")
 }
 }
 
 
-func getAndDecode(client *http.Client, uri string, v interface{}) error {
+func getAndDecode(client HTTPClient, uri string, v interface{}) error {
 	resp, err := client.Get(uri)
 	resp, err := client.Get(uri)
 	if err != nil {
 	if err != nil {
 		return errors.Wrapf(err, "failed to connect to %s", uri)
 		return errors.Wrapf(err, "failed to connect to %s", uri)

+ 9 - 2
authority/provisioner/provisioner.go

@@ -34,6 +34,13 @@ type Interface interface {
 	AuthorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []SignOption, error)
 	AuthorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []SignOption, error)
 }
 }
 
 
+// HTTPClient is the interface implemented by the HTTP clients used by the
+// provisioners.
+type HTTPClient interface {
+	Get(string) (*http.Response, error)
+	Do(*http.Request) (*http.Response, error)
+}
+
 // Uninitialized represents a disabled provisioner. Uninitialized provisioners
 // Uninitialized represents a disabled provisioner. Uninitialized provisioners
 // are created when the Init methods fails.
 // are created when the Init methods fails.
 type Uninitialized struct {
 type Uninitialized struct {
@@ -258,12 +265,12 @@ type Config struct {
 	// certificate can be renewed.
 	// certificate can be renewed.
 	AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
 	AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
 	// WebhookClient is an HTTP client used when performing webhook requests.
 	// WebhookClient is an HTTP client used when performing webhook requests.
-	WebhookClient *http.Client
+	WebhookClient HTTPClient
 	// SCEPKeyManager, if defined, is the interface used by SCEP provisioners.
 	// SCEPKeyManager, if defined, is the interface used by SCEP provisioners.
 	SCEPKeyManager SCEPKeyManager
 	SCEPKeyManager SCEPKeyManager
 	// HTTPClient is an HTTP client that trusts the system cert pool and the CA
 	// HTTPClient is an HTTP client that trusts the system cert pool and the CA
 	// roots.
 	// roots.
-	HTTPClient *http.Client
+	HTTPClient HTTPClient
 	// WrapTransport references the function that should wrap any [http.Transport] initialized
 	// WrapTransport references the function that should wrap any [http.Transport] initialized
 	// down the Config's chain.
 	// down the Config's chain.
 	WrapTransport TransportWrapper
 	WrapTransport TransportWrapper

+ 4 - 5
authority/provisioner/scep.go

@@ -8,7 +8,6 @@ import (
 	"crypto/x509"
 	"crypto/x509"
 	"encoding/pem"
 	"encoding/pem"
 	"fmt"
 	"fmt"
-	"net/http"
 	"time"
 	"time"
 
 
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
@@ -113,14 +112,14 @@ func (s *SCEP) DefaultTLSCertDuration() time.Duration {
 }
 }
 
 
 type challengeValidationController struct {
 type challengeValidationController struct {
-	client        *http.Client
+	client        HTTPClient
 	wrapTransport httptransport.Wrapper
 	wrapTransport httptransport.Wrapper
 	webhooks      []*Webhook
 	webhooks      []*Webhook
 }
 }
 
 
 // newChallengeValidationController creates a new challengeValidationController
 // newChallengeValidationController creates a new challengeValidationController
 // that performs challenge validation through webhooks.
 // that performs challenge validation through webhooks.
-func newChallengeValidationController(client *http.Client, tw httptransport.Wrapper, webhooks []*Webhook) *challengeValidationController {
+func newChallengeValidationController(client HTTPClient, tw httptransport.Wrapper, webhooks []*Webhook) *challengeValidationController {
 	scepHooks := []*Webhook{}
 	scepHooks := []*Webhook{}
 	for _, wh := range webhooks {
 	for _, wh := range webhooks {
 		if wh.Kind != linkedca.Webhook_SCEPCHALLENGE.String() {
 		if wh.Kind != linkedca.Webhook_SCEPCHALLENGE.String() {
@@ -179,14 +178,14 @@ func (c *challengeValidationController) Validate(ctx context.Context, csr *x509.
 }
 }
 
 
 type notificationController struct {
 type notificationController struct {
-	client        *http.Client
+	client        HTTPClient
 	wrapTransport httptransport.Wrapper
 	wrapTransport httptransport.Wrapper
 	webhooks      []*Webhook
 	webhooks      []*Webhook
 }
 }
 
 
 // newNotificationController creates a new notificationController
 // newNotificationController creates a new notificationController
 // that performs SCEP notifications through webhooks.
 // that performs SCEP notifications through webhooks.
-func newNotificationController(client *http.Client, tw httptransport.Wrapper, webhooks []*Webhook) *notificationController {
+func newNotificationController(client HTTPClient, tw httptransport.Wrapper, webhooks []*Webhook) *notificationController {
 	scepHooks := []*Webhook{}
 	scepHooks := []*Webhook{}
 	for _, wh := range webhooks {
 	for _, wh := range webhooks {
 		if wh.Kind != linkedca.Webhook_NOTIFYING.String() {
 		if wh.Kind != linkedca.Webhook_NOTIFYING.String() {

+ 7 - 6
authority/provisioner/webhook.go

@@ -18,6 +18,7 @@ import (
 
 
 	"github.com/smallstep/linkedca"
 	"github.com/smallstep/linkedca"
 
 
+	"github.com/smallstep/certificates/authority/poolhttp"
 	"github.com/smallstep/certificates/internal/httptransport"
 	"github.com/smallstep/certificates/internal/httptransport"
 	"github.com/smallstep/certificates/middleware/requestid"
 	"github.com/smallstep/certificates/middleware/requestid"
 	"github.com/smallstep/certificates/templates"
 	"github.com/smallstep/certificates/templates"
@@ -31,7 +32,7 @@ type WebhookSetter interface {
 }
 }
 
 
 type WebhookController struct {
 type WebhookController struct {
-	client        *http.Client
+	client        HTTPClient
 	wrapTransport httptransport.Wrapper
 	wrapTransport httptransport.Wrapper
 	webhooks      []*Webhook
 	webhooks      []*Webhook
 	certType      linkedca.Webhook_CertType
 	certType      linkedca.Webhook_CertType
@@ -146,7 +147,7 @@ type Webhook struct {
 // [http.RoundTripper].
 // [http.RoundTripper].
 type TransportWrapper = httptransport.Wrapper
 type TransportWrapper = httptransport.Wrapper
 
 
-func (w *Webhook) DoWithContext(ctx context.Context, client *http.Client, tw TransportWrapper, reqBody *webhook.RequestBody, data any) (*webhook.ResponseBody, error) {
+func (w *Webhook) DoWithContext(ctx context.Context, client HTTPClient, tw TransportWrapper, reqBody *webhook.RequestBody, data any) (*webhook.ResponseBody, error) {
 	tmpl, err := template.New("url").Funcs(templates.StepFuncMap()).Parse(w.URL)
 	tmpl, err := template.New("url").Funcs(templates.StepFuncMap()).Parse(w.URL)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
@@ -206,11 +207,11 @@ retry:
 	}
 	}
 
 
 	if w.DisableTLSClientAuth {
 	if w.DisableTLSClientAuth {
-		transport, ok := client.Transport.(*http.Transport)
-		if !ok {
-			transport = httptransport.New()
+		var transport *http.Transport
+		if ct, ok := client.(poolhttp.Transporter); ok {
+			transport = ct.Transport()
 		} else {
 		} else {
-			transport = transport.Clone()
+			transport = httptransport.New()
 		}
 		}
 
 
 		if transport.TLSClientConfig != nil {
 		if transport.TLSClientConfig != nil {

+ 17 - 9
templates/templates.go

@@ -5,6 +5,7 @@ import (
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"strings"
 	"strings"
+	"sync"
 	"text/template"
 	"text/template"
 
 
 	"github.com/Masterminds/sprig/v3"
 	"github.com/Masterminds/sprig/v3"
@@ -29,6 +30,22 @@ const (
 	Directory TemplateType = "directory"
 	Directory TemplateType = "directory"
 )
 )
 
 
+var (
+	stepFuncMap  template.FuncMap
+	stepFuncOnce sync.Once
+)
+
+// StepFuncMap returns sprig.TxtFuncMap but removing the "env" and "expandenv"
+// functions to avoid any leak of information.
+func StepFuncMap() template.FuncMap {
+	stepFuncOnce.Do(func() {
+		stepFuncMap = sprig.TxtFuncMap()
+		delete(stepFuncMap, "env")
+		delete(stepFuncMap, "expandenv")
+	})
+	return stepFuncMap
+}
+
 // Templates is a collection of templates and variables.
 // Templates is a collection of templates and variables.
 type Templates struct {
 type Templates struct {
 	SSH  *SSHTemplates          `json:"ssh,omitempty"`
 	SSH  *SSHTemplates          `json:"ssh,omitempty"`
@@ -282,12 +299,3 @@ func mkdir(path string, perm os.FileMode) error {
 	}
 	}
 	return nil
 	return nil
 }
 }
-
-// StepFuncMap returns sprig.TxtFuncMap but removing the "env" and "expandenv"
-// functions to avoid any leak of information.
-func StepFuncMap() template.FuncMap {
-	m := sprig.TxtFuncMap()
-	delete(m, "env")
-	delete(m, "expandenv")
-	return m
-}