-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uses AWS SDK BuildableClient for custom HTTP client #116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,32 @@ | ||
package httpclient | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" | ||
"github.com/hashicorp/aws-sdk-go-base/v2/internal/config" | ||
"github.com/hashicorp/go-cleanhttp" | ||
) | ||
|
||
func DefaultHttpClient(c *config.Config) (*http.Client, error) { | ||
httpClient := cleanhttp.DefaultClient() | ||
transport := httpClient.Transport.(*http.Transport) | ||
|
||
tlsConfig := transport.TLSClientConfig | ||
if tlsConfig == nil { | ||
tlsConfig = &tls.Config{} | ||
transport.TLSClientConfig = tlsConfig | ||
} | ||
tlsConfig.MinVersion = tls.VersionTLS12 | ||
|
||
if c.Insecure { | ||
tlsConfig.InsecureSkipVerify = true | ||
} | ||
|
||
if c.HTTPProxy != "" { | ||
proxyUrl, err := url.Parse(c.HTTPProxy) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing HTTP proxy URL: %w", err) | ||
} | ||
|
||
transport.Proxy = http.ProxyURL(proxyUrl) | ||
} | ||
|
||
return httpClient, nil | ||
func DefaultHttpClient(c *config.Config) (*awshttp.BuildableClient, error) { | ||
var err error | ||
|
||
httpClient := awshttp.NewBuildableClient(). | ||
WithTransportOptions(func(tr *http.Transport) { | ||
if c.Insecure { | ||
tlsConfig := tr.TLSClientConfig | ||
tlsConfig.InsecureSkipVerify = true | ||
} | ||
if c.HTTPProxy != "" { | ||
var proxyUrl *url.URL | ||
proxyUrl, parseErr := url.Parse(c.HTTPProxy) | ||
if parseErr != nil { | ||
err = fmt.Errorf("error parsing HTTP proxy URL: %w", parseErr) | ||
} | ||
tr.Proxy = http.ProxyURL(proxyUrl) | ||
} | ||
}) | ||
|
||
return httpClient, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package httpclient_test | ||
|
||
import ( | ||
"crypto/tls" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws/transport/http" | ||
"github.com/hashicorp/aws-sdk-go-base/v2/internal/config" | ||
"github.com/hashicorp/aws-sdk-go-base/v2/internal/httpclient" | ||
) | ||
|
||
func TestHTTPClientConfiguration_basic(t *testing.T) { | ||
client, err := httpclient.DefaultHttpClient(&config.Config{}) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
|
||
transport := client.GetTransport() | ||
|
||
if a, e := transport.MaxIdleConns, http.DefaultHTTPTransportMaxIdleConns; a != e { | ||
t.Errorf("expected MaxIdleConns to be %d, got %d", e, a) | ||
} | ||
if a, e := transport.MaxIdleConnsPerHost, http.DefaultHTTPTransportMaxIdleConnsPerHost; a != e { | ||
t.Errorf("expected MaxIdleConnsPerHost to be %d, got %d", e, a) | ||
} | ||
if a, e := transport.IdleConnTimeout, http.DefaultHTTPTransportIdleConnTimeout; a != e { | ||
t.Errorf("expected IdleConnTimeout to be %s, got %s", e, a) | ||
} | ||
if a, e := transport.TLSHandshakeTimeout, http.DefaultHTTPTransportTLSHandleshakeTimeout; a != e { | ||
t.Errorf("expected TLSHandshakeTimeout to be %s, got %s", e, a) | ||
} | ||
if a, e := transport.ExpectContinueTimeout, http.DefaultHTTPTransportExpectContinueTimeout; a != e { | ||
t.Errorf("expected ExpectContinueTimeout to be %s, got %s", e, a) | ||
} | ||
if !transport.ForceAttemptHTTP2 { | ||
t.Error("expected ForceAttemptHTTP2 to be true, got false") | ||
} | ||
|
||
tlsConfig := transport.TLSClientConfig | ||
if a, e := int(tlsConfig.MinVersion), tls.VersionTLS12; a != e { | ||
t.Errorf("expected tlsConfig.MinVersion to be %d, got %d", e, a) | ||
} | ||
if tlsConfig.InsecureSkipVerify { | ||
t.Error("expected InsecureSkipVerify to be false, got true") | ||
} | ||
} | ||
|
||
func TestHTTPClientConfiguration_insecureHTTPS(t *testing.T) { | ||
client, err := httpclient.DefaultHttpClient(&config.Config{ | ||
Insecure: true, | ||
}) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
|
||
transport := client.GetTransport() | ||
|
||
tlsConfig := transport.TLSClientConfig | ||
if !tlsConfig.InsecureSkipVerify { | ||
t.Error("expected InsecureSkipVerify to be true, got false") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't love AWS's Javaisms...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AWS is a Java shop :)