Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # This script tests the loading, saving, filtering capabilities of the OVAL object model.
- # This script generates and validates (using the oscap tool) one OVAL document
- # for each OVAL definition.
- # Loads files: "./build/ssg-*-oval.xml"
- # Store files: "./build/single_ovals/OVAL_DEF_ID.xml"
- # In case of an import error, run the .pyenv.sh file available
- # in the project root or add it to PYTHONPATH manually.
- # Command: $ source .pyenv.sh
- import glob
- import os
- import subprocess
- import threading
- from ssg.xml import open_xml, get_namespaces_from, register_namespaces
- from ssg.oval_object_model import load_oval_document
- SOURCE_FILE_PATH = "./build/ssg-*-oval.xml*"
- def _register_name_spaces(path):
- ns = get_namespaces_from(path)
- register_namespaces(ns)
- def _get_oval_definitions_ids(root_el):
- return load_oval_document(root_el).definitions.keys()
- def _validate_oval_document(path):
- error_code = subprocess.call(f"oscap oval validate {path}", shell=True)
- if error_code:
- print(f"NOT VALID OVAL DOCUMENT: {path}")
- def _save_oval_document(oval_document, path):
- with open(path, "wb") as fd:
- oval_document.save_as_xml(fd)
- def _process_oval_document(oval_document, def_id):
- try:
- ref = oval_document.get_all_references_of_definition(def_id)
- except Exception as error:
- print("Error:")
- print(def_id)
- print(error)
- return
- oval_document.keep_referenced_components(ref)
- def main():
- threads = []
- skip_validate_for = ("ocp", "eks")
- for file_path in glob.iglob(SOURCE_FILE_PATH):
- file_prefix, _ = os.path.splitext(os.path.basename(file_path))
- root_el = open_xml(file_path)
- _register_name_spaces(file_path)
- for def_id in _get_oval_definitions_ids(root_el):
- oval_document = load_oval_document(root_el)
- _process_oval_document(oval_document, def_id)
- path = f"./build/single_ovals/{file_prefix}-{def_id}.xml"
- _save_oval_document(oval_document, path)
- if not any([file_prefix.startswith(f"ssg-{x}") for x in skip_validate_for]):
- x = threading.Thread(target=_validate_oval_document, args=(path,))
- x.start()
- threads.append(x)
- for thread in threads:
- thread.join()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment