diff --git a/cfg/agentinfo/info.go b/cfg/agentinfo/info.go index 05b0130e7c..1b0363263a 100644 --- a/cfg/agentinfo/info.go +++ b/cfg/agentinfo/info.go @@ -19,6 +19,10 @@ const versionFilename = "CWAGENT_VERSION" // We will fall back to a major version if no valid version file is found const fallbackVersion = "1" +var isRunningAsRoot = func() bool { + return os.Getuid() == 0 +} + var ( VersionStr string BuildStr string = "No Build Date" @@ -49,6 +53,11 @@ func Build() string { func Plugins() string { outputs := strings.Join(OutputPlugins, " ") inputs := strings.Join(InputPlugins, " ") + + if !isRunningAsRoot() { + inputs += " run_as_user" // `inputs` is never empty, or agent will not start + } + return fmt.Sprintf("inputs:(%s) outputs:(%s)", inputs, outputs) } diff --git a/cfg/agentinfo/info_test.go b/cfg/agentinfo/info_test.go index d0a421efa5..3480e912d5 100644 --- a/cfg/agentinfo/info_test.go +++ b/cfg/agentinfo/info_test.go @@ -79,8 +79,16 @@ func TestPlugins(t *testing.T) { InputPlugins = []string{"a", "b", "c"} OutputPlugins = []string{"x", "y", "z"} + isRunningAsRoot = func() bool { return true } plugins := Plugins() - expected := "inputs:(a b c) outputs:(x y z)" + expected := fmt.Sprintf("inputs:(a b c) outputs:(x y z)") + if plugins != expected { + t.Errorf("wrong plugins string constructed '%v', expecting '%v'", plugins, expected) + } + + isRunningAsRoot = func() bool { return false } + plugins = Plugins() + expected = fmt.Sprintf("inputs:(a b c run_as_user) outputs:(x y z)") if plugins != expected { t.Errorf("wrong plugins string constructed '%v', expecting '%v'", plugins, expected) } @@ -93,6 +101,8 @@ func TestUserAgent(t *testing.T) { InputPlugins = []string{"a", "b", "c"} OutputPlugins = []string{"x", "y", "z"} + isRunningAsRoot = func() bool { return true } + 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 {