25
25
26
26
public class CxAuth {
27
27
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 ;
35
35
private static final Gson gson = new Gson ();
36
36
37
37
public CxAuth (CxScanConfig scanConfig , Logger log )
@@ -101,7 +101,7 @@ private URI getFile(URI jarLocation, final String fileName) throws IOException {
101
101
102
102
try {
103
103
fileURI = extract (zipFile , fileName );
104
- log .info ("Location of the jar file: " + fileURI );
104
+ log .info ("Location of the jar file: {}" , fileURI ) ;
105
105
} finally {
106
106
zipFile .close ();
107
107
}
@@ -156,34 +156,49 @@ private static void close(final Closeable stream) {
156
156
}
157
157
}
158
158
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 );
161
161
List <String > commands = initialCommands ();
162
162
commands .add ("scan" );
163
163
commands .add ("show" );
164
164
commands .add ("--scan-id" );
165
165
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 )
168
168
log .info ("Scan retrieved" );
169
169
else
170
170
log .info ("Did not receive the scan" );
171
+
171
172
return scanObject ;
172
173
}
173
174
174
- private CxScan runExecutionCommands (List <String > commands ) throws IOException , InterruptedException {
175
+ private CxCommandOutput runExecutionCommands (List <String > commands ) throws IOException , InterruptedException {
175
176
log .info ("Process submitting to the executor" );
176
177
ExecutionService exec = new ExecutionService ();
177
- BufferedReader br = exec .executeCommand (commands );
178
+ Process process = exec .executeCommand (commands );
178
179
String line ;
179
180
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 ();
180
185
while ((line = br .readLine ()) != null ) {
181
186
log .info (line );
182
- if (isJSONValid (line , CxScan .class ))
187
+ if (isJSONValid (line , CxScan .class )) {
183
188
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 ());
184
198
}
185
199
log .info ("Process returned from the executor" );
186
- return scanObject ;
200
+ process .destroy ();
201
+ return cxCommandOutput ;
187
202
}
188
203
189
204
private CxScan transformToCxScanObject (String line ) {
@@ -199,7 +214,7 @@ private CxScan transformToCxScanObject(String line) {
199
214
}
200
215
201
216
public List <String > initialCommandsCommon () {
202
- List <String > commands = new ArrayList <String >();
217
+ List <String > commands = new ArrayList <>();
203
218
commands .add (executable .getPath ());
204
219
addAuthCredentials (commands );
205
220
@@ -237,30 +252,36 @@ public Integer cxAuthValidate() throws IOException, InterruptedException {
237
252
return executionService .executeCommandSync (commands );
238
253
}
239
254
240
- public List < CxScan > cxAstScanList () throws IOException , InterruptedException {
255
+ public CxCommandOutput cxAstScanList () throws IOException , InterruptedException {
241
256
log .info ("Initialized scan list retrieval" );
242
257
List <String > commands = initialCommands ();
243
258
commands .add ("scan" );
244
259
commands .add ("list" );
245
260
246
261
ExecutionService exec = new ExecutionService ();
247
- BufferedReader br = exec .executeCommand (commands );
262
+ Process process = exec .executeCommand (commands );
248
263
String line ;
249
264
List <CxScan > list = new ArrayList <>();
265
+ InputStream is = process .getInputStream ();
266
+ InputStreamReader isr = new InputStreamReader (is );
267
+ BufferedReader br = new BufferedReader (isr );
250
268
while ((line = br .readLine ()) != null ) {
251
269
if (isJSONValid (line , List .class ) && !line .isEmpty ())
252
270
list = transformToCxScanList (line );
253
271
}
254
272
br .close ();
273
+ CxCommandOutput cxCommandOutput = new CxCommandOutput ();
274
+ cxCommandOutput .setScanObjectList (list );
275
+ cxCommandOutput .setExitCode (process .exitValue ());
255
276
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 ());
257
278
else
258
279
log .info ("Not able to retrieve scan list" );
259
280
260
- return list ;
281
+ return cxCommandOutput ;
261
282
}
262
283
263
- public CxScan cxScanCreate (Map <CxParamType , String > params ) throws IOException , InterruptedException {
284
+ public CxCommandOutput cxScanCreate (Map <CxParamType , String > params ) throws IOException , InterruptedException {
264
285
log .info ("Initialized scan creation" );
265
286
List <String > commands = initialCommands ();
266
287
commands .add ("scan" );
@@ -291,6 +312,8 @@ public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException,
291
312
return runExecutionCommands (commands );
292
313
}
293
314
315
+
316
+
294
317
private void addIndividualParams (List <String > commands , String value ) {
295
318
Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
296
319
while (m .find ())
0 commit comments