Skip to content

Commit

Permalink
Added run_as_user as an input plugin in the UserAgent. (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Aug 17, 2021
1 parent 26a8d28 commit f84ce16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cfg/agentinfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 11 additions & 1 deletion cfg/agentinfo/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit f84ce16

Please sign in to comment.