2222import java .util .zip .ZipEntry ;
2323import java .util .zip .ZipFile ;
2424
25-
2625public class CxAuth {
2726 private Logger log = LoggerFactory .getLogger (CxAuth .class .getName ());
2827 private String baseuri ;
2928 private String key ;
3029 private String secret ;
31- private String token ;
32- private URI executable = null ;
30+ private String apikey ;
31+ private URI executable = null ;
3332 private static final Gson gson = new Gson ();
3433
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 ();
5549 }
56- if (log != null ) {
50+
51+ if (log != null ) {
5752 this .log = log ;
5853 }
5954 }
6055
61-
6256 private URI packageExecutable () throws IOException , URISyntaxException {
6357 String osName = System .getProperty ("os.name" );
6458
@@ -89,8 +83,7 @@ private URI getJarURI() throws URISyntaxException {
8983 return (uri );
9084 }
9185
92- private URI getFile ( URI jarLocation , final String fileName )
93- throws IOException {
86+ private URI getFile (URI jarLocation , final String fileName ) throws IOException {
9487 final File location ;
9588 final URI fileURI ;
9689 location = new File (jarLocation );
@@ -113,20 +106,18 @@ private URI getFile( URI jarLocation, final String fileName)
113106 return (fileURI );
114107 }
115108
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 {
118110 final File tempFile ;
119111 final ZipEntry entry ;
120112 final InputStream zipStream ;
121113 OutputStream fileStream ;
122114
123- tempFile = File .createTempFile (fileName ," " );
115+ tempFile = File .createTempFile (fileName , " " );
124116 tempFile .deleteOnExit ();
125117 entry = zipFile .getEntry (fileName );
126118
127119 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 ());
130121 }
131122
132123 zipStream = zipFile .getInputStream (entry );
@@ -162,13 +153,13 @@ private static void close(final Closeable stream) {
162153 }
163154
164155 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 );
166157 List <String > commands = initialCommands ();
167158 commands .add ("scan" );
168159 commands .add ("show" );
169160 commands .add (id );
170161 CxScan scanObject = runExecutionCommands (commands );
171- if (scanObject != null )
162+ if (scanObject != null )
172163 log .info ("Scan retrieved" );
173164 else
174165 log .info ("Did not receive the scan" );
@@ -183,14 +174,14 @@ private CxScan runExecutionCommands(List<String> commands) throws IOException, I
183174 CxScan scanObject = null ;
184175 while ((line = br .readLine ()) != null ) {
185176 log .info (line );
186- if (isJSONValid (line ,CxScan .class ))
177+ if (isJSONValid (line , CxScan .class ))
187178 scanObject = transformToCxScanObject (line );
188179 }
189180 log .info ("Process returned from the executor" );
190181 return scanObject ;
191182 }
192183
193- private CxScan transformToCxScanObject (String line ) {
184+ private CxScan transformToCxScanObject (String line ) {
194185 ObjectMapper objectMapper = new ObjectMapper ();
195186 CxScan scanObject ;
196187 try {
@@ -224,11 +215,11 @@ public List<CxScan> cxAstScanList() throws IOException, InterruptedException {
224215 String line ;
225216 List <CxScan > list = new ArrayList <>();
226217 while ((line = br .readLine ()) != null ) {
227- if (isJSONValid (line ,List .class ) && !line .isEmpty ())
218+ if (isJSONValid (line , List .class ) && !line .isEmpty ())
228219 list = transformToCxScanList (line );
229220 }
230221 br .close ();
231- if (list != null && !list .isEmpty ())
222+ if (list != null && !list .isEmpty ())
232223 log .info ("Retrieved scan list with size: " + list .size ());
233224 else
234225 log .info ("Not able to retrieve scan list" );
@@ -242,22 +233,20 @@ public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException,
242233 commands .add ("create" );
243234
244235 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 ) {
246237 addIndividualParams (commands , param .getValue ());
247- }
248- else if (param .getKey ().toString ().length () == 1 ) {
238+ } else if (param .getKey ().toString ().length () == 1 ) {
249239 commands .add ("-" + param .getKey ().toString ().toLowerCase ());
250- if (param .getValue () != null )
240+ if (param .getValue () != null )
251241 commands .add (param .getValue ());
252242 else
253243 commands .add (" " );
254244
255- }
256- else if (param .getKey () != CxParamType .ADDITIONAL_PARAMETERS ) {
245+ } else if (param .getKey () != CxParamType .ADDITIONAL_PARAMETERS ) {
257246 String paramValue = param .getKey ().toString ();
258- paramValue = "--" + paramValue .replace ("_" ,"-" ).toLowerCase ();
247+ paramValue = "--" + paramValue .replace ("_" , "-" ).toLowerCase ();
259248 commands .add (paramValue );
260- if (param .getValue () != null )
249+ if (param .getValue () != null )
261250 commands .add (param .getValue ());
262251 else
263252 commands .add (" " );
@@ -270,22 +259,20 @@ else if(param.getKey() != CxParamType.ADDITIONAL_PARAMETERS) {
270259
271260 private void addIndividualParams (List <String > commands , String value ) {
272261 Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
273- while (m .find ())
262+ while (m .find ())
274263 commands .add (m .group (1 ));
275264 }
276265
277266 private void addAuthCredentials (List <String > commands ) {
278- if (key != null && secret != null ) {
267+ if (key != null && secret != null ) {
279268 commands .add ("--client-id" );
280269 commands .add (key );
281- commands .add ("--secret" );
270+ commands .add ("--client- secret" );
282271 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 {
289276 log .info ("KEY/SECRET/TOKEN not received" );
290277 }
291278 }
@@ -296,23 +283,20 @@ private List<CxScan> transformToCxScanList(String line) throws IOException {
296283 try {
297284 scanList = objectMapper .readValue (line , new TypeReference <List <CxScan >>() {
298285 });
299- }
300- catch (JsonProcessingException e ) {
286+ } catch (JsonProcessingException e ) {
301287 return null ;
302288 }
303289 return scanList ;
304290
305291 }
306292
307- private boolean isJSONValid (String jsonInString , Object object ) {
293+ private boolean isJSONValid (String jsonInString , Object object ) {
308294 try {
309295 gson .fromJson (jsonInString , (Type ) object );
310296 return true ;
311- } catch (com .google .gson .JsonSyntaxException ex ) {
297+ } catch (com .google .gson .JsonSyntaxException ex ) {
312298 return false ;
313299 }
314300 }
315301
316302}
317-
318-
0 commit comments