Skip to content

Commit

Permalink
APIGOV-29459 - Fix user agent header parsing for hostname with fqdn (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekschauhan authored Jan 6, 2025
1 parent 607affa commit 60a4994
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/util/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

var (
agentInfoRe = regexp.MustCompile(`^([a-zA-Z]*)\/(\d*.\d*.\d*)[-]?([a-z0-9]*?) SDK/(\d*.\d*.\d.*) ([a-z][a-zA-Z0-9-]*) ([a-z][a-zA-Z0-9-]*) (binary|docker)[ ]?(reactive)?`)
agentInfoReV2 = regexp.MustCompile(`^([a-zA-Z]*)\/(\d*.\d*.\d*)[-]?([-a-z0-9A-Z]*?) \(sdkVer:(\d*.\d*.\d.*)\; env:([a-zA-Z0-9-]*)\; agent:([a-zA-Z0-9-]*)\; reactive:(true|false)\; hostname:([a-zA-Z0-9-_]*)\) ??(grpc-go.*\/\d*.\d*.\d*)?$`)
agentInfoReV2 = regexp.MustCompile(`^([a-zA-Z]*)\/(\d*.\d*.\d*)[-]?([-a-z0-9A-Z]*?) \(sdkVer:(\d*.\d*.\d.*)\; env:([a-zA-Z0-9-]*)\; agent:([a-zA-Z0-9-]*)\; reactive:(true|false)\; hostname:([a-zA-Z0-9-_.]*)\) ??(grpc-go.*\/\d*.\d*.\d*)?$`)
)

type CentralUserAgent struct {
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestFormatUserAgents(t *testing.T) {

func TestParseUserAgents(t *testing.T) {
hostname, _ := os.Hostname()
hostname2 := "test-test.abc.com"
tests := []struct {
name string
userAgent string
Expand Down Expand Up @@ -132,6 +133,21 @@ func TestParseUserAgents(t *testing.T) {
userAgent: "invalid user-agent",
expectedUA: nil,
},
{
name: "test-6",
userAgent: fmt.Sprintf("Test/1.0.0-APIGOV-Test (sdkVer:1.0.0; env:env; agent:agent; reactive:false; hostname:%s)", hostname2),
expectedUA: &CentralUserAgent{
AgentType: "Test",
Version: "1.0.0",
CommitSHA: "7e7eb72d",
SDKVersion: "1.0.0",
Environment: "env",
AgentName: "agent",
IsGRPC: false,
HostName: hostname2,
UseGRPCStatusUpdate: false,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 60a4994

Please sign in to comment.