Skip to content

Commit 19e9280

Browse files
committed
separating logger configurations
1 parent a4a8f67 commit 19e9280

File tree

3 files changed

+66
-65
lines changed

3 files changed

+66
-65
lines changed

src/main/wrapper/CxWrapper.ts

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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";
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";
66
import { getLoggerWithFilePath, logger } from "./loggerConfig";
77
import * as fs from "fs"
88
import * as os from "os";
@@ -15,10 +15,10 @@ export class CxWrapper {
1515

1616
constructor(cxScanConfig: CxConfig, logFilePath?: string) {
1717
let path = require("path");
18-
18+
1919
getLoggerWithFilePath(logFilePath)
20-
21-
if (cxScanConfig.clientId && cxScanConfig.clientSecret) {
20+
21+
if (cxScanConfig.clientId && cxScanConfig.clientSecret) {
2222
logger.info("Received clientId and clientSecret");
2323
this.config.clientId = cxScanConfig.clientId;
2424
this.config.clientSecret = cxScanConfig.clientSecret;
@@ -119,7 +119,7 @@ export class CxWrapper {
119119
const exec = new ExecutionService();
120120
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
121121
}
122-
122+
123123
async scanCancel(id: string): Promise<CxCommandOutput> {
124124
const commands: string[] = [CxConstants.CMD_SCAN, CxConstants.SUB_CMD_CANCEL, CxConstants.SCAN_ID, id];
125125
commands.push(...this.initializeCommands(false));
@@ -134,15 +134,15 @@ export class CxWrapper {
134134
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_TYPE);
135135
}
136136

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

145-
async projectList(filters:string): Promise<CxCommandOutput> {
145+
async projectList(filters: string): Promise<CxCommandOutput> {
146146
const validated_filters = this.filterArguments(filters);
147147
const commands: string[] = [CxConstants.CMD_PROJECT, "list"].concat(validated_filters);
148148
commands.push(...this.initializeCommands(true));
@@ -153,32 +153,32 @@ export class CxWrapper {
153153
async projectBranches(projectId: string, filters: string): Promise<CxCommandOutput> {
154154
// Verify and add possible branch filter by name
155155
const validated_filters = this.filterArguments(CxConstants.BRANCH_NAME + filters)
156-
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);
157157
commands.push(...this.initializeCommands(false));
158158
const exec = new ExecutionService();
159159
return await exec.executeCommands(this.config.pathToExecutable, commands);
160160
}
161161

162162
async projectShow(projectId: string): Promise<CxCommandOutput> {
163-
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];
164164
commands.push(...this.initializeCommands(true));
165165
const exec = new ExecutionService();
166166
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PROJECT_TYPE);
167167
}
168168

169-
async triageShow(projectId: string, similarityId: string, scanType: string ): Promise<CxCommandOutput> {
169+
async triageShow(projectId: string, similarityId: string, scanType: string): Promise<CxCommandOutput> {
170170
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_SHOW, CxConstants.PROJECT_ID, projectId, CxConstants.SIMILARITY_ID, similarityId, CxConstants.SCAN_TYPES_SUB_CMD, scanType];
171171
commands.push(...this.initializeCommands(true));
172172
const exec = new ExecutionService();
173173
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PREDICATE_TYPE);
174-
}
175-
174+
}
175+
176176
async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string): Promise<CxCommandOutput> {
177177
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];
178178
commands.push(...this.initializeCommands(false));
179179
const exec = new ExecutionService();
180180
return await exec.executeCommands(this.config.pathToExecutable, commands);
181-
}
181+
}
182182

183183
async getResultsList(scanId: string) {
184184
const exec = new ExecutionService();
@@ -187,7 +187,7 @@ export class CxWrapper {
187187
// Executes the command and creates a result file
188188
await exec.executeResultsCommands(this.config.pathToExecutable, commands)
189189
// Reads the result file and retrieves the results
190-
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);
191191
}
192192

193193
async getResultsSummary(scanId: string): Promise<CxCommandOutput> {
@@ -197,24 +197,24 @@ export class CxWrapper {
197197
// Executes the command and creates a result file
198198
await exec.executeResultsCommands(this.config.pathToExecutable, commands);
199199
// Reads the result file and retrieves the results
200-
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);
201201
}
202202

203-
async getResults(scanId: string, resultType:string, outputFileName: string, outputFilePath: string) {
203+
async getResults(scanId: string, resultType: string, outputFileName: string, outputFilePath: string) {
204204
const commands = this.resultsShow(scanId, resultType, outputFileName, outputFilePath)
205205
const exec = new ExecutionService();
206206
return await exec.executeCommands(this.config.pathToExecutable, commands);
207207
}
208208

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];
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];
211211
commands.push(...this.initializeCommands(true));
212212
const exec = new ExecutionService();
213-
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);
214214
}
215215

216216
resultsShow(scanId: string, reportFormat: string, outputFileName: string, outputPath: string): string[] {
217-
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];
218218
if (outputFileName) {
219219
commands.push(CxConstants.OUTPUT_NAME);
220220
commands.push(outputFileName);
@@ -243,8 +243,7 @@ export class CxWrapper {
243243
for (const bflNode of bflNodes) {
244244
for (const resultNode of resultNodes) {
245245

246-
if(this.compareNodes(bflNode,resultNode))
247-
{
246+
if (this.compareNodes(bflNode, resultNode)) {
248247
return resultNodes.indexOf(resultNode);
249248
}
250249
}
@@ -254,22 +253,22 @@ export class CxWrapper {
254253

255254
}
256255

257-
compareNodes(bflNode: CxBFL, resultNode : any): boolean{
256+
compareNodes(bflNode: CxBFL, resultNode: any): boolean {
258257

259258
return bflNode.line == resultNode.line &&
260-
bflNode.column == resultNode.column &&
261-
bflNode.length == resultNode.length &&
262-
bflNode.name == resultNode.name &&
263-
bflNode.method == resultNode.method &&
264-
bflNode.domType == resultNode.domType &&
265-
bflNode.fileName == resultNode.fileName &&
266-
bflNode.fullName == resultNode.fullName &&
267-
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;
268267
}
269268

270-
filterArguments(filters:string):string[]{
269+
filterArguments(filters: string): string[] {
271270
let r = [];
272-
if(filters.length>0){
271+
if (filters.length > 0) {
273272
r.push(CxConstants.FILTER);
274273
r.push(filters);
275274
}

src/main/wrapper/loggerConfig.ts

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

3-
export function getLoggerWithFilePath(logFilePath ?: string) {
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+
}
414

5-
if(logFilePath) {
6-
configure({
7-
appenders: {
8-
file: { type: 'fileSync', filename: logFilePath ? logFilePath : "" },
9-
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
10-
},
11-
categories: {
12-
default: { appenders: ['console', 'file'], level: "info" }
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+
}
1525

16-
} else {
17-
configure({
18-
appenders: {
19-
console: { type: 'stdout', layout: { type: "messagePassThrough" } },
20-
},
21-
categories: {
22-
default: { appenders: ['console'], level: "info" }
23-
}
24-
});
25-
}
26-
}
26+
export function getLoggerWithFilePath(logFilePath?: string) {
27+
logFilePath ? configurationWithFile(logFilePath) : configurationWithoutFile()
28+
}
2729

2830
export const logger = getLogger();

src/tests/ScanTest.test.ts

Lines changed: 6 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
})

0 commit comments

Comments
 (0)