Example #1
0
def build_oval_document_for_definition(defid):
    """For a definition, build a complete oval_definitions document for it"""

    if defid is None:
        return None

    elements_index = lib_search.ElementsIndex(False)

    if verbose:
        print(
            "    ---- Resolving all elements needed to build comprehensive document..."
        )
    oval_ids = elements_index.find_downstream_ids(defid)
    file_paths = elements_index.get_paths_from_ids(oval_ids)

    if verbose:
        print(
            "    ---- Importing separate elements into comprehensive document...."
        )
    oval = OvalDocument(None)
    for path in file_paths:
        element = OvalElement.fromStandaloneFile(path)
        if element is None:
            print(":::: None from path: ", path)
            return None
        oval.addElement(element, True)

    return etree.fromstring(oval.to_string())
def writeFile(path, element, verbose=False):
    
    if verbose:
        if os.path.exists(path):
            # TODO  Determine if the element has not changed
            print("## Overwrite existing file: ", path)
        else:
            print("@@ Creating new file: ", path)
    
    # Get the namespace of this element
    namespace = element.getNamespace()
    # Register this namespace with the parser as the default namespace
    xml.etree.ElementTree.register_namespace('', namespace)
    e = element.getElement()
    # Fix up the element so it will print nicely
    OvalDocument.indent(e)
    # Create a new ElementTree with this element as the root
    tree = ElementTree(e)
    # And finally, write the full tree to a file including the xml declaration
    parent = os.path.dirname(path)
    if not os.path.isdir(parent):
        try :
            os.makedirs(parent, 0o755, True)
        except:
            return False
    
    tree.write(path, "UTF-8", True)
#     xml.etree.ElementTree.dump(tree)
    return True
Example #3
0
def build_comprehensive_oval_document(changes):
    """
    Builds an XML tree which contains all elements affected by the changes
    """

    global debug
    global verbose

    if changes is None or len(changes) < 1:
        return None

    if verbose:
        print("    ---- Getting OVAL ID's for all changed files...")
    oval_ids_changed = {
        lib_repo.path_to_oval_id(filepath)
        for filepath in changes
    }

    # find all upstream ids
    if verbose:
        print(
            "    ---- Locating parent definitions for all changed elements...")
    elements_index = lib_search.ElementsIndex(False)
    upstream_ids = elements_index.find_upstream_ids(oval_ids_changed, set())

    # filter affected to definition ids
    affected_def_ids = {
        oval_id
        for oval_id in upstream_ids
        if lib_repo.get_element_type_from_oval_id(oval_id) == 'definition'
    }

    # get all downstream elements
    if verbose:
        print(
            "    ---- Resolving all elements needed to build comprehensive document..."
        )
    oval_ids = elements_index.find_downstream_ids(affected_def_ids,
                                                  affected_def_ids)
    file_paths = elements_index.get_paths_from_ids(oval_ids)

    if verbose:
        print(
            "    ---- Importing separate elements into comprehensive document...."
        )
    oval = OvalDocument(None)
    for path in file_paths:
        element = OvalElement.fromStandaloneFile(path)
        if element is None:
            print(":::: None from path: ", path)
            return None
        oval.addElement(element, True)

    return etree.fromstring(oval.to_string())
Example #4
0
def build_comprehensive_oval_document(changes):
    """
    Builds an XML tree which contains all elements affected by the changes
    """

    global debug
    global verbose

    if changes is None or len(changes) < 1:
        return None

    if verbose:
        print("    ---- Getting OVAL ID's for all changed files...")
    oval_ids_changed = {lib_repo.path_to_oval_id(filepath) for filepath in changes}

    # find all upstream ids
    if verbose:
        print("    ---- Locating parent definitions for all changed elements...")
    elements_index = lib_search.ElementsIndex(False)
    upstream_ids = elements_index.find_upstream_ids(oval_ids_changed, set())

    # filter affected to definition ids
    affected_def_ids = {
        oval_id for oval_id in upstream_ids if lib_repo.get_element_type_from_oval_id(oval_id) == "definition"
    }

    # get all downstream elements
    if verbose:
        print("    ---- Resolving all elements needed to build comprehensive document...")
    oval_ids = elements_index.find_downstream_ids(affected_def_ids, affected_def_ids)
    file_paths = elements_index.get_paths_from_ids(oval_ids)

    if verbose:
        print("    ---- Importing separate elements into comprehensive document....")
    oval = OvalDocument(None)
    for path in file_paths:
        element = OvalElement.fromStandaloneFile(path)
        if element is None:
            print(":::: None from path: ", path)
            return None
        oval.addElement(element, True)

    return etree.fromstring(oval.to_string())
