Skip to content

Commit

Permalink
Change npm params to wrap audit params interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
asafambar committed Sep 27, 2023
1 parent 5a0d1c7 commit 79bff29
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 31 deletions.
2 changes: 1 addition & 1 deletion xray/commands/audit/sca/java/javautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func hasLoop(idsAdded []string, idToAdd string) bool {
return false
}

func BuildDependencyTree(params *xrayutils.AuditBasicParams, tech coreutils.Technology) ([]*xrayUtils.GraphNode, []string, error) {
func BuildDependencyTree(params xrayutils.AuditParams, tech coreutils.Technology) ([]*xrayUtils.GraphNode, []string, error) {
serverDetails, err := params.ServerDetails()
if err != nil {
return nil, nil, err
Expand Down
15 changes: 9 additions & 6 deletions xray/commands/audit/sca/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
ignoreScriptsFlag = "--ignore-scripts"
)

func BuildDependencyTree(params *utils.AuditBasicParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) {
func BuildDependencyTree(params utils.AuditParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) {
currentDir, err := coreutils.GetWorkingDirectory()
if err != nil {
return
Expand Down Expand Up @@ -50,12 +50,15 @@ func BuildDependencyTree(params *utils.AuditBasicParams) (dependencyTrees []*xra
return
}

func createTreeDepsParam(params *utils.AuditBasicParams) biutils.NpmTreeDepListParam {
return biutils.NpmTreeDepListParam{
Args: addIgnoreScriptsFlag(params.Args()),
IgnoreNodeModules: params.NpmIgnoreNodeModules(),
OverWritePackageLock: params.NpmOverwritePackageLock(),
func createTreeDepsParam(params utils.AuditParams) biutils.NpmTreeDepListParam {
npmTreeDepParam := biutils.NpmTreeDepListParam{
Args: addIgnoreScriptsFlag(params.Args()),
}
if npmParams, ok := params.(utils.AuditNpmParams); ok {
npmTreeDepParam.IgnoreNodeModules = npmParams.NpmIgnoreNodeModules()
npmTreeDepParam.OverWritePackageLock = npmParams.NpmOverwritePackageLock()
}
return npmTreeDepParam
}

// Add the --ignore-scripts to prevent execution of npm scripts during npm install.
Expand Down
2 changes: 1 addition & 1 deletion xray/commands/audit/scarunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func getDirectDependenciesFromTree(dependencyTrees []*xrayCmdUtils.GraphNode) []
return directDependencies.ToSlice()
}

func GetTechDependencyTree(params *xrayutils.AuditBasicParams, tech coreutils.Technology) (flatTree *xrayCmdUtils.GraphNode, fullDependencyTrees []*xrayCmdUtils.GraphNode, err error) {
func GetTechDependencyTree(params xrayutils.AuditParams, tech coreutils.Technology) (flatTree *xrayCmdUtils.GraphNode, fullDependencyTrees []*xrayCmdUtils.GraphNode, err error) {
logMessage := fmt.Sprintf("Calculating %s dependencies", tech.ToFormal())
log.Info(logMessage)
if params.Progress() != nil {
Expand Down
17 changes: 14 additions & 3 deletions xray/commands/curation/curationaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ type CurationAuditCommand struct {
workingDirs []string
OriginPath string
parallelRequests int
*utils.AuditBasicParams
utils.AuditParams
}

func NewCurationAuditCommand() *CurationAuditCommand {
return &CurationAuditCommand{
extractPoliciesRegex: regexp.MustCompile(extractPoliciesRegexTemplate),
AuditBasicParams: &utils.AuditBasicParams{},
AuditParams: &utils.AuditBasicParams{},
}
}

Expand Down Expand Up @@ -192,8 +192,19 @@ func (ca *CurationAuditCommand) doCurateAudit(results map[string][]*PackageStatu
return nil
}

func (ca *CurationAuditCommand) getAuditParamsByTech(tech coreutils.Technology) utils.AuditParams {
switch tech {

Check failure on line 196 in xray/commands/curation/curationaudit.go

View workflow job for this annotation

GitHub Actions / Static-Check

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case coreutils.Npm:
return utils.AuditNpmParams{
AuditParams: ca.AuditParams}.
SetNpmIgnoreNodeModules(true).
SetNpmOverwritePackageLock(true)
}
return ca.AuditParams
}

func (ca *CurationAuditCommand) auditTree(tech coreutils.Technology, results map[string][]*PackageStatus) error {
flattenGraph, fullDependenciesTree, err := audit.GetTechDependencyTree(ca.AuditBasicParams, tech)
flattenGraph, fullDependenciesTree, err := audit.GetTechDependencyTree(ca.getAuditParamsByTech(tech), tech)
if err != nil {
return err
}
Expand Down
46 changes: 26 additions & 20 deletions xray/utils/auditbasicparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ import (
ioUtils "github.com/jfrog/jfrog-client-go/utils/io"
)

type AuditParams interface {
DirectDependencies() []string
AppendDependenciesForApplicabilityScan(directDependencies []string) *AuditBasicParams
ServerDetails() (*config.ServerDetails, error)
SetServerDetails(serverDetails *config.ServerDetails) *AuditBasicParams
PipRequirementsFile() string
SetPipRequirementsFile(requirementsFile string) *AuditBasicParams
ExcludeTestDependencies() bool
SetExcludeTestDependencies(excludeTestDependencies bool) *AuditBasicParams
UseWrapper() bool
SetUseWrapper(useWrapper bool) *AuditBasicParams
InsecureTls() bool
SetInsecureTls(insecureTls bool) *AuditBasicParams
Technologies() []string
SetTechnologies(technologies []string) *AuditBasicParams
Progress() ioUtils.ProgressMgr
SetProgress(progress ioUtils.ProgressMgr)
Args() []string
SetNpmScope(depType string) *AuditBasicParams
OutputFormat() OutputFormat
DepsRepo() string
SetDepsRepo(depsRepo string) *AuditBasicParams
IgnoreConfigFile() bool
SetIgnoreConfigFile(ignoreConfigFile bool) *AuditBasicParams
}

type AuditBasicParams struct {
serverDetails *config.ServerDetails
outputFormat OutputFormat
Expand All @@ -18,8 +44,6 @@ type AuditBasicParams struct {
args []string
depsRepo string
ignoreConfigFile bool
npmIgnoreNodeModules bool
npmOverWritePackageLock bool
}

func (abp *AuditBasicParams) DirectDependencies() []string {
Expand Down Expand Up @@ -62,14 +86,6 @@ func (abp *AuditBasicParams) UseWrapper() bool {
return abp.useWrapper
}

func (abp *AuditBasicParams) NpmIgnoreNodeModules() bool {
return abp.npmIgnoreNodeModules
}

func (abp *AuditBasicParams) NpmOverwritePackageLock() bool {
return abp.npmOverWritePackageLock
}

func (abp *AuditBasicParams) SetUseWrapper(useWrapper bool) *AuditBasicParams {
abp.useWrapper = useWrapper
return abp
Expand Down Expand Up @@ -141,13 +157,3 @@ func (abp *AuditBasicParams) SetIgnoreConfigFile(ignoreConfigFile bool) *AuditBa
abp.ignoreConfigFile = ignoreConfigFile
return abp
}

func (abp *AuditBasicParams) SetNpmIgnoreNodeModules(ignoreNpmNodeModules bool) *AuditBasicParams {
abp.npmIgnoreNodeModules = ignoreNpmNodeModules
return abp
}

func (abp *AuditBasicParams) SetNpmOverwritePackageLock(overwritePackageLock bool) *AuditBasicParams {
abp.npmOverWritePackageLock = overwritePackageLock
return abp
}
25 changes: 25 additions & 0 deletions xray/utils/packageManagerAuditParams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

type AuditNpmParams struct {
AuditParams
npmIgnoreNodeModules bool
npmOverWritePackageLock bool
}

func (abp AuditNpmParams) SetNpmIgnoreNodeModules(ignoreNpmNodeModules bool) AuditNpmParams {
abp.npmIgnoreNodeModules = ignoreNpmNodeModules
return abp
}

func (abp AuditNpmParams) SetNpmOverwritePackageLock(overwritePackageLock bool) AuditNpmParams {
abp.npmOverWritePackageLock = overwritePackageLock
return abp
}

func (abp AuditNpmParams) NpmIgnoreNodeModules() bool {
return abp.npmIgnoreNodeModules
}

func (abp AuditNpmParams) NpmOverwritePackageLock() bool {
return abp.npmOverWritePackageLock
}

0 comments on commit 79bff29

Please sign in to comment.