/
iac-output.ts
122 lines (106 loc) · 3.13 KB
/
iac-output.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import chalk from 'chalk';
import * as Debug from 'debug';
import { IacTestResult } from '../../../lib/snyk-test/iac-test-result';
import { getSeverityValue } from './formatters';
import { printPath } from './formatters/remediation-based-format-issues';
import { AnnotatedIacIssue } from '../../../lib/snyk-test/iac-test-result';
import { titleCaseText } from './formatters/legacy-format-issue';
const debug = Debug('iac-output');
function formatIacIssue(
issue: AnnotatedIacIssue,
isNew: boolean,
path: string[],
): string {
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
},
},
};
const newBadge = isNew ? ' (new)' : '';
const name = issue.subType ? ` in ${chalk.bold(issue.subType)}` : '';
let introducedBy = '';
if (path) {
// In this mode, we show only one path by default, for compactness
const pathStr = printPath(path);
introducedBy = `\n introduced by ${pathStr}`;
}
const description = extractOverview(issue.description).trim();
const descriptionLine = `\n ${description}\n`;
return (
severitiesColourMapping[issue.severity].colorFunc(
` ✗ ${chalk.bold(issue.title)}${newBadge} [${titleCaseText(
issue.severity,
)} Severity]`,
) +
` [${issue.id}]` +
name +
introducedBy +
descriptionLine
);
}
function extractOverview(description: string): string {
if (!description) {
return '';
}
const overviewRegExp = /## Overview([\s\S]*?)(?=##|(# Details))/m;
const overviewMatches = overviewRegExp.exec(description);
return (overviewMatches && overviewMatches[1]) || '';
}
export function getIacDisplayedOutput(
res: IacTestResult,
testedInfoText: string,
meta: string,
prefix: string,
): string {
const issuesTextArray = [
chalk.bold.white('\nInfrastructure as code issues:'),
];
const NotNew = false;
const issues: AnnotatedIacIssue[] = res.result.cloudConfigResults;
debug(`iac display output - ${issues.length} issues`);
issues
.sort((a, b) => getSeverityValue(b.severity) - getSeverityValue(a.severity))
.forEach((issue) => {
issuesTextArray.push(
formatIacIssue(issue, NotNew, issue.cloudConfigPath),
);
});
const issuesInfoOutput: string[] = [];
debug(`Iac display output - ${issuesTextArray.length} issues text`);
if (issuesTextArray.length > 0) {
issuesInfoOutput.push(issuesTextArray.join('\n'));
}
let body = issuesInfoOutput.join('\n\n') + '\n\n' + meta;
const vulnCountText = `found ${issues.length} issues`;
const summary = testedInfoText + ', ' + chalk.red.bold(vulnCountText);
body = body + '\n\n' + summary;
return prefix + body;
}
export function capitalizePackageManager(type) {
switch (type) {
case 'k8sconfig': {
return 'Kubernetes';
}
case 'helmconfig': {
return 'Helm';
}
case 'terraformconfig': {
return 'Terraform';
}
default: {
return 'Infrastracture as Code';
}
}
}