-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only run advance security scanners on supported tech (#347)
- Loading branch information
Showing
5 changed files
with
150 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as vscode from 'vscode'; | ||
import { IUsageFeature } from 'jfrog-client-js'; | ||
import { PackageType } from '../types/projectType'; | ||
import { SupportedScans } from '../scanLogic/scanManager'; | ||
import { ConnectionManager } from '../connect/connectionManager'; | ||
import { ApplicabilityRunner } from '../scanLogic/scanRunners/applicabilityScan'; | ||
|
||
export class UsageUtils { | ||
private static getUsageFeaturesByExistTech( | ||
projectDescriptors: Map<PackageType, vscode.Uri[]>, | ||
scanSuffix: string, | ||
onlyInclude?: PackageType[] | ||
): IUsageFeature[] { | ||
let features: IUsageFeature[] = []; | ||
for (const [techEnum, descriptors] of projectDescriptors.entries()) { | ||
// If we only support subset of the techs, check if tech is supported. | ||
if (onlyInclude && onlyInclude.length > 0 && !onlyInclude.includes(techEnum)) { | ||
continue; | ||
} | ||
// Only add to usage if found descriptors for tech. | ||
if (!!descriptors && descriptors.length > 0) { | ||
const featureName: string = PackageType[techEnum].toLowerCase() + '-' + scanSuffix; | ||
features.push({ featureId: featureName }); | ||
} | ||
} | ||
return features; | ||
} | ||
|
||
/** | ||
* Sends usage report for all techs we found project descriptors of and for each advance scan that was preformed. | ||
* @param supportedScans - the entitlement for each scan | ||
* @param projectDescriptors - map of all project descriptors by their tech. | ||
* @param connectionManager - manager containing Artifactory details if configured. | ||
*/ | ||
public static async sendUsageReport( | ||
supportedScans: SupportedScans, | ||
projectDescriptors: Map<PackageType, vscode.Uri[]>, | ||
connectionManager: ConnectionManager | ||
) { | ||
let features: IUsageFeature[] = []; | ||
if (supportedScans.dependencies) { | ||
features.push(...this.getUsageFeaturesByExistTech(projectDescriptors, 'deps')); | ||
} | ||
if (supportedScans.applicability) { | ||
features.push(...this.getUsageFeaturesByExistTech(projectDescriptors, 'contextual', ApplicabilityRunner.supportedPackageTypes())); | ||
} | ||
if (supportedScans.iac) { | ||
features.push({ featureId: 'iac' }); | ||
} | ||
if (supportedScans.secrets) { | ||
features.push({ featureId: 'secrets' }); | ||
} | ||
if (features.length === 0) { | ||
return; | ||
} | ||
await connectionManager.sendUsageReport(features); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { IUsageFeature } from 'jfrog-client-js'; | ||
import { ConnectionManager } from '../../main/connect/connectionManager'; | ||
import { assert } from 'chai'; | ||
import { LogManager } from '../../main/log/logManager'; | ||
import { SupportedScans } from '../../main/scanLogic/scanManager'; | ||
import { PackageType } from '../../main/types/projectType'; | ||
import { UsageUtils } from '../../main/utils/usageUtils'; | ||
import { Uri } from 'vscode'; | ||
|
||
describe('Usage Utils Tests', async () => { | ||
const logManager: LogManager = new LogManager().activate(); | ||
|
||
[ | ||
{ | ||
test: 'Not supported', | ||
supportedScans: {} as SupportedScans, | ||
descriptors: getDummyDescriptors(), | ||
expectedFeatures: [], | ||
expectedReportSent: false | ||
}, | ||
{ | ||
test: 'With dependencies scan', | ||
supportedScans: { dependencies: true } as SupportedScans, | ||
descriptors: getDummyDescriptors(PackageType.Go, PackageType.Npm), | ||
expectedFeatures: [{ featureId: 'go-deps' }, { featureId: 'npm-deps' }], | ||
expectedReportSent: true | ||
}, | ||
{ | ||
test: 'With advance scan', | ||
supportedScans: { dependencies: true, applicability: true, iac: true, secrets: true } as SupportedScans, | ||
descriptors: getDummyDescriptors(PackageType.Go, PackageType.Npm), | ||
expectedFeatures: [ | ||
{ featureId: 'go-deps' }, | ||
{ featureId: 'npm-deps' }, | ||
{ featureId: 'npm-contextual' }, | ||
{ featureId: 'iac' }, | ||
{ featureId: 'secrets' } | ||
], | ||
expectedReportSent: true | ||
} | ||
].forEach(testCase => { | ||
it('Send usage report features - ' + testCase.test, async () => { | ||
const dummyConnectionManager: ConnectionMangerDummy = new ConnectionMangerDummy(logManager); | ||
dummyConnectionManager.expectedFeatures = testCase.expectedFeatures; | ||
await UsageUtils.sendUsageReport(testCase.supportedScans, testCase.descriptors, dummyConnectionManager).then(() => | ||
assert.equal(dummyConnectionManager.reportSent, testCase.expectedReportSent) | ||
); | ||
}); | ||
}); | ||
|
||
function getDummyDescriptors(...types: PackageType[]): Map<PackageType, Uri[]> { | ||
let descriptors: Map<PackageType, Uri[]> = new Map<PackageType, Uri[]>(); | ||
for (let type of types) { | ||
descriptors.set(type, [Uri.parse('/somewhere/file'), Uri.parse('/somewhere/other')]); | ||
} | ||
return descriptors; | ||
} | ||
}); | ||
|
||
class ConnectionMangerDummy extends ConnectionManager { | ||
private _expectedFeatures: IUsageFeature[] = []; | ||
private _reportSent: boolean = false; | ||
|
||
/** @override */ | ||
public async sendUsageReport(featureArray: IUsageFeature[]): Promise<void> { | ||
this._reportSent = true; | ||
assert.sameDeepMembers(featureArray, this._expectedFeatures); | ||
} | ||
|
||
public set expectedFeatures(value: IUsageFeature[]) { | ||
this._expectedFeatures = value; | ||
this._reportSent = false; | ||
} | ||
|
||
public get reportSent(): boolean { | ||
return this._reportSent; | ||
} | ||
} |