Skip to content

Commit

Permalink
Added validation on empty values (#13)
Browse files Browse the repository at this point in the history
* Added validation on empty values

* AST-3601 Do not validate tenant as it is optional and validate project-name in scan create

* AST-3601 Raise version
  • Loading branch information
AndreGCX authored Aug 23, 2021
1 parent 9f1ee99 commit 8e1f081
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.checkmarx.ast</groupId>
<artifactId>ast-cli-java-wrapper</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/com/checkmarx/ast/scans/CxAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public CxAuth(CxScanConfig scanConfig, Logger log) throws IOException, URISyntax
this.secret = scanConfig.getClientSecret();
this.apikey = scanConfig.getApiKey();

validateConfigValues();

if (scanConfig.getPathToExecutable() != null && !scanConfig.getPathToExecutable().isEmpty()) {
File file = new File(scanConfig.getPathToExecutable());
this.executable = file.toURI();
Expand All @@ -58,6 +60,16 @@ public CxAuth(CxScanConfig scanConfig, Logger log) throws IOException, URISyntax
}
}

private void validateConfigValues(){
if (StringUtils.isEmpty(this.baseuri)) {
throw new CxException("Checkmarx server URL was not set");
}

if (StringUtils.isEmpty(this.apikey) && (StringUtils.isEmpty(this.key) && StringUtils.isEmpty(this.secret))) {
throw new CxException("Credentials were not set");
}
}

private URI packageExecutable() throws IOException, URISyntaxException {
String osName = System.getProperty("os.name");

Expand Down Expand Up @@ -94,7 +106,7 @@ private URI getFile(URI jarLocation, final String fileName) throws IOException {
location = new File(jarLocation);

if (location.isDirectory()) {
fileURI = URI.create(jarLocation.toString() + fileName);
fileURI = URI.create(jarLocation + fileName);
} else {
final ZipFile zipFile;

Expand Down Expand Up @@ -218,7 +230,7 @@ private String runResultExecutionCommands(List<String> commands) throws IOExcept
Process process = exec.executeCommand(commands);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
Expand All @@ -235,7 +247,7 @@ private CxCommandOutput runExecutionCommands(List<String> commands) throws IOExc
ExecutionService exec = new ExecutionService();
Process process = exec.executeCommand(commands);
String line;
CxScan scanObject = null;
CxScan scanObject;
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Expand Down Expand Up @@ -347,6 +359,10 @@ public CxCommandOutput cxScanCreate(Map<CxParamType, String> params) throws IOEx
commands.add("scan");
commands.add("create");

if (!params.containsKey(CxParamType.PROJECT_NAME)) {
throw new CxException("Checkmarx project name was not set");
}

for (Map.Entry<CxParamType, String> param : params.entrySet()) {
if (param.getKey() == CxParamType.ADDITIONAL_PARAMETERS && param.getValue() != null) {
addIndividualParams(commands, param.getValue());
Expand Down Expand Up @@ -392,9 +408,9 @@ private void addAuthCredentials(List<String> commands) {
}
}

private List<CxScan> transformToCxScanList(String line) throws IOException {
private List<CxScan> transformToCxScanList(String line) {
ObjectMapper objectMapper = new ObjectMapper();
List<CxScan> scanList = null;
List<CxScan> scanList;
try {
scanList = objectMapper.readValue(line, new TypeReference<List<CxScan>>() {
});
Expand All @@ -409,12 +425,12 @@ public boolean isValidJSON(final String json) {
boolean valid = false;
try {
final JsonParser parser = new ObjectMapper().createParser(json);
//noinspection StatementWithEmptyBody
while (parser.nextToken() != null) {
}
valid = true;
} catch (IOException ignored) {
}
;
return valid;
}

Expand Down

0 comments on commit 8e1f081

Please sign in to comment.