22
22
import java .util .zip .ZipEntry ;
23
23
import java .util .zip .ZipFile ;
24
24
25
-
26
25
public class CxAuth {
27
26
private Logger log = LoggerFactory .getLogger (CxAuth .class .getName ());
28
27
private String baseuri ;
29
28
private String key ;
30
29
private String secret ;
31
- private String token ;
32
- private URI executable = null ;
30
+ private String apikey ;
31
+ private URI executable = null ;
33
32
private static final Gson gson = new Gson ();
34
33
35
- public CxAuth (CxScanConfig scanConfig , Logger log ) throws InterruptedException , IOException , URISyntaxException {
36
- if (scanConfig != null ) {
37
- this .baseuri = scanConfig .getBaseuri ();
38
- if (scanConfig .getKey () != null && scanConfig .getSecret () != null ) {
39
- this .key = scanConfig .getKey ();
40
- this .secret = scanConfig .getSecret ();
41
- }
42
- else if (scanConfig .getToken () != null ) {
43
- this .token = scanConfig .getToken ();
44
- }
45
- else {
46
- log .info ("Did not receive Key/Secret/Token" );
47
- }
48
- if (scanConfig .getPathToExecutable () != null && !scanConfig .getPathToExecutable ().isEmpty ()) {
49
- File file = new File (scanConfig .getPathToExecutable ());
50
- this .executable = file .toURI ();
51
- }
52
- else {
53
- this .executable = packageExecutable ();
54
- }
34
+ public CxAuth (CxScanConfig scanConfig , Logger log )
35
+ throws InterruptedException , IOException , URISyntaxException , CxExeception {
36
+ if (scanConfig == null ) throw new CxExeception ("CxScanConfig object returned as null!" );
37
+ this .baseuri = scanConfig .getBaseuri ();
38
+ if (scanConfig .getClientId () != null && scanConfig .getClientSecret () != null ) {
39
+ this .key = scanConfig .getClientId ();
40
+ this .secret = scanConfig .getClientSecret ();
41
+ } else if (scanConfig .getApikey () != null ) {
42
+ this .apikey = scanConfig .getApikey ();
43
+ }
44
+ if (scanConfig .getPathToExecutable () != null && !scanConfig .getPathToExecutable ().isEmpty ()) {
45
+ File file = new File (scanConfig .getPathToExecutable ());
46
+ this .executable = file .toURI ();
47
+ } else {
48
+ this .executable = packageExecutable ();
55
49
}
56
- if (log != null ) {
50
+
51
+ if (log != null ) {
57
52
this .log = log ;
58
53
}
59
54
}
60
55
61
-
62
56
private URI packageExecutable () throws IOException , URISyntaxException {
63
57
String osName = System .getProperty ("os.name" );
64
58
@@ -89,8 +83,7 @@ private URI getJarURI() throws URISyntaxException {
89
83
return (uri );
90
84
}
91
85
92
- private URI getFile ( URI jarLocation , final String fileName )
93
- throws IOException {
86
+ private URI getFile (URI jarLocation , final String fileName ) throws IOException {
94
87
final File location ;
95
88
final URI fileURI ;
96
89
location = new File (jarLocation );
@@ -113,20 +106,18 @@ private URI getFile( URI jarLocation, final String fileName)
113
106
return (fileURI );
114
107
}
115
108
116
- private static URI extract (final ZipFile zipFile , final String fileName )
117
- throws IOException {
109
+ private static URI extract (final ZipFile zipFile , final String fileName ) throws IOException {
118
110
final File tempFile ;
119
111
final ZipEntry entry ;
120
112
final InputStream zipStream ;
121
113
OutputStream fileStream ;
122
114
123
- tempFile = File .createTempFile (fileName ," " );
115
+ tempFile = File .createTempFile (fileName , " " );
124
116
tempFile .deleteOnExit ();
125
117
entry = zipFile .getEntry (fileName );
126
118
127
119
if (entry == null ) {
128
- throw new FileNotFoundException ("cannot find file: " + fileName
129
- + " in archive: " + zipFile .getName ());
120
+ throw new FileNotFoundException ("cannot find file: " + fileName + " in archive: " + zipFile .getName ());
130
121
}
131
122
132
123
zipStream = zipFile .getInputStream (entry );
@@ -162,13 +153,13 @@ private static void close(final Closeable stream) {
162
153
}
163
154
164
155
public CxScan cxScanShow (String id ) throws IOException , InterruptedException {
165
- log .info ("Initialized scan retrieval for id: " + id );
156
+ log .info ("Initialized scan retrieval for id: " + id );
166
157
List <String > commands = initialCommands ();
167
158
commands .add ("scan" );
168
159
commands .add ("show" );
169
160
commands .add (id );
170
161
CxScan scanObject = runExecutionCommands (commands );
171
- if (scanObject != null )
162
+ if (scanObject != null )
172
163
log .info ("Scan retrieved" );
173
164
else
174
165
log .info ("Did not receive the scan" );
@@ -183,14 +174,14 @@ private CxScan runExecutionCommands(List<String> commands) throws IOException, I
183
174
CxScan scanObject = null ;
184
175
while ((line = br .readLine ()) != null ) {
185
176
log .info (line );
186
- if (isJSONValid (line ,CxScan .class ))
177
+ if (isJSONValid (line , CxScan .class ))
187
178
scanObject = transformToCxScanObject (line );
188
179
}
189
180
log .info ("Process returned from the executor" );
190
181
return scanObject ;
191
182
}
192
183
193
- private CxScan transformToCxScanObject (String line ) {
184
+ private CxScan transformToCxScanObject (String line ) {
194
185
ObjectMapper objectMapper = new ObjectMapper ();
195
186
CxScan scanObject ;
196
187
try {
@@ -224,11 +215,11 @@ public List<CxScan> cxAstScanList() throws IOException, InterruptedException {
224
215
String line ;
225
216
List <CxScan > list = new ArrayList <>();
226
217
while ((line = br .readLine ()) != null ) {
227
- if (isJSONValid (line ,List .class ) && !line .isEmpty ())
218
+ if (isJSONValid (line , List .class ) && !line .isEmpty ())
228
219
list = transformToCxScanList (line );
229
220
}
230
221
br .close ();
231
- if (list != null && !list .isEmpty ())
222
+ if (list != null && !list .isEmpty ())
232
223
log .info ("Retrieved scan list with size: " + list .size ());
233
224
else
234
225
log .info ("Not able to retrieve scan list" );
@@ -242,22 +233,20 @@ public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException,
242
233
commands .add ("create" );
243
234
244
235
for (Map .Entry <CxParamType , String > param : params .entrySet ()) {
245
- if (param .getKey () == CxParamType .ADDITIONAL_PARAMETERS && param .getValue () != null ){
236
+ if (param .getKey () == CxParamType .ADDITIONAL_PARAMETERS && param .getValue () != null ) {
246
237
addIndividualParams (commands , param .getValue ());
247
- }
248
- else if (param .getKey ().toString ().length () == 1 ) {
238
+ } else if (param .getKey ().toString ().length () == 1 ) {
249
239
commands .add ("-" + param .getKey ().toString ().toLowerCase ());
250
- if (param .getValue () != null )
240
+ if (param .getValue () != null )
251
241
commands .add (param .getValue ());
252
242
else
253
243
commands .add (" " );
254
244
255
- }
256
- else if (param .getKey () != CxParamType .ADDITIONAL_PARAMETERS ) {
245
+ } else if (param .getKey () != CxParamType .ADDITIONAL_PARAMETERS ) {
257
246
String paramValue = param .getKey ().toString ();
258
- paramValue = "--" + paramValue .replace ("_" ,"-" ).toLowerCase ();
247
+ paramValue = "--" + paramValue .replace ("_" , "-" ).toLowerCase ();
259
248
commands .add (paramValue );
260
- if (param .getValue () != null )
249
+ if (param .getValue () != null )
261
250
commands .add (param .getValue ());
262
251
else
263
252
commands .add (" " );
@@ -270,22 +259,20 @@ else if(param.getKey() != CxParamType.ADDITIONAL_PARAMETERS) {
270
259
271
260
private void addIndividualParams (List <String > commands , String value ) {
272
261
Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
273
- while (m .find ())
262
+ while (m .find ())
274
263
commands .add (m .group (1 ));
275
264
}
276
265
277
266
private void addAuthCredentials (List <String > commands ) {
278
- if (key != null && secret != null ) {
267
+ if (key != null && secret != null ) {
279
268
commands .add ("--client-id" );
280
269
commands .add (key );
281
- commands .add ("--secret" );
270
+ commands .add ("--client- secret" );
282
271
commands .add (secret );
283
- }
284
- else if (token != null ) {
285
- commands .add ("--token" );
286
- commands .add (token );
287
- }
288
- else {
272
+ } else if (apikey != null ) {
273
+ commands .add ("--apikey" );
274
+ commands .add (apikey );
275
+ } else {
289
276
log .info ("KEY/SECRET/TOKEN not received" );
290
277
}
291
278
}
@@ -296,23 +283,20 @@ private List<CxScan> transformToCxScanList(String line) throws IOException {
296
283
try {
297
284
scanList = objectMapper .readValue (line , new TypeReference <List <CxScan >>() {
298
285
});
299
- }
300
- catch (JsonProcessingException e ) {
286
+ } catch (JsonProcessingException e ) {
301
287
return null ;
302
288
}
303
289
return scanList ;
304
290
305
291
}
306
292
307
- private boolean isJSONValid (String jsonInString , Object object ) {
293
+ private boolean isJSONValid (String jsonInString , Object object ) {
308
294
try {
309
295
gson .fromJson (jsonInString , (Type ) object );
310
296
return true ;
311
- } catch (com .google .gson .JsonSyntaxException ex ) {
297
+ } catch (com .google .gson .JsonSyntaxException ex ) {
312
298
return false ;
313
299
}
314
300
}
315
301
316
302
}
317
-
318
-
0 commit comments