testenv: avoid dns lookups for *.localhost.pomerium.io (#5372)

* testenv: avoid dns lookups for localhost.pomerium.io

* linter pass
This commit is contained in:
Joe Kralicky 2024-12-02 12:29:15 -05:00 committed by GitHub
parent 55e13f9608
commit 39e789529e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 142 additions and 8 deletions

View file

@ -11,6 +11,7 @@ import (
"net"
"net/http"
"net/http/cookiejar"
"net/http/httptrace"
"net/url"
"strconv"
"strings"
@ -22,6 +23,7 @@ import (
"github.com/pomerium/pomerium/internal/retry"
"github.com/pomerium/pomerium/internal/testenv"
"github.com/pomerium/pomerium/internal/testenv/values"
"github.com/pomerium/pomerium/pkg/telemetry/requestid"
"google.golang.org/protobuf/proto"
)
@ -33,6 +35,7 @@ type RequestOptions struct {
body any
clientCerts []tls.Certificate
client *http.Client
trace *httptrace.ClientTrace
}
type RequestOption func(*RequestOptions)
@ -77,6 +80,12 @@ func Client(c *http.Client) RequestOption {
}
}
func WithClientTrace(ct *httptrace.ClientTrace) RequestOption {
return func(o *RequestOptions) {
o.trace = ct
}
}
// Body sets the body of the request.
// The argument can be one of the following types:
// - string
@ -220,7 +229,11 @@ func (h *httpUpstream) Do(method string, r testenv.Route, opts ...RequestOption)
RawQuery: options.query.Encode(),
})
}
req, err := http.NewRequest(method, u.String(), nil)
ctx := h.Env().Context()
if options.trace != nil {
ctx = httptrace.WithClientTrace(ctx, options.trace)
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), nil)
if err != nil {
return nil, err
}
@ -249,13 +262,14 @@ func (h *httpUpstream) Do(method string, r testenv.Route, opts ...RequestOption)
}
newClient := func() *http.Client {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{
RootCAs: h.Env().ServerCAs(),
Certificates: options.clientCerts,
}
transport.DialTLSContext = nil
c := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: h.Env().ServerCAs(),
Certificates: options.clientCerts,
},
},
Transport: requestid.NewRoundTripper(transport),
}
c.Jar, _ = cookiejar.New(&cookiejar.Options{})
return &c
@ -273,7 +287,7 @@ func (h *httpUpstream) Do(method string, r testenv.Route, opts ...RequestOption)
}
var resp *http.Response
if err := retry.Retry(h.Env().Context(), "http", func(ctx context.Context) error {
if err := retry.Retry(ctx, "http", func(ctx context.Context) error {
var err error
if options.authenticateAs != "" {
resp, err = authenticateFlow(ctx, client, req, options.authenticateAs) //nolint:bodyclose