Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve recursive scan detection #1105

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion xray/commands/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ func (auditCmd *AuditCommand) Run() (err error) {
if err != nil {
return
}
// If no workingDirs were provided by the user, we apply a recursive scan on the root repository
applyRecursiveScan := len(auditCmd.workingDirs) == 0

auditParams := NewAuditParams().
SetXrayGraphScanParams(auditCmd.CreateXrayGraphScanParams()).
SetWorkingDirs(workingDirs).
SetMinSeverityFilter(auditCmd.minSeverityFilter).
SetFixableOnly(auditCmd.fixableOnly).
SetGraphBasicParams(auditCmd.AuditBasicParams).
SetThirdPartyApplicabilityScan(auditCmd.thirdPartyApplicabilityScan).
SetExclusions(auditCmd.exclusions)
SetExclusions(auditCmd.exclusions).
SetApplyRecursiveScan(applyRecursiveScan)
auditResults, err := RunAudit(auditParams)
if err != nil {
return
Expand Down
6 changes: 6 additions & 0 deletions xray/commands/audit/auditparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AuditParams struct {
xrayVersion string
// Include third party dependencies source code in the applicability scan.
thirdPartyApplicabilityScan bool
applyRecursiveScan bool
}

func NewAuditParams() *AuditParams {
Expand Down Expand Up @@ -50,6 +51,11 @@ func (params *AuditParams) SetExclusions(exclusions []string) *AuditParams {
return params
}

func (params *AuditParams) SetApplyRecursiveScan(applyRecursiveScan bool) *AuditParams {
params.applyRecursiveScan = applyRecursiveScan
return params
}

func (params *AuditParams) SetXrayGraphScanParams(xrayGraphScanParams *services.XrayGraphScanParams) *AuditParams {
params.xrayGraphScanParams = xrayGraphScanParams
return params
Expand Down
10 changes: 5 additions & 5 deletions xray/commands/audit/scarunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func runScaScan(params *AuditParams, results *xrayutils.Results) (err error) {

// Calculate the scans to preform
func getScaScansToPreform(currentWorkingDir string, params *AuditParams) (scansToPreform []*xrayutils.ScaScanResult) {
requestedDirectories, isRecursive := getRequestedDirectoriesToScan(currentWorkingDir, params)
requestedDirectories := getRequestedDirectoriesToScan(currentWorkingDir, params)
for _, requestedDirectory := range requestedDirectories {
// Detect descriptors and technologies in the requested directory.
techToWorkingDirs, err := coreutils.DetectTechnologiesDescriptors(requestedDirectory, isRecursive, params.Technologies(), getRequestedDescriptors(params), getExcludePattern(params, isRecursive))
techToWorkingDirs, err := coreutils.DetectTechnologiesDescriptors(requestedDirectory, params.applyRecursiveScan, params.Technologies(), getRequestedDescriptors(params), getExcludePattern(params, params.applyRecursiveScan))
if err != nil {
log.Warn("Couldn't detect technologies in", requestedDirectory, "directory.", err.Error())
continue
Expand Down Expand Up @@ -119,15 +119,15 @@ func getExcludePattern(params *AuditParams, recursive bool) string {
// Get the directories to scan base on the given parameters.
// If no working directories were specified, the current working directory will be returned with recursive mode.
// If working directories were specified, the recursive mode will be false.
func getRequestedDirectoriesToScan(currentWorkingDir string, params *AuditParams) ([]string, bool) {
func getRequestedDirectoriesToScan(currentWorkingDir string, params *AuditParams) []string {
workingDirs := datastructures.MakeSet[string]()
for _, wd := range params.workingDirs {
workingDirs.Add(wd)
}
if len(params.workingDirs) == 0 {
return []string{currentWorkingDir}, true
return []string{currentWorkingDir}
}
return workingDirs.ToSlice(), false
return workingDirs.ToSlice()
}

// Preform the SCA scan for the given scan information.
Expand Down
35 changes: 17 additions & 18 deletions xray/commands/audit/scarunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ func TestGetExcludePattern(t *testing.T) {

func TestGetRequestedDirectoriesToScan(t *testing.T) {
tests := []struct {
name string
cwd string
params func() *AuditParams
expectedRecursive bool
expectedDirs []string
name string
cwd string
params func() *AuditParams
expectedDirs []string
}{
{
name: "Test specific directories",
Expand All @@ -182,23 +181,20 @@ func TestGetRequestedDirectoriesToScan(t *testing.T) {
param.SetWorkingDirs([]string{filepath.Join("tmp", "dir1"), filepath.Join("tmp", "dir2")})
return param
},
expectedRecursive: false,
expectedDirs: []string{filepath.Join("tmp", "dir1"), filepath.Join("tmp", "dir2")},
expectedDirs: []string{filepath.Join("tmp", "dir1"), filepath.Join("tmp", "dir2")},
},
{
name: "Test recursive",
cwd: "tmp",
params: NewAuditParams,
expectedRecursive: true,
expectedDirs: []string{"tmp"},
name: "Test recursive",
cwd: "tmp",
params: NewAuditParams,
expectedDirs: []string{"tmp"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dirs, recursive := getRequestedDirectoriesToScan(test.cwd, test.params())
dirs := getRequestedDirectoriesToScan(test.cwd, test.params())
assert.ElementsMatch(t, test.expectedDirs, dirs)
assert.Equal(t, test.expectedRecursive, recursive)
})
}
}
Expand All @@ -217,7 +213,7 @@ func TestGetScaScansToPreform(t *testing.T) {
name: "Test specific technologies",
wd: dir,
params: func() *AuditParams {
param := NewAuditParams()
param := NewAuditParams().SetApplyRecursiveScan(true)
param.SetTechnologies([]string{"maven", "npm", "go"})
return param
},
Expand All @@ -244,9 +240,12 @@ func TestGetScaScansToPreform(t *testing.T) {
},
},
{
name: "Test all",
wd: dir,
params: NewAuditParams,
name: "Test all",
wd: dir,
params: func() *AuditParams {
param := NewAuditParams().SetApplyRecursiveScan(true)
return param
},
expected: []*xrayutils.ScaScanResult{
{
Technology: coreutils.Maven,
Expand Down
Loading