def writeFile(path, element, verbose=False):
    
    if verbose:
        if os.path.exists(path):
            print("## Overwrite existing file: ", path)
        else:
            print("@@ Creating new file: ", path)
    
    # Get the namespace of this element
    namespace = element.getNamespace()
    # Register this namespace with the parser as the default namespace
    xml.etree.ElementTree.register_namespace('', namespace)
    e = element.getElement()
    # Fix up the element so it will print nicely
    OvalDocument.indent(e)
    # Create a new ElementTree with this element as the root
    tree = ElementTree(e)
    # And finally, write the full tree to a file including the xml declaration
    tree.write(path, "UTF-8", True)
Example #6
0
def writeFile(path, element, verbose=False):

    if verbose:
        if os.path.exists(path):
            # TODO  Determine if the element has not changed
            print("## Overwrite existing file: ", path)
        else:
            print("@@ Creating new file: ", path)

    # Get the namespace of this element
    namespace = element.getNamespace()
    # Register this namespace with the parser as the default namespace
    xml.etree.ElementTree.register_namespace("", namespace)
    xml.etree.ElementTree.register_namespace(
        "oval", "http://oval.mitre.org/XMLSchema/oval-common-5")
    xml.etree.ElementTree.register_namespace(
        "oval-def", "http://oval.mitre.org/XMLSchema/oval-definitions-5")
    xml.etree.ElementTree.register_namespace(
        "xsi", "http://www.w3.org/2001/XMLSchema-instance")

    e = element.getElement()

    # Fix up the element so it will print nicely
    OvalDocument.indent(e)
    # Create a new ElementTree with this element as the root
    tree = ElementTree(e)
    # And finally, write the full tree to a file not including the xml declaration
    parent = os.path.dirname(path)
    if not os.path.isdir(parent):
        try:
            os.makedirs(parent, 0o0755, True)
            os.chmod(parent, 0o0755)
        except:
            return False

    # WKM CHANGE
    tree.write(path, "UTF-8", False, None, "xml")
    os.chmod(path, 0o0664)
    return True
Example #7
0
def build_oval_document_for_definition(defid):
    """For a definition, build a complete oval_definitions document for it"""

    if defid is None:
        return None
    
    elements_index = lib_search.ElementsIndex(False)
    
    if verbose:
        print("    ---- Resolving all elements needed to build comprehensive document...")
    oval_ids = elements_index.find_downstream_ids(defid)
    file_paths = elements_index.get_paths_from_ids(oval_ids)
    
    if verbose:
        print("    ---- Importing separate elements into comprehensive document....")
    oval = OvalDocument(None)
    for path in file_paths:
        element = OvalElement.fromStandaloneFile(path)
        if element is None:
            print (":::: None from path: ", path)
            return None
        oval.addElement(element, True)
                
    return etree.fromstring(oval.to_string())
Example #8
0
def writeFile(path, element, verbose=False):
    
    if verbose:
        if os.path.exists(path):
            # TODO  Determine if the element has not changed
            print("## Overwrite existing file: ", path)
        else:
            print("@@ Creating new file: ", path)
    
    # Get the namespace of this element
    namespace = element.getNamespace()
    # Register this namespace with the parser as the default namespace
    xml.etree.ElementTree.register_namespace("", namespace)
    xml.etree.ElementTree.register_namespace("oval", "http://oval.mitre.org/XMLSchema/oval-common-5")
    xml.etree.ElementTree.register_namespace("oval-def", "http://oval.mitre.org/XMLSchema/oval-definitions-5")
    xml.etree.ElementTree.register_namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")

    e = element.getElement()

    # Fix up the element so it will print nicely
    OvalDocument.indent(e)
    # Create a new ElementTree with this element as the root
    tree = ElementTree(e)
    # And finally, write the full tree to a file not including the xml declaration
    parent = os.path.dirname(path)
    if not os.path.isdir(parent):
        try :
            os.makedirs(parent, 0o0755, True)
            os.chmod(parent, 0o0755)
        except:
            return False

    # WKM CHANGE
    tree.write(path, "UTF-8", False, None, "xml")
    os.chmod(path, 0o0664)
    return True
Example #9
0
def decompose(filename, verbose):
    oval = OvalDocument(None)

    if not oval.parseFromFile(filename):
        print("\n >> Unable to parse source file '{0}':  no actions taken".
              format(filename))
        return

    deflist = oval.getDefinitions()
    #    if not deflist or deflist is None or len(deflist) < 1:
    #        print("\n ## Error:  this document does not contain any OVAL definitions.  No further action will be taken")
    #        return

    if verbose:
        print(" Number of definitions to process: ", len(deflist))

    repository_root = lib_repo.get_repository_root_path()

    writeFiles(deflist, repository_root, verbose)
    writeFiles(oval.getTests(), repository_root, verbose)
    writeFiles(oval.getObjects(), repository_root, verbose)
    writeFiles(oval.getStates(), repository_root, verbose)
    writeFiles(oval.getVariables(), repository_root, verbose)
