Skip to content

Commit

Permalink
AST-38524 Vorpal extension (#881)
Browse files Browse the repository at this point in the history
* first working code

* WIP: 10ba35b docs(changelog): update release notes

* settings

* tests

* Merge branch 'vorpal-engine-extention' of https://github.com/Checkmarx/ast-vscode-extension into vorpal-engine-extention

* delete problems when disabled vorpal

* test

* tests

* tests

* test

* test

* ignore log files

* ignore settings.json windows

* tests

* fix tests

* tests

* onDidChangeActiveTextEditor

* tests

* log settings

* fix tests

* tests

* fixes

* fix

* Update launch.json

* Update package.json

* ignore system files

* tests

* test name

* try

* revert

* Update launch.json

* try

* fix

* remove test

* order

* fix mock

* vorpal tests

* try import again

* clear tests

* fix test mock

* setting name without space

* timeout

* fix the test

* beezrat hashem

* vorpal test

* settings test

* add test

* test

* test cases

* code review

* await

* latest wrapper and settings descreption

* formmater

* change log.err to log.warn in scan vorpal

* support Critical severities

* Update extension.ts

* Delete src/test/9.vorpal.test.ts

* code review

---------

Co-authored-by: AlvoBen <[email protected]>
  • Loading branch information
tamarleviCm and AlvoBen authored Aug 15, 2024
1 parent 539bb51 commit 8005f82
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 26 deletions.
27 changes: 15 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@
{
"title": "Checkmarx One",
"id": "ast-results",
"order": 3,
"order": 4,
"properties": {
"checkmarxOne.apiKey": {
"type": "string",
Expand Down Expand Up @@ -825,7 +825,7 @@
{
"title": "Checkmarx AI Security Champion",
"id": "ask-kics",
"order": 2,
"order": 3,
"properties": {
"CheckmarxSecurityChampion.key": {
"type": "string",
Expand All @@ -849,6 +849,19 @@
]
}
}
},
{
"title": "Activate Vorpal Auto Scanning",
"id": "vorpal",
"order": 2,
"properties": {
"CheckmarxVorpal.ActivateVorpalAutoScanning": {
"type": "boolean",
"order": 3,
"default": false,
"markdownDescription": "Scans your file as you code"
}
}
}
]
},
Expand All @@ -861,7 +874,7 @@
"copytestproject": "copyfiles -u 2 \"src/resources/**/*\" out/test/ -E",
"copymedia": "copyfiles \"media/icons/*\" out/ -E",
"configure-husky": "npx husky install && npx husky add .husky/pre-commit \"npx --no-install lint-staged\"",
"test": "export TEST=true && npm run compile && extest setup-and-run './out/test/**/*test.js' -c 1.71.0 -i -r .",
"test": "export TEST=true && npm run compile && extest setup-and-run './out/test/**/*test.js' -c 1.87.2 -i -r .",
"win-test": "set TEST=true&& npm run compile && extest setup-and-run './out/test/**/*test.js' -c 1.87.2 -i -r ."
},
"devDependencies": {
Expand All @@ -883,6 +896,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {

"@checkmarxdev/ast-cli-javascript-wrapper": "0.0.111",
"copyfiles": "2.4.1",
"eslint-config-prettier": "^9.1.0",
Expand Down
80 changes: 80 additions & 0 deletions src/commands/vorpalCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as vscode from "vscode";
import { Logs } from "../models/logs";
import {
clearVorpalProblems,
installVorpal,
scanVorpal,
} from "../vorpal/vorpalService";
import { constants } from "../utils/common/constants";

let timeout = null;
export class VorpalCommand {
context: vscode.ExtensionContext;
logs: Logs;
onDidChangeTextDocument: vscode.Disposable;
constructor(context: vscode.ExtensionContext, logs: Logs) {
this.context = context;
this.logs = logs;
}
public async registerVorpal() {
try {
const vorpalActive = vscode.workspace
.getConfiguration(constants.CheckmarxVorpal)
.get(constants.ActivateVorpalAutoScanning) as boolean;
if (vorpalActive) {
await this.installVorpal();
await this.registerVorpalScanOnChangeText();
this.logs.info(constants.vorpalStart);
} else {
await this.disposeVorpalScanOnChangeText();
await clearVorpalProblems();
this.logs.info(constants.vorpalDisabled);
}
} catch (error) {
console.error(error);
}
}
public installVorpal() {
installVorpal(this.logs);
this.onDidChangeTextDocument = vscode.workspace.onDidChangeTextDocument(
// Must be no less than 2000ms. Otherwise, the temporary file can be deleted before the vorpal scan is finished.
this.debounce(this.onTextChange, 2000)
);
}

public onTextChange(event) {
try {
scanVorpal(event.document, this.logs);
} catch (error) {
console.error(error);
this.logs.warn("fail to scan vorpal");
}
}
// Debounce function
public debounce(func, wait) {
const context = this;
console.log("onDidChangeTextDocument");
return function (...args) {
try {
const later = () => {
clearTimeout(timeout);
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
} catch (error) {
console.error(error);
}
};
}

public registerVorpalScanOnChangeText() {
this.context.subscriptions.push(this.onDidChangeTextDocument);
}
public disposeVorpalScanOnChangeText() {
if (this.onDidChangeTextDocument) {
this.onDidChangeTextDocument.dispose();
this.context.subscriptions.push(this.onDidChangeTextDocument);
}
}
}
42 changes: 38 additions & 4 deletions src/cx/cx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import CxScan from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/scan/CxSc
import CxProject from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/project/CxProject";
import CxCodeBashing from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/codebashing/CxCodeBashing";
import { CxConfig } from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/wrapper/CxConfig";
import {
constants
} from "../utils/common/constants";
import { constants } from "../utils/common/constants";
import { getFilePath, getResultsFilePath } from "../utils/utils";
import { SastNode } from "../models/sastNode";
import AstError from "../exceptions/AstError";
Expand All @@ -17,6 +15,7 @@ import { CxPlatform } from "./cxPlatform";
import { CxCommandOutput } from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/wrapper/CxCommandOutput";
import { ChildProcessWithoutNullStreams } from "child_process";
import CxLearnMoreDescriptions from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/learnmore/CxLearnMoreDescriptions";
import CxVorpal from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/vorpal/CxVorpal";
import { messages } from "../utils/common/messages";
export class Cx implements CxPlatform {
async scaScanCreate(sourcePath: string): Promise<CxScaRealtime | undefined> {
Expand Down Expand Up @@ -425,4 +424,39 @@ export class Cx implements CxPlatform {
statusBarItem.text = text;
show ? statusBarItem.show() : statusBarItem.hide();
}
}
async installVorpal(): Promise<CxVorpal> {
let config = this.getAstConfiguration();
if (!config) {
config = new CxConfig();
}
const cx = new CxWrapper(config);
const scans = await cx.scanVorpal(null, true, constants.vsCodeAgent);
if (scans.payload && scans.exitCode === 0) {
return scans.payload[0];
} else {
return this.getVorpalError(scans.status, "Failed to run vorpal engine");
}
}

private getVorpalError(scanStatus: string, errorMessage: string) {
console.error(errorMessage);
const errorRes = new CxVorpal();
errorRes.error = scanStatus;
return errorRes;
}

async scanVorpal(sourcePath: string): Promise<CxVorpal> {
let config = this.getAstConfiguration();
if (!config) {
config = new CxConfig();
}
const cx = new CxWrapper(config);
const scans = await cx.scanVorpal(sourcePath, false, constants.vsCodeAgent);
if (scans.payload && scans.exitCode === 0) {
return scans.payload[0];
} else {
return this.getVorpalError(scans.status, "Fail to call vorpal scan");
}
}
}

13 changes: 12 additions & 1 deletion src/cx/cxMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { CxConfig } from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/wra
import { getFilePath } from "../utils/utils";
import { writeFileSync } from "fs";
import { CxPlatform } from "./cxPlatform";
import CxVorpal from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/vorpal/CxVorpal";
import { EMPTY_RESULTS_SCAN_ID } from "../test/utils/envs";

export class CxMock implements CxPlatform {


// eslint-disable-next-line @typescript-eslint/no-explicit-any
async scaScanCreate(): Promise<CxScaRealtime[] | any> {
return [
Expand Down Expand Up @@ -552,7 +555,7 @@ export class CxMock implements CxPlatform {
await this.sleep(1000);
return [{ conversationId: '0', response: ["Mock message response from gpt"] }];
}

sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Expand All @@ -561,5 +564,13 @@ export class CxMock implements CxPlatform {
statusBarItem.text = text;
show ? statusBarItem.show() : statusBarItem.hide();
}

installVorpal(): Promise<CxVorpal> {
return null;
}

async scanVorpal(sourcePath: string): Promise<CxVorpal> {
return new CxVorpal();
}
}

12 changes: 12 additions & 0 deletions src/cx/cxPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Logs } from "../models/logs";
import { ChildProcessWithoutNullStreams } from "child_process";
import { CxCommandOutput } from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/wrapper/CxCommandOutput";
import CxLearnMoreDescriptions from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/learnmore/CxLearnMoreDescriptions";
import CxVorpal from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/vorpal/CxVorpal";

export interface CxPlatform {
/**
Expand Down Expand Up @@ -176,5 +177,16 @@ export interface CxPlatform {
* @param statusBarItem The {@link vscode.StatusBarItem} associated with the results.
*/
updateStatusBarItem(text: string, show: boolean, statusBarItem: vscode.StatusBarItem);

/**
* install the Vorpal engine
*/
installVorpal(): Promise<CxVorpal>;

/**
* Scan the edited file in the vorpal engine and show the results in the problem section
* @param sourcePath the edited file sent to the vorpal engine
*/
scanVorpal(sourcePath: string): Promise<CxVorpal>;
}

5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { WorkspaceListener } from "./utils/listener/workspaceListener";
import { DocAndFeedbackView } from "./views/docsAndFeedbackView/docAndFeedbackView";
import { messages } from "./utils/common/messages";
import { commands } from "./utils/common/commands";
import { VorpalCommand } from "./commands/vorpalCommand";

export async function activate(context: vscode.ExtensionContext) {
// Create logs channel and make it visible
Expand Down Expand Up @@ -175,6 +176,8 @@ export async function activate(context: vscode.ExtensionContext) {
}
}
});
const vorpalCommand = new VorpalCommand(context, logs);
vorpalCommand.registerVorpal();
// Register Settings
const commonCommand = new CommonCommand(context, logs);
commonCommand.registerSettings();
Expand All @@ -186,7 +189,7 @@ export async function activate(context: vscode.ExtensionContext) {
// SCA auto scanning enablement
await commonCommand.executeCheckScaScanEnabled();
// execute command to listen to settings change
await executeCheckSettingsChange(kicsStatusBarItem, logs);
await executeCheckSettingsChange(kicsStatusBarItem, logs, vorpalCommand);

const treeCommand = new TreeCommand(
context,
Expand Down
2 changes: 1 addition & 1 deletion src/test/0.welcome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("Welcome view test", () => {
let bench: Workbench;

before(async function () {
this.timeout(8000);
this.timeout(100000);
bench = new Workbench();
});

Expand Down
Loading

0 comments on commit 8005f82

Please sign in to comment.