Skip to content

Commit

Permalink
Run jas scanners in parallel (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
orz25 committed Jun 18, 2024
1 parent 3546ec2 commit bb2f45c
Show file tree
Hide file tree
Showing 38 changed files with 728 additions and 417 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
env:
GOPROXY: direct
GRADLE_OPTS: -Dorg.gradle.daemon=false
CI: true
JFROG_CLI_LOG_LEVEL: DEBUG
steps:
# Install dependencies
- name: Install Go
Expand Down
4 changes: 2 additions & 2 deletions artifactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestDownloadAnalyzerManagerIfNeeded(t *testing.T) {
defer setEnvCallBack()

// Download
err := utils.DownloadAnalyzerManagerIfNeeded()
err := utils.DownloadAnalyzerManagerIfNeeded(0)
assert.NoError(t, err)

// Validate Analyzer manager app & checksum.sh2 file exist
Expand All @@ -236,7 +236,7 @@ func TestDownloadAnalyzerManagerIfNeeded(t *testing.T) {
// Validate no second download occurred
firstFileStat, err := os.Stat(amPath)
assert.NoError(t, err)
err = utils.DownloadAnalyzerManagerIfNeeded()
err = utils.DownloadAnalyzerManagerIfNeeded(0)
assert.NoError(t, err)
secondFileStat, err := os.Stat(amPath)
assert.NoError(t, err)
Expand Down
33 changes: 28 additions & 5 deletions audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,27 @@ func addDummyPackageDescriptor(t *testing.T, hasPackageJson bool) {
// JAS

func TestXrayAuditJasSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, string(format.SimpleJson), filepath.Join("jas", "jas"))
output := testXrayAuditJas(t, filepath.Join("jas", "jas"), "3")
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 6, 3, 0, 2, 2)
}

func TestXrayAuditJasSimpleJsonWithOneThread(t *testing.T) {
output := testXrayAuditJas(t, filepath.Join("jas", "jas"), "1")
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 6, 3, 0, 2, 2)
}

func TestXrayAuditJasSimpleJsonWithConfig(t *testing.T) {
output := testXrayAuditJas(t, string(format.SimpleJson), filepath.Join("jas", "jas-config"))
output := testXrayAuditJas(t, filepath.Join("jas", "jas-config"), "3")
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 1, 3, 0, 2, 2)
}

func TestXrayAuditJasNoViolationsSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, string(format.SimpleJson), filepath.Join("package-managers", "npm", "npm"))
output := testXrayAuditJas(t, filepath.Join("package-managers", "npm", "npm"), "3")
securityTestUtils.VerifySimpleJsonScanResults(t, output, 0, 1, 0)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 1)
}

func testXrayAuditJas(t *testing.T, format string, project string) string {
func testXrayAuditJas(t *testing.T, project string, threads string) string {
securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
Expand All @@ -464,7 +469,7 @@ func testXrayAuditJas(t *testing.T, format string, project string) string {
assert.NoError(t, err)
chdirCallback := clientTests.ChangeDirWithCallback(t, baseWd, tempDirPath)
defer chdirCallback()
return securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+format)
return securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson), "--threads="+threads)
}

func TestXrayAuditDetectTech(t *testing.T) {
Expand Down Expand Up @@ -553,3 +558,21 @@ func validateAnalyticsBasicEvent(t *testing.T, output string) {
assert.NotEmpty(t, event.TotalScanDuration)
assert.True(t, event.TotalFindings > 0)
}

func TestAuditOnEmptyProject(t *testing.T) {
securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
projectDir := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), filepath.Join("projects", "empty_project", "python_project_with_no_deps"))
// Copy the multi project from the testdata to a temp dir
assert.NoError(t, biutils.CopyDir(projectDir, tempDirPath, true, nil))
// Configure a new server named "default"
securityTestUtils.CreateJfrogHomeConfig(t, true)
defer securityTestUtils.CleanTestsHomeEnv()
baseWd, err := os.Getwd()
assert.NoError(t, err)
chdirCallback := clientTests.ChangeDirWithCallback(t, baseWd, tempDirPath)
defer chdirCallback()
output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson))
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0)
}
10 changes: 4 additions & 6 deletions cli/docs/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-cli-security/commands/audit/sca"
"github.com/jfrog/jfrog-cli-security/commands/curation"
"github.com/jfrog/jfrog-cli-security/commands/xray/offlineupdate"
)

