|
1 | 1 | import fs from 'fs'; |
2 | 2 | import {ChecklistResults} from '../../../src/ckl-mapper/checklist-mapper'; |
| 3 | +import {Stigdata, Checklist} from '../../../src/ckl-mapper/checklistJsonix'; |
3 | 4 | import {replaceCKLVersion} from '../../utils'; |
4 | 5 | import {InvalidChecklistMetadataException} from '../../../src/ckl-mapper/checklist-metadata-utils'; |
5 | 6 |
|
6 | 7 | describe('previously_checklist_converted_hdf_to_checklist', () => { |
7 | 8 | it('Successfully converts HDF to Checklist', () => { |
8 | | - const mapper = new ChecklistResults( |
9 | | - JSON.parse( |
10 | | - fs.readFileSync( |
11 | | - 'sample_jsons/checklist_mapper/checklist-RHEL8V1R3-hdf.json', |
12 | | - {encoding: 'utf-8'} |
13 | | - ) |
14 | | - ) |
| 9 | + const hdfData = loadJsonFile( |
| 10 | + 'sample_jsons/checklist_mapper/checklist-RHEL8V1R3-hdf.json' |
15 | 11 | ); |
16 | | - |
17 | | - // fs.writeFileSync( |
18 | | - // 'sample_jsons/checklist_mapper/converted-RHEL8V1R3.ckl', |
19 | | - // mapper.toCkl() |
20 | | - // ); |
21 | | - |
| 12 | + const mapper = new ChecklistResults(hdfData); |
22 | 13 | const expected = fs.readFileSync( |
23 | 14 | 'sample_jsons/checklist_mapper/converted-RHEL8V1R3.ckl', |
24 | 15 | 'utf-8' |
25 | 16 | ); |
26 | 17 | const converted = mapper.toCkl(); |
27 | | - |
28 | 18 | expect(converted).toEqual(replaceCKLVersion(expected)); |
29 | 19 | }); |
30 | | -}); |
31 | 20 |
|
32 | | -describe('previously_checklist_converted_hdf_to_checklist', () => { |
33 | 21 | it('Successfully converts HDF with multiple stigs to Checklist', () => { |
34 | | - const mapper = new ChecklistResults( |
35 | | - JSON.parse( |
36 | | - fs.readFileSync( |
37 | | - 'sample_jsons/checklist_mapper/three_stig_checklist-hdf.json', |
38 | | - {encoding: 'utf-8'} |
39 | | - ) |
40 | | - ) |
| 22 | + const hdfData = loadJsonFile( |
| 23 | + 'sample_jsons/checklist_mapper/three_stig_checklist-hdf.json' |
41 | 24 | ); |
42 | | - |
43 | | - // fs.writeFileSync( |
44 | | - // 'sample_jsons/checklist_mapper/converted-three-stig-checklist.ckl', |
45 | | - // mapper.toCkl() |
46 | | - // ); |
47 | | - |
| 25 | + const mapper = new ChecklistResults(hdfData); |
48 | 26 | const expected = fs.readFileSync( |
49 | 27 | 'sample_jsons/checklist_mapper/converted-three-stig-checklist.ckl', |
50 | 28 | 'utf-8' |
51 | 29 | ); |
52 | 30 | const converted = mapper.toCkl(); |
53 | | - |
54 | 31 | expect(converted).toEqual(replaceCKLVersion(expected)); |
55 | 32 | }); |
56 | 33 | }); |
57 | 34 |
|
58 | 35 | describe('non_checklist_converted_hdf_to_checklist', () => { |
59 | 36 | it('Successfully converts HDF to Checklist', () => { |
60 | | - const mapper = new ChecklistResults( |
61 | | - JSON.parse( |
62 | | - fs.readFileSync('sample_jsons/nessus_mapper/nessus-hdf-10.0.0.3.json', { |
63 | | - encoding: 'utf-8' |
64 | | - }) |
65 | | - ) |
| 37 | + const hdfData = loadJsonFile( |
| 38 | + 'sample_jsons/nessus_mapper/nessus-hdf-10.0.0.3.json' |
66 | 39 | ); |
67 | | - |
68 | | - // fs.writeFileSync( |
69 | | - // 'sample_jsons/checklist_mapper/converted-nessus.ckl', |
70 | | - // mapper.toCkl() |
71 | | - // ); |
72 | | - |
| 40 | + const mapper = new ChecklistResults(hdfData); |
73 | 41 | const expected = fs.readFileSync( |
74 | 42 | 'sample_jsons/checklist_mapper/converted-nessus.ckl', |
75 | 43 | 'utf-8' |
76 | 44 | ); |
77 | 45 | const converted = mapper.toCkl(); |
78 | | - |
79 | 46 | expect(converted).toEqual(replaceCKLVersion(expected)); |
80 | 47 | }); |
81 | 48 | }); |
82 | 49 |
|
83 | 50 | describe('Small RHEL8 HDF file', () => { |
84 | 51 | it('can be successfully converted from HDF to Checklist', () => { |
85 | | - const mapper = new ChecklistResults( |
86 | | - JSON.parse( |
87 | | - fs.readFileSync( |
88 | | - 'sample_jsons/attestations/rhel8_sample_oneOfEachControlStatus.json', |
89 | | - { |
90 | | - encoding: 'utf-8' |
91 | | - } |
92 | | - ) |
93 | | - ) |
| 52 | + const hdfData = loadJsonFile( |
| 53 | + 'sample_jsons/attestations/rhel8_sample_oneOfEachControlStatus.json' |
94 | 54 | ); |
95 | | - |
96 | | - // fs.writeFileSync( |
97 | | - // 'sample_jsons/checklist_mapper/converted-rhel8_sample_oneOfEachControlStatus.ckl', |
98 | | - // mapper.toCkl() |
99 | | - // ); |
100 | | - |
| 55 | + const mapper = new ChecklistResults(hdfData); |
101 | 56 | const expected = fs.readFileSync( |
102 | 57 | 'sample_jsons/checklist_mapper/converted-rhel8_sample_oneOfEachControlStatus.ckl', |
103 | 58 | 'utf-8' |
104 | 59 | ); |
105 | 60 | const converted = mapper.toCkl(); |
106 | | - |
107 | 61 | expect(converted).toEqual(replaceCKLVersion(expected)); |
108 | 62 | }); |
109 | 63 | }); |
110 | 64 |
|
111 | 65 | describe('Small RHEL 7 with severity and severity override tags', () => { |
112 | 66 | it('can be successfully converted from HDF to Checklist', () => { |
113 | | - const mapper = new ChecklistResults( |
114 | | - JSON.parse( |
115 | | - fs.readFileSync( |
116 | | - 'sample_jsons/checklist_mapper/sample_input_report/RHEL7_overrides_hdf.json', |
117 | | - { |
118 | | - encoding: 'utf-8' |
119 | | - } |
120 | | - ) |
121 | | - ) |
| 67 | + const hdfData = loadJsonFile( |
| 68 | + 'sample_jsons/checklist_mapper/sample_input_report/RHEL7_overrides_hdf.json' |
122 | 69 | ); |
123 | | - |
124 | | - // fs.writeFileSync( |
125 | | - // 'sample_jsons/checklist_mapper/converted-rhel7_overrides.ckl', |
126 | | - // mapper.toCkl() |
127 | | - // ); |
128 | | - |
| 70 | + const mapper = new ChecklistResults(hdfData); |
129 | 71 | const expected = fs.readFileSync( |
130 | 72 | 'sample_jsons/checklist_mapper/converted-rhel7_overrides.ckl', |
131 | 73 | 'utf-8' |
132 | 74 | ); |
133 | 75 | const converted = mapper.toCkl(); |
134 | | - |
135 | 76 | expect(converted).toEqual(replaceCKLVersion(expected)); |
136 | 77 | }); |
137 | 78 | }); |
138 | 79 |
|
139 | 80 | describe('hdf_profile_with_invalid_metadata', () => { |
140 | 81 | it('Throws InvalidChecklistFormatException when trying to convert to checklist with invalid metadata', () => { |
141 | | - // ensures that checklist metadata is being validated |
142 | | - const fileContents = JSON.parse( |
143 | | - fs.readFileSync( |
144 | | - 'sample_jsons/checklist_mapper/sample_input_report/invalid_metadata.json', |
145 | | - {encoding: 'utf-8'} |
146 | | - ) |
| 82 | + const fileContents = loadJsonFile( |
| 83 | + 'sample_jsons/checklist_mapper/sample_input_report/invalid_metadata.json' |
147 | 84 | ); |
148 | 85 | expect(() => new ChecklistResults(fileContents)).toThrowError( |
149 | 86 | InvalidChecklistMetadataException |
150 | 87 | ); |
151 | 88 | }); |
152 | 89 | }); |
153 | 90 |
|
154 | | -describe('hdf_profile_with_invalid_metadata', () => { |
155 | | - it('Throws InvalidChecklistFormatException when trying to convert to checklist with invalid metadata', () => { |
156 | | - // ensures that checklist metadata is being validated |
157 | | - const fileContents = JSON.parse( |
158 | | - fs.readFileSync( |
159 | | - 'sample_jsons/checklist_mapper/sample_input_report/invalid_metadata.json', |
160 | | - {encoding: 'utf-8'} |
161 | | - ) |
162 | | - ); |
163 | | - expect(() => new ChecklistResults(fileContents)).toThrowError( |
164 | | - InvalidChecklistMetadataException |
| 91 | +describe('checklist_mapper_severity_mapping', () => { |
| 92 | + it('Maps control V-61867 to correct severity category', () => { |
| 93 | + const hdfData = loadJsonFile( |
| 94 | + 'sample_jsons/attestations/triple_overlay_profile_sample.json' |
165 | 95 | ); |
| 96 | + const mapper = new ChecklistResults(hdfData); |
| 97 | + const jsonixData = mapper.getJsonix(); |
| 98 | + const severity = extractSeverity(jsonixData, 2, 1); |
| 99 | + const status = extractStatus(jsonixData, 2); |
| 100 | + expect(severity).toBe('medium'); |
| 101 | + expect(status).toBe('Not_Applicable'); |
166 | 102 | }); |
167 | 103 | }); |
| 104 | + |
| 105 | +/** |
| 106 | + * Load and parse the file. |
| 107 | + * @param filePath Path to the file. |
| 108 | + * @returns Parsed data. |
| 109 | + */ |
| 110 | +function loadJsonFile(filePath: string): any { |
| 111 | + return JSON.parse(fs.readFileSync(filePath, {encoding: 'utf-8'})); |
| 112 | +} |
| 113 | +/** |
| 114 | + * Extract the severity string for a specific control from the mapper. |
| 115 | + * @param jsonixData Checklist data in jsonix format. |
| 116 | + * @param vulnIndex Index of the vulnerability in the list. |
| 117 | + * @param stigdataIndex Index of the stigdata element. |
| 118 | + * @returns Severity string. |
| 119 | + */ |
| 120 | +function extractSeverity( |
| 121 | + jsonixData: Checklist, |
| 122 | + vulnIndex: number, |
| 123 | + stigdataIndex: number |
| 124 | +): string | undefined { |
| 125 | + const istig = (jsonixData?.value as Stigdata)?.stigs?.istig[0]; |
| 126 | + const vuln = istig?.vuln[vulnIndex]; |
| 127 | + const stigdataElement = vuln?.stigdata[stigdataIndex]; |
| 128 | + return stigdataElement?.attributedata ?? undefined; |
| 129 | +} |
| 130 | +/** |
| 131 | + * Extract the status string for a specific control from the mapper. |
| 132 | + * @param jsonixData Checklist data in jsonix format. |
| 133 | + * @param vulnIndex Index of the vulnerability in the list. |
| 134 | + * @returns Status string. |
| 135 | + */ |
| 136 | +function extractStatus( |
| 137 | + jsonixData: Checklist, |
| 138 | + vulnIndex: number |
| 139 | +): string | undefined { |
| 140 | + const istig = (jsonixData?.value as Stigdata)?.stigs?.istig[0]; |
| 141 | + const vuln = istig?.vuln[vulnIndex]; |
| 142 | + const status = vuln?.status; |
| 143 | + return status; |
| 144 | +} |
| 145 | +/** |
| 146 | + * Save the CKL output to a file. |
| 147 | + * In the case that the expected output changes, the schema changes, or additional tests are created, |
| 148 | + * this function can be used as a convenience to update the expected output. |
| 149 | + * NOTE: Only use this function to generate the expected output once. Do not overwrite the expected output every time a test runs. |
| 150 | + * @param mapper ChecklistResults instance. |
| 151 | + * @param outputPath Path to save the CKL output. |
| 152 | + */ |
| 153 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 154 | +function saveCklOutput(mapper: ChecklistResults, outputPath: string): void { |
| 155 | + const cklOutput = mapper.toCkl(); |
| 156 | + fs.writeFileSync(outputPath, cklOutput); |
| 157 | +} |
0 commit comments