-
Notifications
You must be signed in to change notification settings - Fork 578
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 #1664 from snyk/feat/add-snyk-code-as-plugin-for-test
Feat/add snyk code as plugin for test
- Loading branch information
Showing
26 changed files
with
6,516 additions
and
4 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
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
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,13 @@ | ||
import { CustomError } from './custom-error'; | ||
|
||
export class FeatureNotSupportedForOrgError extends CustomError { | ||
public readonly org: string; | ||
|
||
constructor(org: string, additionalUserHelp = '') { | ||
super(`Unsupported action for org ${org}.`); | ||
this.code = 422; | ||
this.org = org; | ||
|
||
this.userMessage = `Feature is not supported for org ${org}. ${additionalUserHelp}`; | ||
} | ||
} |
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,108 @@ | ||
import { analyzeFolders, AnalysisSeverity } from '@snyk/code-client'; | ||
import { Log, ReportingDescriptor, Result } from 'sarif'; | ||
import { SEVERITY } from '../../snyk-test/legacy'; | ||
import { api } from '../../api-token'; | ||
import * as config from '../../config'; | ||
import spinner = require('../../spinner'); | ||
import { Options } from '../../types'; | ||
import { analysisProgressUpdate } from './utils'; | ||
import { FeatureNotSupportedBySnykCodeError } from './errors/unsupported-feature-snyk-code-error'; | ||
|
||
export async function getCodeAnalysisAndParseResults( | ||
root: string, | ||
options: Options, | ||
): Promise<Log> { | ||
const currentLabel = ''; | ||
await spinner.clearAll(); | ||
analysisProgressUpdate(currentLabel); | ||
const codeAnalysis = await getCodeAnalysis(root, options); | ||
spinner.clearAll(); | ||
return parseSecurityResults(codeAnalysis); | ||
} | ||
|
||
async function getCodeAnalysis(root: string, options: Options): Promise<Log> { | ||
const baseURL = config.CODE_CLIENT_PROXY_URL; | ||
const sessionToken = api() || ''; | ||
|
||
const severity = options.severityThreshold | ||
? severityToAnalysisSeverity(options.severityThreshold) | ||
: AnalysisSeverity.info; | ||
const paths: string[] = [root]; | ||
const sarif = true; | ||
|
||
const result = await analyzeFolders({ | ||
baseURL, | ||
sessionToken, | ||
severity, | ||
paths, | ||
sarif, | ||
}); | ||
|
||
return result.sarifResults!; | ||
} | ||
|
||
function severityToAnalysisSeverity(severity: SEVERITY): AnalysisSeverity { | ||
if (severity === SEVERITY.CRITICAL) { | ||
throw new FeatureNotSupportedBySnykCodeError(SEVERITY.CRITICAL); | ||
} | ||
const severityLevel = { | ||
low: 1, | ||
medium: 2, | ||
high: 3, | ||
}; | ||
return severityLevel[severity]; | ||
} | ||
|
||
function parseSecurityResults(codeAnalysis: Log): Log { | ||
let securityRulesMap; | ||
|
||
const rules = codeAnalysis.runs[0].tool.driver.rules; | ||
const results = codeAnalysis.runs[0].results; | ||
|
||
if (rules) { | ||
securityRulesMap = getSecurityRulesMap(rules); | ||
codeAnalysis.runs[0].tool.driver.rules = Object.values(securityRulesMap); | ||
} | ||
if (results && securityRulesMap) { | ||
codeAnalysis.runs[0].results = getSecurityResultsOnly( | ||
results, | ||
Object.keys(securityRulesMap), | ||
); | ||
} | ||
|
||
return codeAnalysis; | ||
} | ||
|
||
function getSecurityRulesMap( | ||
rules: ReportingDescriptor[], | ||
): { [ruleId: string]: ReportingDescriptor[] } { | ||
const securityRulesMap = rules.reduce((acc, rule) => { | ||
const { id: ruleId, properties } = rule; | ||
const isSecurityRule = properties?.tags?.some( | ||
(tag) => tag.toLowerCase() === 'security', | ||
); | ||
if (isSecurityRule) { | ||
acc[ruleId] = rule; | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
return securityRulesMap; | ||
} | ||
|
||
function getSecurityResultsOnly( | ||
results: Result[], | ||
securityRules: string[], | ||
): Result[] { | ||
const securityResults = results.reduce((acc: Result[], result: Result) => { | ||
const isSecurityResult = securityRules.some( | ||
(securityRule) => securityRule === result?.ruleId, | ||
); | ||
if (isSecurityResult) { | ||
acc.push(result); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
return securityResults; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/lib/plugins/sast/errors/unsupported-feature-snyk-code-error.ts
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,13 @@ | ||
import { CustomError } from '../../../errors/custom-error'; | ||
|
||
export class FeatureNotSupportedBySnykCodeError extends CustomError { | ||
public readonly feature: string; | ||
|
||
constructor(feature: string, additionalUserHelp = '') { | ||
super(`Unsupported action for ${feature}.`); | ||
this.code = 422; | ||
this.feature = feature; | ||
|
||
this.userMessage = `'${feature}' is not supported for snyk code. ${additionalUserHelp}`; | ||
} | ||
} |
Oops, something went wrong.