Expand Down Expand Up @@ -105,8 +104,7 @@ const (
WorkingDirs = "working-dirs"

// Unique curation flags
CurationOutput = "curation-format"
CurationThreads = "curation-threads"
CurationOutput = "curation-format"
)

// Mapping between security commands (key) and their flags (key).
Expand All @@ -125,10 +123,11 @@ var commandFlags = map[string][]string{
},
Audit: {
url, user, password, accessToken, ServerId, InsecureTls, Project, Watches, RepoPath, Licenses, OutputFormat, ExcludeTestDeps,
useWrapperAudit, DepType, RequirementsFile, Fail, ExtendedTable, WorkingDirs, ExclusionsAudit, Mvn, Gradle, Npm, Pnpm, Yarn, Go, Nuget, Pip, Pipenv, Poetry, MinSeverity, FixableOnly, ThirdPartyContextualAnalysis,
useWrapperAudit, DepType, RequirementsFile, Fail, ExtendedTable, WorkingDirs, ExclusionsAudit, Mvn, Gradle, Npm,
Pnpm, Yarn, Go, Nuget, Pip, Pipenv, Poetry, MinSeverity, FixableOnly, ThirdPartyContextualAnalysis, Threads,
},
CurationAudit: {
CurationOutput, WorkingDirs, CurationThreads, RequirementsFile,
CurationOutput, WorkingDirs, Threads, RequirementsFile,
},
// TODO: Deprecated commands (remove at next CLI major version)
AuditMvn: {
Expand Down Expand Up @@ -219,7 +218,6 @@ var flagsMap = map[string]components.Flag{
components.SetHiddenBoolFlag(),
),
RequirementsFile: components.NewStringFlag(RequirementsFile, "[Pip] Defines pip requirements file name. For example: 'requirements.txt'."),
CurationThreads: components.NewStringFlag(Threads, "Number of working threads.", components.WithIntDefaultValue(curation.TotalConcurrentRequests)),
CurationOutput: components.NewStringFlag(OutputFormat, "Defines the output format of the command. Acceptable values are: table, json.", components.WithStrDefaultValue("table")),
}

Expand Down
19 changes: 12 additions & 7 deletions cli/scancommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,12 @@ func AuditCmd(c *components.Context) error {
}
}
auditCmd.SetTechnologies(technologies)
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
auditCmd.SetThreads(threads)
err = progressbar.ExecWithProgress(auditCmd)

// Reporting error if Xsc service is enabled
reportErrorIfExists(err, auditCmd)
return err
Expand Down Expand Up @@ -428,11 +432,7 @@ func AuditSpecificCmd(c *components.Context, technology techutils.Technology) er
}

func CurationCmd(c *components.Context) error {
threadsFlag, err := c.GetIntFlagValue(flags.Threads)
if err != nil {
return err
}
threads, err := curation.DetectNumOfThreads(threadsFlag)
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
Expand Down Expand Up @@ -470,6 +470,10 @@ func DockerScan(c *components.Context, image string) error {
return printHelp()
}
// Run the command
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
Expand Down Expand Up @@ -498,7 +502,8 @@ func DockerScan(c *components.Context, image string) error {
SetPrintExtendedTable(c.GetBoolFlagValue(flags.ExtendedTable)).
SetBypassArchiveLimits(c.GetBoolFlagValue(flags.BypassArchiveLimits)).
SetFixableOnly(c.GetBoolFlagValue(flags.FixableOnly)).
SetMinSeverityFilter(minSeverity)
SetMinSeverityFilter(minSeverity).
SetThreads(threads)
if c.GetStringFlagValue(flags.Watches) != "" {
containerScanCommand.SetWatches(splitByCommaAndTrim(c.GetStringFlagValue(flags.Watches)))
}
Expand Down
Loading

0 comments on commit bb2f45c

Please sign in to comment.