|
1 | | -import { |
2 | | - IPolicyValidationPluginBeta1, |
3 | | - IPolicyValidationContextBeta1, |
4 | | - PolicyValidationPluginReportBeta1, |
5 | | -} from 'aws-cdk-lib'; |
6 | | -import { Report, processReport } from './report'; |
7 | | -import { exec } from './utils'; |
8 | | - |
9 | | -export interface CheckovValidatorProps { |
10 | | - /** |
11 | | - * List of checks to run |
12 | | - * |
13 | | - * @default - all checks are run |
14 | | - */ |
15 | | - readonly check?: string[]; |
16 | | - |
17 | | - /** |
18 | | - * List of checks to skip |
19 | | - * |
20 | | - * @default - no checks are skipped |
21 | | - */ |
22 | | - readonly skipCheck?: string[]; |
23 | | -} |
24 | | - |
25 | | -/** |
26 | | - * A validation plugin using checkov |
27 | | - */ |
28 | | -export class CheckovValidator implements IPolicyValidationPluginBeta1 { |
29 | | - public readonly name: string; |
30 | | - |
31 | | - private readonly checkov: string; |
32 | | - private readonly check: string[]; |
33 | | - private readonly skipCheck: string[]; |
34 | | - |
35 | | - private templatePaths: string[] = []; |
36 | | - |
37 | | - constructor(props: CheckovValidatorProps = {}) { |
38 | | - this.name = 'cdk-validator-checkov'; |
39 | | - |
40 | | - this.checkov = 'checkov'; // possible improvement allow Docker usage |
41 | | - this.check = props.check ?? []; |
42 | | - this.skipCheck = props.skipCheck ?? []; |
43 | | - } |
44 | | - |
45 | | - validate(context: IPolicyValidationContextBeta1): PolicyValidationPluginReportBeta1 { |
46 | | - this.templatePaths = context.templatePaths; |
47 | | - |
48 | | - return this.execCheckov(); |
49 | | - } |
50 | | - |
51 | | - private execCheckov(): PolicyValidationPluginReportBeta1 { |
52 | | - const flags = [ |
53 | | - '--framework', |
54 | | - 'cloudformation', |
55 | | - 'terraform_json', |
56 | | - '--output', |
57 | | - 'json', |
58 | | - '--soft-fail', |
59 | | - ]; |
60 | | - |
61 | | - this.templatePaths.forEach((templatePath) => { |
62 | | - flags.push('-f'); |
63 | | - flags.push(templatePath); |
64 | | - }); |
65 | | - |
66 | | - if (this.check?.length) { |
67 | | - flags.push('--check'); |
68 | | - flags.push(this.check.join(',')); |
69 | | - } |
70 | | - |
71 | | - if (this.skipCheck?.length) { |
72 | | - flags.push('--skip-check'); |
73 | | - flags.push(this.skipCheck.join(',')); |
74 | | - } |
75 | | - |
76 | | - try { |
77 | | - const report: Report = exec([this.checkov, ...flags], { |
78 | | - json: true, |
79 | | - env: { |
80 | | - LOG_LEVEL: 'ERROR', |
81 | | - }, |
82 | | - }); |
83 | | - |
84 | | - return processReport(report); |
85 | | - } catch (e) { |
86 | | - console.error(`checkov plugin failed to scan the given templates. Error: ${e}`); |
87 | | - |
88 | | - return { |
89 | | - success: false, |
90 | | - violations: [], |
91 | | - }; |
92 | | - } |
93 | | - } |
94 | | -} |
| 1 | +export { CheckovValidator, CheckovValidatorProps } from './plugin'; |
0 commit comments