Skip to content

Commit e339386

Browse files
Feature/results update (#14)
* use new cli version * update cli version to 2.0.0-rc.22
1 parent be046d2 commit e339386

File tree

6 files changed

+58
-78
lines changed

6 files changed

+58
-78
lines changed

src/main/CxAuth.ts

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {CxScanConfig} from "./CxScanConfig";
22
import {CxParamType} from "./CxParamType";
33
import {ExecutionService} from "./ExecutionService";
4-
import {spawn} from "child_process";
5-
import {CxResultType} from "./CxResultType";
64
import {CxCommandOutput} from "./CxCommandOutput";
75
import * as fs from "fs"
6+
import * as os from "os";
7+
import * as path from "path";
88

99

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

140-
async getResultsList(scanId: string, formatType: string) {
141-
this.commands = this.initializeCommands(false);
142-
this.commands.push("result");
143-
this.commands.push("list");
144-
if (scanId) {
145-
this.commands.push("--scan-id")
146-
this.commands.push(scanId)
147-
} else {
148-
console.log("Scan Id not provided")
149-
}
150-
if (formatType) {
151-
this.commands.push("--format")
152-
this.commands.push(formatType)
153-
}
154-
let exec = new ExecutionService();
155-
return await exec.executeResultsCommands(this.pathToExecutable, this.commands)
140+
async getResultsList(scanId: string) {
141+
return this.executeResultsCommands(scanId, "json", ".json");
156142
}
157143

158-
async getResultsSummary(scanId: string, formatType: string, target: string) {
159-
this.commands = this.initializeCommands(false);
160-
this.commands.push("result");
161-
this.commands.push("summary");
162-
if (scanId) {
163-
this.commands.push("--scan-id")
164-
this.commands.push(scanId)
165-
} else {
166-
console.log("Scan Id not provided")
167-
}
168-
if (formatType) {
169-
this.commands.push("--format")
170-
this.commands.push(formatType)
171-
}
172-
if (target) {
173-
this.commands.push("--target")
174-
this.commands.push(target)
175-
}
176-
let exec = new ExecutionService();
177-
return await exec.executeResultsCommands(this.pathToExecutable, this.commands)
144+
async getResultsSummary(scanId: string): Promise<string> {
145+
return this.executeResultsCommands(scanId, "summaryHTML", ".html");
178146
}
179147

180-
async getResults(scanId: string, targetPath: string, resultParam: CxResultType) {
181-
this.commands = this.initializeCommands(false);
182-
this.commands.push("result");
183-
this.commands.push(resultParam);
184-
if (targetPath) {
185-
this.commands.push("--target");
186-
this.commands.push(targetPath);
187-
}
188-
const cp = spawn(this.pathToExecutable, this.commands);
189-
cp.stdout.on('data', (data: any) => {
190-
console.log(`stdout: ${data}`);
191-
const fs = require('fs');
192-
fs.readFile((targetPath) ? targetPath : "./simple-results.json", 'utf-8', (err: any, data: any) => {
193-
if (err) {
194-
throw err;
195-
}
196-
const val = JSON.stringify(JSON.parse(data), null, 2);
197-
fs.writeFile((targetPath) ? targetPath : "./simple-results.json", val, (err: any) => {
198-
if (err) {
199-
throw err;
200-
}
201-
console.log("Data has been written to file successfully.");
202-
});
148+
async getResults(scanId: string, resultType:string, outputFileName: string, outputFilePath: string) {
149+
this.commands = this.createResultCommand(scanId, resultType, outputFileName, outputFilePath)
203150

204-
});
151+
const exec = new ExecutionService();
152+
return await exec.executeCommands(this.pathToExecutable, this.commands);
153+
}
205154

206-
});
155+
async executeResultsCommands(scanId: string, resultType: string, fileExtension: string): Promise<string> {
156+
const fileName = new Date().getTime().toString();
157+
this.commands = this.createResultCommand(scanId, resultType, fileName, os.tmpdir())
158+
159+
const exec = new ExecutionService();
160+
await exec.executeResultsCommands(this.pathToExecutable, this.commands)
161+
162+
const filePath = path.join(os.tmpdir(), fileName + fileExtension)
163+
164+
return fs.readFileSync(filePath,'utf8');
165+
}
166+
167+
createResultCommand(scanId: string, reportFormat: string, outputFileName: string, outputPath: string): string[] {
168+
const resultCommands = this.initializeCommands(false);
169+
resultCommands.push("result");
170+
resultCommands.push("--scan-id");
171+
resultCommands.push(scanId);
172+
resultCommands.push("--report-format");
173+
resultCommands.push(reportFormat);
174+
175+
if (outputFileName) {
176+
resultCommands.push("--output-name")
177+
resultCommands.push(outputFileName)
178+
}
179+
if (outputPath) {
180+
resultCommands.push("--output-path")
181+
resultCommands.push(outputPath)
182+
}
183+
184+
return resultCommands;
207185
}
208186
}
209187

src/main/ExecutionService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ export class ExecutionService {
7878
});
7979
});
8080
}
81+
8182
}

