"""
A couple generic XCCDF utilities used by build_all_guides.py and
build_all_remediations.py
Author: Martin Preisler <mpreisle@redhat.com>
"""
from __future__ import absolute_import
import re
from .constants import XCCDF12_NS
# if a profile ID ends with a string listed here we skip it
PROFILE_ID_SKIPLIST = ["test", "index", "default"]
# filler XCCDF 1.2 prefix which we will strip to avoid very long filenames
PROFILE_ID_PREFIX = ("^xccdf_org.*content_profile_")
[docs]
def get_benchmark_id_title_map(input_tree):
"""
Extracts a mapping of benchmark IDs to their titles from an XML tree.
Args:
input_tree (xml.etree.ElementTree.ElementTree): The XML tree containing benchmark data.
Returns:
dict: A dictionary where the keys are benchmark IDs (str) and the values are benchmark
titles (str).
"""
input_root = input_tree.getroot()
ret = {}
candidates = []
scrape_benchmarks(input_root, XCCDF12_NS, candidates)
for _, elem in candidates:
_id = elem.get("id")
if _id is None:
continue
title = "<unknown>"
for element in elem.findall("{%s}title" % (XCCDF12_NS)):
title = element.text
break
ret[_id] = title
return ret
[docs]
def get_profile_short_id(long_id):
"""
Shortens the given profile ID if it matches the XCCDF 1.2 long ID format.
Args:
long_id (str): The long profile ID to be shortened.
Returns:
str: The shortened profile ID if the long ID matches the XCCDF 1.2 format, otherwise
returns the original long ID.
"""
if re.search(PROFILE_ID_PREFIX, long_id):
return long_id[re.search(PROFILE_ID_PREFIX, long_id).end():]
return long_id
[docs]
def scrape_benchmarks(root, namespace, dest):
"""
Add all benchmark elements in root to dest list.
This function searches for all elements with the tag 'Benchmark' within the given XML root
element, using the specified namespace. It then adds these elements to the destination list
'dest' along with their namespace. If the root element itself is a 'Benchmark', it is also
added to the list.
Args:
root (xml.etree.ElementTree.Element): The root XML element to search within.
namespace (str): The XML namespace to use when searching for 'Benchmark' elements.
dest (list): The list to which found 'Benchmark' elements and their namespace will be added.
Returns:
None
"""
dest.extend([
(namespace, elem)
for elem in list(root.findall(".//{%s}Benchmark" % (namespace)))
])
if root.tag == "{%s}Benchmark" % (namespace):
dest.append((namespace, root))