Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions pkg/commandhandler/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
package commandhandler

import (
"github.com/Azure/azure-extension-platform/pkg/constants"
"github.com/Azure/azure-extension-platform/pkg/logging"
"github.com/pkg/errors"
"io"
"os"
"os/exec"
"path/filepath"

"github.com/Azure/azure-extension-platform/pkg/constants"
"github.com/Azure/azure-extension-platform/pkg/logging"
"github.com/pkg/errors"
)

type ICommandHandler interface {
Expand Down Expand Up @@ -70,10 +71,13 @@ func execCmdInDirWithAction(cmd, workingDir, logDir string, waitForCompletion bo
stdOutFile.Close()
}

stdErrFile, err3 := os.OpenFile(errFileName, os.O_RDONLY, constants.FilePermissions_UserOnly_ReadWrite)
stdErrContents, err3 := os.ReadFile(errFileName)
if err3 == nil {
el.InfoFromStream("stderr:", stdErrFile)
stdErrFile.Close()
el.Info("stderr: %s", stdErrContents)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, my fear here is that we've dodged a security issue with a bug that causes these events to not reach Kusto. There's a high likelihood that these events will contain security credentials.

Please check with Norberto and Simon on whether there's any plan to scrub these generically for creds. Otherwise, I fear we may have to replicate the logic we have for the BI data to scrub them here.


if execErr != nil {
return exitCode, errors.Wrap(execErr, string(stdErrContents))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't fail the command if we can't read the standard error, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I didn't change the behavior with regards to that.
If the command execution results in an error, and we are able to read the standard error, then we return a combined error. Otherwise, we just return the original error at the end of the function.

}
}

} else {
Expand Down
17 changes: 16 additions & 1 deletion pkg/commandhandler/cmd_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ package commandhandler

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -97,3 +99,16 @@ func TestCommandWithEnvironmentVariableEmpty(t *testing.T) {
assert.NoError(t, err, "stdout file should be read")
assert.Contains(t, string(fileInfo), "", "stdout message should be as expected")
}

func TestNonExistingCommand(t *testing.T) {
defer cleanupTest()
cmd := New()
retcode, err := cmd.Execute("command_does_not_exist", workingDir, workingDir, true, extensionLogger)
assert.Equal(t, commandNotExistReturnCode, retcode)
assert.Error(t, err, "command execution should fail")
assert.Contains(t, err.Error(), "command_does_not_exist: not found", "error returned by cmd.Execute should include stderr")

fileInfo, err := os.ReadFile(path.Join(workingDir, "stderr"))
assert.NoError(t, err, "stderr file should be read")
assert.Contains(t, string(fileInfo), "command_does_not_exist: not found", "stderr message should be as expected")
}
13 changes: 3 additions & 10 deletions pkg/commandhandler/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
package commandhandler

import (
"github.com/Azure/azure-extension-platform/pkg/logging"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"strings"
"testing"

"github.com/Azure/azure-extension-platform/pkg/logging"
"github.com/stretchr/testify/assert"
)

var workingDir = path.Join(".", "testdir", "currentWorkingDir")
Expand Down Expand Up @@ -43,11 +44,3 @@ func TestStderr(t *testing.T) {
stdoutResult := strings.TrimSuffix(strings.TrimSuffix(string(fileBytes), lineReturnCharacter), " ")
assert.Equal(t, "1 2 3 4", stdoutResult)
}

func TestNonExistingCommand(t *testing.T) {
defer cleanupTest()
cmd := New()
retcode, err := cmd.Execute("command_does_not_exist", workingDir, workingDir, true, extensionLogger)
assert.Equal(t, commandNotExistReturnCode, retcode)
assert.Error(t, err)
}
16 changes: 15 additions & 1 deletion pkg/commandhandler/cmd_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package commandhandler

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -140,3 +141,16 @@ func TestDoesntWaitForCompletionEnvironmentVariable(t *testing.T) {
duration := endTime.Sub(startTime)
assert.Less(t, duration, time.Second, "execute shouldn't block")
}

func TestNonExistingCommand(t *testing.T) {
defer cleanupTest()
cmd := New()
retcode, err := cmd.Execute("command_does_not_exist", workingDir, workingDir, true, extensionLogger)
assert.Equal(t, commandNotExistReturnCode, retcode)
assert.Error(t, err, "command execution should fail")
assert.Contains(t, err.Error(), "is not recognized as an internal or external command", "error returned by cmd.Execute should include stderr")

fileInfo, err := os.ReadFile(path.Join(workingDir, "stderr"))
assert.NoError(t, err, "stderr file should be read")
assert.Contains(t, string(fileInfo), "is not recognized as an internal or external command", "stderr message should be as expected")
}