-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON content extension #130
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think that it's the best idea to have a test file that contains 714 rules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay. This test is labelled as an integration test. So it can be excluded from the normal test run using
pytest -m "not integration_test"
to reduce testing time. I think this test should check more than just the number of rules, but if I get too specific, the test will break after changing the JSON schema.