Skip to content

Commit 32eb317

Browse files
authored
Updates to java wrapper (#2)
* Added exception for null CxScanConfig object , formatted the code
1 parent 8411bd2 commit 32eb317

File tree

15 files changed

+198
-110
lines changed

15 files changed

+198
-110
lines changed

.github/workflows/main.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
name: Integration Tests
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
- master
9+
pull_request:
10+
types: [opened, reopened, synchronize]
11+
branches:
12+
- master
13+
- main
14+
15+
jobs:
16+
run_tests:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout the repository
20+
uses: actions/checkout@v2
21+
- name: Set up JDK 11
22+
uses: actions/setup-java@v1
23+
with:
24+
java-version: 11
25+
- name: Copy executable
26+
run: cp ./src/main/resources/cx-exe /tmp/
27+
- name: Permissions to executable
28+
run: sudo chmod 777 /tmp/cx-exe
29+
- name: Run tests with Maven
30+
env:
31+
CX_CLIENT_ID: ${{ secrets.CLIENT_ID}}
32+
CX_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET}}
33+
CX_BASE_URI : ${{ secrets.BASE_URI }}
34+
PATH_TO_EXECUTABLE: /tmp/cx-exe
35+
run: mvn -B test --file pom.xml

.github/workflows/maven-publish.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: Maven Package
55

66
on:
77
release:
8-
types: [created]
8+
types: [created]
99

1010
jobs:
1111
build:
@@ -17,15 +17,23 @@ jobs:
1717

1818
steps:
1919
- uses: actions/checkout@v2
20-
- name: Set up JDK 8
20+
- name: Set up JDK 11
2121
uses: actions/setup-java@v2
2222
with:
23-
java-version: '8'
23+
java-version: '11'
2424
distribution: 'adopt'
2525
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
2626
settings-path: ${{ github.workspace }} # location for the settings.xml file
27-
27+
- name: Copy executable
28+
run: cp ./src/main/resources/cx-exe /tmp/
29+
- name: Permissions to executable
30+
run: sudo chmod 777 /tmp/cx-exe
2831
- name: Build with Maven
32+
env:
33+
CX_CLIENT_ID: ${{ secrets.CLIENT_ID}}
34+
CX_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET}}
35+
CX_BASE_URI : ${{ secrets.BASE_URI }}
36+
PATH_TO_EXECUTABLE: /tmp/cx-exe
2937
run: mvn -B package --file pom.xml
3038

3139
- name: Publish to GitHub Packages Apache Maven

pom.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.checkmarx.ast</groupId>
66
<artifactId>ast-cli-java-wrapper</artifactId>
7-
<version>1.0.0</version>
7+
<version>1.0.1</version>
88
<packaging>jar</packaging>
99

1010
<dependencies>
@@ -56,6 +56,12 @@
5656
<version>RELEASE</version>
5757
<scope>test</scope>
5858
</dependency>
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
<version>4.13.1</version>
63+
<scope>test</scope>
64+
</dependency>
5965
</dependencies>
6066

6167
<build>
@@ -92,14 +98,10 @@
9298
<name>CheckmarxDev</name>
9399
<url>https://maven.pkg.github.com/CheckmarxDev/ast-cli-java-wrapper</url>
94100
</repository>
95-
<repository>
96-
<id>ossrh</id>
97-
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
98-
</repository>
99101
</distributionManagement>
100102
<properties>
101-
<maven.compiler.source>8</maven.compiler.source>
102-
<maven.compiler.target>8</maven.compiler.target>
103+
<maven.compiler.source>11</maven.compiler.source>
104+
<maven.compiler.target>11</maven.compiler.target>
103105
</properties>
104106
<developers>
105107
<developer>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.checkmarx.ast;
2+
3+
public class CheckmarxExeception extends RuntimeException{
4+
public CheckmarxExeception(String errorMessage) {
5+
super(errorMessage);
6+
}
7+
}

src/main/java/com/checkmarx/ast/CxAuth.java

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,37 @@
2222
import java.util.zip.ZipEntry;
2323
import java.util.zip.ZipFile;
2424

25-
2625
public 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-
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.checkmarx.ast;
22

33
public enum CxAuthType {
4-
TOKEN,
5-
KEYSECRET,
6-
ENVIRONMENT
7-
8-
}
4+
TOKEN, KEYSECRET, ENVIRONMENT
95

6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.checkmarx.ast;
2+
3+
public class CxExeception extends RuntimeException{
4+
public CxExeception(String errorMessage) {
5+
super(errorMessage);
6+
}
7+
}
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
package com.checkmarx.ast;
22

33
public enum CxParamType {
4-
S,
5-
V,
6-
G,
7-
D,
8-
PROJECT_NAME,
9-
SCAN_TYPES,
10-
PRESET_NAME,
11-
FILTER,
12-
DIRECTORY,
13-
ADDITIONAL_PARAMETERS,
14-
AGENT
4+
S, V, G, D, PROJECT_NAME, SCAN_TYPES, SAST_PRESET_NAME, FILTER, DIRECTORY, ADDITIONAL_PARAMETERS, AGENT
155
}

0 commit comments

Comments
 (0)