diff --git a/src/main/wrapper/CxConstants.ts b/src/main/wrapper/CxConstants.ts index 7c97c64e..487723ff 100644 --- a/src/main/wrapper/CxConstants.ts +++ b/src/main/wrapper/CxConstants.ts @@ -107,8 +107,8 @@ export enum CxConstants { SEVERITY_MEDIUM = "medium", STATE_CONFIRMED = "confirmed", CMD_LEARN_MORE = "learn-more", - IDE_SCANS_KEY = " scan.config.plugins.ideScans", - AI_GUIDED_REMEDIATION_KEY = " scan.config.plugins.aiGuidedRemediation", + IDE_SCANS_KEY = "scan.config.plugins.ideScans", + AI_GUIDED_REMEDIATION_KEY = "scan.config.plugins.aiGuidedRemediation", AI_MCP_SERVER_KEY = "scan.config.plugins.aiMcpServer" } diff --git a/src/main/wrapper/CxWrapper.ts b/src/main/wrapper/CxWrapper.ts index eb1ecdf8..b8c645b2 100644 --- a/src/main/wrapper/CxWrapper.ts +++ b/src/main/wrapper/CxWrapper.ts @@ -8,6 +8,7 @@ import * as fs from "fs" import * as os from "os"; import CxBFL from "../bfl/CxBFL"; import path = require('path'); +import {getTrimmedMapValue} from "./utils"; type ParamTypeMap = Map; @@ -56,6 +57,7 @@ export class CxWrapper { } } + initializeCommands(formatRequired: boolean): string[] { const list: string[] = []; if (this.config.clientId) { @@ -353,30 +355,38 @@ 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"; - } + async ideScansEnabled(): Promise { + const commands: string[] = [CxConstants.CMD_UTILS, CxConstants.SUB_CMD_TENANT]; + commands.push(...this.initializeCommands(false)); - async guidedRemediationEnabled() : 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.AI_GUIDED_REMEDIATION_KEY) && output.get(CxConstants.AI_GUIDED_REMEDIATION_KEY).toLowerCase() === " true"; - } + const exec = new ExecutionService(); + const output = await exec.executeMapTenantOutputCommands(this.config.pathToExecutable, commands); + const value = getTrimmedMapValue(output, CxConstants.IDE_SCANS_KEY); + return value?.toLowerCase() === "true"; +} - async aiMcpServerEnabled(): Promise { + async guidedRemediationEnabled(): 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.AI_MCP_SERVER_KEY) && - output.get(CxConstants.AI_MCP_SERVER_KEY).toLowerCase() === "true"; + + const value = getTrimmedMapValue(output, CxConstants.AI_GUIDED_REMEDIATION_KEY); + return value?.toLowerCase() === "true"; +} + + + async aiMcpServerEnabled(): 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); + + const value = getTrimmedMapValue(output, CxConstants.AI_MCP_SERVER_KEY); + return value?.toLowerCase() === "true"; } async kicsChat(apikey: string, file: string, line: number, severity: string, vulnerability: string, input: string, conversationId?: string, model?: string): Promise { diff --git a/src/main/wrapper/utils.ts b/src/main/wrapper/utils.ts new file mode 100644 index 00000000..2f3b1712 --- /dev/null +++ b/src/main/wrapper/utils.ts @@ -0,0 +1,11 @@ +export function getTrimmedMapValue(map: Map, targetKey: string): string | undefined { + const entries = Array.from(map.entries()); + + for (const [key, value] of entries) { + if (key.trim() === targetKey) { + return value.trim(); + } + } + + return undefined; +} \ No newline at end of file diff --git a/src/tests/ScanTest.test.ts b/src/tests/ScanTest.test.ts index 646d0eb9..6c6f8f51 100644 --- a/src/tests/ScanTest.test.ts +++ b/src/tests/ScanTest.test.ts @@ -125,6 +125,13 @@ describe("ScanCreate cases", () => { expect(aiEnabled).toBeDefined(); }) + it("Should check if AI MCP server is active", async () => { + const cxScanConfig = new BaseTest(); + const auth = new CxWrapper(cxScanConfig); + const aiMcpEnabled: boolean = await auth.aiMcpServerEnabled(); + expect(typeof aiMcpEnabled).toBe("boolean"); +}); + it('ScanAsca fail case Without extensions', async () => { const auth = new CxWrapper(cxScanConfig); const cxCommandOutput: CxCommandOutput = await auth.scanAsca("tsc/tests/data/python-file");