Skip to content

Commit 595f2ce

Browse files
em-c-rodDMedina6
andauthored
Add test for ckl severity (#6666)
* add test to describe the ckl severity on output Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> * add severity tag in sample triple overlay profile json Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> * clarify comments in test file Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> * lint Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> * move severity test into reverse mapper test file, remove a duplicate test, and refactor test file Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> * add severity tags to other controls in the test file and remove unused function Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> * add convenience saveCklOutput function in test file and refactor loadfile function name Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> --------- Signed-off-by: Emily Rodriguez <erodriguez92@gatech.edu> Co-authored-by: DMedina6 <158589619+DMedina6@users.noreply.github.com>
1 parent 7eb5175 commit 595f2ce

File tree

2 files changed

+87
-92
lines changed

2 files changed

+87
-92
lines changed

libs/hdf-converters/sample_jsons/attestations/triple_overlay_profile_sample.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@
967967
}
968968
],
969969
"tags": {
970+
"severity": "medium",
970971
"gtitle": "SRG-APP-000516-DB-999900",
971972
"gid": "V-61409",
972973
"rid": "SV-75899r1_rule",
@@ -1022,6 +1023,7 @@
10221023
"impact": 0.5,
10231024
"refs": [],
10241025
"tags": {
1026+
"severity": "medium",
10251027
"gtitle": "SRG-APP-000516-DB-999900",
10261028
"gid": "V-61449",
10271029
"rid": "SV-75939r3_rule",
@@ -1081,6 +1083,7 @@
10811083
}
10821084
],
10831085
"tags": {
1086+
"severity": "medium",
10841087
"gtitle": "SRG-APP-000133-DB-000179",
10851088
"gid": "V-61867",
10861089
"rid": "SV-76357r2_rule",
@@ -1136,6 +1139,7 @@
11361139
"impact": 0.5,
11371140
"refs": [],
11381141
"tags": {
1142+
"severity": "medium",
11391143
"gtitle": "SRG-APP-000098-DB-000042",
11401144
"gid": "V-61635",
11411145
"rid": "SV-76125r1_rule",
@@ -1196,6 +1200,7 @@
11961200
"impact": 0.5,
11971201
"refs": [],
11981202
"tags": {
1203+
"severity": "medium",
11991204
"gtitle": "SRG-APP-000141-DB-000090",
12001205
"gid": "V-61677",
12011206
"rid": "SV-76167r3_rule",
Lines changed: 82 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,157 @@
11
import fs from 'fs';
22
import {ChecklistResults} from '../../../src/ckl-mapper/checklist-mapper';
3+
import {Stigdata, Checklist} from '../../../src/ckl-mapper/checklistJsonix';
34
import {replaceCKLVersion} from '../../utils';
45
import {InvalidChecklistMetadataException} from '../../../src/ckl-mapper/checklist-metadata-utils';
56

67
describe('previously_checklist_converted_hdf_to_checklist', () => {
78
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'
1511
);
16-
17-
// fs.writeFileSync(
18-
// 'sample_jsons/checklist_mapper/converted-RHEL8V1R3.ckl',
19-
// mapper.toCkl()
20-
// );
21-
12+
const mapper = new ChecklistResults(hdfData);
2213
const expected = fs.readFileSync(
2314
'sample_jsons/checklist_mapper/converted-RHEL8V1R3.ckl',
2415
'utf-8'
2516
);
2617
const converted = mapper.toCkl();
27-
2818
expect(converted).toEqual(replaceCKLVersion(expected));
2919
});
30-
});
3120

32-
describe('previously_checklist_converted_hdf_to_checklist', () => {
3321
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'
4124
);
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);
4826
const expected = fs.readFileSync(
4927
'sample_jsons/checklist_mapper/converted-three-stig-checklist.ckl',
5028
'utf-8'
5129
);
5230
const converted = mapper.toCkl();
53-
5431
expect(converted).toEqual(replaceCKLVersion(expected));
5532
});
5633
});
5734

5835
describe('non_checklist_converted_hdf_to_checklist', () => {
5936
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'
6639
);
67-
68-
// fs.writeFileSync(
69-
// 'sample_jsons/checklist_mapper/converted-nessus.ckl',
70-
// mapper.toCkl()
71-
// );
72-
40+
const mapper = new ChecklistResults(hdfData);
7341
const expected = fs.readFileSync(
7442
'sample_jsons/checklist_mapper/converted-nessus.ckl',
7543
'utf-8'
7644
);
7745
const converted = mapper.toCkl();
78-
7946
expect(converted).toEqual(replaceCKLVersion(expected));
8047
});
8148
});
8249

8350
describe('Small RHEL8 HDF file', () => {
8451
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'
9454
);
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);
10156
const expected = fs.readFileSync(
10257
'sample_jsons/checklist_mapper/converted-rhel8_sample_oneOfEachControlStatus.ckl',
10358
'utf-8'
10459
);
10560
const converted = mapper.toCkl();
106-
10761
expect(converted).toEqual(replaceCKLVersion(expected));
10862
});
10963
});
11064

11165
describe('Small RHEL 7 with severity and severity override tags', () => {
11266
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'
12269
);
123-
124-
// fs.writeFileSync(
125-
// 'sample_jsons/checklist_mapper/converted-rhel7_overrides.ckl',
126-
// mapper.toCkl()
127-
// );
128-
70+
const mapper = new ChecklistResults(hdfData);
12971
const expected = fs.readFileSync(
13072
'sample_jsons/checklist_mapper/converted-rhel7_overrides.ckl',
13173
'utf-8'
13274
);
13375
const converted = mapper.toCkl();
134-
13576
expect(converted).toEqual(replaceCKLVersion(expected));
13677
});
13778
});
13879

13980
describe('hdf_profile_with_invalid_metadata', () => {
14081
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'
14784
);
14885
expect(() => new ChecklistResults(fileContents)).toThrowError(
14986
InvalidChecklistMetadataException
15087
);
15188
});
15289
});
15390

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'
16595
);
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');
166102
});
167103
});
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

Comments
 (0)