Skip to content

Commit f7a3267

Browse files
authored
fix: [sc-69853] Improve debug messaging for command execution (#23)
* fix: prevent shell check error to stop command execution * fix: add logging_level on header
1 parent a502cb5 commit f7a3267

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

cmd/agent_smith/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (svc *serviceParams) Execute(stop <-chan struct{}, running chan<- struct{})
8989
}
9090

9191
// Show header
92-
logger.Info("Agent Smith started", "version", version.Version, "os", runtime.GOOS, "device_id", device.DeviceId)
92+
logger.Info("Agent Smith started", "version", version.Version, "os", runtime.GOOS, "device_id", device.DeviceId, "logging_level", device.LoggingLevel)
9393

9494
defer func() {
9595
logger.Info("Service stopped")

internal/interpreter/bash.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"golang.org/x/text/transform"
1818
)
1919

20+
const bashVersionCheckCommand = "echo \"$BASH_VERSION\""
21+
2022
func executeUsingBash(ctx context.Context, message *Message, device agent.Device, logger hclog.Logger) []byte {
2123
// Parse the commands
2224
commandBytes, err := base64.StdEncoding.DecodeString(message.Commands)
@@ -35,14 +37,14 @@ func executeUsingBash(ctx context.Context, message *Message, device agent.Device
3537
shell := "bash"
3638

3739
if logger.IsDebug() {
38-
cmd := exec.CommandContext(ctx, shell, "-c", "echo \"$BASH_VERSION\"")
39-
outBytes, err := cmd.Output()
40+
cmd := exec.CommandContext(ctx, shell, "-c", bashVersionCheckCommand)
41+
combinedOutputBytes, err := cmd.CombinedOutput()
42+
combinedOutput := string(combinedOutputBytes)
4043
if err != nil {
41-
logger.Error("Shell version check failed", "error", err)
42-
return errorResultBytes(err)
44+
logger.Error("Shell version check failed", "error", err, "combined_output", combinedOutput)
4345
}
4446

45-
version := strings.TrimSpace(string(outBytes))
47+
version := strings.TrimSpace(combinedOutput)
4648

4749
logger.Debug("Shell version", "shell", shell, "version", version)
4850
logger.Debug("Commands to execute", "commands", commands)

internal/interpreter/powershell.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"golang.org/x/text/transform"
1919
)
2020

21+
const powershellVersionCheckCommand = "\"$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)\""
22+
2123
func executeUsingPowershell(ctx context.Context, message *Message, device agent.Device, logger hclog.Logger) []byte {
2224
// Parse the commands
2325
commandBytes, err := base64.StdEncoding.DecodeString(message.Commands)
@@ -39,14 +41,14 @@ func executeUsingPowershell(ctx context.Context, message *Message, device agent.
3941
}
4042

4143
if logger.IsDebug() {
42-
cmd := exec.CommandContext(ctx, shell, "-Command", "\"$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Build).$($PSVersionTable.PSVersion.Revision)\"")
43-
outBytes, err := cmd.Output()
44+
cmd := exec.CommandContext(ctx, shell, "-Command", powershellVersionCheckCommand)
45+
combinedOutputBytes, err := cmd.CombinedOutput()
46+
combinedOutput := string(combinedOutputBytes)
4447
if err != nil {
45-
logger.Error("Shell version check failed", "error", err)
46-
return errorResultBytes(err)
48+
logger.Error("Shell version check failed", "error", err, "combined_output", combinedOutput)
4749
}
4850

49-
version := strings.TrimSpace(string(outBytes))
51+
version := strings.TrimSpace(combinedOutput)
5052

5153
logger.Debug("Shell version", "shell", shell, "version", version)
5254
logger.Debug("Commands to execute", "commands", commands)

0 commit comments

Comments
 (0)