Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Sep 27, 2023
1 parent 408f5f2 commit acd38e9
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions xray/commands/audit/jas/applicability/applicabilitymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/jfrog/build-info-go/utils/pythonutils"
"github.com/jfrog/jfrog-cli-core/v2/xray/commands/audit/jas"
"os"
"os/exec"
"path/filepath"
"regexp"
Expand All @@ -23,6 +24,7 @@ import (
const (
applicabilityScanType = "analyze-applicability"
applicabilityScanCommand = "ca"
pipVirtualEnvVariable = "VIRTUAL_ENV"
)

type ApplicabilityScanManager struct {
Expand Down Expand Up @@ -51,7 +53,7 @@ func RunApplicabilityScan(xrayResults []services.ScanResponse, directDependencie
return
}

// Add python modules folders if needed
// Add python modules folders path to working dirs if needed.
if thirdPartyContextualAnalysis && slices.Contains(scannedTechnologies, coreutils.Pip) {
appendPipModulesToScanWorkingDir(applicabilityScanManager)
}
Expand All @@ -66,14 +68,13 @@ func RunApplicabilityScan(xrayResults []services.ScanResponse, directDependencie

func newApplicabilityScanManager(xrayScanResults []services.ScanResponse, directDependencies []string, scanner *jas.JasScanner, thirdPartyScan bool) (manager *ApplicabilityScanManager) {
directDependenciesCves := extractDirectDependenciesCvesFromScan(xrayScanResults, directDependencies)
applicabilityManager := &ApplicabilityScanManager{
return &ApplicabilityScanManager{
applicabilityScanResults: []*sarif.Run{},
directDependenciesCves: directDependenciesCves,
xrayResults: xrayScanResults,
scanner: scanner,
thirdPartyScan: thirdPartyScan,
}
return applicabilityManager
}

// This function gets a list of xray scan responses that contain direct and indirect vulnerabilities and returns only direct
Expand Down Expand Up @@ -176,19 +177,16 @@ func (asm *ApplicabilityScanManager) runAnalyzerManager() error {
return asm.scanner.AnalyzerManager.Exec(asm.scanner.ConfigFileName, applicabilityScanCommand, filepath.Dir(asm.scanner.AnalyzerManager.AnalyzerManagerFullPath), asm.scanner.ServerDetails)
}

// When thirdPartyScan is enabled we need to remove ignore patterns based on technologies
// and add extra working dir paths to be scanned.
// When thirdPartyScan is enabled we need to remove ignore patterns based on technologies.
func (asm *ApplicabilityScanManager) getSkipDirsAndAppendWdIfNeeded() (skipDirs []string) {
if !asm.thirdPartyScan {
return jas.SkippedDirs
}
for _, tech := range asm.techs {
switch tech {
case coreutils.Npm:
skipDirs = removeElementFromSlice(jas.SkippedDirs, jas.NodeModulesPattern)
case coreutils.Pip:
skipDirs = removeElementFromSlice(jas.SkippedDirs, jas.VirtualEnvPattern)
}
if slices.Contains(asm.techs, coreutils.Npm) {
skipDirs = removeElementFromSlice(jas.SkippedDirs, jas.NodeModulesPattern)
}
if slices.Contains(asm.techs, coreutils.Pip) {
skipDirs = removeElementFromSlice(jas.SkippedDirs, jas.VirtualEnvPattern)
}
return
}
Expand All @@ -211,19 +209,25 @@ func appendPipModulesToScanWorkingDir(applicabilityManager *ApplicabilityScanMan
}

func getPipRoot() (path string, err error) {
// When virtual env is active, we can get the path from the env variable.
virtualEnvPath := os.Getenv(pipVirtualEnvVariable)
if virtualEnvPath != "" {
return virtualEnvPath, nil
}
// Get modules location
pythonExe, _ := pythonutils.GetPython3Executable()
command := exec.Command(pythonExe, "-m", "pip", "-V")
outBuffer := bytes.NewBuffer([]byte{})
command.Stdout = outBuffer
if err = command.Run(); err != nil {
return
}
// Define a regular expression to match the path based on the returned output.
// Extract path from output
re := regexp.MustCompile(`from (.+) \(python`)
output := outBuffer.String()
match := re.FindStringSubmatch(output)
if len(match) >= 2 {
// Scan the parent of the result dir.
// Modules are located at the parent directory of pip.
path = strings.TrimSuffix(match[1], "/pip")
} else {
err = fmt.Errorf("failed to get pip env root folder, pip -V outout : %s", output)
Expand Down

0 comments on commit acd38e9

Please sign in to comment.