Skip to content

Commit

Permalink
Remove timeout from advanced security scan
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva committed Nov 15, 2023
1 parent 463e218 commit 0080e5d
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 169 deletions.
12 changes: 2 additions & 10 deletions src/main/scanLogic/scanRunners/analyzerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { IProxyConfig, JfrogClient } from 'jfrog-client-js';
import { ConnectionUtils } from '../../connect/connectionUtils';
import { Configuration } from '../../utils/configuration';
import { Translators } from '../../utils/translators';
import { RunUtils } from '../../utils/runUtils';

/**
* Analyzer manager is responsible for running the analyzer on the workspace.
Expand Down Expand Up @@ -93,20 +92,13 @@ export class AnalyzerManager {
return false;
}

public async runWithTimeout(checkCancel: () => void, args: string[], executionLogDirectory?: string): Promise<void> {
await AnalyzerManager.FINISH_UPDATE_PROMISE;
await RunUtils.runWithTimeout(AnalyzerManager.TIMEOUT_MILLISECS, checkCancel, {
title: this._binary.name,
task: this.run(args, executionLogDirectory)
});
}

/**
* Execute the cmd command to run the binary with given arguments
* @param args - the arguments for the command
* @param executionLogDirectory - the directory to save the execution log in
*/
private async run(args: string[], executionLogDirectory?: string): Promise<any> {
public async run(args: string[], executionLogDirectory?: string): Promise<any> {
await AnalyzerManager.FINISH_UPDATE_PROMISE;
let std: any = await this._binary.run(args, this.createEnvForRun(executionLogDirectory));
if (std.stdout && std.stdout.length > 0) {
this._logManager.logMessage('Done executing with log, log:\n' + std.stdout, 'DEBUG');
Expand Down
2 changes: 1 addition & 1 deletion src/main/scanLogic/scanRunners/applicabilityScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ApplicabilityRunner extends JasRunner {

/** @override */
protected async runBinary(yamlConfigPath: string, executionLogDirectory: string | undefined, checkCancel: () => void): Promise<void> {
await this.executeBinary(checkCancel, ['ca', yamlConfigPath], executionLogDirectory);
await this.runAnalyzerManager(checkCancel, ['ca', yamlConfigPath], executionLogDirectory);
}

/** @override */
Expand Down
2 changes: 1 addition & 1 deletion src/main/scanLogic/scanRunners/iacScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class IacRunner extends JasRunner {

/** @override */
protected async runBinary(yamlConfigPath: string, executionLogDirectory: string | undefined, checkCancel: () => void): Promise<void> {
await this.executeBinary(checkCancel, ['iac', yamlConfigPath], executionLogDirectory);
await this.runAnalyzerManager(checkCancel, ['iac', yamlConfigPath], executionLogDirectory);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/scanLogic/scanRunners/jasRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export abstract class JasRunner {
}

/**
* Execute the cmd command to run the binary with given arguments and an option to abort the operation.
* @param checkCancel - Check if should cancel
* Run Analyzer Manager with given arguments and an option to abort the operation.
* @param args - Arguments for the command
* @param executionLogDirectory - Directory to save the execution log in
*/
protected async executeBinary(checkCancel: () => void, args: string[], executionLogDirectory?: string): Promise<void> {
await this._analyzerManager.runWithTimeout(checkCancel, args, executionLogDirectory);
protected async runAnalyzerManager(checkCancel: () => void, args: string[], executionLogDirectory?: string): Promise<void> {
checkCancel();
await this._analyzerManager.run(args, executionLogDirectory);
}

protected logStartScanning(request: AnalyzeScanRequest): void {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scanLogic/scanRunners/sastScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SastRunner extends JasRunner {
checkCancel: () => void,
responsePath: string
): Promise<void> {
await this.executeBinary(checkCancel, ['zd', yamlConfigPath, responsePath], executionLogDirectory);
await this.runAnalyzerManager(checkCancel, ['zd', yamlConfigPath, responsePath], executionLogDirectory);
}

/** @override */
Expand Down
2 changes: 1 addition & 1 deletion src/main/scanLogic/scanRunners/secretsScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class SecretsRunner extends JasRunner {

/** @override */
protected async runBinary(yamlConfigPath: string, executionLogDirectory: string | undefined, checkCancel: () => void): Promise<void> {
await this.executeBinary(checkCancel, ['sec', yamlConfigPath], executionLogDirectory);
await this.runAnalyzerManager(checkCancel, ['sec', yamlConfigPath], executionLogDirectory);
}

/**
Expand Down
49 changes: 0 additions & 49 deletions src/main/utils/runUtils.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,4 @@
import { ScanTimeoutError } from './scanUtils';

export interface Task<T> {
title: string;
task: Promise<T>;
}

export class RunUtils {
// every 0.1 sec
private static readonly CHECK_INTERVAL_MILLISECS: number = 100;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when:
* 1. Any Promise is rejected.
* 2. Cancel was requested.
* 3. Timeout reached.
* @param timeout - time in millisecs until execution timeout
* @param checkCancel - check if cancel was requested
* @param tasks - the promises that the new promise will wrap
* @returns Promise that wrap the given promises
*/
static async runWithTimeout<T>(timeout: number, checkCancel: () => void, ...tasks: (Promise<T> | Task<T>)[]): Promise<T[]> {
let results: T[] = [];
const wrappedTasks: Promise<T>[] = <Promise<T>[]>tasks.map(async (task, index) => {
let result: T = <T>await Promise.race([
// Add task from argument
!(task instanceof Promise) && task.task ? task.task : task,
// Add task to check if cancel was requested from the user or reached timeout
this.checkCancelAndTimeoutTask(!(task instanceof Promise) && task.title ? task.title : '' + (index + 1), timeout, checkCancel)
]);
results.push(result);
});
await Promise.all(wrappedTasks);
return results;
}

/**
* Async task that checks if an abort signal was given.
* If the active task is <= 0 the task is completed
* @param tasksBundle - an object that holds the information about the active async tasks count and the abort signal for them
*/
private static async checkCancelAndTimeoutTask(title: string, timeout: number, checkCancel: () => void): Promise<void> {
let checkInterval: number = timeout < RunUtils.CHECK_INTERVAL_MILLISECS ? timeout : RunUtils.CHECK_INTERVAL_MILLISECS;
for (let elapsed: number = 0; elapsed < timeout; elapsed += checkInterval) {
checkCancel();
await this.delay(checkInterval);
}
throw new ScanTimeoutError(title, timeout);
}

/**
* Sleep and delay task for sleepIntervalMilliseconds
* @param sleepIntervalMilliseconds - the amount of time in milliseconds to wait
Expand Down
89 changes: 0 additions & 89 deletions src/test/tests/runUtils.test.ts

This file was deleted.

20 changes: 7 additions & 13 deletions src/test/tests/scanAnlayzerRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AnalyzeScanRequest, AnalyzerScanRun, ScanType } from '../../main/scanLo
import { JasRunner } from '../../main/scanLogic/scanRunners/jasRunner';
import { AppsConfigModule } from '../../main/utils/jfrogAppsConfig/jfrogAppsConfig';
import { RunUtils } from '../../main/utils/runUtils';
import { NotEntitledError, ScanCancellationError, ScanTimeoutError, ScanUtils } from '../../main/utils/scanUtils';
import { NotEntitledError, ScanCancellationError, ScanUtils } from '../../main/utils/scanUtils';
import { Translators } from '../../main/utils/translators';
import { AnalyzerManager } from '../../main/scanLogic/scanRunners/analyzerManager';

Expand Down Expand Up @@ -41,7 +41,6 @@ describe('Analyzer BinaryRunner tests', async () => {

function createDummyBinaryRunner(
connection: ConnectionManager = connectionManager,
timeout: number = AnalyzerManager.TIMEOUT_MILLISECS,
dummyAction: () => Promise<void> = () => Promise.resolve()
): JasRunner {
return new (class extends JasRunner {
Expand All @@ -57,14 +56,14 @@ describe('Analyzer BinaryRunner tests', async () => {
_executionLogDirectory: string | undefined,
checkCancel: () => void
): Promise<void> {
await RunUtils.runWithTimeout(timeout, checkCancel, dummyAction());
checkCancel();
await dummyAction();
}
})(connection, dummyName, logManager, new AppsConfigModule(''), {} as AnalyzerManager);
}

function createDummyAnalyzerManager(
connection: ConnectionManager = connectionManager,
timeout: number = AnalyzerManager.TIMEOUT_MILLISECS,
dummyAction: () => Promise<void> = () => Promise.resolve()
): AnalyzerManager {
return new (class extends AnalyzerManager {
Expand All @@ -80,7 +79,8 @@ describe('Analyzer BinaryRunner tests', async () => {
_executionLogDirectory: string | undefined,
checkCancel: () => void
): Promise<void> {
await RunUtils.runWithTimeout(timeout, checkCancel, dummyAction());
checkCancel();
await dummyAction();
}
})(connection, logManager);
}
Expand Down Expand Up @@ -215,13 +215,7 @@ describe('Analyzer BinaryRunner tests', async () => {
shouldAbort: true,
expectedErr: new ScanCancellationError()
},
{
name: 'Timeout',
timeout: 1,
createDummyResponse: true,
shouldAbort: false,
expectedErr: new ScanTimeoutError('' + 1, 1)
},

{
name: 'Response not created',
timeout: AnalyzerManager.TIMEOUT_MILLISECS,
Expand All @@ -237,7 +231,7 @@ describe('Analyzer BinaryRunner tests', async () => {
let requestPath: string = path.join(tempFolder, 'request');
let responsePath: string = path.join(tempFolder, 'response');

let runner: JasRunner = createDummyBinaryRunner(connectionManager, test.timeout, async () => {
let runner: JasRunner = createDummyBinaryRunner(connectionManager, async () => {
if (test.shouldAbort) {
throw new ScanCancellationError();
} else if (test.name === 'Not entitled') {
Expand Down

0 comments on commit 0080e5d

Please sign in to comment.