From 58d6ffbc6256843279fbfe0104f5e4cdf02323df Mon Sep 17 00:00:00 2001 From: yahavi Date: Wed, 29 Nov 2023 14:24:06 +0200 Subject: [PATCH] Race condition in RunCmdWithOutputParser --- go.mod | 2 + go.sum | 4 +- utils/command.go | 65 -------------------------------- utils/goutils.go | 9 +++-- utils/pythonutils/pipenvutils.go | 5 ++- utils/pythonutils/piputils.go | 3 +- utils/pythonutils/utils.go | 3 +- 7 files changed, 16 insertions(+), 75 deletions(-) delete mode 100644 utils/command.go diff --git a/go.mod b/go.mod index 4f584429..3a3a97bd 100644 --- a/go.mod +++ b/go.mod @@ -26,3 +26,5 @@ require ( golang.org/x/sys v0.12.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/jfrog/gofrog => github.com/eranturgeman/gofrog v0.0.0-20231129122220-0328729640c2 diff --git a/go.sum b/go.sum index e4bd549c..a1cc6b83 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/jfrog/gofrog v1.3.1 h1:QqAwQXCVReT724uga1AYqG/ZyrNQ6f+iTxmzkb+YFQk= -github.com/jfrog/gofrog v1.3.1/go.mod h1:IFMc+V/yf7rA5WZ74CSbXe+Lgf0iApEQLxRZVzKRUR0= +github.com/eranturgeman/gofrog v0.0.0-20231129122220-0328729640c2 h1:DNuz2sqfF4B/Acrv0ve0ZdAWRIqdOQOziPvk1Uxpt4M= +github.com/eranturgeman/gofrog v0.0.0-20231129122220-0328729640c2/go.mod h1:AQo5Fq0G9nDEF6icH7MYQK0iohR4HuEAXl8jaxRuT6Q= github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y70BU= github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= diff --git a/utils/command.go b/utils/command.go deleted file mode 100644 index 6f1a9d91..00000000 --- a/utils/command.go +++ /dev/null @@ -1,65 +0,0 @@ -package utils - -import ( - "bytes" - "fmt" - "io" - "os/exec" - "strings" -) - -type Command struct { - Executable string - CmdName string - CmdArgs []string - Dir string - StrWriter io.WriteCloser - ErrWriter io.WriteCloser -} - -func NewCommand(executable, cmdName string, cmdArgs []string) *Command { - return &Command{Executable: executable, CmdName: cmdName, CmdArgs: cmdArgs} -} - -func (config *Command) RunWithOutput() (data []byte, err error) { - cmd := config.GetCmd() - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err = cmd.Run() - if err != nil { - return nil, fmt.Errorf("failed running command: '%s %s' with error: %s - %s", - cmd.Dir, - strings.Join(cmd.Args, " "), - err.Error(), - stderr.String(), - ) - } - return stdout.Bytes(), nil -} - -func (config *Command) GetCmd() (cmd *exec.Cmd) { - var cmdStr []string - if config.CmdName != "" { - cmdStr = append(cmdStr, config.CmdName) - } - if config.CmdArgs != nil && len(config.CmdArgs) > 0 { - cmdStr = append(cmdStr, config.CmdArgs...) - } - cmd = exec.Command(config.Executable, cmdStr...) - cmd.Dir = config.Dir - return -} - -func (config *Command) GetEnv() map[string]string { - return map[string]string{} -} - -func (config *Command) GetStdWriter() io.WriteCloser { - return config.StrWriter -} - -func (config *Command) GetErrWriter() io.WriteCloser { - return config.ErrWriter -} diff --git a/utils/goutils.go b/utils/goutils.go index a74763ee..ceef4b23 100644 --- a/utils/goutils.go +++ b/utils/goutils.go @@ -6,6 +6,7 @@ import ( "regexp" "runtime" + "github.com/jfrog/gofrog/io" "github.com/jfrog/gofrog/version" "os" @@ -39,7 +40,7 @@ func RunGo(goArg []string, repoUrl string) error { return err } - goCmd := NewCommand("go", "", goArg) + goCmd := io.NewCommand("go", "", goArg) err = prepareGlobalRegExp() if err != nil { return err @@ -148,7 +149,7 @@ func runDependenciesCmd(projectDir string, commandArgs []string, log Log) (outpu } }() } - goCmd := NewCommand("go", "", commandArgs) + goCmd := io.NewCommand("go", "", commandArgs) goCmd.Dir = projectDir err = prepareGlobalRegExp() @@ -230,7 +231,7 @@ func GetParsedGoVersion() (*version.Version, error) { } func getGoVersion() (string, error) { - goCmd := NewCommand("go", "version", nil) + goCmd := io.NewCommand("go", "version", nil) output, err := gofrogcmd.RunCmdOutput(goCmd) return output, err } @@ -288,7 +289,7 @@ func GetGoModCachePath() (string, error) { // GetGOPATH returns the location of the GOPATH func getGOPATH() (string, error) { - goCmd := NewCommand("go", "env", []string{"GOPATH"}) + goCmd := io.NewCommand("go", "env", []string{"GOPATH"}) output, err := gofrogcmd.RunCmdOutput(goCmd) if err != nil { return "", fmt.Errorf("could not find GOPATH env: %s", err.Error()) diff --git a/utils/pythonutils/pipenvutils.go b/utils/pythonutils/pipenvutils.go index e8fa590d..c2c7647d 100644 --- a/utils/pythonutils/pipenvutils.go +++ b/utils/pythonutils/pipenvutils.go @@ -2,7 +2,8 @@ package pythonutils import ( "encoding/json" - "github.com/jfrog/build-info-go/utils" + + "github.com/jfrog/gofrog/io" ) // Executes pipenv graph. @@ -11,7 +12,7 @@ import ( // 'topLevelPackagesList' - list of all top level dependencies ( root dependencies only) func getPipenvDependencies(srcPath string) (dependenciesGraph map[string][]string, topLevelDependencies []string, err error) { // Run pipenv graph - pipenvGraphCmd := utils.NewCommand("pipenv", "graph", []string{"--json"}) + pipenvGraphCmd := io.NewCommand("pipenv", "graph", []string{"--json"}) pipenvGraphCmd.Dir = srcPath output, err := pipenvGraphCmd.RunWithOutput() if err != nil { diff --git a/utils/pythonutils/piputils.go b/utils/pythonutils/piputils.go index 9305e9c7..5953c64b 100644 --- a/utils/pythonutils/piputils.go +++ b/utils/pythonutils/piputils.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/jfrog/build-info-go/utils" + "github.com/jfrog/gofrog/io" ) // Executes the pip-dependency-map script and returns a dependency map of all the installed pip packages in the current environment to and another list of the top level dependencies @@ -18,7 +19,7 @@ func getPipDependencies(srcPath, dependenciesDirName string) (map[string][]strin if err != nil { return nil, nil, err } - localPipdeptree := utils.NewCommand("python", "", []string{localPipdeptreeScript, "--json"}) + localPipdeptree := io.NewCommand("python", "", []string{localPipdeptreeScript, "--json"}) localPipdeptree.Dir = srcPath output, err := localPipdeptree.RunWithOutput() if err != nil { diff --git a/utils/pythonutils/utils.go b/utils/pythonutils/utils.go index 00147fa9..8b2df340 100644 --- a/utils/pythonutils/utils.go +++ b/utils/pythonutils/utils.go @@ -9,6 +9,7 @@ import ( "github.com/jfrog/build-info-go/entities" "github.com/jfrog/build-info-go/utils" + "github.com/jfrog/gofrog/io" gofrogcmd "github.com/jfrog/gofrog/io" ) @@ -210,7 +211,7 @@ func InstallWithLogParsing(tool PythonTool, commandArgs []string, log utils.Log, // Add verbosity flag to pipenv commands to collect necessary data commandArgs = append(commandArgs, "-v") } - installCmd := utils.NewCommand(string(tool), "install", commandArgs) + installCmd := io.NewCommand(string(tool), "install", commandArgs) installCmd.Dir = srcPath dependenciesMap := map[string]entities.Dependency{}