Skip to content

Commit aee32c6

Browse files
committed
fix: use awshttp.BuildableClient in NewAWSSDKHTTPClient to prevent panic in air-gapped regions (#3672)
In air-gapped regions, the AWS SDK's resolveCustomCABundle() needs to inject custom CA certificates via WithTransportOptions on the HTTP client. Using a plain *http.Client causes a panic because it cannot be type-asserted to *awshttp.BuildableClient. The New() function already correctly uses awshttp.NewBuildableClient(), but NewAWSSDKHTTPClient() (used by awsutils.go, ec2metadatawrapper.go, ec2wrapper.go, and imds.go) still returned *http.Client. Replace &http.Client{Timeout: ...} with awshttp.NewBuildableClient().WithTimeout(...) to preserve the BuildableClient type while still setting the timeout. Panic: unable to add custom RootCAs HTTPClient, has no WithTransportOptions, *http.Client
1 parent cb7ea5e commit aee32c6

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

pkg/awsutils/awssession/session.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package awssession
1616
import (
1717
"context"
1818
"fmt"
19-
"net/http"
2019
"os"
2120
"strconv"
2221
"time"
@@ -39,8 +38,10 @@ const (
3938
)
4039

4140
// NewAWSSDKHTTPClient returns a new HTTP client with the configured AWS SDK timeout.
42-
func NewAWSSDKHTTPClient() *http.Client {
43-
return &http.Client{Timeout: getHTTPTimeout()}
41+
// It returns *awshttp.BuildableClient (instead of *http.Client) so the SDK can
42+
// inject custom CA bundles via WithTransportOptions in air-gapped regions.
43+
func NewAWSSDKHTTPClient() *awshttp.BuildableClient {
44+
return awshttp.NewBuildableClient().WithTimeout(getHTTPTimeout())
4445
}
4546

4647
var (
@@ -62,7 +63,7 @@ func getHTTPTimeout() time.Duration {
6263

6364
// New will return aws.Config to be used by Service Clients.
6465
func New(ctx context.Context) (aws.Config, error) {
65-
httpClient := awshttp.NewBuildableClient().WithTimeout(getHTTPTimeout())
66+
httpClient := NewAWSSDKHTTPClient()
6667
optFns := []func(*config.LoadOptions) error{
6768
config.WithHTTPClient(httpClient),
6869
config.WithRetryMaxAttempts(maxRetries),

pkg/awsutils/awssession/session_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func TestNew_SetsHTTPClientTimeout(t *testing.T) {
5555
func TestNewAWSSDKHTTPClient_SetsTimeout(t *testing.T) {
5656
client := NewAWSSDKHTTPClient()
5757
assert.NotNil(t, client)
58-
assert.Equal(t, DefaultAWSSDKClientTimeout, client.Timeout)
58+
assert.Equal(t, DefaultAWSSDKClientTimeout, client.GetTimeout())
5959
}
6060

6161
func TestNewAWSSDKHTTPClient_RespectsEnv(t *testing.T) {
6262
t.Setenv(httpTimeoutEnv, "20")
6363
client := NewAWSSDKHTTPClient()
64-
assert.Equal(t, 20*time.Second, client.Timeout)
64+
assert.Equal(t, 20*time.Second, client.GetTimeout())
6565
}

0 commit comments

Comments
 (0)