src/main/resources/cx-linux

-52 KB
Binary file not shown.

src/main/resources/cx-mac

-4.28 KB
Binary file not shown.

src/main/resources/cx.exe

-2.5 KB
Binary file not shown.

src/tests/CxAuthCall.test.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const auth = new CxAuth(cxScanConfig);
2424
describe("ScanCreate cases",() => {
2525
it('ScanCreate Successful case wait mode', async () => {
2626
const data = await auth.scanCreate(params);
27-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
27+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
2828
const ScanObject = cxCommandOutput.scanObjectList.pop()
2929
const scanShowObject = await auth.scanShow(ScanObject.ID);
3030
console.log(" Json object from successful wait mode case: " + JSON.stringify(scanShowObject))
@@ -35,7 +35,7 @@ describe("ScanCreate cases",() => {
3535
params.set(CxParamType.BRANCH, "master");
3636
//params.set(CxParamType.PROJECT_NAME, "ASTJavascriptWrapperTest");
3737
const data = await auth.scanCreate(params);
38-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
38+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
3939
const ScanObject = cxCommandOutput.scanObjectList.pop()
4040
const scanShowObject = await auth.scanShow(ScanObject.ID);
4141
console.log(" Json object from successful wait mode case with branch: " +JSON.stringify(scanShowObject))
@@ -46,7 +46,7 @@ describe("ScanCreate cases",() => {
4646
it('ScanCreate Failure case', async () => {
4747
params.set(CxParamType.SAST_PRESET_NAME, "Checkmarx Default Fake");
4848
const data = await auth.scanCreate(params);
49-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
49+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
5050
const ScanObject = cxCommandOutput.scanObjectList.pop()
5151
const scanShowObject = await auth.scanShow(ScanObject.ID);
5252
console.log(" Json object from failure case: " + JSON.stringify(scanShowObject))
@@ -58,7 +58,7 @@ describe("ScanCreate cases",() => {
5858
params.set(CxParamType.SAST_PRESET_NAME, "Checkmarx Default");
5959
params.set(CxParamType.ADDITIONAL_PARAMETERS, "--nowait");
6060
const data = await auth.scanCreate(params);
61-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
61+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
6262
const ScanObject = cxCommandOutput.scanObjectList.pop()
6363
const scanShowObject = await auth.scanShow(ScanObject.ID);
6464
console.log(" Json object from successful no wait mode case: " + JSON.stringify(scanShowObject))
@@ -70,53 +70,54 @@ describe("ScanCreate cases",() => {
7070
describe("ScanList cases",() => {
7171
it('ScanList Successful case', async () => {
7272
const data = await auth.scanList();
73-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
73+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
7474
expect(cxCommandOutput.scanObjectList.length).toBeGreaterThan(0);
7575
});
7676
});
7777

7878
describe("ProjectList cases",() => {
7979
it('ProjectList Successful case', async () => {
8080
const data = await auth.projectList();
81-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
81+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
8282
expect(cxCommandOutput.scanObjectList.length).toBeGreaterThan(0);
8383
});
8484
});
8585

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

9697
it('Result List Successful case', async () => {
9798
const data = await auth.scanList();
98-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
99+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
99100
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
100-
const written = await auth.getResultsList(sampleId,"json")
101+
const written = await auth.getResultsList(sampleId)
101102
console.log(written)
102103
expect(written.length).toBeGreaterThan(0);
103104
});
104105

105106
it('Result summary html file generation successful case', async () => {
106107
const data = await auth.scanList();
107-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
108+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
108109
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
109-
const written = await auth.getResultsSummary(sampleId,"html","./test.html")
110+
const written = await auth.getResults(sampleId,"summaryHTML","test", ".")
110111
console.log(written)
111112
const file = await fileExists("./test.html");
112113
expect(file).toBe(true);
113114
});
114115

115116
it('Result summary html string successful case', async () => {
116117
const data = await auth.scanList();
117-
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
118+
const cxCommandOutput: CxCommandOutput = JSON.parse(JSON.stringify(data))
118119
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
119-
const written = await auth.getResultsSummary(sampleId,"html",null)
120+
const written = await auth.getResultsSummary(sampleId)
120121
console.log(written)
121122
expect(written.length).toBeGreaterThan(0);
122123
});

0 commit comments

Comments
 (0)