From 12449998156ca1f98891a74e62a2852a421ef424 Mon Sep 17 00:00:00 2001 From: Matthew Burket Date: Thu, 29 Jun 2023 20:33:28 -0500 Subject: [PATCH] Add tests for OpenSCAP results files for task xccdf_result_to_oscal_ar_test Signed-off-by: Matthew Burket --- .../input-oscap-arf-results/results_arf.xml | 49 ++++++ .../xccdf/input-oscap-results/results.xml | 39 +++++ .../results_arf.oscal.json | 165 ++++++++++++++++++ .../output-oscap-results/results.oscal.json | 165 ++++++++++++++++++ ...to-oscal-ar-input-oscap-arf-results.config | 4 + ...ult-to-oscal-ar-input-oscap-results.config | 4 + .../tasks/xccdf_result_to_oscal_ar_test.py | 44 +++++ 7 files changed, 470 insertions(+) create mode 100644 tests/data/tasks/xccdf/input-oscap-arf-results/results_arf.xml create mode 100644 tests/data/tasks/xccdf/input-oscap-results/results.xml create mode 100644 tests/data/tasks/xccdf/output-oscap-arf-results/results_arf.oscal.json create mode 100644 tests/data/tasks/xccdf/output-oscap-results/results.oscal.json create mode 100644 tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-arf-results.config create mode 100644 tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-results.config diff --git a/tests/data/tasks/xccdf/input-oscap-arf-results/results_arf.xml b/tests/data/tasks/xccdf/input-oscap-arf-results/results_arf.xml new file mode 100644 index 000000000..4285d1feb --- /dev/null +++ b/tests/data/tasks/xccdf/input-oscap-arf-results/results_arf.xml @@ -0,0 +1,49 @@ + + + + + + + + OSCAP Scan Result + + kube-c18ler8d06m877hrn7jg-roks8-default-00000319.iks.mycorp + + chroot:///host + OpenSCAP + 1.3.3 + + + + + + + + + + + + + + + + notselected + CCE-83691-6 + + + pass + CCE-27078-5 + + + + + + 74.713684 + + + + + + diff --git a/tests/data/tasks/xccdf/input-oscap-results/results.xml b/tests/data/tasks/xccdf/input-oscap-results/results.xml new file mode 100644 index 000000000..c3bb2a576 --- /dev/null +++ b/tests/data/tasks/xccdf/input-oscap-results/results.xml @@ -0,0 +1,39 @@ + + + + + OSCAP Scan Result + + kube-c18ler8d06m877hrn7jg-roks8-default-00000319.iks.mycorp + + chroot:///host + OpenSCAP + 1.3.3 + + + + + + + + + + + + + + + + notselected + CCE-83691-6 + + + pass + CCE-27078-5 + + + + + 74.713684 + + diff --git a/tests/data/tasks/xccdf/output-oscap-arf-results/results_arf.oscal.json b/tests/data/tasks/xccdf/output-oscap-arf-results/results_arf.oscal.json new file mode 100644 index 000000000..5047ece39 --- /dev/null +++ b/tests/data/tasks/xccdf/output-oscap-arf-results/results_arf.oscal.json @@ -0,0 +1,165 @@ +{ + "results": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "title": "XCCDF", + "description": "XCCDF Scan Results", + "start": "2021-06-08T02:35:55+00:00", + "end": "2021-06-08T02:35:55+00:00", + "local-definitions": { + "components": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "Service", + "title": "rhel7", + "description": "rhel7", + "status": { + "state": "operational" + } + } + ], + "inventory-items": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "description": "inventory", + "props": [ + { + "name": "target", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "kube-c18ler8d06m877hrn7jg-roks8-default-00000319.iks.mycorp" + }, + { + "name": "target_type", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "rhel7" + } + ], + "implemented-components": [ + { + "component-uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821" + } + ] + } + ], + "assessment-assets": { + "components": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "Validator", + "title": "OpenSCAP", + "description": "OpenSCAP", + "props": [ + { + "name": "scanner_name", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "OpenSCAP" + }, + { + "name": "scanner_version", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "1.3.3" + }, + { + "name": "version", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP" + }, + { + "name": "severity", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "medium" + }, + { + "name": "weight", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "1.000000" + }, + { + "name": "benchmark_id", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.ssgproject.content_benchmark_RHEL-7" + }, + { + "name": "benchmark_href", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "/content/ssg-rhel7-ds.xml" + }, + { + "name": "id", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.open-scap_testresult_xccdf_org.ssgproject.content_profile_cis" + } + ], + "status": { + "state": "operational" + } + } + ], + "assessment-platforms": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821" + } + ] + } + }, + "reviewed-controls": { + "control-selections": [ + {} + ] + }, + "observations": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "description": "xccdf_org.ssgproject.content_rule_prefer_64bit_os", + "props": [ + { + "name": "idref", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.ssgproject.content_rule_prefer_64bit_os" + }, + { + "name": "result", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "notselected" + } + ], + "methods": [ + "TEST-AUTOMATED" + ], + "subjects": [ + { + "subject-uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "inventory-item" + } + ], + "collected": "2023-11-30T23:00:03+00:00" + }, + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "description": "xccdf_org.ssgproject.content_rule_disable_prelink", + "props": [ + { + "name": "idref", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.ssgproject.content_rule_disable_prelink" + }, + { + "name": "result", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "pass" + } + ], + "methods": [ + "TEST-AUTOMATED" + ], + "subjects": [ + { + "subject-uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "inventory-item" + } + ], + "collected": "2023-11-30T23:00:03+00:00" + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/data/tasks/xccdf/output-oscap-results/results.oscal.json b/tests/data/tasks/xccdf/output-oscap-results/results.oscal.json new file mode 100644 index 000000000..5047ece39 --- /dev/null +++ b/tests/data/tasks/xccdf/output-oscap-results/results.oscal.json @@ -0,0 +1,165 @@ +{ + "results": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "title": "XCCDF", + "description": "XCCDF Scan Results", + "start": "2021-06-08T02:35:55+00:00", + "end": "2021-06-08T02:35:55+00:00", + "local-definitions": { + "components": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "Service", + "title": "rhel7", + "description": "rhel7", + "status": { + "state": "operational" + } + } + ], + "inventory-items": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "description": "inventory", + "props": [ + { + "name": "target", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "kube-c18ler8d06m877hrn7jg-roks8-default-00000319.iks.mycorp" + }, + { + "name": "target_type", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "rhel7" + } + ], + "implemented-components": [ + { + "component-uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821" + } + ] + } + ], + "assessment-assets": { + "components": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "Validator", + "title": "OpenSCAP", + "description": "OpenSCAP", + "props": [ + { + "name": "scanner_name", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "OpenSCAP" + }, + { + "name": "scanner_version", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "1.3.3" + }, + { + "name": "version", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP" + }, + { + "name": "severity", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "medium" + }, + { + "name": "weight", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "1.000000" + }, + { + "name": "benchmark_id", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.ssgproject.content_benchmark_RHEL-7" + }, + { + "name": "benchmark_href", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "/content/ssg-rhel7-ds.xml" + }, + { + "name": "id", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.open-scap_testresult_xccdf_org.ssgproject.content_profile_cis" + } + ], + "status": { + "state": "operational" + } + } + ], + "assessment-platforms": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821" + } + ] + } + }, + "reviewed-controls": { + "control-selections": [ + {} + ] + }, + "observations": [ + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "description": "xccdf_org.ssgproject.content_rule_prefer_64bit_os", + "props": [ + { + "name": "idref", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.ssgproject.content_rule_prefer_64bit_os" + }, + { + "name": "result", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "notselected" + } + ], + "methods": [ + "TEST-AUTOMATED" + ], + "subjects": [ + { + "subject-uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "inventory-item" + } + ], + "collected": "2023-11-30T23:00:03+00:00" + }, + { + "uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "description": "xccdf_org.ssgproject.content_rule_disable_prelink", + "props": [ + { + "name": "idref", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "xccdf_org.ssgproject.content_rule_disable_prelink" + }, + { + "name": "result", + "ns": "https://ibm.github.io/compliance-trestle/schemas/oscal/ar/OpenSCAP", + "value": "pass" + } + ], + "methods": [ + "TEST-AUTOMATED" + ], + "subjects": [ + { + "subject-uuid": "56666738-0f9a-4e38-9aac-c0fad00a5821", + "type": "inventory-item" + } + ], + "collected": "2023-11-30T23:00:03+00:00" + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-arf-results.config b/tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-arf-results.config new file mode 100644 index 000000000..6bc4d66f6 --- /dev/null +++ b/tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-arf-results.config @@ -0,0 +1,4 @@ +[task.xccdf-result-to-oscal-ar] +input-dir = tests/data/tasks/xccdf/input-oscap-arf-results +output-dir = tests/data/tasks/xccdf/output-oscap-arf-results +output-overwrite = true diff --git a/tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-results.config b/tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-results.config new file mode 100644 index 000000000..6c2b63668 --- /dev/null +++ b/tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-results.config @@ -0,0 +1,4 @@ +[task.xccdf-result-to-oscal-ar] +input-dir = tests/data/tasks/xccdf/input-oscap-results +output-dir = tests/data/tasks/xccdf/output-oscap-results +output-overwrite = true diff --git a/tests/trestle/tasks/xccdf_result_to_oscal_ar_test.py b/tests/trestle/tasks/xccdf_result_to_oscal_ar_test.py index 9d7c87018..614c9dc44 100644 --- a/tests/trestle/tasks/xccdf_result_to_oscal_ar_test.py +++ b/tests/trestle/tasks/xccdf_result_to_oscal_ar_test.py @@ -48,6 +48,8 @@ def uuid_mock2(self): cf06 = 'tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-xml-rhel7.config' cf07 = 'tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-xml-ocp4.config' cf08 = 'tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-configmaps.config' +cf09 = 'tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-results.config' +cf10 = 'tests/data/tasks/xccdf/test-xccdf-result-to-oscal-ar-input-oscap-arf-results.config' def setup_config(path: str): @@ -442,3 +444,45 @@ def test_xccdf_execute_input_configmaps(tmp_path, monkeypatch: MonkeyPatch): f_produced = d_produced / fn result = text_files_equal(f_expected, f_produced) assert result + + +def test_xccdf_execute_input_oscap_result(tmp_path, monkeypatch: MonkeyPatch): + monkeybusiness = MonkeyBusiness() + monkeypatch.setattr(uuid, 'uuid4', monkeybusiness.uuid_mock1) + xccdf.XccdfTransformer.set_timestamp('2023-11-30T23:00:03+00:00') + config = setup_config(cf09) + section = config['task.xccdf-result-to-oscal-ar'] + d_expected = pathlib.Path(section['output-dir']) + d_produced = tmp_path + section['output-dir'] = str(d_produced) + tgt = xccdf_result_to_oscal_ar.XccdfResultToOscalAR(section) + retval = tgt.execute() + assert retval == TaskOutcome.SUCCESS + list_dir = os.listdir(d_produced) + assert len(list_dir) == 1 + for fn in list_dir: + f_expected = d_expected / fn + f_produced = d_produced / fn + result = text_files_equal(f_expected, f_produced) + assert result + + +def test_xccdf_execute_input_oscap_arf_result(tmp_path, monkeypatch: MonkeyPatch): + monkeybusiness = MonkeyBusiness() + monkeypatch.setattr(uuid, 'uuid4', monkeybusiness.uuid_mock1) + xccdf.XccdfTransformer.set_timestamp('2023-11-30T23:00:03+00:00') + config = setup_config(cf10) + section = config['task.xccdf-result-to-oscal-ar'] + d_expected = pathlib.Path(section['output-dir']) + d_produced = tmp_path + section['output-dir'] = str(d_produced) + tgt = xccdf_result_to_oscal_ar.XccdfResultToOscalAR(section) + retval = tgt.execute() + assert retval == TaskOutcome.SUCCESS + list_dir = os.listdir(d_produced) + assert len(list_dir) == 1 + for fn in list_dir: + f_expected = d_expected / fn + f_produced = d_produced / fn + result = text_files_equal(f_expected, f_produced) + assert result