Skip to content

Commit 6a37da8

Browse files
author
Sumit Morchhale
committed
Added code changes for engine api list feature
1 parent 162c48d commit 6a37da8

File tree

5 files changed

+185
-1
lines changed

5 files changed

+185
-1
lines changed

src/main/engines/CxEngines.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// interface Api {
2+
// api_url: string;
3+
// api_name: string;
4+
// description: string;
5+
// }
6+
7+
// interface Engine {
8+
// engine_id: string;
9+
// engine_name: string;
10+
// apis: Api[];
11+
// }
12+
13+
// class EngineCollection {
14+
// engines: Engine[];
15+
16+
// constructor(data: any) {
17+
// this.engines = data.engines.map((engine: any) => ({
18+
// engine_id: engine.engine_id,
19+
// engine_name: engine.engine_name,
20+
// apis: engine.apis.map((api: any) => ({
21+
// api_url: api.api_url,
22+
// api_name: api.api_name,
23+
// description: api.description,
24+
// })),
25+
// }));
26+
// }
27+
// }
28+
29+
30+
// type Engine = {
31+
// engine_id: string;
32+
// engine_name: string;
33+
// apis: Array<{
34+
// api_url: string;
35+
// api_name: string;
36+
// description: string;
37+
// }>;
38+
// };
39+
40+
// export default class DataParser {
41+
// private rawData: any;
42+
43+
// constructor(rawData: any) {
44+
// this.rawData = data;
45+
// }
46+
47+
// public static getParsedData(rawData: any): Array<Engine> {
48+
// this.rawData=rawData
49+
// return this.rawData.engines.map((data: any) => ({
50+
// engine_id: engine.engine_id,
51+
// engine_name: engine.engine_name,
52+
// apis: engine.apis.map((api: any) => ({
53+
// api_url: api.api_url,
54+
// api_name: api.api_name,
55+
// description: api.description
56+
// }))
57+
// }));
58+
// }
59+
// }
60+
61+
62+
63+
64+
65+
66+
interface Api {
67+
api_url: string;
68+
api_name: string;
69+
description: string;
70+
}
71+
72+
interface Engine {
73+
engine_id: string;
74+
engine_name: string;
75+
apis: Api[];
76+
}
77+
78+
export default class EngineParser {
79+
public static parseEngine(json: any): Engine[] {
80+
if (json && Array.isArray(json.engines)) {
81+
return json.engines.map((engine: any) => ({
82+
engine_id: engine.engine_id,
83+
engine_name: engine.engine_name,
84+
apis: engine.apis.map((api: any) => ({
85+
api_url: api.api_url,
86+
api_name: api.api_name,
87+
description: api.description
88+
}))
89+
}));
90+
} else {
91+
throw new Error("Invalid JSON format");
92+
}
93+
}
94+
}
95+
96+
97+

src/main/wrapper/CxConstants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,8 @@ export enum CxConstants {
106106
STATE_CONFIRMED = "confirmed",
107107
CMD_LEARN_MORE = "learn-more",
108108
IDE_SCANS_KEY = " scan.config.plugins.ideScans",
109-
AI_GUIDED_REMEDIATION_KEY = " scan.config.plugins.aiGuidedRemediation"
109+
AI_GUIDED_REMEDIATION_KEY = " scan.config.plugins.aiGuidedRemediation",
110+
CMD_ENGINES = "engines",
111+
ENGINE_NAME="--engine-name",
112+
ENGINE_TYPE="EngineParser"
110113
}

src/main/wrapper/CxWrapper.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ export class CxWrapper {
177177
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PROJECT_TYPE);
178178
}
179179