Example #10
0
def main():
    """
    Breaks the OVAL file into its constituent elements and writes each of those into the repository
    """
    
    
    
    parser = argparse.ArgumentParser(description='Separates an OVAL file into its component parts and saves them to the repository.')
    options = parser.add_argument_group('options')
    options.add_argument('-f', '--file', required=True, help='The name of the source file')
    options.add_argument('-v', '--verbose', required=False, action="store_true", help='Enable more verbose messages')
    args = vars(parser.parse_args())

    oval = OvalDocument(None)
    filename = args['file']
    if args['verbose']:
        verbose = True
    else:
        verbose = False
        
    
    if not oval.parseFromFile(filename):
        print("\n >> Unable to parse source file '{0}':  no actions taken".format(filename))
        return

    deflist = oval.getDefinitions()
    if not deflist or deflist is None or len(deflist) < 1:
        print("\n ## Error:  this document does not contain any OVAL definitions.  No further action will be taken")
        return
        
    if verbose:
        print(" Number of definitions to process: ", len(deflist))


    repository_root = lib_repo.get_repository_root_path()
    
    writeFiles(deflist, repository_root, verbose)
    writeFiles(oval.getTests(), repository_root, verbose)
    writeFiles(oval.getObjects(), repository_root, verbose)
    writeFiles(oval.getStates(), repository_root, verbose)
    writeFiles(oval.getVariables(), repository_root, verbose)
Example #11
0
def decompose(filename, verbose):
    oval = OvalDocument(None)
    
    if not oval.parseFromFile(filename):
        print("\n >> Unable to parse source file '{0}':  no actions taken".format(filename))
        return

    deflist = oval.getDefinitions()
#    if not deflist or deflist is None or len(deflist) < 1:
#        print("\n ## Error:  this document does not contain any OVAL definitions.  No further action will be taken")
#        return
        
    if verbose:
        print(" Number of definitions to process: ", len(deflist))


    repository_root = lib_repo.get_repository_root_path()
    
    writeFiles(deflist, repository_root, verbose)
    writeFiles(oval.getTests(), repository_root, verbose)
    writeFiles(oval.getObjects(), repository_root, verbose)
    writeFiles(oval.getStates(), repository_root, verbose)
    writeFiles(oval.getVariables(), repository_root, verbose)
Example #12
0
def main():
    """
    """
    
    print("------------- Opening OVAL file -------------")
    
    tree = ElementTree()
    tree.parse("OvalTest.xml")
    document = OvalDocument(tree)
#     document.parseFromFile("OvalTest.xml")

    generator = document.getGenerator()
    if not generator:
        print("## Oops:  Generator element not found")
    else:
        print("--   Product: ", generator.getProduct())
        print("--    Schema: ", generator.getSchemaVersion())
        print("-- Timestamp: ", generator.getTimestamp())
        print("--   Edition: ", generator.getExtra("def:edition_field", OvalDocument.NS_DEFAULT))
        
    print("\n")
    
    def_list = document.getDefinitions()
    if not def_list:
        print("## Oops:  No definitions found in OVAL document")
    else:
        print("  Number of definitions: ", len(def_list), ", Expected = 489")
    
    test_list = document.getTests()
    if not test_list:
        print("## Oops:  No tests found in OVAL document")
    else:
        print("  Number of tests: ", len(test_list), ", Expected = 1439")
        
    object_list = document.getObjects()
    if not object_list:
        print("## Oops:  No objects found in OVAL document")
    else:
        print("  Number of objects: ", len(object_list), ", Expected = 1276")

    state_list = document.getStates()
    if not state_list:
        print("## Oops:  No states found in OVAL document")
    else:
        print("  Number of states: ", len(state_list), ", Expected = 713")

    var_list = document.getVariables()
    if not var_list:
        print("  Number of variables: 0, Expected = 0")
    else:
        print("  Number of variables: ", len(var_list), ", Expected = ")
        
        
    print("\n  Find the definition with OVAL ID 'oval:org.mitre.oval:def:22382'")
    definition = document.getElementByID("oval:org.mitre.oval:def:22382")
    if definition is None:
        print("##  Oops:  definition not found")
    else:
        print("  Found.  Definition details:")
        print("      Class: ", definition.getClass())
        print("    Version: ", definition.getVersion())
        definition.incrementVersion()
        print("New Version: ", definition.getVersion())
        
        meta = definition.getMetadata()
        if meta is None:
            print("      ## Oops.  Definition metadata not found")
        else:
            print("      Title: ", meta.getTitle())
            
            repo = meta.getOvalRepositoryInformation()
            if repo is None:
                print("        ### Oops.  No repository information")
            else:
                print("     ----- From the repository ------")
                print("         Status: ", repo.getStatus())
                repo.setStatus("Munged")
                print("     New Status: ", repo.getStatus())
                print("    Min Version: ", repo.getMinimumSchemaVersion())
                repo.setMinimumSchemaVersion("5.9")
                print("New Min Version: ", repo.getMinimumSchemaVersion())