Skip to content

Commit

Permalink
Merge pull request #95 from hashicorp/session-user-agent
Browse files Browse the repository at this point in the history
Exposes AWS SDK v1 session user agent handling
  • Loading branch information
gdavison authored Jan 31, 2022
2 parents 3a80a6f + 4a08a49 commit ca44ae1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type APNInfo = config.APNInfo

type AssumeRole = config.AssumeRole

type UserAgentProducts = config.UserAgentProducts

type UserAgentProduct = config.UserAgentProduct

const (
Expand Down
34 changes: 19 additions & 15 deletions v2/awsv1shim/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,7 @@ func GetSession(awsC *awsv2.Config, c *awsbase.Config) (*session.Session, error)
sess = sess.Copy(&aws.Config{MaxRetries: aws.Int(c.MaxRetries)})
}

// AWS SDK Go automatically adds a User-Agent product to HTTP requests,
// which contains helpful information about the SDK version and runtime.
// The configuration of additional User-Agent header products should take
// precedence over that product. Since the AWS SDK Go request package
// functions only append, we must PushFront on the build handlers instead
// of PushBack.
if c.APNInfo != nil {
sess.Handlers.Build.PushFront(
request.MakeAddToUserAgentFreeFormHandler(c.APNInfo.BuildUserAgentString()),
)
}

if len(c.UserAgent) > 0 {
sess.Handlers.Build.PushBack(request.MakeAddToUserAgentFreeFormHandler(c.UserAgent.BuildUserAgentString()))
}
SetSessionUserAgent(sess, c.APNInfo, c.UserAgent)

// Add custom input from ENV to the User-Agent request header
// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/9149
Expand Down Expand Up @@ -160,3 +146,21 @@ func convertDualStackEndpointState(value awsv2.DualStackEndpointState) endpoints
return endpoints.DualStackEndpointStateUnset
}
}

func SetSessionUserAgent(sess *session.Session, apnInfo *awsbase.APNInfo, userAgentProducts awsbase.UserAgentProducts) {
// AWS SDK Go automatically adds a User-Agent product to HTTP requests,
// which contains helpful information about the SDK version and runtime.
// The configuration of additional User-Agent header products should take
// precedence over that product. Since the AWS SDK Go request package
// functions only append, we must PushFront on the build handlers instead
// of PushBack.
if apnInfo != nil {
sess.Handlers.Build.PushFront(
request.MakeAddToUserAgentFreeFormHandler(apnInfo.BuildUserAgentString()),
)
}

if len(userAgentProducts) > 0 {
sess.Handlers.Build.PushBack(request.MakeAddToUserAgentFreeFormHandler(userAgentProducts.BuildUserAgentString()))
}
}

0 comments on commit ca44ae1

Please sign in to comment.