Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 41 additions & 63 deletions src/main/CxAuth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {CxScanConfig} from "./CxScanConfig";
import {CxParamType} from "./CxParamType";
import {ExecutionService} from "./ExecutionService";
import {spawn} from "child_process";
import {CxResultType} from "./CxResultType";
import {CxCommandOutput} from "./CxCommandOutput";
import * as fs from "fs"
import * as os from "os";
import * as path from "path";


type ParamTypeMap = Map<CxParamType, string>;
Expand Down Expand Up @@ -137,73 +137,51 @@ export class CxAuth {
return await exec.executeCommands(this.pathToExecutable, this.commands);
}

async getResultsList(scanId: string, formatType: string) {
this.commands = this.initializeCommands(false);
this.commands.push("result");
this.commands.push("list");
if (scanId) {
this.commands.push("--scan-id")
this.commands.push(scanId)
} else {
console.log("Scan Id not provided")
}
if (formatType) {
this.commands.push("--format")
this.commands.push(formatType)
}
let exec = new ExecutionService();
return await exec.executeResultsCommands(this.pathToExecutable, this.commands)
async getResultsList(scanId: string) {
return this.executeResultsCommands(scanId, "json", ".json");
}

async getResultsSummary(scanId: string, formatType: string, target: string) {
this.commands = this.initializeCommands(false);
this.commands.push("result");
this.commands.push("summary");
if (scanId) {
this.commands.push("--scan-id")
this.commands.push(scanId)
} else {
console.log("Scan Id not provided")
}
if (formatType) {
this.commands.push("--format")
this.commands.push(formatType)
}
if (target) {
this.commands.push("--target")
this.commands.push(target)
}
let exec = new ExecutionService();
return await exec.executeResultsCommands(this.pathToExecutable, this.commands)
async getResultsSummary(scanId: string): Promise<string> {
return this.executeResultsCommands(scanId, "summaryHTML", ".html");
}

async getResults(scanId: string, targetPath: string, resultParam: CxResultType) {
this.commands = this.initializeCommands(false);
this.commands.push("result");
this.commands.push(resultParam);
if (targetPath) {
this.commands.push("--target");
this.commands.push(targetPath);
}
const cp = spawn(this.pathToExecutable, this.commands);
cp.stdout.on('data', (data: any) => {
console.log(`stdout: ${data}`);
const fs = require('fs');
fs.readFile((targetPath) ? targetPath : "./simple-results.json", 'utf-8', (err: any, data: any) => {
if (err) {
throw err;
}
const val = JSON.stringify(JSON.parse(data), null, 2);
fs.writeFile((targetPath) ? targetPath : "./simple-results.json", val, (err: any) => {
if (err) {
throw err;
}
console.log("Data has been written to file successfully.");
});
async getResults(scanId: string, resultType:string, outputFileName: string, outputFilePath: string) {
this.commands = this.createResultCommand(scanId, resultType, outputFileName, outputFilePath)

});
const exec = new ExecutionService();
return await exec.executeCommands(this.pathToExecutable, this.commands);
}

});
async executeResultsCommands(scanId: string, resultType: string, fileExtension: string): Promise<string> {
const fileName = new Date().getTime().toString();
this.commands = this.createResultCommand(scanId, resultType, fileName, os.tmpdir())

const exec = new ExecutionService();
await exec.executeResultsCommands(this.pathToExecutable, this.commands)

const filePath = path.join(os.tmpdir(), fileName + fileExtension)

return fs.readFileSync(filePath,'utf8');
}

createResultCommand(scanId: string, reportFormat: string, outputFileName: string, outputPath: string): string[] {
const resultCommands = this.initializeCommands(false);
resultCommands.push("result");
resultCommands.push("--scan-id");
resultCommands.push(scanId);
resultCommands.push("--report-format");
resultCommands.push(reportFormat);

if (outputFileName) {
resultCommands.push("--output-name")
resultCommands.push(outputFileName)
}
if (outputPath) {
resultCommands.push("--output-path")
resultCommands.push(outputPath)
}

return resultCommands;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/ExecutionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ export class ExecutionService {
});
});
}

}
Binary file modified src/main/resources/cx-linux
Binary file not shown.
Binary file modified src/main/resources/cx-mac
Binary file not shown.
Binary file modified src/main/resources/cx.exe
Binary file not shown.
31 changes: 16 additions & 15 deletions src/tests/CxAuthCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const auth = new CxAuth(cxScanConfig);
describe("ScanCreate cases",() => {
it('ScanCreate Successful case wait mode', async () => {
const data = await auth.scanCreate(params);
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
const ScanObject = cxCommandOutput.scanObjectList.pop()
const scanShowObject = await auth.scanShow(ScanObject.ID);
console.log(" Json object from successful wait mode case: " + JSON.stringify(scanShowObject))
Expand All @@ -35,7 +35,7 @@ describe("ScanCreate cases",() => {
params.set(CxParamType.BRANCH, "master");
//params.set(CxParamType.PROJECT_NAME, "ASTJavascriptWrapperTest");
const data = await auth.scanCreate(params);
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
const ScanObject = cxCommandOutput.scanObjectList.pop()
const scanShowObject = await auth.scanShow(ScanObject.ID);
console.log(" Json object from successful wait mode case with branch: " +JSON.stringify(scanShowObject))
Expand All @@ -46,7 +46,7 @@ describe("ScanCreate cases",() => {
it('ScanCreate Failure case', async () => {
params.set(CxParamType.SAST_PRESET_NAME, "Checkmarx Default Fake");
const data = await auth.scanCreate(params);
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
const ScanObject = cxCommandOutput.scanObjectList.pop()
const scanShowObject = await auth.scanShow(ScanObject.ID);
console.log(" Json object from failure case: " + JSON.stringify(scanShowObject))
Expand All @@ -58,7 +58,7 @@ describe("ScanCreate cases",() => {
params.set(CxParamType.SAST_PRESET_NAME, "Checkmarx Default");
params.set(CxParamType.ADDITIONAL_PARAMETERS, "--nowait");
const data = await auth.scanCreate(params);
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
const ScanObject = cxCommandOutput.scanObjectList.pop()
const scanShowObject = await auth.scanShow(ScanObject.ID);
console.log(" Json object from successful no wait mode case: " + JSON.stringify(scanShowObject))
Expand All @@ -70,53 +70,54 @@ describe("ScanCreate cases",() => {
describe("ScanList cases",() => {
it('ScanList Successful case', async () => {
const data = await auth.scanList();
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
expect(cxCommandOutput.scanObjectList.length).toBeGreaterThan(0);
});
});

describe("ProjectList cases",() => {
it('ProjectList Successful case', async () => {
const data = await auth.projectList();
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
expect(cxCommandOutput.scanObjectList.length).toBeGreaterThan(0);
});
});

describe("Results cases",() => {
it('Result Test Successful case', async () => {
const data = await auth.scanList();
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
const written = await auth.getResults(sampleId,"test.json",null)
const written = await auth.getResults(sampleId,"json","jsonList", ".")
console.log(written)
expect(cxCommandOutput.scanObjectList.length).toBeGreaterThan(0);
const file = await fileExists("./jsonList.json");
expect(file).toBe(true);
});

it('Result List Successful case', async () => {
const data = await auth.scanList();
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
const written = await auth.getResultsList(sampleId,"json")
const written = await auth.getResultsList(sampleId)
console.log(written)
expect(written.length).toBeGreaterThan(0);
});

it('Result summary html file generation successful case', async () => {
const data = await auth.scanList();
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
const written = await auth.getResultsSummary(sampleId,"html","./test.html")
const written = await auth.getResults(sampleId,"summaryHTML","test", ".")
console.log(written)
const file = await fileExists("./test.html");
expect(file).toBe(true);
});

it('Result summary html string successful case', async () => {
const data = await auth.scanList();
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
const written = await auth.getResultsSummary(sampleId,"html",null)
const written = await auth.getResultsSummary(sampleId)
console.log(written)
expect(written.length).toBeGreaterThan(0);
});
Expand Down