Skip to content

Commit fa0095e

Browse files
authored
Merge pull request #92 from CheckmarxDev/feature/AST-8454-add-scan-cancel
added command for scan cancel
2 parents 7f20a66 + 19e9280 commit fa0095e

File tree

4 files changed

+94
-53
lines changed

4 files changed

+94
-53
lines changed

src/main/wrapper/CxConstants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export enum CxConstants {
2121
SUB_CMD_BRANCHES = "branches",
2222
CMD_SCAN = "scan",
2323
SUB_CMD_SHOW = "show",
24+
SUB_CMD_CANCEL = "cancel",
2425
SUB_CMD_LIST = "list",
2526
SUB_CMD_CREATE = "create",
2627
CMD_TRIAGE = "triage",

src/main/wrapper/CxWrapper.ts

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {CxConfig} from "./CxConfig";
2-
import {CxParamType} from "./CxParamType";
3-
import {CxConstants} from "./CxConstants";
4-
import {ExecutionService} from "./ExecutionService";
5-
import {CxCommandOutput} from "./CxCommandOutput";
6-
import { logger } from "./loggerConfig";
1+
import { CxConfig } from "./CxConfig";
2+
import { CxParamType } from "./CxParamType";
3+
import { CxConstants } from "./CxConstants";
4+
import { ExecutionService } from "./ExecutionService";
5+
import { CxCommandOutput } from "./CxCommandOutput";
6+
import { getLoggerWithFilePath, logger } from "./loggerConfig";
77
import * as fs from "fs"
88
import * as os from "os";
99
import CxBFL from "../bfl/CxBFL";
@@ -13,9 +13,12 @@ type ParamTypeMap = Map<CxParamType, string>;
1313
export class CxWrapper {
1414
config: CxConfig = new CxConfig();
1515

16-
constructor(cxScanConfig: CxConfig) {
16+
constructor(cxScanConfig: CxConfig, logFilePath?: string) {
1717
let path = require("path");
18-
if (cxScanConfig.clientId && cxScanConfig.clientSecret) {
18+
19+
getLoggerWithFilePath(logFilePath)
20+
21+
if (cxScanConfig.clientId && cxScanConfig.clientSecret) {
1922
logger.info("Received clientId and clientSecret");
2023
this.config.clientId = cxScanConfig.clientId;
2124
this.config.clientSecret = cxScanConfig.clientSecret;
@@ -117,22 +120,29 @@ export class CxWrapper {
117120
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
118121
}
119122

123+
async scanCancel(id: string): Promise<CxCommandOutput> {
124+
const commands: string[] = [CxConstants.CMD_SCAN, CxConstants.SUB_CMD_CANCEL, CxConstants.SCAN_ID, id];
125+
commands.push(...this.initializeCommands(false));
126+
const exec = new ExecutionService();
127+
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
128+
}
129+
120130
async scanShow(id: string): Promise<CxCommandOutput> {
121131
const commands: string[] = [CxConstants.CMD_SCAN, CxConstants.SUB_CMD_SHOW, CxConstants.SCAN_ID, id];
122132
commands.push(...this.initializeCommands(true));
123133
const exec = new ExecutionService();
124134
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
125135
}
126136

127-
async scanList(filters:string): Promise<CxCommandOutput> {
137+
async scanList(filters: string): Promise<CxCommandOutput> {
128138
const validated_filters = this.filterArguments(filters);
129139
const commands: string[] = [CxConstants.CMD_SCAN, "list"].concat(validated_filters);
130140
commands.push(...this.initializeCommands(true));
131141
const exec = new ExecutionService();
132142
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
133143
}
134144

135-
async projectList(filters:string): Promise<CxCommandOutput> {
145+
async projectList(filters: string): Promise<CxCommandOutput> {
136146
const validated_filters = this.filterArguments(filters);
137147
const commands: string[] = [CxConstants.CMD_PROJECT, "list"].concat(validated_filters);
138148
commands.push(...this.initializeCommands(true));
@@ -143,32 +153,32 @@ export class CxWrapper {
143153
async projectBranches(projectId: string, filters: string): Promise<CxCommandOutput> {
144154
// Verify and add possible branch filter by name
145155
const validated_filters = this.filterArguments(CxConstants.BRANCH_NAME + filters)
146-
const commands: string[] = [CxConstants.CMD_PROJECT , CxConstants.SUB_CMD_BRANCHES, CxConstants.PROJECT_ID, projectId].concat(validated_filters);
156+
const commands: string[] = [CxConstants.CMD_PROJECT, CxConstants.SUB_CMD_BRANCHES, CxConstants.PROJECT_ID, projectId].concat(validated_filters);
147157
commands.push(...this.initializeCommands(false));
148158
const exec = new ExecutionService();
149159
return await exec.executeCommands(this.config.pathToExecutable, commands);
150160
}
151161

152162
async projectShow(projectId: string): Promise<CxCommandOutput> {
153-
const commands: string[] = [CxConstants.CMD_PROJECT, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID,projectId];
163+
const commands: string[] = [CxConstants.CMD_PROJECT, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID, projectId];
154164
commands.push(...this.initializeCommands(true));
155165
const exec = new ExecutionService();
156166
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PROJECT_TYPE);
157167
}
158168

159-
async triageShow(projectId: string, similarityId: string, scanType: string ): Promise<CxCommandOutput> {
169+
async triageShow(projectId: string, similarityId: string, scanType: string): Promise<CxCommandOutput> {
160170
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID, projectId, CxConstants.SIMILARITY_ID, similarityId, CxConstants.SCAN_TYPES_SUB_CMD, scanType];
161171
commands.push(...this.initializeCommands(true));
162172
const exec = new ExecutionService();
163173
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PREDICATE_TYPE);
164-
}
165-
174+
}
175+
166176
async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string): Promise<CxCommandOutput> {
167177
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_UPDATE, CxConstants.PROJECT_ID, projectId, CxConstants.SIMILARITY_ID, similarityId, CxConstants.SCAN_TYPES_SUB_CMD, scanType, CxConstants.STATE, state, CxConstants.COMMENT, comment, CxConstants.SEVERITY, severity];
168178
commands.push(...this.initializeCommands(false));
169179
const exec = new ExecutionService();
170180
return await exec.executeCommands(this.config.pathToExecutable, commands);
171-
}
181+
}
172182

173183
async getResultsList(scanId: string) {
174184
const exec = new ExecutionService();
@@ -177,7 +187,7 @@ export class CxWrapper {
177187
// Executes the command and creates a result file
178188
await exec.executeResultsCommands(this.config.pathToExecutable, commands)
179189
// Reads the result file and retrieves the results
180-
return exec.executeResultsCommandsFile(scanId, CxConstants.FORMAT_JSON, CxConstants.FORMAT_JSON_FILE, commands,this.config.pathToExecutable,fileName);
190+
return exec.executeResultsCommandsFile(scanId, CxConstants.FORMAT_JSON, CxConstants.FORMAT_JSON_FILE, commands, this.config.pathToExecutable, fileName);
181191
}
182192

183193
async getResultsSummary(scanId: string): Promise<CxCommandOutput> {
@@ -187,24 +197,24 @@ export class CxWrapper {
187197
// Executes the command and creates a result file
188198
await exec.executeResultsCommands(this.config.pathToExecutable, commands);
189199
// Reads the result file and retrieves the results
190-
return exec.executeResultsCommandsFile(scanId, CxConstants.FORMAT_HTML, CxConstants.FORMAT_HTML_FILE, commands,this.config.pathToExecutable,fileName);
200+
return exec.executeResultsCommandsFile(scanId, CxConstants.FORMAT_HTML, CxConstants.FORMAT_HTML_FILE, commands, this.config.pathToExecutable, fileName);
191201
}
192202

193-
async getResults(scanId: string, resultType:string, outputFileName: string, outputFilePath: string) {
203+
async getResults(scanId: string, resultType: string, outputFileName: string, outputFilePath: string) {
194204
const commands = this.resultsShow(scanId, resultType, outputFileName, outputFilePath)
195205
const exec = new ExecutionService();
196206
return await exec.executeCommands(this.config.pathToExecutable, commands);
197207
}
198208

199-
async codeBashingList(cweId:string,language:string,queryName:string): Promise<CxCommandOutput> {
200-
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.CMD_CODE_BASHING, CxConstants.LANGUAGE, language, CxConstants.VULNERABILITY_TYPE , queryName, CxConstants.CWE_ID , cweId];
209+
async codeBashingList(cweId: string, language: string, queryName: string): Promise<CxCommandOutput> {
210+
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.CMD_CODE_BASHING, CxConstants.LANGUAGE, language, CxConstants.VULNERABILITY_TYPE, queryName, CxConstants.CWE_ID, cweId];
201211
commands.push(...this.initializeCommands(true));
202212
const exec = new ExecutionService();
203-
return await exec.executeCommands(this.config.pathToExecutable, commands,CxConstants.CODE_BASHING_TYPE);
213+
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.CODE_BASHING_TYPE);
204214
}
205215

206216
resultsShow(scanId: string, reportFormat: string, outputFileName: string, outputPath: string): string[] {
207-
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.SUB_CMD_SHOW, CxConstants.SCAN_ID, scanId,CxConstants.REPORT_FORMAT , reportFormat];
217+
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.SUB_CMD_SHOW, CxConstants.SCAN_ID, scanId, CxConstants.REPORT_FORMAT, reportFormat];
208218
if (outputFileName) {
209219
commands.push(CxConstants.OUTPUT_NAME);
210220
commands.push(outputFileName);
@@ -233,8 +243,7 @@ export class CxWrapper {
233243
for (const bflNode of bflNodes) {
234244
for (const resultNode of resultNodes) {
235245

236-
if(this.compareNodes(bflNode,resultNode))
237-
{
246+
if (this.compareNodes(bflNode, resultNode)) {
238247
return resultNodes.indexOf(resultNode);
239248
}
240249
}
@@ -244,22 +253,22 @@ export class CxWrapper {
244253

245254
}
246255

247-
compareNodes(bflNode: CxBFL, resultNode : any): boolean{
256+
compareNodes(bflNode: CxBFL, resultNode: any): boolean {
248257

249258
return bflNode.line == resultNode.line &&
250-
bflNode.column == resultNode.column &&
251-
bflNode.length == resultNode.length &&
252-
bflNode.name == resultNode.name &&
253-
bflNode.method == resultNode.method &&
254-
bflNode.domType == resultNode.domType &&
255-
bflNode.fileName == resultNode.fileName &&
256-
bflNode.fullName == resultNode.fullName &&
257-
bflNode.methodLine == resultNode.methodLine;
259+
bflNode.column == resultNode.column &&
260+
bflNode.length == resultNode.length &&
261+
bflNode.name == resultNode.name &&
262+
bflNode.method == resultNode.method &&
263+
bflNode.domType == resultNode.domType &&
264+
bflNode.fileName == resultNode.fileName &&
265+
bflNode.fullName == resultNode.fullName &&
266+
bflNode.methodLine == resultNode.methodLine;
258267
}
259268

260-
filterArguments(filters:string):string[]{
269+
filterArguments(filters: string): string[] {
261270
let r = [];
262-
if(filters.length>0){
271+
if (filters.length > 0) {
263272
r.push(CxConstants.FILTER);
264273
r.push(filters);
265274
}

src/main/wrapper/loggerConfig.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import { configure, getLogger } from 'log4js';
22

3-
// Appenders
4-
configure({
5-
appenders: {
6-
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
7-
},
8-
categories: {
9-
default: { appenders: ['console'], level: "info" }
10-
}
11-
});
12-
13-
// Fetch logger and export
3+
function configurationWithFile(logFilePath: string) {
4+
return configure({
5+
appenders: {
6+
file: { type: 'fileSync', filename: logFilePath },
7+
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
8+
},
9+
categories: {
10+
default: { appenders: ['console', 'file'], level: "info" }
11+
}
12+
});
13+
}
14+
15+
function configurationWithoutFile() {
16+
return configure({
17+
appenders: {
18+
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
19+
},
20+
categories: {
21+
default: { appenders: ['console',], level: "info" }
22+
}
23+
});
24+
}
25+
26+
export function getLoggerWithFilePath(logFilePath?: string) {
27+
logFilePath ? configurationWithFile(logFilePath) : configurationWithoutFile()
28+
}
29+
1430
export const logger = getLogger();

src/tests/ScanTest.test.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {CxWrapper} from '../main/wrapper/CxWrapper';
2-
import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
3-
import {CxParamType} from "../main/wrapper/CxParamType";
4-
import {BaseTest} from "./BaseTest";
1+
import { CxWrapper } from '../main/wrapper/CxWrapper';
2+
import { CxCommandOutput } from "../main/wrapper/CxCommandOutput";
3+
import { CxParamType } from "../main/wrapper/CxParamType";
4+
import { BaseTest } from "./BaseTest";
55

6-
describe("ScanCreate cases",() => {
6+
describe("ScanCreate cases", () => {
77
let cxScanConfig = new BaseTest();
88
it('ScanList Successful case', async () => {
99
const auth = new CxWrapper(cxScanConfig);
@@ -53,7 +53,7 @@ describe("ScanCreate cases",() => {
5353
const cxCommandOutput: CxCommandOutput = await auth.scanCreate(params);
5454
const scanObject = cxCommandOutput.payload.pop();
5555
const scanShowObject = await auth.scanShow(scanObject.id);
56-
console.log(" Json object from successful wait mode case with branch: " +JSON.stringify(scanShowObject));
56+
console.log(" Json object from successful wait mode case with branch: " + JSON.stringify(scanShowObject));
5757
expect(scanShowObject.payload.pop().status).toEqual("Completed");
5858

5959
})
@@ -72,4 +72,19 @@ describe("ScanCreate cases",() => {
7272
console.log(" Json object from successful no wait mode case: " + JSON.stringify(scanShowObject));
7373
expect(scanShowObject.payload.pop().status).toEqual("Running");
7474
})
75+
76+
it('ScanCancel Successful case', async () => {
77+
const params = new Map();
78+
params.set(CxParamType.PROJECT_NAME, "ast-cli-javascript-integration-cancel");
79+
params.set(CxParamType.S, "./src");
80+
params.set(CxParamType.BRANCH, "master");
81+
params.set(CxParamType.FILTER, "*.ts,!**/node_modules/**/*");
82+
params.set(CxParamType.ADDITIONAL_PARAMETERS, "--async");
83+
const auth = new CxWrapper(cxScanConfig);
84+
const cxCommandOutput: CxCommandOutput = await auth.scanCreate(params);
85+
const scanObject = cxCommandOutput.payload.pop();
86+
await auth.scanCancel(scanObject.id)
87+
const scanShowObject = await auth.scanShow(scanObject.id);
88+
expect(scanShowObject.exitCode).toEqual(0);
89+
})
7590
});

0 commit comments

Comments
 (0)