-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from Honny1/expand-json-content
JSON content extension
- Loading branch information
Showing
18 changed files
with
821 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2022, Red Hat, Inc. | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import argparse | ||
import json | ||
import sys | ||
|
||
from jsonschema import validate | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(prog='JSON Schema validator') | ||
parser.add_argument("-s", | ||
"--schema", | ||
type=str, | ||
default="./tests/json_schema_of_report.json", | ||
help="Path to schema of JSON to validate." | ||
) | ||
parser.add_argument('JSON', | ||
type=argparse.FileType("r"), | ||
nargs='?', | ||
default=sys.stdin, | ||
help="JSON file source. Default: stdin" | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def validate_json(schema_src, json_file): | ||
json_schema = None | ||
json_data = None | ||
|
||
with open(schema_src, "r", encoding="utf-8") as schema_file: | ||
json_schema = json.load(schema_file) | ||
|
||
json_data = json.load(json_file) | ||
json_file.close() | ||
|
||
validate(json_data, json_schema) | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
validate_json(args.schema, args.JSON) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
openscap_report/scap_results_parser/data_structures/json_transformation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2022, Red Hat, Inc. | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
|
||
def rearrange_references(dictionary_json): | ||
global_references = {} | ||
for rule in dictionary_json["rules"].values(): | ||
new_rule_references = [] | ||
for ref in rule["references"]: | ||
global_references[ref["text"]] = ref["href"] | ||
new_rule_references.append(ref["text"]) | ||
rule["references"] = new_rule_references | ||
dictionary_json["references"] = global_references | ||
|
||
|
||
def rearrange_identifiers(dictionary_json): | ||
global_identifiers = {} | ||
for rule in dictionary_json["rules"].values(): | ||
new_rule_identifiers = [] | ||
for ident in rule["identifiers"]: | ||
global_identifiers[ident["text"]] = ident["system"] | ||
new_rule_identifiers.append(ident["text"]) | ||
rule["identifiers"] = new_rule_identifiers | ||
dictionary_json["identifiers"] = global_identifiers | ||
|
||
|
||
def _get_dict_or_value(val): | ||
if isinstance(val, list): | ||
out = [] | ||
for item in val: | ||
out.append(_get_dict_or_value(item)) | ||
return out | ||
if isinstance(val, dict): | ||
return remove_empty_values(val) | ||
return val | ||
|
||
|
||
def is_not_empty(val): | ||
if val is None: | ||
return False | ||
if isinstance(val, float): | ||
return True | ||
return len(val) > 0 | ||
|
||
|
||
def remove_empty_values(dictionary_json): | ||
out = {} | ||
for key, val in dictionary_json.items(): | ||
clean_value = _get_dict_or_value(val) | ||
if is_not_empty(clean_value): | ||
out[key] = clean_value | ||
return out | ||
|
||
|
||
def remove_not_selected_rules(dictionary_json, ids_of_selected_rules): | ||
selected_rules = {} | ||
for rule_id, rule in dictionary_json["rules"].items(): | ||
if rule_id in ids_of_selected_rules or ( | ||
rule["result"] != "notselected" and not is_not_empty(ids_of_selected_rules) | ||
): | ||
selected_rules[rule_id] = rule | ||
dictionary_json["rules"] = selected_rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2022, Red Hat, Inc. | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import json | ||
|
||
import pytest | ||
from jsonschema import validate | ||
|
||
from openscap_report.debug_settings import DebugSetting | ||
from openscap_report.report_generators.json import JSONReportGenerator | ||
from tests.unit_tests.test_data_structure import get_parser, get_report | ||
|
||
from ..constants import PATH_TO_ARF, PATH_TO_JSON_SCHEMA | ||
|
||
|
||
@pytest.mark.integration_test | ||
def test_json_structure_with_schema(): | ||
json_schema = None | ||
with open(PATH_TO_JSON_SCHEMA, "r", encoding="utf-8") as schema_file: | ||
json_schema = json.load(schema_file) | ||
json_gen = JSONReportGenerator(get_parser(PATH_TO_ARF)) | ||
json_data = json_gen.generate_report(DebugSetting()).read().decode("utf-8") | ||
validate(json.loads(json_data), json_schema) | ||
|
||
|
||
@pytest.mark.integration_test | ||
def test_json_count_of_rules(): | ||
report = get_report() | ||
json_dict = report.as_dict_for_default_json() | ||
assert len(json_dict["rules"]) == 714 |
Oops, something went wrong.