Skip to content

Commit

Permalink
fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Aug 30, 2023
1 parent 7a6082d commit c5e63af
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Supported package management tools:
- Go
- Maven
- NuGet
- .NET
- npm
- Pip
- Pipenv
Expand Down
19 changes: 14 additions & 5 deletions packagehandlers/commonpackagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,26 @@ func (cph *CommonPackageHandler) UpdateDependency(vulnDetails *utils.Vulnerabili
commandArgs := []string{installationCommand}
commandArgs = append(commandArgs, extraArgs...)
versionOperator := vulnDetails.Technology.GetPackageVersionOperator()
fixedPackage := strings.TrimSpace(impactedPackage) + versionOperator + strings.TrimSpace(vulnDetails.SuggestedFixedVersion)
commandArgs = append(commandArgs, strings.Split(fixedPackage, " ")...)
return runPackageMangerCommand(vulnDetails.Technology.GetExecCommandName(), commandArgs)
fixedPackageArgs := getFixedPackage(impactedPackage, versionOperator, vulnDetails.SuggestedFixedVersion)
commandArgs = append(commandArgs, fixedPackageArgs...)
return runPackageMangerCommand(vulnDetails.Technology.GetExecCommandName(), vulnDetails.Technology.ToString(), commandArgs)
}

func runPackageMangerCommand(commandName string, commandArgs []string) error {
func runPackageMangerCommand(commandName string, techName string, commandArgs []string) error {
fullCommand := commandName + " " + strings.Join(commandArgs, " ")
log.Debug(fmt.Sprintf("Running '%s'", fullCommand))
output, err := exec.Command(commandName, commandArgs...).CombinedOutput() // #nosec G204
if err != nil {
return fmt.Errorf("%s command failed: %s\n%s", fullCommand, err.Error(), output)
return fmt.Errorf("failed to update %s dependency: '%s' command failed: %s\n%s", techName, fullCommand, err.Error(), output)
}
return nil
}

// Returns the updated package and version as it should be run in the update command:
// If the package manager expects a single string (example: <packName>@<version>) it returns []string{<packName>@<version>}
// If the command args suppose to be seperated by spaces (example: <packName> -v <version>) it returns []string{<packName>, "-v", <version>}
func getFixedPackage(impactedPackage string, versionOperator string, suggestedFixedVersion string) (fixedPackageArgs []string) {
fixedPackageString := strings.TrimSpace(impactedPackage) + versionOperator + strings.TrimSpace(suggestedFixedVersion)
fixedPackageArgs = strings.Split(fixedPackageString, " ")
return
}
7 changes: 1 addition & 6 deletions packagehandlers/nugetpackagehandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package packagehandlers

import (
"fmt"
"github.com/jfrog/frogbot/utils"
)

Expand All @@ -24,9 +23,5 @@ func (nph *NugetPackageHandler) UpdateDependency(vulnDetails *utils.Vulnerabilit
}

func (nph *NugetPackageHandler) updateDirectDependency(vulnDetails *utils.VulnerabilityDetails) (err error) {
err = nph.CommonPackageHandler.UpdateDependency(vulnDetails, vulnDetails.Technology.GetPackageInstallationCommand(), dotnetPackageUpgradeExtraArg)
if err != nil {
err = fmt.Errorf("failed to update NuGet package with error:\n%w", err)
}
return
return nph.CommonPackageHandler.UpdateDependency(vulnDetails, vulnDetails.Technology.GetPackageInstallationCommand(), dotnetPackageUpgradeExtraArg)
}
27 changes: 27 additions & 0 deletions packagehandlers/packagehandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,30 @@ func uniquePackageManagerChecks(t *testing.T, test dependencyFixTest) {
default:
}
}

func TestGetFixedPackage(t *testing.T) {
var testcases = []struct {
impactedPackage string
versionOperator string
suggestedFixedVersion string
expectedOutput []string
}{
{
impactedPackage: "snappier",
versionOperator: " -v ",
suggestedFixedVersion: "1.1.1",
expectedOutput: []string{"snappier", "-v", "1.1.1"},
},
{
impactedPackage: "json",
versionOperator: "@",
suggestedFixedVersion: "10.0.0",
expectedOutput: []string{"[email protected]"},
},
}

for _, test := range testcases {
fixedPackageArgs := getFixedPackage(test.impactedPackage, test.versionOperator, test.suggestedFixedVersion)
assert.Equal(t, test.expectedOutput, fixedPackageArgs)
}
}
2 changes: 1 addition & 1 deletion packagehandlers/pythonpackagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (py *PythonPackageHandler) handlePoetry(vulnDetails *utils.VulnerabilityDet
return
}
// Update Poetry lock file as well
return runPackageMangerCommand(coreutils.Poetry.GetExecCommandName(), []string{"update"})
return runPackageMangerCommand(coreutils.Poetry.GetExecCommandName(), coreutils.Poetry.ToString(), []string{"update"})
}

func (py *PythonPackageHandler) handlePip(vulnDetails *utils.VulnerabilityDetails) (err error) {
Expand Down

0 comments on commit c5e63af

Please sign in to comment.