util: fix logging in ExecuteCommandWithNSEnter

log the nsenter and its argument after executing
the command with the nsenter CLI.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2022-04-14 15:45:11 +05:30 committed by mergify[bot]
parent 6b34e6c899
commit c245436ec4

View File

@ -38,9 +38,9 @@ const InvalidPoolID int64 = -1
// context.TODO(), the command will be logged after it was executed.
func ExecuteCommandWithNSEnter(ctx context.Context, netPath, program string, args ...string) (string, string, error) {
var (
sanitizedArgs = StripSecretInArgs(args)
stdoutBuf bytes.Buffer
stderrBuf bytes.Buffer
stdoutBuf bytes.Buffer
stderrBuf bytes.Buffer
nsenter = "nsenter"
)
// check netPath exists
@ -49,8 +49,8 @@ func ExecuteCommandWithNSEnter(ctx context.Context, netPath, program string, arg
}
// nsenter --net=%s -- <program> <args>
args = append([]string{fmt.Sprintf("--net=%s", netPath), "--", program}, args...)
cmd := exec.Command("nsenter", args...) // #nosec:G204, commands executing not vulnerable.
sanitizedArgs := StripSecretInArgs(args)
cmd := exec.Command(nsenter, args...) // #nosec:G204, commands executing not vulnerable.
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
@ -59,7 +59,7 @@ func ExecuteCommandWithNSEnter(ctx context.Context, netPath, program string, arg
stderr := stderrBuf.String()
if err != nil {
err = fmt.Errorf("an error (%w) occurred while running %s args: %v", err, program, sanitizedArgs)
err = fmt.Errorf("an error (%w) occurred while running %s args: %v", err, nsenter, sanitizedArgs)
if ctx != context.TODO() {
log.UsefulLog(ctx, "%s", err)
}
@ -68,7 +68,7 @@ func ExecuteCommandWithNSEnter(ctx context.Context, netPath, program string, arg
}
if ctx != context.TODO() {
log.UsefulLog(ctx, "command succeeded: %s %v", program, sanitizedArgs)
log.UsefulLog(ctx, "command succeeded: %s %v", nsenter, sanitizedArgs)
}
return stdout, stderr, nil