Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow override useragent with json config #65

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cfg/agentinfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"runtime"
"strings"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
)

const versionFilename = "CWAGENT_VERSION"
Expand All @@ -22,6 +24,8 @@ var (
BuildStr string = "No Build Date"
InputPlugins []string
OutputPlugins []string

userAgent string
)

func Version() string {
Expand Down Expand Up @@ -49,7 +53,14 @@ func Plugins() string {
}

func UserAgent() string {
return fmt.Sprintf("%s %s", FullVersion(), Plugins())
if userAgent == "" {
ua := os.Getenv(envconfig.CWAGENT_USER_AGENT)
if ua == "" {
ua = fmt.Sprintf("%s %s", FullVersion(), Plugins())
}
userAgent = ua
}
return userAgent
}

func FullVersion() string {
Expand Down
29 changes: 29 additions & 0 deletions cfg/agentinfo/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
package agentinfo

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
)

func TestVersionUseInjectedIfAvailable(t *testing.T) {
Expand Down Expand Up @@ -81,3 +85,28 @@ func TestPlugins(t *testing.T) {
t.Errorf("wrong plugins string constructed '%v', expecting '%v'", plugins, expected)
}
}

func TestUserAgent(t *testing.T) {
userAgent = ""
VersionStr = "VSTR"
BuildStr = "BSTR"
InputPlugins = []string{"a", "b", "c"}
OutputPlugins = []string{"x", "y", "z"}

ua := UserAgent()
expected := fmt.Sprintf("CWAgent/VSTR (%v; %v; %v) BSTR inputs:(a b c) outputs:(x y z)", runtime.Version(), runtime.GOOS, runtime.GOARCH)
if ua != expected {
t.Errorf("wrong UserAgent string constructed '%v', expecting '%v'", ua, expected)
}
}

func TestUserAgentEnvOverride(t *testing.T) {
userAgent = ""
os.Setenv(envconfig.CWAGENT_USER_AGENT, "CUSTOM CWAGENT USER AGENT")
expected := "CUSTOM CWAGENT USER AGENT"

ua := UserAgent()
if ua != expected {
t.Errorf("UserAgent should use value configured in environment variable CWAGENT_USER_AGENT, but '%v' found, expecting '%v'", ua, expected)
}
}
11 changes: 6 additions & 5 deletions cfg/envconfig/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package envconfig

const (
//the following are the names of environment variables
HTTP_PROXY = "HTTP_PROXY"
HTTPS_PROXY = "HTTPS_PROXY"
NO_PROXY = "NO_PROXY"
AWS_CSM_ENABLED = "AWS_CSM_ENABLED"
AWS_CA_BUNDLE = "AWS_CA_BUNDLE"
HTTP_PROXY = "HTTP_PROXY"
HTTPS_PROXY = "HTTPS_PROXY"
NO_PROXY = "NO_PROXY"
AWS_CSM_ENABLED = "AWS_CSM_ENABLED"
AWS_CA_BUNDLE = "AWS_CA_BUNDLE"
CWAGENT_USER_AGENT = "CWAGENT_USER_AGENT"
)
10 changes: 10 additions & 0 deletions translator/toenvconfig/toEnvConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@ import (
"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/internal/csm"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/agent"
"github.com/aws/amazon-cloudwatch-agent/translator/util"
)

const userAgentKey = "user_agent"

func ToEnvConfig(jsonConfigValue map[string]interface{}) []byte {
envVars := make(map[string]string)
// If csm has a configuration section, then also turn on csm for the agent itself
if _, ok := jsonConfigValue[csm.JSONSectionKey]; ok {
envVars[envconfig.AWS_CSM_ENABLED] = "TRUE"
}

// Set CWAGENT_USER_AGENT to env config if specified by the json config in agent section
if agentMap, ok := jsonConfigValue[agent.SectionKey].(map[string]interface{}); ok {
if userAgent, ok := agentMap[userAgentKey].(string); ok {
envVars[envconfig.CWAGENT_USER_AGENT] = userAgent
}
}

proxy := util.GetHttpProxy(context.CurrentContext().Proxy())
if len(proxy) > 0 {
envVars[envconfig.HTTP_PROXY] = proxy[commonconfig.HttpProxy]
Expand Down
4 changes: 3 additions & 1 deletion translator/toenvconfig/toEnvConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func TestLogMetricAndLog(t *testing.T) {

func TestCompleteConfig(t *testing.T) {
resetContext()
expectedEnvVars := map[string]string{}
expectedEnvVars := map[string]string{
"CWAGENT_USER_AGENT": "CUSTOM USER AGENT VALUE",
}
checkIfTranslateSucceed(t, ReadFromFile("../totomlconfig/sampleConfig/complete_linux_config.json"), "linux", expectedEnvVars)
checkIfTranslateSucceed(t, ReadFromFile("../totomlconfig/sampleConfig/complete_windows_config.json"), "windows", expectedEnvVars)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"metrics_collection_interval": 10,
"logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
"internal": true,
"user_agent": "CUSTOM USER AGENT VALUE",
"credentials": {
"role_arn": "global_role_arn_value"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"metrics_collection_interval": 60,
"logfile": "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log",
"internal": true,
"user_agent": "CUSTOM USER AGENT VALUE",
"credentials": {
"role_arn": "global_role_arn_value"
}
Expand Down