diff --git a/src/main/CxAuth.ts b/src/main/CxAuth.ts index b5c51e01..3e3a6b8f 100644 --- a/src/main/CxAuth.ts +++ b/src/main/CxAuth.ts @@ -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; @@ -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 { + 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 { + 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; } } diff --git a/src/main/ExecutionService.ts b/src/main/ExecutionService.ts index 6473533f..ed4cfdc3 100644 --- a/src/main/ExecutionService.ts +++ b/src/main/ExecutionService.ts @@ -78,4 +78,5 @@ export class ExecutionService { }); }); } + } diff --git a/src/main/resources/cx-linux b/src/main/resources/cx-linux index d1cc8fb5..49b9f215 100644 Binary files a/src/main/resources/cx-linux and b/src/main/resources/cx-linux differ diff --git a/src/main/resources/cx-mac b/src/main/resources/cx-mac index ae159641..a7b169e9 100644 Binary files a/src/main/resources/cx-mac and b/src/main/resources/cx-mac differ diff --git a/src/main/resources/cx.exe b/src/main/resources/cx.exe index 25558441..022d50a8 100644 Binary files a/src/main/resources/cx.exe and b/src/main/resources/cx.exe differ diff --git a/src/tests/CxAuthCall.test.ts b/src/tests/CxAuthCall.test.ts index 9c33f4a1..ebad926c 100644 --- a/src/tests/CxAuthCall.test.ts +++ b/src/tests/CxAuthCall.test.ts @@ -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)) @@ -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)) @@ -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)) @@ -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)) @@ -70,7 +70,7 @@ 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); }); }); @@ -78,7 +78,7 @@ describe("ScanList cases",() => { 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); }); }); @@ -86,27 +86,28 @@ describe("ProjectList cases",() => { 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); @@ -114,9 +115,9 @@ describe("Results cases",() => { 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); });