This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from quantum-sec/feature/VAPT-72
VAPT-72: Add support for trivy
- Loading branch information
Showing
7 changed files
with
233 additions
and
7 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
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 @@ | ||
export { TrivyCollector } from './trivy-collector'; |
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,120 @@ | ||
import { Logger } from '../utils'; | ||
import { createLoggerFixture } from '../test/logger.fixture'; | ||
import { TrivyCollector } from './trivy-collector'; | ||
import { CheckResult } from '../check-result'; | ||
|
||
describe('TrivyCollector', () => { | ||
|
||
let collector: TrivyCollector; | ||
let logger: Logger; | ||
|
||
beforeEach(() => { | ||
logger = createLoggerFixture(); | ||
collector = new TrivyCollector(logger); | ||
|
||
spyOn(collector, 'spawn').and.returnValue(new Promise((resolve) => { | ||
resolve('TEST_OUTPUT'); | ||
})); | ||
}); | ||
|
||
describe('constructor()', () => { | ||
it('should set the tool ID', () => { | ||
expect(collector.toolId).toEqual('trivy'); | ||
}); | ||
}); | ||
|
||
describe('getToolVersion()', () => { | ||
it('should call trivy with the --version flag', async () => { | ||
const result = await collector.getToolVersion({}); | ||
expect(result).toEqual('TEST_OUTPUT'); | ||
expect(collector.spawn).toHaveBeenCalledTimes(1); | ||
expect(collector.spawn).toHaveBeenCalledWith('trivy', ['--version'], {}); | ||
}); | ||
}); | ||
|
||
describe('getResults()', () => { | ||
it('should call trivy with preset options', async () => { | ||
collector._argv = { | ||
'image-name': 'TEST_IMAGE', | ||
} as any; | ||
spyOn(collector, 'parseResults').and.returnValue('TEST_RESULTS' as any); | ||
const result = await collector.getResults({}); | ||
expect(result).toEqual('TEST_RESULTS' as any); | ||
|
||
const expectedArgs = [ | ||
'--quiet', | ||
'image', | ||
'--security-checks', | ||
'vuln,config', | ||
'--exit-code', | ||
'0', | ||
'-f', | ||
'json', | ||
'--light', | ||
'TEST_IMAGE', | ||
]; | ||
expect(collector.spawn).toHaveBeenCalledTimes(1); | ||
expect(collector.spawn).toHaveBeenCalledWith('trivy', expectedArgs, {}); | ||
|
||
expect(collector.parseResults).toHaveBeenCalledTimes(1); | ||
expect(collector.parseResults).toHaveBeenCalledWith('TEST_OUTPUT'); | ||
}); | ||
it('should error when image name is not specified', async () => { | ||
await expectAsync(collector.getResults({})) | ||
.toBeRejectedWith(new Error('You must specify an --image-name argument.')); | ||
}); | ||
|
||
}); | ||
|
||
describe('parseResults()', () => { | ||
|
||
let raw: string; | ||
|
||
beforeEach(()=> { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
raw = JSON.stringify([{ | ||
Vulnerabilities: [{ | ||
VulnerabilityID: 'TEST_VULNERABILITY_ID', | ||
PkgName: 'TEST_PACKAGE_NAME', | ||
InstalledVersion: 'TEST_INSTALLED_VERSION', | ||
Layer: { | ||
Digest: 'TEST_DIGEST', | ||
}, | ||
}], | ||
}]); | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
}); | ||
|
||
it('should report passing when the result contains no vulnerabilities', () => { | ||
raw = JSON.stringify([{}]); | ||
const result = collector.parseResults(raw)[0]; | ||
expect(result.checkResult).toEqual(CheckResult.PASS); | ||
}); | ||
|
||
it('should report failing when no vulnerabilities were found', () => { | ||
const result = collector.parseResults(raw)[0]; | ||
expect(result.checkResult).toEqual(CheckResult.FAIL); | ||
}); | ||
|
||
it('should set the checkname to the vulnerabilityID', () => { | ||
const result = collector.parseResults(raw)[0]; | ||
expect(result.checkName).toEqual('TEST_VULNERABILITY_ID'); | ||
}); | ||
|
||
it('should set the packageName to PkgName', () => { | ||
const result = collector.parseResults(raw)[0]; | ||
expect(result.packageName).toEqual('TEST_PACKAGE_NAME'); | ||
}); | ||
|
||
it('should set the packageVersion to InstalledVersion', () => { | ||
const result = collector.parseResults(raw)[0]; | ||
expect(result.packageVersion).toEqual('TEST_INSTALLED_VERSION'); | ||
}); | ||
|
||
it('should set the resourceId to Layer.Digest', () => { | ||
const result = collector.parseResults(raw)[0]; | ||
expect(result.resourceId).toEqual('TEST_DIGEST'); | ||
}); | ||
}); | ||
|
||
}); |
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,74 @@ | ||
import { AnalysisCollectorBase } from '../analysis-collector-base'; | ||
import { CheckResult } from '../check-result'; | ||
import { IResult } from '../result.interface'; | ||
import { Logger } from '../utils'; | ||
|
||
export class TrivyCollector extends AnalysisCollectorBase { | ||
|
||
public constructor(logger: Logger) { | ||
super('trivy', logger); | ||
} | ||
|
||
public override async getToolVersion(options: any): Promise<string> { | ||
const args = ['--version']; | ||
return await this.spawn('trivy', args, options); | ||
} | ||
|
||
public override async getResults(options: any): Promise<IResult[]> { | ||
const imageName = this._argv['image-name']; | ||
if (!imageName) { | ||
throw new Error('You must specify an --image-name argument.'); | ||
} | ||
|
||
const args = ['--quiet', 'image', '--security-checks', 'vuln,config', '--exit-code', '0', '-f', 'json', '--light', imageName]; | ||
const output = await this.spawn('trivy', args, options); | ||
|
||
|
||
this.logger.debug(JSON.stringify(output, null, 2)); | ||
|
||
|
||
return this.parseResults(output); | ||
} | ||
|
||
public parseResults(output: string): IResult[] { | ||
const parsed = JSON.parse(output); | ||
|
||
const results = []; | ||
|
||
for (const set of parsed) { | ||
if (!set.Vulnerabilities) { | ||
const result: IResult = { | ||
checkId: 'vulnerabilities', | ||
checkName: 'No vulnerabilities found.', | ||
checkType: 'container-layer', | ||
checkResult: CheckResult.PASS, | ||
resourceId: set.Target, | ||
vulnerabilityId: set.VulnerabilityID, | ||
packageName: set.PkgName, | ||
packageVersion: set.InstalledVersion, | ||
}; | ||
|
||
results.push(result); | ||
continue; | ||
} | ||
|
||
results.push(...set.Vulnerabilities.map((v) => { | ||
const result: IResult = { | ||
checkId: 'vulnerabilities', | ||
checkName: v.VulnerabilityID, | ||
checkType: 'container-layer', | ||
checkResult: CheckResult.FAIL, | ||
resourceId: v.Layer.Digest, | ||
vulnerabilityId: v.VulnerabilityID, | ||
packageName: v.PkgName, | ||
packageVersion: v.InstalledVersion, | ||
}; | ||
|
||
return result; | ||
})); | ||
|
||
} | ||
return results; | ||
} | ||
|
||
} |