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
1 change: 1 addition & 0 deletions src/main/wrapper/CxConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum CxConstants {
SUB_CMD_BRANCHES = "branches",
CMD_SCAN = "scan",
SUB_CMD_SHOW = "show",
SUB_CMD_CANCEL = "cancel",
SUB_CMD_LIST = "list",
SUB_CMD_CREATE = "create",
CMD_TRIAGE = "triage",
Expand Down
81 changes: 45 additions & 36 deletions src/main/wrapper/CxWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {CxConfig} from "./CxConfig";
import {CxParamType} from "./CxParamType";
import {CxConstants} from "./CxConstants";
import {ExecutionService} from "./ExecutionService";
import {CxCommandOutput} from "./CxCommandOutput";
import { logger } from "./loggerConfig";
import { CxConfig } from "./CxConfig";
import { CxParamType } from "./CxParamType";
import { CxConstants } from "./CxConstants";
import { ExecutionService } from "./ExecutionService";
import { CxCommandOutput } from "./CxCommandOutput";
import { getLoggerWithFilePath, logger } from "./loggerConfig";
import * as fs from "fs"
import * as os from "os";
import CxBFL from "../bfl/CxBFL";
Expand All @@ -13,9 +13,12 @@ type ParamTypeMap = Map<CxParamType, string>;
export class CxWrapper {
config: CxConfig = new CxConfig();

constructor(cxScanConfig: CxConfig) {
constructor(cxScanConfig: CxConfig, logFilePath?: string) {
let path = require("path");
if (cxScanConfig.clientId && cxScanConfig.clientSecret) {

getLoggerWithFilePath(logFilePath)

if (cxScanConfig.clientId && cxScanConfig.clientSecret) {
logger.info("Received clientId and clientSecret");
this.config.clientId = cxScanConfig.clientId;
this.config.clientSecret = cxScanConfig.clientSecret;
Expand Down Expand Up @@ -117,22 +120,29 @@ export class CxWrapper {
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
}

async scanCancel(id: string): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_SCAN, CxConstants.SUB_CMD_CANCEL, CxConstants.SCAN_ID, id];
commands.push(...this.initializeCommands(false));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
}

async scanShow(id: string): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_SCAN, CxConstants.SUB_CMD_SHOW, CxConstants.SCAN_ID, id];
commands.push(...this.initializeCommands(true));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
}

async scanList(filters:string): Promise<CxCommandOutput> {
async scanList(filters: string): Promise<CxCommandOutput> {
const validated_filters = this.filterArguments(filters);
const commands: string[] = [CxConstants.CMD_SCAN, "list"].concat(validated_filters);
commands.push(...this.initializeCommands(true));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
}

async projectList(filters:string): Promise<CxCommandOutput> {
async projectList(filters: string): Promise<CxCommandOutput> {
const validated_filters = this.filterArguments(filters);
const commands: string[] = [CxConstants.CMD_PROJECT, "list"].concat(validated_filters);
commands.push(...this.initializeCommands(true));
Expand All @@ -143,32 +153,32 @@ export class CxWrapper {
async projectBranches(projectId: string, filters: string): Promise<CxCommandOutput> {
// Verify and add possible branch filter by name
const validated_filters = this.filterArguments(CxConstants.BRANCH_NAME + filters)
const commands: string[] = [CxConstants.CMD_PROJECT , CxConstants.SUB_CMD_BRANCHES, CxConstants.PROJECT_ID, projectId].concat(validated_filters);
const commands: string[] = [CxConstants.CMD_PROJECT, CxConstants.SUB_CMD_BRANCHES, CxConstants.PROJECT_ID, projectId].concat(validated_filters);
commands.push(...this.initializeCommands(false));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands);
}

async projectShow(projectId: string): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_PROJECT, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID,projectId];
const commands: string[] = [CxConstants.CMD_PROJECT, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID, projectId];
commands.push(...this.initializeCommands(true));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PROJECT_TYPE);
}

async triageShow(projectId: string, similarityId: string, scanType: string ): Promise<CxCommandOutput> {
async triageShow(projectId: string, similarityId: string, scanType: string): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID, projectId, CxConstants.SIMILARITY_ID, similarityId, CxConstants.SCAN_TYPES_SUB_CMD, scanType];
commands.push(...this.initializeCommands(true));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PREDICATE_TYPE);
}
}

async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string): Promise<CxCommandOutput> {
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];
commands.push(...this.initializeCommands(false));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands);
}
}

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

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

async getResults(scanId: string, resultType:string, outputFileName: string, outputFilePath: string) {
async getResults(scanId: string, resultType: string, outputFileName: string, outputFilePath: string) {
const commands = this.resultsShow(scanId, resultType, outputFileName, outputFilePath)
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands);
}

