-
Notifications
You must be signed in to change notification settings - Fork 350
feat(procstat): add sudo and custom path support #1382
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,9 @@ import ( | |||||||||||||||||||||||||||||||||||||
| "flashcat.cloud/categraf/types" | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| var execCommand = exec.Command | ||||||||||||||||||||||||||||||||||||||
| var execLookPath = exec.LookPath | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const inputName = "procstat" | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| type PID int32 | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,6 +41,9 @@ type Instance struct { | |||||||||||||||||||||||||||||||||||||
| GatherPerPid bool `toml:"gather_per_pid"` | ||||||||||||||||||||||||||||||||||||||
| GatherMoreMetrics []string `toml:"gather_more_metrics"` | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| UseSudo bool `toml:"use_sudo"` | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| UseSudo bool `toml:"use_sudo"` | |
| // UseSudo controls whether external tools such as jstat are executed via | |
| // "sudo". Enable this only if the monitored commands require elevated | |
| // privileges and you have configured sudoers appropriately (for example, | |
| // to allow passwordless execution of the required binaries). Misuse or | |
| // misconfiguration of sudo can introduce security risks. | |
| UseSudo bool `toml:"use_sudo"` | |
| // PathJstat optionally specifies the absolute path to the jstat binary to | |
| // use when collecting JVM-related metrics. If left empty, the plugin will | |
| // search for jstat in the system PATH. |
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security concern: When UseSudo is enabled, the custom PathJstat is passed directly to sudo without validation. This could allow arbitrary command execution if PathJstat contains malicious input (e.g., "../../../bin/malicious"). Consider validating that PathJstat is an absolute path or exists in a safe location, or use exec.LookPath to resolve the binary path before constructing the sudo command.
| bin := ins.PathJstat | |
| args := []string{} | |
| if ins.UseSudo { | |
| args = append(args, ins.PathJstat) | |
| jstatPath := ins.PathJstat | |
| if jstatPath == "" { | |
| jstatPath = "jstat" | |
| } | |
| resolvedJstatPath, err := execLookPath(jstatPath) | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to locate jstat binary '%s': %w", jstatPath, err) | |
| } | |
| bin := resolvedJstatPath | |
| args := []string{} | |
| if ins.UseSudo { | |
| args = append(args, resolvedJstatPath) |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||
| package procstat | ||||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "os/exec" | ||||||||||||||
| "testing" | ||||||||||||||
|
|
||||||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| func TestExecJstat_Command(t *testing.T) { | ||||||||||||||
| defer func() { | ||||||||||||||
| execCommand = exec.Command | ||||||||||||||
| execLookPath = exec.LookPath | ||||||||||||||
| }() | ||||||||||||||
| execLookPath = func(file string) (string, error) { | ||||||||||||||
| return file, nil | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+13
to
+17
|
||||||||||||||
| execLookPath = exec.LookPath | |
| }() | |
| execLookPath = func(file string) (string, error) { | |
| return file, nil | |
| } | |
| }() |
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commented line "expectedArgs ..." suggests incomplete test implementation. Either remove this comment if the field is not needed, or implement complete argument validation for all test cases.
| // expectedArgs ... |
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test coverage missing: The test doesn't cover the combination of UseSudo with a custom path (e.g., UseSudo=true with PathJstat="/usr/bin/jstat"). This is an important scenario to test since it represents a realistic use case.
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test duplicates the default initialization logic from the Init method. The test manually sets PathJstat to "jstat" if empty, which duplicates logic that should be in the Init method. Consider calling ins.Init() instead to ensure the test reflects the actual initialization behavior.
| if ins.PathJstat == "" { | |
| ins.PathJstat = "jstat" | |
| } | |
| ins.Init() |
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test only validates the command name captured in sudo mode but doesn't verify the complete argument list in all cases. The test should assert all expected arguments for each test case. For example, in the "Default" and "Custom Path" cases, the arguments should include "-gc" and "1234". In the "Sudo Enabled" case, all arguments including "jstat", "-gc", and "1234" should be verified. Consider adding an expectedArgs field to the test struct and verifying it for all test cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The execLookPath variable is declared but never used in the current implementation. Since execJstat no longer calls exec.LookPath, this variable should be removed unless there are plans to validate the jstat path before execution.