Skip to content

Commit f717bd1

Browse files
committed
add audit params to python
1 parent 6075488 commit f717bd1

File tree

3 files changed

+200
-71
lines changed

3 files changed

+200
-71
lines changed

commands/audit/sca/python/python.go

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type AuditPython struct {
4343
IsCurationCmd bool
4444
}
4545

46-
func BuildDependencyTree(auditPython *AuditPython) (dependencyTree []*xrayUtils.GraphNode, uniqueDeps []string, downloadUrls map[string]string, err error) {
47-
dependenciesGraph, directDependenciesList, pipUrls, errGetTree := getDependencies(auditPython)
46+
func BuildDependencyTree(params xrayutils2.AuditParams) (dependencyTree []*xrayUtils.GraphNode, uniqueDeps []string, downloadUrls map[string]string, err error) {
47+
dependenciesGraph, directDependenciesList, pipUrls, errGetTree := getDependencies(params)
4848
if errGetTree != nil {
4949
err = errGetTree
5050
return
@@ -69,7 +69,7 @@ func BuildDependencyTree(auditPython *AuditPython) (dependencyTree []*xrayUtils.
6969
return
7070
}
7171

72-
func getDependencies(auditPython *AuditPython) (dependenciesGraph map[string][]string, directDependencies []string, pipUrls map[string]string, err error) {
72+
func getDependencies(params xrayutils2.AuditParams) (dependenciesGraph map[string][]string, directDependencies []string, pipUrls map[string]string, err error) {
7373
wd, err := os.Getwd()
7474
if errorutils.CheckError(err) != nil {
7575
return
@@ -99,25 +99,33 @@ func getDependencies(auditPython *AuditPython) (dependenciesGraph map[string][]s
9999
if err != nil {
100100
return
101101
}
102-
103-
restoreEnv, err := runPythonInstall(auditPython)
104-
defer func() {
105-
err = errors.Join(err, restoreEnv())
106-
}()
107-
if err != nil {
102+
if len(params.Technologies()) == 0 {
103+
err = errors.New("no technology was provided")
108104
return
109105
}
106+
pythonTool := pythonutils.PythonTool(params.Technologies()[0])
107+
108+
if !params.SkipAutoInstall() {
109+
restoreEnv, restoreEnvErr := runPythonInstall(params, pythonTool)
110+
defer func() {
111+
restoreEnvErr = errors.Join(restoreEnvErr, restoreEnv())
112+
}()
113+
if restoreEnvErr != nil {
114+
err = restoreEnvErr
115+
return
116+
}
117+
}
110118

111119
localDependenciesPath, err := config.GetJfrogDependenciesPath()
112120
if err != nil {
113121
return
114122
}
115-
dependenciesGraph, directDependencies, err = pythonutils.GetPythonDependencies(auditPython.Tool, tempDirPath, localDependenciesPath, log.GetLogger())
123+
dependenciesGraph, directDependencies, err = pythonutils.GetPythonDependencies(pythonTool, tempDirPath, localDependenciesPath, log.GetLogger())
116124
if err != nil {
117125
sca.LogExecutableVersion("python")
118-
sca.LogExecutableVersion(string(auditPython.Tool))
126+
sca.LogExecutableVersion(string(pythonTool))
119127
}
120-
if !auditPython.IsCurationCmd {
128+
if !params.IsCurationCmd() {
121129
return
122130
}
123131
pipUrls, errProcessed := processPipDownloadsUrlsFromReportFile()
@@ -181,29 +189,31 @@ type pypiMetaData struct {
181189
Version string `json:"version"`
182190
}
183191

184-
func runPythonInstall(auditPython *AuditPython) (restoreEnv func() error, err error) {
185-
switch auditPython.Tool {
192+
func runPythonInstall(params xrayutils2.AuditParams, tool pythonutils.PythonTool) (restoreEnv func() error, err error) {
193+
switch tool {
186194
case pythonutils.Pip:
187-
return installPipDeps(auditPython)
195+
return installPipDeps(params)
188196
case pythonutils.Pipenv:
189-
return installPipenvDeps(auditPython)
197+
return installPipenvDeps(params)
190198
case pythonutils.Poetry:
191-
return installPoetryDeps(auditPython)
199+
return installPoetryDeps(params)
192200
}
193201
return
194202
}
195203

196-
func installPoetryDeps(auditPython *AuditPython) (restoreEnv func() error, err error) {
204+
func installPoetryDeps(params xrayutils2.AuditParams) (restoreEnv func() error, err error) {
197205
restoreEnv = func() error {
198206
return nil
199207
}
200-
if auditPython.RemotePypiRepo != "" {
201-
rtUrl, username, password, err := utils.GetPypiRepoUrlWithCredentials(auditPython.Server, auditPython.RemotePypiRepo, false)
208+
if params.DepsRepo() != "" {
209+
var serverDetails *config.ServerDetails
210+
serverDetails, err = params.ServerDetails()
211+
rtUrl, username, password, err := utils.GetPypiRepoUrlWithCredentials(serverDetails, params.DepsRepo(), false)
202212
if err != nil {
203213
return restoreEnv, err
204214
}
205215
if password != "" {
206-
err = utils.ConfigPoetryRepo(rtUrl.Scheme+"://"+rtUrl.Host+rtUrl.Path, username, password, auditPython.RemotePypiRepo)
216+
err = utils.ConfigPoetryRepo(rtUrl.Scheme+"://"+rtUrl.Host+rtUrl.Path, username, password, params.DepsRepo())
207217
if err != nil {
208218
return restoreEnv, err
209219
}
@@ -214,7 +224,7 @@ func installPoetryDeps(auditPython *AuditPython) (restoreEnv func() error, err e
214224
return restoreEnv, err
215225
}
216226

217-
func installPipenvDeps(auditPython *AuditPython) (restoreEnv func() error, err error) {
227+
func installPipenvDeps(params xrayutils2.AuditParams) (restoreEnv func() error, err error) {
218228
// Set virtualenv path to venv dir
219229
err = os.Setenv("WORKON_HOME", ".jfrog")
220230
if err != nil {
@@ -223,31 +233,41 @@ func installPipenvDeps(auditPython *AuditPython) (restoreEnv func() error, err e
223233
restoreEnv = func() error {
224234
return os.Unsetenv("WORKON_HOME")
225235
}
226-
if auditPython.RemotePypiRepo != "" {
227-
return restoreEnv, runPipenvInstallFromRemoteRegistry(auditPython.Server, auditPython.RemotePypiRepo)
236+
if params.DepsRepo() != "" {
237+
var serverDetails *config.ServerDetails
238+
serverDetails, err = params.ServerDetails()
239+
if err != nil {
240+
return
241+
}
242+
return restoreEnv, runPipenvInstallFromRemoteRegistry(serverDetails, params.DepsRepo())
228243
}
229244
// Run 'pipenv install -d'
230245
_, err = executeCommand("pipenv", "install", "-d")
231246
return restoreEnv, err
232247
}
233248

234-
func installPipDeps(auditPython *AuditPython) (restoreEnv func() error, err error) {
249+
func installPipDeps(params xrayutils2.AuditParams) (restoreEnv func() error, err error) {
235250
restoreEnv, err = SetPipVirtualEnvPath()
236251
if err != nil {
237252
return
238253
}
239254

240255
remoteUrl := ""
241-
if auditPython.RemotePypiRepo != "" {
242-
remoteUrl, err = utils.GetPypiRepoUrl(auditPython.Server, auditPython.RemotePypiRepo, auditPython.IsCurationCmd)
256+
if params.DepsRepo() != "" {
257+
var serverDetails *config.ServerDetails
258+
serverDetails, err = params.ServerDetails()
259+
if err != nil {
260+
return
261+
}
262+
remoteUrl, err = utils.GetPypiRepoUrl(serverDetails, params.DepsRepo(), params.IsCurationCmd())
243263
if err != nil {
244264
return
245265
}
246266
}
247267

248268
var curationCachePip string
249269
var reportFileName string
250-
if auditPython.IsCurationCmd {
270+
if params.IsCurationCmd() {
251271
// upgrade pip version to 23.0.0, as it is required for the curation command.
252272
if err = upgradePipVersion(CurationPipMinimumVersion); err != nil {
253273
log.Warn(fmt.Sprintf("Failed to upgrade pip version, err: %v", err))
@@ -258,11 +278,11 @@ func installPipDeps(auditPython *AuditPython) (restoreEnv func() error, err erro
258278
reportFileName = pythonReportFile
259279
}
260280

261-
pipInstallArgs := getPipInstallArgs(auditPython.PipRequirementsFile, remoteUrl, curationCachePip, reportFileName, auditPython.InstallCommandArgs...)
281+
pipInstallArgs := getPipInstallArgs(params.PipRequirementsFile(), remoteUrl, curationCachePip, reportFileName, params.InstallCommandArgs()...)
262282
var reqErr error
263283
_, err = executeCommand("python", pipInstallArgs...)
264-
if err != nil && auditPython.PipRequirementsFile == "" {
265-
pipInstallArgs = getPipInstallArgs("requirements.txt", remoteUrl, curationCachePip, reportFileName, auditPython.InstallCommandArgs...)
284+
if err != nil && params.PipRequirementsFile() == "" {
285+
pipInstallArgs = getPipInstallArgs("requirements.txt", remoteUrl, curationCachePip, reportFileName, params.InstallCommandArgs()...)
266286
_, reqErr = executeCommand("python", pipInstallArgs...)
267287
if reqErr != nil {
268288
// Return Pip install error and log the requirements fallback error.
@@ -272,7 +292,7 @@ func installPipDeps(auditPython *AuditPython) (restoreEnv func() error, err erro
272292
}
273293
}
274294
if err != nil || reqErr != nil {
275-
if msgToUser := sca.GetMsgToUserForCurationBlock(auditPython.IsCurationCmd, techutils.Pip, errors.Join(err, reqErr).Error()); msgToUser != "" {
295+
if msgToUser := sca.GetMsgToUserForCurationBlock(params.IsCurationCmd(), techutils.Pip, errors.Join(err, reqErr).Error()); msgToUser != "" {
276296
err = errors.Join(err, errors.New(msgToUser))
277297
}
278298
}

0 commit comments

Comments
 (0)