async codeBashingList(cweId:string,language:string,queryName:string): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.CMD_CODE_BASHING, CxConstants.LANGUAGE, language, CxConstants.VULNERABILITY_TYPE , queryName, CxConstants.CWE_ID , cweId];
async codeBashingList(cweId: string, language: string, queryName: string): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.CMD_CODE_BASHING, CxConstants.LANGUAGE, language, CxConstants.VULNERABILITY_TYPE, queryName, CxConstants.CWE_ID, cweId];
commands.push(...this.initializeCommands(true));
const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands,CxConstants.CODE_BASHING_TYPE);
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.CODE_BASHING_TYPE);
}

resultsShow(scanId: string, reportFormat: string, outputFileName: string, outputPath: string): string[] {
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.SUB_CMD_SHOW, CxConstants.SCAN_ID, scanId,CxConstants.REPORT_FORMAT , reportFormat];
const commands: string[] = [CxConstants.CMD_RESULT, CxConstants.SUB_CMD_SHOW, CxConstants.SCAN_ID, scanId, CxConstants.REPORT_FORMAT, reportFormat];
if (outputFileName) {
commands.push(CxConstants.OUTPUT_NAME);
commands.push(outputFileName);
Expand Down Expand Up @@ -233,8 +243,7 @@ export class CxWrapper {
for (const bflNode of bflNodes) {
for (const resultNode of resultNodes) {

if(this.compareNodes(bflNode,resultNode))
{
if (this.compareNodes(bflNode, resultNode)) {
return resultNodes.indexOf(resultNode);
}
}
Expand All @@ -244,22 +253,22 @@ export class CxWrapper {

}

compareNodes(bflNode: CxBFL, resultNode : any): boolean{
compareNodes(bflNode: CxBFL, resultNode: any): boolean {

return bflNode.line == resultNode.line &&
bflNode.column == resultNode.column &&
bflNode.length == resultNode.length &&
bflNode.name == resultNode.name &&
bflNode.method == resultNode.method &&
bflNode.domType == resultNode.domType &&
bflNode.fileName == resultNode.fileName &&
bflNode.fullName == resultNode.fullName &&
bflNode.methodLine == resultNode.methodLine;
bflNode.column == resultNode.column &&
bflNode.length == resultNode.length &&
bflNode.name == resultNode.name &&
bflNode.method == resultNode.method &&
bflNode.domType == resultNode.domType &&
bflNode.fileName == resultNode.fileName &&
bflNode.fullName == resultNode.fullName &&
bflNode.methodLine == resultNode.methodLine;
}

filterArguments(filters:string):string[]{
filterArguments(filters: string): string[] {
let r = [];
if(filters.length>0){
if (filters.length > 0) {
r.push(CxConstants.FILTER);
r.push(filters);
}
Expand Down
38 changes: 27 additions & 11 deletions src/main/wrapper/loggerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { configure, getLogger } from 'log4js';

// Appenders
configure({
appenders: {
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
},
categories: {
default: { appenders: ['console'], level: "info" }
}
});

// Fetch logger and export
function configurationWithFile(logFilePath: string) {
return configure({
appenders: {
file: { type: 'fileSync', filename: logFilePath },
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
},
categories: {
default: { appenders: ['console', 'file'], level: "info" }
}
});
}

function configurationWithoutFile() {
return configure({
appenders: {
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
},
categories: {
default: { appenders: ['console',], level: "info" }
}
});
}

export function getLoggerWithFilePath(logFilePath?: string) {
logFilePath ? configurationWithFile(logFilePath) : configurationWithoutFile()
}

export const logger = getLogger();
27 changes: 21 additions & 6 deletions src/tests/ScanTest.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {CxWrapper} from '../main/wrapper/CxWrapper';
import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
import {CxParamType} from "../main/wrapper/CxParamType";
import {BaseTest} from "./BaseTest";
import { CxWrapper } from '../main/wrapper/CxWrapper';
import { CxCommandOutput } from "../main/wrapper/CxCommandOutput";
import { CxParamType } from "../main/wrapper/CxParamType";
import { BaseTest } from "./BaseTest";

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

})
Expand All @@ -72,4 +72,19 @@ describe("ScanCreate cases",() => {
console.log(" Json object from successful no wait mode case: " + JSON.stringify(scanShowObject));
expect(scanShowObject.payload.pop().status).toEqual("Running");
})

it('ScanCancel Successful case', async () => {
const params = new Map();
params.set(CxParamType.PROJECT_NAME, "ast-cli-javascript-integration-cancel");
params.set(CxParamType.S, "./src");
params.set(CxParamType.BRANCH, "master");
params.set(CxParamType.FILTER, "*.ts,!**/node_modules/**/*");
params.set(CxParamType.ADDITIONAL_PARAMETERS, "--async");
const auth = new CxWrapper(cxScanConfig);
const cxCommandOutput: CxCommandOutput = await auth.scanCreate(params);
const scanObject = cxCommandOutput.payload.pop();
await auth.scanCancel(scanObject.id)
const scanShowObject = await auth.scanShow(scanObject.id);
expect(scanShowObject.exitCode).toEqual(0);
})
});