Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi committed Dec 3, 2023
2 parents 8a8a5de + 4b2b4a4 commit dfb6e8c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
17 changes: 10 additions & 7 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) {
recursive := len(currentWorkingDir) > 0
for _, requestedDirectory := range getRequestedDirectoriesToScan(currentWorkingDir, params) {
requestedDirectories, isRecursive := getRequestedDirectoriesToScan(currentWorkingDir, params)
for _, requestedDirectory := range requestedDirectories {
// Detect descriptors and technologies in the requested directory.
techToWorkingDirs, err := coreutils.DetectTechnologiesDescriptors(requestedDirectory, recursive, params.Technologies(), getRequestedDescriptors(params), getExcludePattern(params, recursive))
techToWorkingDirs, err := coreutils.DetectTechnologiesDescriptors(requestedDirectory, isRecursive, params.Technologies(), getRequestedDescriptors(params), getExcludePattern(params, isRecursive))
if err != nil {
log.Warn("Couldn't detect technologies in", requestedDirectory, "directory.", err.Error())
continue
Expand Down Expand Up @@ -116,15 +116,18 @@ func getExcludePattern(params *AuditParams, recursive bool) string {
return fspatterns.PrepareExcludePathPattern(exclusions, clientutils.WildCardPattern, recursive)
}

func getRequestedDirectoriesToScan(currentWorkingDir string, params *AuditParams) []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) {
workingDirs := datastructures.MakeSet[string]()
for _, wd := range params.workingDirs {
workingDirs.Add(wd)
}
if workingDirs.Size() == 0 {
workingDirs.Add(currentWorkingDir)
if len(params.workingDirs) == 0 {
return []string{currentWorkingDir}, true
}
return workingDirs.ToSlice()
return workingDirs.ToSlice(), false
}

// Preform the SCA scan for the given scan information.
Expand Down
37 changes: 37 additions & 0 deletions xray/commands/audit/scarunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,43 @@ func TestGetExcludePattern(t *testing.T) {
}
}

func TestGetRequestedDirectoriesToScan(t *testing.T) {
tests := []struct {
name string
cwd string
params func() *AuditParams
expectedRecursive bool
expectedDirs []string
}{
{
name: "Test specific directories",
cwd: "tmp",
params: func() *AuditParams {
param := NewAuditParams()
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")},
},
{
name: "Test recursive",
cwd: "tmp",
params: NewAuditParams,
expectedRecursive: true,
expectedDirs: []string{"tmp"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dirs, recursive := getRequestedDirectoriesToScan(test.cwd, test.params())
assert.ElementsMatch(t, test.expectedDirs, dirs)
assert.Equal(t, test.expectedRecursive, recursive)
})
}
}

func TestGetScaScansToPreform(t *testing.T) {

dir, cleanUp := createTestDir(t)
Expand Down

0 comments on commit dfb6e8c

Please sign in to comment.