-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from hashicorp/v2-add-proxy-config
V2: Add proxy config
- Loading branch information
Showing
10 changed files
with
218 additions
and
135 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,16 @@ | ||
package awsbase | ||
|
||
type Config struct { | ||
AccessKey string | ||
APNInfo *APNInfo | ||
AssumeRole *AssumeRole | ||
CallerDocumentationURL string | ||
CallerName string | ||
DebugLogging bool | ||
IamEndpoint string | ||
Insecure bool | ||
MaxRetries int | ||
Profile string | ||
Region string | ||
SecretKey string | ||
SharedCredentialsFiles []string | ||
SharedConfigFiles []string | ||
SkipCredsValidation bool | ||
SkipMetadataApiCheck bool | ||
StsEndpoint string | ||
Token string | ||
} | ||
import ( | ||
"github.com/hashicorp/aws-sdk-go-base/v2/internal/config" | ||
) | ||
|
||
type APNInfo struct { | ||
PartnerName string | ||
Products []APNProduct | ||
} | ||
// Config, APNInfo, APNProduct, and AssumeRole are aliased to an internal package to break a dependency cycle | ||
// in internal/httpclient. | ||
|
||
type APNProduct struct { | ||
Name string | ||
Version string | ||
Comment string | ||
} | ||
type Config = config.Config | ||
|
||
type AssumeRole struct { | ||
RoleARN string | ||
DurationSeconds int | ||
ExternalID string | ||
Policy string | ||
PolicyARNs []string | ||
SessionName string | ||
Tags map[string]string | ||
TransitiveTagKeys []string | ||
} | ||
type APNInfo = config.APNInfo | ||
|
||
type APNProduct = config.APNProduct | ||
|
||
type AssumeRole = config.AssumeRole |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package config | ||
|
||
import ( | ||
smithyhttp "github.com/aws/smithy-go/transport/http" | ||
) | ||
|
||
// Builds the user-agent string for APN | ||
func (apn APNInfo) BuildUserAgentString() string { | ||
builder := smithyhttp.NewUserAgentBuilder() | ||
builder.AddKeyValue("APN", "1.0") | ||
builder.AddKeyValue(apn.PartnerName, "1.0") | ||
for _, p := range apn.Products { | ||
p.buildUserAgentPart(builder) | ||
} | ||
return builder.Build() | ||
} | ||
|
||
func (p APNProduct) buildUserAgentPart(b *smithyhttp.UserAgentBuilder) { | ||
if p.Name != "" { | ||
if p.Version != "" { | ||
b.AddKeyValue(p.Name, p.Version) | ||
} else { | ||
b.AddKey(p.Name) | ||
} | ||
} | ||
if p.Comment != "" { | ||
b.AddKey("(" + p.Comment + ")") | ||
} | ||
} |
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,45 @@ | ||
package config | ||
|
||
type Config struct { | ||
AccessKey string | ||
APNInfo *APNInfo | ||
AssumeRole *AssumeRole | ||
CallerDocumentationURL string | ||
CallerName string | ||
DebugLogging bool | ||
HTTPProxy string | ||
IamEndpoint string | ||
Insecure bool | ||
MaxRetries int | ||
Profile string | ||
Region string | ||
SecretKey string | ||
SharedCredentialsFiles []string | ||
SharedConfigFiles []string | ||
SkipCredsValidation bool | ||
SkipMetadataApiCheck bool | ||
StsEndpoint string | ||
Token string | ||
} | ||
|
||
type APNInfo struct { | ||
PartnerName string | ||
Products []APNProduct | ||
} | ||
|
||
type APNProduct struct { | ||
Name string | ||
Version string | ||
Comment string | ||
} | ||
|
||
type AssumeRole struct { | ||
RoleARN string | ||
DurationSeconds int | ||
ExternalID string | ||
Policy string | ||
PolicyARNs []string | ||
SessionName string | ||
Tags map[string]string | ||
TransitiveTagKeys []string | ||
} |
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,63 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// CannotAssumeRoleError occurs when AssumeRole cannot complete. | ||
type CannotAssumeRoleError struct { | ||
Config *Config | ||
Err error | ||
} | ||
|
||
func (e CannotAssumeRoleError) Error() string { | ||
if e.Config == nil || e.Config.AssumeRole == nil { | ||
return fmt.Sprintf("cannot assume role: %s", e.Err) | ||
} | ||
|
||
return fmt.Sprintf(`IAM Role (%s) cannot be assumed. | ||
There are a number of possible causes of this - the most common are: | ||
* The credentials used in order to assume the role are invalid | ||
* The credentials do not have appropriate permission to assume the role | ||
* The role ARN is not valid | ||
Error: %s | ||
`, e.Config.AssumeRole.RoleARN, e.Err) | ||
} | ||
|
||
func (e CannotAssumeRoleError) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
func (c *Config) NewCannotAssumeRoleError(err error) CannotAssumeRoleError { | ||
return CannotAssumeRoleError{Config: c, Err: err} | ||
} | ||
|
||
// NoValidCredentialSourcesError occurs when all credential lookup methods have been exhausted without results. | ||
type NoValidCredentialSourcesError struct { | ||
Config *Config | ||
Err error | ||
} | ||
|
||
func (e NoValidCredentialSourcesError) Error() string { | ||
if e.Config == nil { | ||
return fmt.Sprintf("no valid credential sources found: %s", e.Err) | ||
} | ||
|
||
return fmt.Sprintf(`no valid credential sources for %[1]s found. | ||
Please see %[2]s | ||
for more information about providing credentials. | ||
Error: %[3]s | ||
`, e.Config.CallerName, e.Config.CallerDocumentationURL, e.Err) | ||
} | ||
|
||
func (e NoValidCredentialSourcesError) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
func (c *Config) NewNoValidCredentialSourcesError(err error) NoValidCredentialSourcesError { | ||
return NoValidCredentialSourcesError{Config: c, Err: 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,38 @@ | ||
package httpclient | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"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 | ||
} |
Oops, something went wrong.