From 4a08a49d2984cd7f05750d5cdd8516e2dde05662 Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Mon, 31 Jan 2022 13:55:44 -0800 Subject: [PATCH] Exposes AWS SDK v1 session user agent handling --- config.go | 2 ++ v2/awsv1shim/session.go | 34 +++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/config.go b/config.go index aa2ca52d..f7da0642 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,8 @@ type APNInfo = config.APNInfo type AssumeRole = config.AssumeRole +type UserAgentProducts = config.UserAgentProducts + type UserAgentProduct = config.UserAgentProduct const ( diff --git a/v2/awsv1shim/session.go b/v2/awsv1shim/session.go index 2f860590..a5087b6a 100644 --- a/v2/awsv1shim/session.go +++ b/v2/awsv1shim/session.go @@ -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 @@ -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())) + } +}