Skip to content

Commit c986962

Browse files
authored
internal/httptransport: initial implementation of the package (#2098)
* internal/httptransport: initial implementation of the package * authority: refactored for httptransport * ca: refactored for httptransport * test: refactored for httptransport
1 parent 51e253b commit c986962

File tree

8 files changed

+53
-13
lines changed

8 files changed

+53
-13
lines changed

authority/provisioner/webhook.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ import (
1515
"time"
1616

1717
"github.com/pkg/errors"
18+
19+
"go.step.sm/linkedca"
20+
21+
"github.com/smallstep/certificates/internal/httptransport"
1822
"github.com/smallstep/certificates/middleware/requestid"
1923
"github.com/smallstep/certificates/templates"
2024
"github.com/smallstep/certificates/webhook"
21-
"go.step.sm/linkedca"
2225
)
2326

2427
var ErrWebhookDenied = errors.New("webhook server did not allow request")
@@ -200,13 +203,16 @@ retry:
200203
if w.DisableTLSClientAuth {
201204
transport, ok := client.Transport.(*http.Transport)
202205
if !ok {
203-
return nil, errors.New("client transport is not a *http.Transport")
206+
transport = httptransport.New()
207+
} else {
208+
transport = transport.Clone()
204209
}
205-
transport = transport.Clone()
206-
tlsConfig := transport.TLSClientConfig.Clone()
207-
tlsConfig.GetClientCertificate = nil
208-
tlsConfig.Certificates = nil
209-
transport.TLSClientConfig = tlsConfig
210+
211+
if transport.TLSClientConfig != nil {
212+
transport.TLSClientConfig.GetClientCertificate = nil
213+
transport.TLSClientConfig.Certificates = nil
214+
}
215+
210216
client = &http.Client{
211217
Transport: transport,
212218
}

authority/provisioner/webhook_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"go.step.sm/crypto/x509util"
2525
"go.step.sm/linkedca"
2626

27+
"github.com/smallstep/certificates/internal/httptransport"
2728
"github.com/smallstep/certificates/middleware/requestid"
2829
"github.com/smallstep/certificates/webhook"
2930
)
@@ -647,7 +648,8 @@ func TestWebhook_Do(t *testing.T) {
647648
}
648649
cert, err := tls.LoadX509KeyPair("testdata/certs/foo.crt", "testdata/secrets/foo.key")
649650
require.NoError(t, err)
650-
transport := http.DefaultTransport.(*http.Transport).Clone()
651+
652+
transport := httptransport.New()
651653
transport.TLSClientConfig = &tls.Config{
652654
InsecureSkipVerify: true,
653655
Certificates: []tls.Certificate{cert},

ca/ca.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/smallstep/certificates/authority/config"
3434
"github.com/smallstep/certificates/cas/apiv1"
3535
"github.com/smallstep/certificates/db"
36+
"github.com/smallstep/certificates/internal/httptransport"
3637
"github.com/smallstep/certificates/internal/metrix"
3738
"github.com/smallstep/certificates/logging"
3839
"github.com/smallstep/certificates/middleware/requestid"
@@ -196,7 +197,7 @@ func (ca *CA) Init(cfg *config.Config) (*CA, error) {
196197
opts = append(opts, authority.WithMeter(meter))
197198
}
198199

199-
webhookTransport := http.DefaultTransport.(*http.Transport).Clone()
200+
webhookTransport := httptransport.New()
200201
opts = append(opts, authority.WithWebhookClient(&http.Client{Transport: webhookTransport}))
201202

202203
auth, err := authority.New(cfg, opts...)

ca/identity/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111

1212
"github.com/pkg/errors"
13+
"github.com/smallstep/certificates/internal/httptransport"
1314
)
1415

1516
// Client wraps http.Client with a transport using the step root and identity.
@@ -60,7 +61,7 @@ func LoadClient() (*Client, error) {
6061
}
6162

6263
// Prepare transport with information in defaults.json and identity.json
63-
tr := http.DefaultTransport.(*http.Transport).Clone()
64+
tr := httptransport.New()
6465
tr.TLSClientConfig = &tls.Config{
6566
MinVersion: tls.VersionTLS12,
6667
GetClientCertificate: identity.GetClientCertificateFunc(),

ca/identity/client_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"reflect"
1111
"sort"
1212
"testing"
13+
14+
"github.com/smallstep/certificates/internal/httptransport"
1315
)
1416

1517
func returnInput(val string) func() string {
@@ -129,7 +131,7 @@ func TestLoadClient(t *testing.T) {
129131
pool := x509.NewCertPool()
130132
pool.AppendCertsFromPEM(b)
131133

132-
tr := http.DefaultTransport.(*http.Transport).Clone()
134+
tr := httptransport.New()
133135
tr.TLSClientConfig = &tls.Config{
134136
Certificates: []tls.Certificate{crt},
135137
RootCAs: pool,

ca/identity/identity.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"go.step.sm/crypto/pemutil"
2020

2121
"github.com/smallstep/certificates/api"
22+
"github.com/smallstep/certificates/internal/httptransport"
2223
)
2324

2425
// Type represents the different types of identity files.
@@ -295,7 +296,7 @@ func (i *Identity) Renew(client Renewer) error {
295296
return err
296297
}
297298

298-
tr := http.DefaultTransport.(*http.Transport).Clone()
299+
tr := httptransport.New()
299300
tr.TLSClientConfig = &tls.Config{
300301
Certificates: []tls.Certificate{cert},
301302
RootCAs: client.GetRootCAs(),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Package httptransport implements initialization of [http.Transport] instances and related
2+
// functionality.
3+
package httptransport
4+
5+
import (
6+
"net"
7+
"net/http"
8+
"time"
9+
)
10+
11+
// New returns a reference to an [http.Transport] that's initialized just like the
12+
// [http.DefaultTransport] is by the standard library.
13+
func New() *http.Transport {
14+
return &http.Transport{
15+
Proxy: http.ProxyFromEnvironment,
16+
DialContext: (&net.Dialer{
17+
Timeout: 30 * time.Second,
18+
KeepAlive: 30 * time.Second,
19+
}).DialContext,
20+
ForceAttemptHTTP2: true,
21+
MaxIdleConns: 100,
22+
IdleConnTimeout: 90 * time.Second,
23+
TLSHandshakeTimeout: 10 * time.Second,
24+
ExpectContinueTimeout: 1 * time.Second,
25+
}
26+
}

test/integration/scep/common_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/smallstep/certificates/authority/provisioner"
3838
"github.com/smallstep/certificates/ca"
3939
"github.com/smallstep/certificates/cas/apiv1"
40+
"github.com/smallstep/certificates/internal/httptransport"
4041
)
4142

4243
func newCAClient(t *testing.T, caURL, rootFilepath string) *ca.Client {
@@ -170,7 +171,7 @@ func createSCEPClient(t *testing.T, caURL string, root *x509.Certificate) *clien
170171
t.Helper()
171172
trustedRoots := x509.NewCertPool()
172173
trustedRoots.AddCert(root)
173-
transport := http.DefaultTransport.(*http.Transport).Clone()
174+
transport := httptransport.New()
174175
transport.TLSClientConfig = &tls.Config{
175176
RootCAs: trustedRoots,
176177
}

0 commit comments

Comments
 (0)