Thanks to visit codestin.com
Credit goes to pastebin.com

Guest User

Untitled

a guest
Oct 25th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # This script tests the loading, saving, filtering capabilities of the OVAL object model.
  4. # This script generates and validates (using the oscap tool) one OVAL document
  5. # for each OVAL definition.
  6. # Loads files: "./build/ssg-*-oval.xml"
  7. # Store files: "./build/single_ovals/OVAL_DEF_ID.xml"
  8.  
  9. # In case of an import error, run the .pyenv.sh file available
  10. # in the project root or add it to PYTHONPATH manually.
  11. # Command: $ source .pyenv.sh
  12.  
  13. import glob
  14. import os
  15. import subprocess
  16. import threading
  17. from ssg.xml import open_xml, get_namespaces_from, register_namespaces
  18. from ssg.oval_object_model import load_oval_document
  19.  
  20. SOURCE_FILE_PATH = "./build/ssg-*-oval.xml*"
  21.  
  22.  
  23. def _register_name_spaces(path):
  24.     ns = get_namespaces_from(path)
  25.     register_namespaces(ns)
  26.  
  27.  
  28. def _get_oval_definitions_ids(root_el):
  29.     return load_oval_document(root_el).definitions.keys()
  30.  
  31.  
  32. def _validate_oval_document(path):
  33.     error_code = subprocess.call(f"oscap oval validate {path}", shell=True)
  34.     if error_code:
  35.         print(f"NOT VALID OVAL DOCUMENT: {path}")
  36.  
  37.  
  38. def _save_oval_document(oval_document, path):
  39.     with open(path, "wb") as fd:
  40.         oval_document.save_as_xml(fd)
  41.  
  42.  
  43. def _process_oval_document(oval_document, def_id):
  44.     try:
  45.         ref = oval_document.get_all_references_of_definition(def_id)
  46.     except Exception as error:
  47.         print("Error:")
  48.         print(def_id)
  49.         print(error)
  50.         return
  51.  
  52.     oval_document.keep_referenced_components(ref)
  53.  
  54.  
  55. def main():
  56.     threads = []
  57.     skip_validate_for = ("ocp", "eks")
  58.     for file_path in glob.iglob(SOURCE_FILE_PATH):
  59.         file_prefix, _ = os.path.splitext(os.path.basename(file_path))
  60.         root_el = open_xml(file_path)
  61.         _register_name_spaces(file_path)
  62.         for def_id in _get_oval_definitions_ids(root_el):
  63.             oval_document = load_oval_document(root_el)
  64.             _process_oval_document(oval_document, def_id)
  65.  
  66.             path = f"./build/single_ovals/{file_prefix}-{def_id}.xml"
  67.             _save_oval_document(oval_document, path)
  68.  
  69.             if not any([file_prefix.startswith(f"ssg-{x}") for x in skip_validate_for]):
  70.                 x = threading.Thread(target=_validate_oval_document, args=(path,))
  71.                 x.start()
  72.                 threads.append(x)
  73.  
  74.     for thread in threads:
  75.         thread.join()
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     main()
  80.  
Advertisement
Add Comment
Please, Sign In to add comment