diff --git a/src/main/wrapper/CxConfig.ts b/src/main/wrapper/CxConfig.ts index d65c784e..1a01118f 100644 --- a/src/main/wrapper/CxConfig.ts +++ b/src/main/wrapper/CxConfig.ts @@ -6,4 +6,5 @@ export class CxConfig { clientSecret: string; apiKey: string; tenant: string; + additionalParameters:string; } diff --git a/src/main/wrapper/CxConstants.ts b/src/main/wrapper/CxConstants.ts index 3ab1e1e2..933040ef 100644 --- a/src/main/wrapper/CxConstants.ts +++ b/src/main/wrapper/CxConstants.ts @@ -19,6 +19,7 @@ export enum CxConstants { CMD_REMEDIATION = "remediation", SUB_CMD_REMEDIATION_KICS = "kics", SUB_CMD_REMEDIATION_SCA = "sca", + SUB_CMD_TENANT = "tenant", KICS_REMEDIATION_RESULTS_FILE = "--results-file", KICS_REMEDIATION_KICS_FILE = "--kics-files", KICS_REMEDIATION_SIMILARITY_IDS = "--similarity-ids", @@ -76,5 +77,6 @@ export enum CxConstants { SEVERITY_HIGH = "high", SEVERITY_MEDIUM = "medium", STATE_CONFIRMED = "confirmed", - CMD_LEARN_MORE = "learn-more" + CMD_LEARN_MORE = "learn-more", + IDE_SCANS_KEY = " scan.config.plugins.ideScans" } diff --git a/src/main/wrapper/CxWrapper.ts b/src/main/wrapper/CxWrapper.ts index a99605aa..df3d71de 100644 --- a/src/main/wrapper/CxWrapper.ts +++ b/src/main/wrapper/CxWrapper.ts @@ -17,13 +17,13 @@ export class CxWrapper { constructor(cxScanConfig: CxConfig, logFilePath?: string) { getLoggerWithFilePath(logFilePath) - - if (cxScanConfig.clientId && cxScanConfig.clientSecret) { + if (cxScanConfig.apiKey) { + this.config.apiKey = cxScanConfig.apiKey; + } + else if (cxScanConfig.clientId && cxScanConfig.clientSecret) { logger.info("Received clientId and clientSecret"); this.config.clientId = cxScanConfig.clientId; this.config.clientSecret = cxScanConfig.clientSecret; - } else if (cxScanConfig.apiKey) { - this.config.apiKey = cxScanConfig.apiKey; } else { logger.info("Did not receive ClientId/Secret or ApiKey from cli arguments"); } @@ -51,6 +51,9 @@ export class CxWrapper { if (cxScanConfig.tenant) { this.config.tenant = cxScanConfig.tenant; } + if (cxScanConfig.additionalParameters) { + this.config.additionalParameters = cxScanConfig.additionalParameters; + } } initializeCommands(formatRequired: boolean): string[] { @@ -79,6 +82,12 @@ export class CxWrapper { list.push(CxConstants.TENANT); list.push(this.config.tenant); } + if(this.config.additionalParameters){ + // this.config.additionalParameters.forEach(function (param){ + // list.push(param) + // }) + list.push(this.config.additionalParameters) + } if (formatRequired) { list.push(CxConstants.FORMAT); list.push(CxConstants.FORMAT_JSON); @@ -275,6 +284,14 @@ export class CxWrapper { return exec.executeCommands(this.config.pathToExecutable, commands); } + async ideScansEnabled() : Promise { + const commands: string[] = [CxConstants.CMD_UTILS, CxConstants.SUB_CMD_TENANT]; + commands.push(...this.initializeCommands(false)); + const exec = new ExecutionService(); + const output = await exec.executeMapTenantOutputCommands(this.config.pathToExecutable, commands); + return output.has(CxConstants.IDE_SCANS_KEY) && output.get(CxConstants.IDE_SCANS_KEY).toLowerCase() === " true"; + } + getIndexOfBflNode(bflNodes: CxBFL[], resultNodes: any[]): number { diff --git a/src/main/wrapper/ExecutionService.ts b/src/main/wrapper/ExecutionService.ts index f1191111..88e6f394 100644 --- a/src/main/wrapper/ExecutionService.ts +++ b/src/main/wrapper/ExecutionService.ts @@ -21,8 +21,6 @@ import CxPackageData from "../results/CxPackageData"; import CxKicsRemediation from "../remediation/CxKicsRemediation"; - - function isJsonString(s: string) { try { const stringObject = s.split('\n')[0]; @@ -118,6 +116,54 @@ export class ExecutionService { }), this.fsObject]; } + executeMapTenantOutputCommands(pathToExecutable: string, commands: string[]): Promise> { + return (new Promise( (resolve, reject)=> { + let stderr = ""; + let stdout =""; + + this.fsObject = spawner.spawn(pathToExecutable, transformation(commands)); + this.fsObject.on('error', (data: { toString: () => string; }) => { + if (data) { + logger.error(data.toString().replace('\n', '')); + stderr += data.toString(); + } + reject() + }); + this.fsObject.on('exit',(code: number) => { + logger.info("Exit code received from AST-CLI: " + code); + if(code==1){ + stderr = stdout + } + resolve(ExecutionService.onCloseMapTenantOutputCommand(code, stderr, stdout)); + }); + this.fsObject.stdout.on('data', (data: { toString: () => string; }) => { + if (data) { + logger.info(data.toString().replace('\n', '')); + stdout += data.toString(); + } + }); + this.fsObject.stderr.on('data', (data: { toString: () => string; }) => { + if (data) { + logger.error(data.toString().replace('\n', '')); + stderr += data.toString(); + } + }); + })); + } + + private static onCloseMapTenantOutputCommand(code: number, stderr: string, stdout: string): Map { + const result = new Map(); + if (code == 0) { + const tenantSettingsList = stdout.split('\n'); + tenantSettingsList.forEach(tenantSetting => { + tenantSetting.includes('Key') ? result.set(tenantSetting.split(':')[1],tenantSettingsList[tenantSettingsList.indexOf(tenantSetting) +1].split(':')[1]) : null; + }); + } else { + logger.error("Error occurred while executing command: " + stderr); + } + return result; + } + private static onCloseCommand(code: number, stderr: string, stdout: string, output: string) : CxCommandOutput { const cxCommandOutput = new CxCommandOutput(); cxCommandOutput.exitCode = code; diff --git a/src/tests/BaseTest.ts b/src/tests/BaseTest.ts index d5392ea0..46fa0eb0 100644 --- a/src/tests/BaseTest.ts +++ b/src/tests/BaseTest.ts @@ -7,6 +7,7 @@ export class BaseTest { scanId: string; pathToExecutable: string; tenant: string; + additionalParameters:string; constructor() { this.baseUri = process.env["CX_BASE_URI"]; @@ -16,6 +17,7 @@ export class BaseTest { this.tenant = process.env["CX_TENANT"]; this.apiKey = process.env["CX_APIKEY"]; this.scanId = process.env["CX_SCANID"]; + this.additionalParameters = "--debug" if (process.env["PATH_TO_EXECUTABLE"] !== null && process.env["PATH_TO_EXECUTABLE"] !== undefined) { this.pathToExecutable = process.env["PATH_TO_EXECUTABLE"]; } diff --git a/src/tests/ScanTest.test.ts b/src/tests/ScanTest.test.ts index 3eccfa0a..01c5b5ab 100644 --- a/src/tests/ScanTest.test.ts +++ b/src/tests/ScanTest.test.ts @@ -99,4 +99,10 @@ describe("ScanCreate cases", () => { expect(scanObject.results.length).toBeGreaterThan(0); expect(pid).toBeDefined(); }) + + it("Should check if scan create is possible", async() => { + const auth = new CxWrapper(cxScanConfig); + const tenantSettings: boolean = await auth.ideScansEnabled(); + expect(tenantSettings).toBeDefined(); + }) }); \ No newline at end of file