77 "path/filepath"
88 "strings"
99
10+ clientutils "github.com/jfrog/jfrog-client-go/utils"
11+
1012 "github.com/jfrog/jfrog-cli-core/v2/common/format"
1113 "github.com/jfrog/jfrog-cli-core/v2/utils/config"
1214 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
@@ -90,72 +92,119 @@ func (cmd *MaliciousScanCommand) Run() (err error) {
9092 }
9193 }()
9294
93- xrayManager , xrayVersion , err := xray . CreateXrayServiceManagerAndGetVersion ( cmd .serverDetails )
95+ xrayVersion , entitledForJas , workingDirs , err := cmd .validateAndPrepare ( )
9496 if err != nil {
9597 return err
9698 }
9799
98- entitledForJas , err := jas .IsEntitledForJas (xrayManager , xrayVersion )
100+ cmdResults := cmd .initializeCommandResults (xrayVersion , entitledForJas )
101+ populateScanTargets (cmdResults , workingDirs )
102+
103+ scanner , err := cmd .createJasScanner ()
99104 if err != nil {
100105 return err
101106 }
107+
108+ if err = cmd .runMaliciousScans (cmdResults , scanner ); err != nil {
109+ return err
110+ }
111+
112+ if cmd .progress != nil {
113+ if err = cmd .progress .Quit (); err != nil {
114+ return err
115+ }
116+ }
117+
118+ return cmd .outputResults (cmdResults )
119+ }
120+
121+ func (cmd * MaliciousScanCommand ) validateAndPrepare () (xrayVersion string , entitledForJas bool , workingDirs []string , err error ) {
122+ xrayManager , xrayVersion , err := xray .CreateXrayServiceManagerAndGetVersion (cmd .serverDetails )
123+ if err != nil {
124+ return "" , false , nil , err
125+ }
126+
127+ entitledForJas , err = jas .IsEntitledForJas (xrayManager , xrayVersion )
128+ if err != nil {
129+ return "" , false , nil , err
130+ }
102131 if ! entitledForJas {
103- return errors .New ("JAS (Advanced Security) feature is not entitled" )
132+ return "" , false , nil , errors .New ("JAS (Advanced Security) feature is not entitled" )
104133 }
105134
106135 log .Info ("JFrog Xray version is:" , xrayVersion )
107136
108137 if err = jas .DownloadAnalyzerManagerIfNeeded (0 ); err != nil {
109- return fmt .Errorf ("failed to download Analyzer Manager: %w" , err )
138+ return "" , false , nil , fmt .Errorf ("failed to download Analyzer Manager: %w" , err )
110139 }
111140
112- workingDirs , err : = coreutils .GetFullPathsWorkingDirs (cmd .workingDirs )
141+ workingDirs , err = coreutils .GetFullPathsWorkingDirs (cmd .workingDirs )
113142 if err != nil {
114- return err
143+ return "" , false , nil , err
115144 }
116145 logScanPaths (workingDirs )
117146
147+ return xrayVersion , entitledForJas , workingDirs , nil
148+ }
149+
150+ func (cmd * MaliciousScanCommand ) initializeCommandResults (xrayVersion string , entitledForJas bool ) * results.SecurityCommandResults {
118151 cmdResults := results .NewCommandResults (utils .SourceCode )
119152 cmdResults .SetXrayVersion (xrayVersion )
120153 cmdResults .SetEntitledForJas (entitledForJas )
121154 cmdResults .SetResultsContext (results.ResultContext {
122155 IncludeVulnerabilities : true ,
123156 })
157+ return cmdResults
158+ }
124159
125- populateScanTargets (cmdResults , workingDirs )
126-
160+ func (cmd * MaliciousScanCommand ) createJasScanner () (* jas.JasScanner , error ) {
127161 scannerOptions := []jas.JasScannerOption {
128162 jas .WithEnvVars (
129- false , // validateSecrets not relevant for malicious scan
163+ false ,
130164 jas .NotDiffScanEnvValue ,
131165 jas .GetAnalyzerManagerXscEnvVars (
132- "" , // msi
133- "" , // gitRepoUrl
134- "" , // projectKey
135- nil , // watches
166+ "" ,
167+ "" ,
168+ "" ,
169+ nil ,
136170 ),
137171 ),
138172 jas .WithMinSeverity (cmd .minSeverityFilter ),
139173 }
140174
141175 scanner , err := jas .NewJasScanner (cmd .serverDetails , scannerOptions ... )
142176 if err != nil {
143- return fmt .Errorf ("failed to create JAS scanner: %w" , err )
177+ return nil , fmt .Errorf ("failed to create JAS scanner: %w" , err )
144178 }
145179 if scanner == nil {
146- return errors .New ("JAS scanner was not created" )
180+ return nil , errors .New ("JAS scanner was not created" )
147181 }
148182
149- if cmd .customAnalyzerManagerPath != "" {
150- scanner .AnalyzerManager .AnalyzerManagerFullPath = cmd .customAnalyzerManagerPath
151- } else {
152- if scanner .AnalyzerManager .AnalyzerManagerFullPath , err = jas .GetAnalyzerManagerExecutable (); err != nil {
153- return fmt .Errorf ("failed to set analyzer manager executable path: %w" , err )
154- }
183+ if err = cmd .setAnalyzerManagerPath (scanner ); err != nil {
184+ return nil , err
155185 }
156186
157187 log .Debug (fmt .Sprintf ("Using analyzer manager executable at: %s" , scanner .AnalyzerManager .AnalyzerManagerFullPath ))
188+ return scanner , nil
189+ }
190+
191+ func (cmd * MaliciousScanCommand ) setAnalyzerManagerPath (scanner * jas.JasScanner ) error {
192+ if cmd .customAnalyzerManagerPath == "" {
193+ if err := jas .DownloadAnalyzerManagerIfNeeded (0 ); err != nil {
194+ return fmt .Errorf ("failed to download analyzer manager: %s" , err .Error ())
195+ }
196+ var err error
197+ if scanner .AnalyzerManager .AnalyzerManagerFullPath , err = jas .GetAnalyzerManagerExecutable (); err != nil {
198+ return fmt .Errorf ("failed to set analyzer manager executable path: %s" , err .Error ())
199+ }
200+ } else {
201+ scanner .AnalyzerManager .AnalyzerManagerFullPath = cmd .customAnalyzerManagerPath
202+ log .Debug (clientutils .GetLogMsgPrefix (0 , false ) + "using custom analyzer manager binary path" )
203+ }
204+ return nil
205+ }
158206
207+ func (cmd * MaliciousScanCommand ) runMaliciousScans (cmdResults * results.SecurityCommandResults , scanner * jas.JasScanner ) error {
159208 jasScanProducerConsumer := utils .NewSecurityParallelRunner (cmd .threads )
160209
161210 serverDetails , err := cmd .ServerDetails ()
@@ -199,14 +248,11 @@ func (cmd *MaliciousScanCommand) Run() (err error) {
199248 }
200249
201250 jasScanProducerConsumer .Start ()
251+ return nil
252+ }
202253
203- if cmd .progress != nil {
204- if err = cmd .progress .Quit (); err != nil {
205- return err
206- }
207- }
208-
209- if err = output .NewResultsWriter (cmdResults ).
254+ func (cmd * MaliciousScanCommand ) outputResults (cmdResults * results.SecurityCommandResults ) error {
255+ if err := output .NewResultsWriter (cmdResults ).
210256 SetOutputFormat (cmd .outputFormat ).
211257 SetPlatformUrl (cmd .serverDetails .Url ).
212258 SetPrintExtendedTable (false ).
@@ -216,7 +262,7 @@ func (cmd *MaliciousScanCommand) Run() (err error) {
216262 return errors .Join (err , cmdResults .GetErrors ())
217263 }
218264
219- if err = cmdResults .GetErrors (); err != nil {
265+ if err : = cmdResults .GetErrors (); err != nil {
220266 return err
221267 }
222268
@@ -229,10 +275,10 @@ func logScanPaths(workingDirs []string) {
229275 return
230276 }
231277 if len (workingDirs ) == 1 {
232- log .Info ("Scanning path:" , workingDirs [0 ])
278+ log .Debug ("Scanning path:" , workingDirs [0 ])
233279 return
234280 }
235- log .Info ("Scanning paths:" , strings .Join (workingDirs , ", " ))
281+ log .Debug ("Scanning paths:" , strings .Join (workingDirs , ", " ))
236282}
237283
238284func populateScanTargets (cmdResults * results.SecurityCommandResults , workingDirs []string ) {
@@ -244,17 +290,8 @@ func populateScanTargets(cmdResults *results.SecurityCommandResults, workingDirs
244290 cmdResults .NewScanResults (results.ScanTarget {Target : requestedDirectory , Name : filepath .Base (requestedDirectory )})
245291 }
246292
247- if len (workingDirs ) == 0 {
248- currentDir , err := coreutils .GetWorkingDirectory ()
249- if err != nil {
250- cmdResults .AddGeneralError (fmt .Errorf ("failed to get current working directory: %w" , err ), false )
251- return
252- }
253- cmdResults .NewScanResults (results.ScanTarget {Target : currentDir , Name : filepath .Base (currentDir )})
254- }
255-
256293 if len (cmdResults .Targets ) == 0 {
257- log .Warn ("No scan targets were detected. Proceeding with empty scan... " )
294+ log .Warn ("No scan targets were detected." )
258295 return
259296 }
260297
0 commit comments