Skip to content

Commit

Permalink
Merge pull request #86 from hashicorp/add-user-agent
Browse files Browse the repository at this point in the history
Adds Config parameter to append user-agent products
  • Loading branch information
gdavison committed Nov 3, 2021
2 parents a3d642d + 414b1ec commit d449d60
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 21 deletions.
5 changes: 5 additions & 0 deletions aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func commonLoadOptions(c *Config) ([]func(*config.LoadOptions) error, error) {
return stack.Build.Add(apnUserAgentMiddleware(*c.APNInfo), middleware.After)
})
}

if len(c.UserAgent) > 0 {
apiOptions = append(apiOptions, awsmiddleware.AddUserAgentKey(c.UserAgent.BuildUserAgentString()))
}

if v := os.Getenv(constants.AppendUserAgentEnvVar); v != "" {
log.Printf("[DEBUG] Using additional User-Agent Info: %s", v)
apiOptions = append(apiOptions, awsmiddleware.AddUserAgentKey(v))
Expand Down
57 changes: 55 additions & 2 deletions aws_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ func TestUserAgentProducts(t *testing.T) {
SecretKey: servicemocks.MockStaticSecretKey,
APNInfo: &APNInfo{
PartnerName: "partner",
Products: []APNProduct{
Products: []UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
Expand All @@ -1022,7 +1022,7 @@ func TestUserAgentProducts(t *testing.T) {
SecretKey: servicemocks.MockStaticSecretKey,
APNInfo: &APNInfo{
PartnerName: "partner",
Products: []APNProduct{
Products: []UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
Expand All @@ -1040,6 +1040,59 @@ func TestUserAgentProducts(t *testing.T) {
},
ExpectedUserAgent: "APN/1.0 partner/1.0 first/1.2.3 second/1.0.2 " + awsSdkGoUserAgent() + " Last",
},
{
Config: &Config{
AccessKey: servicemocks.MockStaticAccessKey,
Region: "us-east-1",
SecretKey: servicemocks.MockStaticSecretKey,
UserAgent: []UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
},
{
Name: "second",
Version: "1.0.2",
Comment: "a comment",
},
},
},
Description: "User-Agent Products",
ExpectedUserAgent: awsSdkGoUserAgent() + " first/1.2.3 second/1.0.2 (a comment)",
},
{
Config: &Config{
AccessKey: servicemocks.MockStaticAccessKey,
Region: "us-east-1",
SecretKey: servicemocks.MockStaticSecretKey,
APNInfo: &APNInfo{
PartnerName: "partner",
Products: []UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
},
{
Name: "second",
Version: "1.0.2",
Comment: "a comment",
},
},
},
UserAgent: []UserAgentProduct{
{
Name: "third",
Version: "4.5.6",
},
{
Name: "fourth",
Version: "2.1",
},
},
},
Description: "APN and User-Agent Products",
ExpectedUserAgent: "APN/1.0 partner/1.0 first/1.2.3 second/1.0.2 (a comment) " + awsSdkGoUserAgent() + " third/4.5.6 fourth/2.1",
},
}

var (
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ type Config = config.Config

type APNInfo = config.APNInfo

type APNProduct = config.APNProduct

type AssumeRole = config.AssumeRole

type UserAgentProduct = config.UserAgentProduct
13 changes: 0 additions & 13 deletions internal/config/apn_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,3 @@ func (apn APNInfo) BuildUserAgentString() string {
}
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 + ")")
}
}
7 changes: 5 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ type Config struct {
SkipMetadataApiCheck bool
StsEndpoint string
Token string
UserAgent UserAgentProducts
}

type APNInfo struct {
PartnerName string
Products []APNProduct
Products []UserAgentProduct
}

type APNProduct struct {
type UserAgentProduct struct {
Name string
Version string
Comment string
}

type UserAgentProducts []UserAgentProduct

type AssumeRole struct {
RoleARN string
DurationSeconds int
Expand Down
26 changes: 26 additions & 0 deletions internal/config/user_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package config

import (
smithyhttp "github.com/aws/smithy-go/transport/http"
)

func (ua UserAgentProducts) BuildUserAgentString() string {
builder := smithyhttp.NewUserAgentBuilder()
for _, p := range ua {
p.buildUserAgentPart(builder)
}
return builder.Build()
}

func (p UserAgentProduct) 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 + ")")
}
}
4 changes: 4 additions & 0 deletions v2/awsv1shim/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func GetSession(awsC *awsv2.Config, c *awsbase.Config) (*session.Session, error)
)
}

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

// Add custom input from ENV to the User-Agent request header
// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/9149
if v := os.Getenv(constants.AppendUserAgentEnvVar); v != "" {
Expand Down
57 changes: 55 additions & 2 deletions v2/awsv1shim/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ func TestUserAgentProducts(t *testing.T) {
SecretKey: servicemocks.MockStaticSecretKey,
APNInfo: &awsbase.APNInfo{
PartnerName: "partner",
Products: []awsbase.APNProduct{
Products: []awsbase.UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
Expand All @@ -1073,7 +1073,7 @@ func TestUserAgentProducts(t *testing.T) {
SecretKey: servicemocks.MockStaticSecretKey,
APNInfo: &awsbase.APNInfo{
PartnerName: "partner",
Products: []awsbase.APNProduct{
Products: []awsbase.UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
Expand All @@ -1094,6 +1094,59 @@ func TestUserAgentProducts(t *testing.T) {
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
},
// {
// Config: &awsbase.Config{
// AccessKey: servicemocks.MockStaticAccessKey,
// Region: "us-east-1",
// SecretKey: servicemocks.MockStaticSecretKey,
// UserAgent: []awsbase.UserAgentProduct{
// {
// Name: "first",
// Version: "1.2.3",
// },
// {
// Name: "second",
// Version: "1.0.2",
// Comment: "a comment",
// },
// },
// },
// Description: "User-Agent Products",
// ExpectedUserAgent: awsSdkGoUserAgent() + " first/1.2.3 second/1.0.2 (a comment)",
// },
{
Config: &awsbase.Config{
AccessKey: servicemocks.MockStaticAccessKey,
Region: "us-east-1",
SecretKey: servicemocks.MockStaticSecretKey,
APNInfo: &awsbase.APNInfo{
PartnerName: "partner",
Products: []awsbase.UserAgentProduct{
{
Name: "first",
Version: "1.2.3",
},
{
Name: "second",
Version: "1.0.2",
Comment: "a comment",
},
},
},
UserAgent: []awsbase.UserAgentProduct{
{
Name: "third",
Version: "4.5.6",
},
{
Name: "fourth",
Version: "2.1",
},
},
},
Description: "APN and User-Agent Products",
ExpectedUserAgent: "APN/1.0 partner/1.0 first/1.2.3 second/1.0.2 (a comment) " + awsSdkGoUserAgent() + " third/4.5.6 fourth/2.1",
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit d449d60

Please sign in to comment.