2525
2626public class CxAuth {
2727 private Logger log = LoggerFactory .getLogger (CxAuth .class .getName ());
28- private String baseuri ;
29- private String baseAuthUri ;
30- private String tenant ;
31- private String key ;
32- private String secret ;
33- private String apikey ;
34- private URI executable = null ;
28+ private final String baseuri ;
29+ private final String baseAuthUri ;
30+ private final String tenant ;
31+ private final String key ;
32+ private final String secret ;
33+ private final String apikey ;
34+ private final URI executable ;
3535 private static final Gson gson = new Gson ();
3636
3737 public CxAuth (CxScanConfig scanConfig , Logger log )
@@ -101,7 +101,7 @@ private URI getFile(URI jarLocation, final String fileName) throws IOException {
101101
102102 try {
103103 fileURI = extract (zipFile , fileName );
104- log .info ("Location of the jar file: " + fileURI );
104+ log .info ("Location of the jar file: {}" , fileURI ) ;
105105 } finally {
106106 zipFile .close ();
107107 }
@@ -156,34 +156,49 @@ private static void close(final Closeable stream) {
156156 }
157157 }
158158
159- public CxScan cxScanShow (String id ) throws IOException , InterruptedException {
160- log .info ("Initialized scan retrieval for id: " + id );
159+ public CxCommandOutput cxScanShow (String id ) throws IOException , InterruptedException {
160+ log .info ("Initialized scan retrieval for id: {}" , id );
161161 List <String > commands = initialCommands ();
162162 commands .add ("scan" );
163163 commands .add ("show" );
164164 commands .add ("--scan-id" );
165165 commands .add (id );
166- CxScan scanObject = runExecutionCommands (commands );
167- if (scanObject != null )
166+ CxCommandOutput scanObject = runExecutionCommands (commands );
167+ if (scanObject . getScanObjectList () != null && scanObject . getScanObjectList (). size () == 1 )
168168 log .info ("Scan retrieved" );
169169 else
170170 log .info ("Did not receive the scan" );
171+
171172 return scanObject ;
172173 }
173174
174- private CxScan runExecutionCommands (List <String > commands ) throws IOException , InterruptedException {
175+ private CxCommandOutput runExecutionCommands (List <String > commands ) throws IOException , InterruptedException {
175176 log .info ("Process submitting to the executor" );
176177 ExecutionService exec = new ExecutionService ();
177- BufferedReader br = exec .executeCommand (commands );
178+ Process process = exec .executeCommand (commands );
178179 String line ;
179180 CxScan scanObject = null ;
181+ InputStream is = process .getInputStream ();
182+ InputStreamReader isr = new InputStreamReader (is );
183+ BufferedReader br = new BufferedReader (isr );
184+ CxCommandOutput cxCommandOutput = new CxCommandOutput ();
180185 while ((line = br .readLine ()) != null ) {
181186 log .info (line );
182- if (isJSONValid (line , CxScan .class ))
187+ if (isJSONValid (line , CxScan .class )) {
183188 scanObject = transformToCxScanObject (line );
189+ List <CxScan > scanList = new ArrayList <>();
190+ scanList .add (scanObject );
191+ cxCommandOutput .setScanObjectList (scanList );
192+ }
193+ }
194+ br .close ();
195+ if (!process .isAlive ()) {
196+ cxCommandOutput .setExitCode (process .exitValue ());
197+ log .info ("Exit code from AST-CLI: {}" , process .exitValue ());
184198 }
185199 log .info ("Process returned from the executor" );
186- return scanObject ;
200+ process .destroy ();
201+ return cxCommandOutput ;
187202 }
188203
189204 private CxScan transformToCxScanObject (String line ) {
@@ -199,7 +214,7 @@ private CxScan transformToCxScanObject(String line) {
199214 }
200215
201216 public List <String > initialCommandsCommon () {
202- List <String > commands = new ArrayList <String >();
217+ List <String > commands = new ArrayList <>();
203218 commands .add (executable .getPath ());
204219 addAuthCredentials (commands );
205220
@@ -237,30 +252,36 @@ public Integer cxAuthValidate() throws IOException, InterruptedException {
237252 return executionService .executeCommandSync (commands );
238253 }
239254
240- public List < CxScan > cxAstScanList () throws IOException , InterruptedException {
255+ public CxCommandOutput cxAstScanList () throws IOException , InterruptedException {
241256 log .info ("Initialized scan list retrieval" );
242257 List <String > commands = initialCommands ();
243258 commands .add ("scan" );
244259 commands .add ("list" );
245260
246261 ExecutionService exec = new ExecutionService ();
247- BufferedReader br = exec .executeCommand (commands );
262+ Process process = exec .executeCommand (commands );
248263 String line ;
249264 List <CxScan > list = new ArrayList <>();
265+ InputStream is = process .getInputStream ();
266+ InputStreamReader isr = new InputStreamReader (is );
267+ BufferedReader br = new BufferedReader (isr );
250268 while ((line = br .readLine ()) != null ) {
251269 if (isJSONValid (line , List .class ) && !line .isEmpty ())
252270 list = transformToCxScanList (line );
253271 }
254272 br .close ();
273+ CxCommandOutput cxCommandOutput = new CxCommandOutput ();
274+ cxCommandOutput .setScanObjectList (list );
275+ cxCommandOutput .setExitCode (process .exitValue ());
255276 if (list != null && !list .isEmpty ())
256- log .info ("Retrieved scan list with size: " + list .size ());
277+ log .info ("Retrieved scan list with size: {}" , list .size ());
257278 else
258279 log .info ("Not able to retrieve scan list" );
259280
260- return list ;
281+ return cxCommandOutput ;
261282 }
262283
263- public CxScan cxScanCreate (Map <CxParamType , String > params ) throws IOException , InterruptedException {
284+ public CxCommandOutput cxScanCreate (Map <CxParamType , String > params ) throws IOException , InterruptedException {
264285 log .info ("Initialized scan creation" );
265286 List <String > commands = initialCommands ();
266287 commands .add ("scan" );
@@ -291,6 +312,8 @@ public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException,
291312 return runExecutionCommands (commands );
292313 }
293314
315+
316+
294317 private void addIndividualParams (List <String > commands , String value ) {
295318 Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
296319 while (m .find ())
0 commit comments