180+
async engineApiList(filters: string): Promise<CxCommandOutput> {
181+
const validated_filters = this.filterEnginesName(filters);
182+
const commands: string[] = [CxConstants.CMD_ENGINES, "list-api","--output-format", "json"].concat(validated_filters);
183+
commands.push(...this.initializeCommands(false));
184+
const filterEngineCommand = commands.filter(item => item !== "--debug");
185+
console.log(filterEngineCommand)
186+
const exec = new ExecutionService();
187+
return await exec.executeCommands(this.config.pathToExecutable, filterEngineCommand, CxConstants.ENGINE_TYPE);
188+
}
189+
180190
async projectBranches(projectId: string, filters: string): Promise<CxCommandOutput> {
181191
// Verify and add possible branch filter by name
182192
const validated_filters = this.filterArguments(CxConstants.BRANCH_NAME + filters)
@@ -471,4 +481,25 @@ export class CxWrapper {
471481
}
472482
return r;
473483
}
484+
485+
filterEnginesName(filters: string): string[] {
486+
const r = [];
487+
if (filters.length > 0) {
488+
r.push(CxConstants.ENGINE_NAME);
489+
if(filters==="SAST")
490+
{
491+
r.push("SAST");
492+
}
493+
if(filters==="SCA")
494+
{
495+
r.push("SCA");
496+
}
497+
if(filters==="Iac")
498+
{
499+
r.push("Iac");
500+
}
501+
}
502+
503+
return r;
504+
}
474505
}

src/main/wrapper/ExecutionService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import CxScaRealTime from "../scaRealtime/CxScaRealTime";
2323
import CxChat from "../chat/CxChat";
2424
import CxMask from "../mask/CxMask";
2525
import CxAsca from "../asca/CxAsca";
26+
import EngineParser from "../engines/CxEngines";
2627

2728
let skipValue = false;
2829
const fileSourceFlag = "--file-source"
@@ -80,6 +81,7 @@ export class ExecutionService {
8081
});
8182
this.fsObject.on('exit',(code: number) => {
8283

84+
console.log("code",code)
8385
logger.info("Exit code received from AST-CLI: " + code);
8486
if(code==1){
8587
stderr = stdout
@@ -209,6 +211,10 @@ export class ExecutionService {
209211
const projects = CxProject.parseProject(resultObject);
210212
cxCommandOutput.payload = projects;
211213
break;
214+
case CxConstants.ENGINE_TYPE:
215+
const engines = EngineParser.parseEngine(resultObject);
216+
cxCommandOutput.payload = engines
217+
break;
212218
case CxConstants.CODE_BASHING_TYPE:
213219
const codeBashing = CxCodeBashing.parseCodeBashing(resultObject);
214220
cxCommandOutput.payload = codeBashing;

src/tests/EnginesTest.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {CxWrapper} from '../main/wrapper/CxWrapper';
2+
import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
3+
import {BaseTest} from "./BaseTest";
4+
5+
describe("EngineAPIList cases",() => {
6+
const cxScanConfig = new BaseTest();
7+
it('EngineAPIList Successful case1', async () => {
8+
const auth = new CxWrapper(cxScanConfig);
9+
const data = await auth.engineApiList("SAST");
10+
const cxCommandOutput: CxCommandOutput = data;
11+
console.log(cxCommandOutput)
12+
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
13+
});
14+
15+
it('EngineAPIList Successful case2', async () => {
16+
const auth = new CxWrapper(cxScanConfig);
17+
const data = await auth.engineApiList("SCA");
18+
const cxCommandOutput: CxCommandOutput = data;
19+
console.log(cxCommandOutput)
20+
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
21+
});
22+
23+
it('EngineAPIList Successful case3', async () => {
24+
const auth = new CxWrapper(cxScanConfig);
25+
const data = await auth.engineApiList("Iac");
26+
const cxCommandOutput: CxCommandOutput = data;
27+
console.log(cxCommandOutput)
28+
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
29+
});
30+
31+
it('EngineAPIList Successful case4', async () => {
32+
const auth = new CxWrapper(cxScanConfig);
33+
const data = await auth.engineApiList("");
34+
const cxCommandOutput: CxCommandOutput = data;
35+
console.log(cxCommandOutput)
36+
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
37+
});
38+
39+
it('EngineAPIList Successful case5', async () => {
40+
const auth = new CxWrapper(cxScanConfig);
41+
const data = await auth.engineApiList("xyz");
42+
const cxCommandOutput: CxCommandOutput = data;
43+
console.log(cxCommandOutput)
44+
expect(cxCommandOutput.exitCode).toBe(1);
45+
});
46+
47+
});

0 commit comments

Comments
 (0)