ssg.rule_dir_stats module
This module contains common code shared by utils/rule_dir_stats.py and utils/rule_dir_diff.py. This code includes functions for walking the output of the utils/rule_dir_json.py script, and filtering functions used in both scripts.
- ssg.rule_dir_stats.filter_rule_ids(all_keys, queries)[source]
Filters a set of keys based on a set of queries.
A set of queries is a comma separated list of queries, where a query is either a rule id or a substring thereof.
- Parameters:
all_keys (iterable) – An iterable containing all possible keys.
queries (str) – A comma-separated list of queries, where each query is either a rule id or a substring thereof. If the literal string “all” is provided, all keys are returned.
- Returns:
- A set of keys from all_keys that match any of the queries. If queries is empty, an
empty set is returned. If queries is “all”, all keys are returned.
- Return type:
set
- ssg.rule_dir_stats.get_affected_products(rule_obj)[source]
Extracts and returns the set of affected products from a given rule object.
- Parameters:
rule_obj (dict) – A dictionary representing a rule, which contains a ‘products’ key.
- Returns:
A set of products affected by the rule.
- Return type:
set
- ssg.rule_dir_stats.get_all_affected_products(args, rule_obj)[source]
From a rule_obj, return the set of affected products from rule.yml, and all fixes and checks.
If args.strict is set, this function is equivalent to get_affected_products. Otherwise, it includes ovals and fix content based on the values of args.fixes_only and args.ovals_only.
- Parameters:
args (Namespace) – The arguments passed to the script, containing flags such as strict, fixes_only, and ovals_only.
rule_obj (dict) – The rule object containing information about the rule, including affected products, oval products, and remediation products.
- Returns:
A set of affected products based on the rule object and the provided arguments.
- Return type:
set
- ssg.rule_dir_stats.missing_oval(rule_obj)[source]
For a rule object, check if it is missing an OVAL.
- Parameters:
rule_obj (dict) – A dictionary representing the rule object. It must contain the keys ‘id’ and ‘ovals’.
- Returns:
- A message indicating the rule ID that is missing all OVALs, or None if the rule has
OVALs.
- Return type:
str
- ssg.rule_dir_stats.missing_remediation(rule_obj, r_type)[source]
Check if a rule object is missing a remediation of a specified type.
- Parameters:
rule_obj (dict) – The rule object containing rule details.
r_type (str) – The type of remediation to check for.
- Returns:
- A message indicating the rule ID and the missing remediation type, if the remediation
is missing. Otherwise, returns None.
- Return type:
str
- ssg.rule_dir_stats.product_names_oval(rule_obj)[source]
Checks the consistency between the product names and OVAL object names for a given rule object.
- Parameters:
rule_obj (dict) –
A dictionary representing a rule object. It should contain: - ‘id’ (str): The identifier of the rule. - ‘ovals’ (dict): A dictionary where keys are OVAL filenames and values are
dictionaries containing ‘products’ (list of product names).
- Returns:
- A message indicating if there is a mismatch between the product name and OVAL object
name. The message format is “rule_id:<rule_id> has a different product and OVALs names: <product> is not <oval_product>”. Returns None if all product names match their corresponding OVAL object names.
- Return type:
str
- ssg.rule_dir_stats.product_names_remediation(rule_obj, r_type)[source]
Checks the consistency between the scope of platforms and the product names of the remediations of a given type for a rule object.
- Parameters:
rule_obj (dict) – A dictionary representing the rule object, which contains an ‘id’ key and a ‘remediations’ key. The ‘remediations’ key is a dictionary where keys are remediation types and values are dictionaries of remediation names and their associated products.
r_type (str) – The type of remediation to check (e.g., ‘bash’, ‘ansible’).
- Returns:
- A message indicating the rule ID and the inconsistency found, if any.
The message specifies the rule ID, the remediation type, the product name that is inconsistent, and the expected product name. Returns None if no inconsistencies are found.
- Return type:
str
- ssg.rule_dir_stats.two_plus_oval(rule_obj)[source]
Check if a rule object has two or more OVALs.
- Parameters:
rule_obj (dict) – A dictionary representing a rule object. It should have the following keys: - ‘id’ (str): The identifier of the rule. - ‘ovals’ (list): A list of OVAL identifiers associated with the rule.
- Returns:
- A formatted string indicating the rule ID and its associated OVALs if there are two
or more OVALs.
- Return type:
str
- ssg.rule_dir_stats.two_plus_remediation(rule_obj, r_type)[source]
Check if a rule object has two or more remediations of a specified type.
- Parameters:
rule_obj (dict) – The rule object containing rule details and remediations.
r_type (str) – The type of remediation to check for.
- Returns:
- A formatted string indicating the rule ID and the remediations if there are two or
more of the specified type.
- Return type:
str
- ssg.rule_dir_stats.walk_rule_stats(rule_output)[source]
Walk the output of a rule, generating statistics about affected ovals, remediations, and generating verbose output in a stable order.
- Parameters:
rule_output (dict) – The output of a rule containing information about ovals and remediations.
- Returns:
- A tuple containing the following elements:
affected_ovals (int): The number of affected ovals.
affected_remediations (int): The number of affected remediations.
all_affected_remediations (int): The number of rules where all remediations are affected.
affected_remediations_type (defaultdict): A dictionary with the count of each type of affected remediation.
all_output (list): A list of all affected ovals and remediations in a stable order.
- Return type:
tuple
- ssg.rule_dir_stats.walk_rules(args, known_rules, oval_func, remediation_func)[source]
Walks through a dictionary of known rules, conditionally calling provided functions to generate OVAL and remediation content, and returns the number of visited rules along with the output for each visited rule.
- Parameters:
args (object) – An object containing arguments that control the behavior of the function.
known_rules (dict) – A dictionary where keys are rule IDs and values are rule objects.
oval_func (function) – A function to be called for each rule to generate OVAL content.
remediation_func (function) – A function to be called for each rule to generate remediation content.
- Returns:
affected_rules (int): The number of rules that were visited. verbose_output (dict): A dictionary containing the output for each visited rule.
- Return type:
tuple
The output structure is a dict as follows:
{ rule_id: { "oval": oval_func(args, rule_obj), "ansible": remediation_func(args, "ansible", rule_obj), "anaconda": remediation_func(args, "anaconda", rule_obj), "bash": remediation_func(args, "bash", rule_obj), "puppet": remediation_func(args, "puppet", rule_obj) }, ... }
The arguments supplied to oval_func are args and rule_obj. The arguments supplied to remediation_func are args, the remediation type, and rule_obj. The input rule_obj structure is the value of known_rules[rule_id].
- ssg.rule_dir_stats.walk_rules_diff(args, left_rules, right_rules, oval_func, remediation_func)[source]
Walk through two dictionaries of known rules and generate five sets of output.
Does not understand renaming of rule_ids as this would depend on disk content to reflect these differences. Unless significantly more data is added to the rule_obj structure (contents of rule.yml, ovals, remediations, etc.), all information besides ‘title’ is not uniquely identifying or could be easily updated.
- Parameters:
args – Arguments to be passed to the walk_rules and walk_rules_parallel functions.
left_rules (dict) – Dictionary of rules on the left side.
right_rules (dict) – Dictionary of rules on the right side.
oval_func (function) – Function to process OVAL definitions.
remediation_func (function) – Function to process remediation scripts.
- Returns:
- A five-tuple containing:
left_only_data: Data for rules only in left_rules.
right_only_data: Data for rules only in right_rules.
left_changed_data: Data for rules in both left_rules and right_rules but changed in left_rules.
right_changed_data: Data for rules in both left_rules and right_rules but changed in right_rules.
common_data: Data for rules common to both left_rules and right_rules.
- Return type:
tuple
- ssg.rule_dir_stats.walk_rules_diff_stats(results)[source]
Takes the results of walk_rules_diff (results) and generates five sets of output statistics.
- Parameters:
results (list) – A list of five elements, where each element is a tuple containing affected rules and verbose output.
- Returns:
- A tuple containing five elements, each representing the statistics for left_only
rules, right_only rules, shared left rules, shared right rules, and shared common rules. Each element in the tuple is itself a tuple containing: - affected_rules (int): Number of affected rules. - affected_ovals (int): Number of affected OVAL definitions. - affected_remediations (int): Number of affected remediations. - all_affected_remediations (int): Total number of affected remediations. - affected_remediations_type (dict): Dictionary with remediation types as keys and
counts as values.
all_output (list): List of all output data.
- Return type:
tuple
- Raises:
AssertionError – If the length of results is not 5 or the length of output_data is not 5.
- ssg.rule_dir_stats.walk_rules_parallel(args, left_rules, right_rules, oval_func, remediation_func)[source]
Walks two sets of known_rules (left_rules and right_rules) with identical keys and returns left_only, right_only, and common_only output from _walk_rule.
If the outputted data for a rule when called on left_rules and right_rules is the same, it is added to common_only. Only rules which output different data will have their data added to left_only and right_only respectively.
- Parameters:
args – Arguments to be passed to the _walk_rule function.
left_rules (dict) – Dictionary of rules on the left side.
right_rules (dict) – Dictionary of rules on the right side.
oval_func (function) – Function to process OVAL definitions.
remediation_func (function) – Function to process remediation scripts.
- Returns:
- A tuple containing three elements:
left_only (tuple): A tuple containing the count of affected rules and the verbose output for rules only in left_rules.
right_only (tuple): A tuple containing the count of affected rules and the verbose output for rules only in right_rules.
common_only (tuple): A tuple containing the count of affected rules and the verbose output for rules common to both left_rules and right_rules.
- Return type:
tuple
- Raises:
AssertionError – If the sets of keys in left_rules and right_rules are not identical.
- ssg.rule_dir_stats.walk_rules_stats(args, known_rules, oval_func, remediation_func)[source]
Walk a dictionary of known_rules and generate simple aggregate statistics for all visited rules.
The oval_func and remediation_func arguments behave according to walk_rules(). An effort is made to provide consistently ordered verbose_output by sorting all visited keys and the keys of ssg.build_remediations.REMEDIATION_MAP.
- Parameters:
args – Arguments passed to the function.
known_rules (dict) – A dictionary of known rules to be processed.
oval_func (function) – Function to process OVAL definitions.
remediation_func (function) – Function to process remediations.
- Returns:
- A tuple containing:
affected_rules (int): Number of affected rules.
affected_ovals (int): Number of affected OVAL definitions.
affected_remediations (int): Number of affected remediations.
all_affected_remediations (int): Total number of affected remediations.
affected_remediations_type (dict): Dictionary with the count of each type of affected remediation.
all_output (list): Ordered output of all functions.
- Return type:
tuple