Skip to content

Commit 077847b

Browse files
committed
passing tech got from TargetResults to BuildDependencyTree
1 parent d5d089d commit 077847b

File tree

4 files changed

+47
-46
lines changed

4 files changed

+47
-46
lines changed

commands/audit/sca/python/python.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const (
3434
CurationPipMinimumVersion = "23.0.0"
3535
)
3636

37-
func BuildDependencyTree(params utils.AuditParams) (dependencyTree []*clientutils.GraphNode, uniqueDeps []string, downloadUrls map[string]string, err error) {
38-
dependenciesGraph, directDependenciesList, pipUrls, errGetTree := getDependencies(params)
37+
func BuildDependencyTree(params utils.AuditParams, technology techutils.Technology) (dependencyTree []*clientutils.GraphNode, uniqueDeps []string, downloadUrls map[string]string, err error) {
38+
dependenciesGraph, directDependenciesList, pipUrls, errGetTree := getDependencies(params, technology)
3939
if errGetTree != nil {
4040
err = errGetTree
4141
return
@@ -60,7 +60,7 @@ func BuildDependencyTree(params utils.AuditParams) (dependencyTree []*clientutil
6060
return
6161
}
6262

63-
func getDependencies(params utils.AuditParams) (dependenciesGraph map[string][]string, directDependencies []string, pipUrls map[string]string, err error) {
63+
func getDependencies(params utils.AuditParams, technology techutils.Technology) (dependenciesGraph map[string][]string, directDependencies []string, pipUrls map[string]string, err error) {
6464
wd, err := os.Getwd()
6565
if errorutils.CheckError(err) != nil {
6666
return
@@ -91,8 +91,8 @@ func getDependencies(params utils.AuditParams) (dependenciesGraph map[string][]s
9191
return
9292
}
9393

94-
pythonTool := pythonutils.PythonTool(params.Technologies()[0])
95-
if !params.SkipAutoInstall() {
94+
pythonTool := pythonutils.PythonTool(technology)
95+
if technology == techutils.Pipenv || !params.SkipAutoInstall() {
9696
var restoreEnv func() error
9797
restoreEnv, err = runPythonInstall(params, pythonTool)
9898
defer func() {

commands/audit/sca/python/python_test.go

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ func TestBuildPipDependencyListSetuppy(t *testing.T) {
2727
defer cleanUp()
2828
// Run getModulesDependencyTrees
2929
params := clisecurityutils.AuditBasicParams{}
30-
params.AddTechnologyIfNotExist(techutils.Pip.String())
31-
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params)
30+
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params, techutils.Pip)
3231
assert.NoError(t, err)
3332
assert.Contains(t, uniqueDeps, PythonPackageTypeIdentifier+"pexpect:4.8.0")
3433
assert.Contains(t, uniqueDeps, PythonPackageTypeIdentifier+"ptyprocess:0.7.0")
@@ -55,9 +54,8 @@ func TestPipDependencyListCustomInstallArgs(t *testing.T) {
5554
assert.NoError(t, os.Chdir(filepath.Join(actualMainPath, "referenceproject")))
5655
// Run getModulesDependencyTrees
5756
params := clisecurityutils.AuditBasicParams{}
58-
params.AddTechnologyIfNotExist(techutils.Pip.String())
5957
params.SetInstallCommandArgs([]string{"--force-reinstall"})
60-
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params)
58+
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params, techutils.Pip)
6159
validatePipRequirementsProject(t, err, uniqueDeps, rootNode)
6260
}
6361

@@ -67,9 +65,8 @@ func TestBuildPipDependencyListSetuppyForCuration(t *testing.T) {
6765
defer cleanUp()
6866
// Run getModulesDependencyTrees
6967
params := clisecurityutils.AuditBasicParams{}
70-
params.AddTechnologyIfNotExist(techutils.Pip.String())
7168
params.SetIsCurationCmd(true)
72-
rootNode, uniqueDeps, downloadUrls, err := BuildDependencyTree(&params)
69+
rootNode, uniqueDeps, downloadUrls, err := BuildDependencyTree(&params, techutils.Pip)
7370
assert.NoError(t, err)
7471
assert.Contains(t, uniqueDeps, PythonPackageTypeIdentifier+"pexpect:4.8.0")
7572
assert.Contains(t, uniqueDeps, PythonPackageTypeIdentifier+"ptyprocess:0.7.0")
@@ -100,8 +97,7 @@ func TestPipDependencyListRequirementsFallback(t *testing.T) {
10097
defer cleanUp()
10198
// No requirements file field specified, expect the command to use the fallback 'pip install -r requirements.txt' command
10299
params := clisecurityutils.AuditBasicParams{}
103-
params.AddTechnologyIfNotExist(techutils.Pip.String())
104-
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params)
100+
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params, techutils.Pip)
105101
validatePipRequirementsProject(t, err, uniqueDeps, rootNode)
106102
}
107103

@@ -125,9 +121,8 @@ func TestBuildPipDependencyListRequirements(t *testing.T) {
125121
defer cleanUp()
126122
// Run getModulesDependencyTrees
127123
params := clisecurityutils.AuditBasicParams{}
128-
params.AddTechnologyIfNotExist(techutils.Pip.String())
129124
params.SetPipRequirementsFile("requirements.txt")
130-
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params)
125+
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params, techutils.Pip)
131126
assert.NoError(t, err)
132127
assert.Contains(t, uniqueDeps, PythonPackageTypeIdentifier+"pexpect:4.7.0")
133128
assert.Contains(t, uniqueDeps, PythonPackageTypeIdentifier+"ptyprocess:0.7.0")
@@ -154,8 +149,7 @@ func TestBuildPipenvDependencyList(t *testing.T) {
154149
}
155150
// Run getModulesDependencyTrees
156151
params := clisecurityutils.AuditBasicParams{}
157-
params.AddTechnologyIfNotExist(techutils.Pipenv.String())
158-
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params)
152+
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params, techutils.Pipenv)
159153
if err != nil {
160154
t.Fatal(err)
161155
}
@@ -191,8 +185,7 @@ func TestBuildPoetryDependencyList(t *testing.T) {
191185
}
192186
// Run getModulesDependencyTrees
193187
params := clisecurityutils.AuditBasicParams{}
194-
params.AddTechnologyIfNotExist(techutils.Poetry.String())
195-
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params)
188+
rootNode, uniqueDeps, _, err := BuildDependencyTree(&params, techutils.Poetry)
196189
if err != nil {
197190
t.Fatal(err)
198191
}
@@ -210,25 +203,38 @@ func TestBuildPoetryDependencyList(t *testing.T) {
210203
}
211204

212205
func TestBuildDependencyTreeWhenInstallForbidden(t *testing.T) {
206+
// This feature is currently supported and tested for Pip and Poetry only
213207
testcases := []struct {
214208
name string
215209
testDir string
216-
technology string
210+
technology techutils.Technology
217211
installBeforeFetchingInitialDeps bool
218212
}{
213+
// pip
219214
{
220215
name: "pip: project not installed | install forbidden",
221216
testDir: filepath.Join("projects", "package-managers", "python", "pip", "pip", "requirementsproject"),
222-
technology: techutils.Pip.String(),
217+
technology: techutils.Pip,
223218
installBeforeFetchingInitialDeps: false,
224219
},
225220
{
226221
name: "pip: project installed before dep tree construction| install forbidden",
227222
testDir: filepath.Join("projects", "package-managers", "python", "pip", "pip", "requirementsproject"),
228-
technology: techutils.Pip.String(),
223+
technology: techutils.Pip,
224+
installBeforeFetchingInitialDeps: true,
225+
},
226+
{
227+
name: "poetry: project not installed | install forbidden",
228+
testDir: filepath.Join("projects", "package-managers", "python", "poetry", "poetry"),
229+
technology: techutils.Poetry,
230+
installBeforeFetchingInitialDeps: false,
231+
},
232+
{
233+
name: "poetry: project installed before dep tree construction| install forbidden",
234+
testDir: filepath.Join("projects", "package-managers", "python", "poetry", "poetry"),
235+
technology: techutils.Poetry,
229236
installBeforeFetchingInitialDeps: true,
230237
},
231-
// TODO add similar test cases for pipenv and poetry
232238
}
233239

234240
for _, test := range testcases {
@@ -239,27 +245,25 @@ func TestBuildDependencyTreeWhenInstallForbidden(t *testing.T) {
239245
// Create virtual env according to package manager if needed
240246
if !test.installBeforeFetchingInitialDeps {
241247
// If we install before calling BuildDependencyTree a virtual environment is going to be created, and we don't have to do it manually
242-
switch test.technology {
243-
case techutils.Pip.String():
248+
if test.technology == techutils.Pip {
244249
restoreEnv, err := SetPipVirtualEnvPath()
245250
defer func() {
246-
assert.NoError(t, restoreEnv(), "restoring env after pip virtual env creation failed")
251+
assert.NoError(t, restoreEnv(), "restoring env after setting pip virtual env creation failed")
247252
}()
248253
require.NoError(t, err)
249-
default:
250254
}
251255
}
252256

253257
// Setting scan params
254-
params := (&clisecurityutils.AuditBasicParams{}).SetSkipAutoInstall(true).AddTechnologyIfNotExist(test.technology)
255-
if test.technology == techutils.Pip.String() {
258+
params := (&clisecurityutils.AuditBasicParams{}).SetSkipAutoInstall(true)
259+
if test.technology == techutils.Pip {
256260
params.SetPipRequirementsFile("requirements.txt")
257261
}
258262

259263
if test.installBeforeFetchingInitialDeps {
260264
restoreEnv, err := runPythonInstall(params, pythonutils.PythonTool(test.technology))
261265
defer func() {
262-
assert.NoError(t, restoreEnv(), "restoring env after pip virtual env creation failed")
266+
assert.NoError(t, restoreEnv(), "restoring env after setting "+test.technology+" virtual env creation failed")
263267
}()
264268
require.NoError(t, err)
265269
}
@@ -269,12 +273,21 @@ func TestBuildDependencyTreeWhenInstallForbidden(t *testing.T) {
269273
assert.NoError(t, err)
270274
// We use the dependencies graph and not the list of dependencies since the list includes only direct dependencies
271275
dependenciesGraphBeforeBuildDepTree, _, err := pythonutils.GetPythonDependencies(pythonutils.PythonTool(test.technology), testDir, localDependenciesPath, log.GetLogger())
272-
print(dependenciesGraphBeforeBuildDepTree)
273-
dependenciesBeforeBuildDepTree := maps.Keys(dependenciesGraphBeforeBuildDepTree)
274276
assert.NoError(t, err)
275277

278+
var dependenciesBeforeBuildDepTree []string
279+
switch test.technology {
280+
case techutils.Pip:
281+
dependenciesBeforeBuildDepTree = maps.Keys(dependenciesGraphBeforeBuildDepTree)
282+
case techutils.Poetry:
283+
if len(dependenciesGraphBeforeBuildDepTree) != 0 {
284+
mapKey := maps.Keys(dependenciesGraphBeforeBuildDepTree)[0]
285+
dependenciesBeforeBuildDepTree = dependenciesGraphBeforeBuildDepTree[mapKey]
286+
}
287+
}
288+
276289
// Build dependency tree
277-
_, uniqueDeps, _, err := BuildDependencyTree(params)
290+
_, uniqueDeps, _, err := BuildDependencyTree(params, test.technology)
278291
require.NoError(t, err)
279292
var trimmedUniqueDeps []string
280293
for _, dep := range uniqueDeps {

commands/audit/scarunner.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ func GetTechDependencyTree(params xrayutils.AuditParams, artifactoryServerDetail
255255
case techutils.Go:
256256
depTreeResult.FullDepTrees, uniqueDeps, err = _go.BuildDependencyTree(params)
257257
case techutils.Pipenv, techutils.Pip, techutils.Poetry:
258-
params.AddTechnologyIfNotExist(tech.String())
259258
depTreeResult.FullDepTrees, uniqueDeps,
260-
depTreeResult.DownloadUrls, err = python.BuildDependencyTree(params)
259+
depTreeResult.DownloadUrls, err = python.BuildDependencyTree(params, tech)
261260
case techutils.Nuget:
262261
depTreeResult.FullDepTrees, uniqueDeps, err = nuget.BuildDependencyTree(params)
263262
case techutils.Cocoapods:

utils/auditbasicparams.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type AuditParams interface {
2121
SetInsecureTls(insecureTls bool) *AuditBasicParams
2222
Technologies() []string
2323
SetTechnologies(technologies []string) *AuditBasicParams
24-
AddTechnologyIfNotExist(technology string) *AuditBasicParams
2524
Progress() ioUtils.ProgressMgr
2625
SetProgress(progress ioUtils.ProgressMgr)
2726
Args() []string
@@ -177,16 +176,6 @@ func (abp *AuditBasicParams) SetTechnologies(technologies []string) *AuditBasicP
177176
return abp
178177
}
179178

180-
func (abp *AuditBasicParams) AddTechnologyIfNotExist(technology string) *AuditBasicParams {
181-
for _, tech := range abp.technologies {
182-
if tech == technology {
183-
return abp
184-
}
185-
}
186-
abp.technologies = append(abp.technologies, technology)
187-
return abp
188-
}
189-
190179
func (abp *AuditBasicParams) SetScansToPerform(scansToPerform []SubScanType) *AuditBasicParams {
191180
abp.scansToPerform = scansToPerform
192181
return abp

0 commit comments

Comments
 (0)