Skip to content

Commit

Permalink
aws/session: Allow HTTP Proxy with custom CA bundle (aws#2343)
Browse files Browse the repository at this point in the history
Ensures Go HTTP Client's  `ProxyFromEnvironment` functionality is still enabled when  custom CA bundles are used with the SDK.

Fix aws#2287
  • Loading branch information
jasdel authored Mar 1, 2019
2 parents 36cc7fd + e413fe6 commit 2c864ae
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
26 changes: 26 additions & 0 deletions aws/session/cabundle_transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// +build go1.7

package session

import (
"net"
"net/http"
"time"
)

// Transport that should be used when a custom CA bundle is specified with the
// SDK.
func getCABundleTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
22 changes: 22 additions & 0 deletions aws/session/cabundle_transport_1_5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build !go1.6,go1.5

package session

import (
"net"
"net/http"
"time"
)

// Transport that should be used when a custom CA bundle is specified with the
// SDK.
func getCABundleTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
}
23 changes: 23 additions & 0 deletions aws/session/cabundle_transport_1_6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build !go1.7,go1.6

package session

import (
"net"
"net/http"
"time"
)

// Transport that should be used when a custom CA bundle is specified with the
// SDK.
func getCABundleTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
30 changes: 30 additions & 0 deletions aws/session/custom_ca_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ func TestNewSession_WithCustomCABundle_Option(t *testing.T) {
}
}

func TestNewSession_WithCustomCABundle_HTTPProxyAvailable(t *testing.T) {
skipTravisTest(t)

oldEnv := initSessionTestEnv()
defer awstesting.PopEnv(oldEnv)

s, err := NewSessionWithOptions(Options{
Config: aws.Config{
HTTPClient: &http.Client{},
Region: aws.String("mock-region"),
Credentials: credentials.AnonymousCredentials,
},
CustomCABundle: bytes.NewReader(awstesting.TLSBundleCA),
})
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if s == nil {
t.Fatalf("expect session to be created, got none")
}

tr := s.Config.HTTPClient.Transport.(*http.Transport)
if tr.Proxy == nil {
t.Fatalf("expect transport proxy, was nil")
}
if tr.TLSClientConfig.RootCAs == nil {
t.Fatalf("expect TLS config to have root CAs")
}
}

func TestNewSession_WithCustomCABundle_OptionPriority(t *testing.T) {
skipTravisTest(t)

Expand Down
5 changes: 4 additions & 1 deletion aws/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ func loadCustomCABundle(s *Session, bundle io.Reader) error {
}
}
if t == nil {
t = &http.Transport{}
// Nil transport implies `http.DefaultTransport` should be used. Since
// the SDK cannot modify, nor copy the `DefaultTransport` specifying
// the values the next closest behavior.
t = getCABundleTransport()
}

p, err := loadCertPool(bundle)
Expand Down

0 comments on commit 2c864ae

Please sign in to comment.