Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tool for identifying the most used components #11730

Merged
merged 7 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion build-scripts/profile_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import argparse

try:
from utils.profile_tool import command_stats, command_sub, command_most_used_rules
from utils.profile_tool import (
command_stats,
command_sub,
command_most_used_rules,
command_most_used_components,
)
except ImportError:
print("The ssg module could not be found.")
print(
Expand Down Expand Up @@ -276,13 +281,31 @@ def parse_most_used_rules_subcommand(subparsers):
)


def parse_most_used_components(subparsers):
parser_most_used_components = subparsers.add_parser(
"most-used-components",
description=(
"Generates list of all components used by the rules in existing profiles."
" In various formats."
),
help="Generates list of all components used by the rules in existing profiles.",
)
parser_most_used_components.add_argument(
"--format",
default="plain",
choices=["plain", "json", "csv"],
help="Which format to use for output.",
)


def parse_args():
parser = argparse.ArgumentParser(description="Profile statistics and utilities tool")
subparsers = parser.add_subparsers(title="subcommands", dest="subcommand", required=True)

parse_stats_subcommand(subparsers)
parse_sub_subcommand(subparsers)
parse_most_used_rules_subcommand(subparsers)
parse_most_used_components(subparsers)

args = parser.parse_args()

Expand Down Expand Up @@ -319,6 +342,7 @@ def parse_args():
"stats": command_stats,
"sub": command_sub,
"most-used-rules": command_most_used_rules,
"most-used-components": command_most_used_components,
}


Expand Down
10 changes: 10 additions & 0 deletions docs/manual/developer/05_tools_and_utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ The result will be a list of rules with the number of uses in the profiles.
The list can be generated as plain text, JSON or CVS.
Via the `--format FORMAT` parameter.

The tool can also generate a list of the most used component based on rules contained in profiles from the entire project:

```bash
$ ./build-scripts/profile_tool.py most-used-components
```

The result will be a list of rules with the number of uses in the profiles.
The list can be generated as plain text, JSON or CVS.
Via the `--format FORMAT` parameter.

## Generating Controls from DISA's XCCDF Files

If you want a control file for product from DISA's XCCDF files you can run the following command:
Expand Down
1 change: 1 addition & 0 deletions utils/profile_tool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .sub import command_sub
from .stats import command_stats
from .most_used_rules import command_most_used_rules
from .most_used_components import command_most_used_components
70 changes: 70 additions & 0 deletions utils/profile_tool/most_used_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import json
import sys
import os
from ssg.components import Component
from .most_used_rules import _sorted_dict_by_num_value

PYTHON_2 = sys.version_info[0] < 3

if not PYTHON_2:
from .most_used_rules import _get_profiles_for_product
from ..controleval import (
load_controls_manager,
get_available_products,
)


def _count_components(components, rules_list, components_out):
for rule in rules_list:
component = get_component_name_by_rule_id(rule, components)
if component in components_out:
components_out[component] += 1
else:
components_out[component] = 1


def get_component_name_by_rule_id(rule_id, components):
for component in components.values():
if rule_id in component.rules:
return component.name
return "without_component"


def load_components(components_dir):
components = {}
for component_file in os.listdir(os.path.abspath(components_dir)):
component_path = os.path.join(components_dir, component_file)
component = Component(component_path)
components[component.name] = component
return components


def _process_all_products_from_controls(components_out):
components = load_components("./components/")
if PYTHON_2:
raise Exception("This feature is not supported for python2.")

for product in get_available_products():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this moment, we use "components" only for Fedora, RHEL 7, RHEL 8, RHEL 9 and RHEL 10 products. It doesn't make sense to include into stats any other products or any controls used in the products that don't use components.

One of consequences of this behavior is that we have without_component: 12125 which involves all rules that aren't part of linux_os benchmark.

If a product is using the "components" feature it sets the components_root key in its product.yml.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I have improved the product selection to those with components_root.

controls_manager = load_controls_manager("./controls/", product)
for profile in _get_profiles_for_product(controls_manager, product):
_count_components(components, profile.rules, components_out)


def command_most_used_components(args):
components = {}

_process_all_products_from_controls(components)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn't make sense to combine results from all products to a single number. Currently from the output we can see that the selinux component is used 1660 times. But this number is the count of rules multiplied by their occurence in profiles multiplied by their occurence in products.

I think that a product ID should be a parameter of this script and the results should be produced for the given product.

Copy link
Collaborator Author

@Honny1 Honny1 Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limitation on product will be done by #11733 .


sorted_components = _sorted_dict_by_num_value(components)

f_string = "{}: {}"

if args.format == "json":
print(json.dumps(sorted_components, indent=4))
return
elif args.format == "csv":
print("component_name,count_of_rules")
f_string = "{},{}"

for rule_id, rule_count in sorted_components.items():
print(f_string.format(rule_id, rule_count))
11 changes: 4 additions & 7 deletions utils/profile_tool/most_used_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ def _process_all_products_from_controls(rules):
_count_rules_per_rules_list(profile.rules, rules)


def _sorted_rules(rules):
sorted_rules = {
k: v
for k, v in sorted(rules.items(), key=lambda x: x[1], reverse=True)
}
return sorted_rules
def _sorted_dict_by_num_value(dict_):
sorted_ = {k: v for k, v in sorted(dict_.items(), key=lambda x: x[1], reverse=True)}
return sorted_


def command_most_used_rules(args):
Expand All @@ -65,7 +62,7 @@ def command_most_used_rules(args):
for benchmark in args.BENCHMARKS:
_count_rules_per_benchmark(benchmark, rules)

sorted_rules = _sorted_rules(rules)
sorted_rules = _sorted_dict_by_num_value(rules)

f_string = "{}: {}"

Expand Down
Loading