-
Notifications
You must be signed in to change notification settings - Fork 705
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 rules #11439
Merged
marcusburghardt
merged 10 commits into
ComplianceAsCode:master
from
Honny1:most-used-rules
Feb 29, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab8e424
Add subcommand with arguments
Honny1 4d204ce
Add getter of profiles stats
Honny1 7afd088
Add calculation of most used rules
Honny1 29776c2
Add documentation for a new script capability
Honny1 ed3c530
Add test
Honny1 2bc6052
Change command line arguments
Honny1 8d9c7de
Add profile collected from controll files
Honny1 f68e1e8
Add processing of controlfiles
Honny1 34cb17f
Update docuentation
Honny1 18bfd53
Fix python2 support
Honny1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import sys | ||
import pytest | ||
from argparse import Namespace | ||
from utils.profile_tool import command_most_used_rules | ||
|
||
DATA_DIR = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "..", "ssg-module", "data") | ||
) | ||
DATA_STREAM_PATH = os.path.join(DATA_DIR, "simple_data_stream.xml") | ||
|
||
|
||
def get_fake_args(): | ||
return Namespace( | ||
subcommand="most-used-rules", BENCHMARKS=[str(DATA_STREAM_PATH)], format="plain" | ||
) | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info[0] < 3, reason="requires python3") | ||
def test_command(capsys): | ||
command_most_used_rules(get_fake_args()) | ||
captured = capsys.readouterr() | ||
assert "xccdf_com.example.www_rule_test-pass: 1" in captured.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .sub import command_sub | ||
from .stats import command_stats | ||
from .most_used_rules import command_most_used_rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import sys | ||
import json | ||
|
||
from ssg.build_profile import XCCDFBenchmark | ||
|
||
|
||
PYTHON_2 = sys.version_info[0] < 3 | ||
|
||
if not PYTHON_2: | ||
from .profile import get_profile | ||
from ..controleval import ( | ||
load_controls_manager, | ||
get_available_products, | ||
get_product_profiles_files, | ||
) | ||
|
||
|
||
def _count_rules_per_rules_list(rules_list, rules): | ||
for rule in rules_list: | ||
if rule in rules: | ||
rules[rule] += 1 | ||
else: | ||
rules[rule] = 1 | ||
|
||
|
||
def _count_rules_per_benchmark(benchmark, rules): | ||
benchmark = XCCDFBenchmark(benchmark) | ||
for profile in benchmark.get_all_profile_stats(): | ||
_count_rules_per_rules_list(profile.get("rules", []), rules) | ||
|
||
|
||
def _get_profiles_for_product(ctrls_mgr, product): | ||
profiles_files = get_product_profiles_files(product) | ||
|
||
profiles = [] | ||
for file in profiles_files: | ||
profiles.append(get_profile(profiles_files, file, ctrls_mgr.policies)) | ||
return profiles | ||
|
||
|
||
def _process_all_products_from_controls(rules): | ||
if PYTHON_2: | ||
raise Exception("This feature is not supported for python2.") | ||
|
||
for product in get_available_products(): | ||
controls_manager = load_controls_manager("./controls/", product) | ||
for profile in _get_profiles_for_product(controls_manager, product): | ||
_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 command_most_used_rules(args): | ||
rules = {} | ||
|
||
if not args.BENCHMARKS: | ||
_process_all_products_from_controls(rules) | ||
else: | ||
for benchmark in args.BENCHMARKS: | ||
_count_rules_per_benchmark(benchmark, rules) | ||
|
||
sorted_rules = _sorted_rules(rules) | ||
|
||
f_string = "{}: {}" | ||
|
||
if args.format == "json": | ||
print(json.dumps(sorted_rules, indent=4)) | ||
return | ||
elif args.format == "csv": | ||
print("rule_id,count_of_profiles") | ||
f_string = "{},{}" | ||
|
||
for rule_id, rule_count in sorted_rules.items(): | ||
print(f_string.format(rule_id, rule_count)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from ..controleval import get_parameter_from_yaml | ||
|
||
|
||
def _get_extends_profile_path(profiles_files, profile_name): | ||
for profile_path in profiles_files: | ||
if f"{profile_name}.profile" in profile_path: | ||
return profile_path | ||
return None | ||
|
||
|
||
def _process_extends(profiles_files, file, policies, profile): | ||
extends = get_parameter_from_yaml(file, "extends") | ||
if isinstance(extends, str): | ||
profile_path = _get_extends_profile_path(profiles_files, extends) | ||
if profile_path is None: | ||
raise Exception("There is no Extension '{}' Profile.".format(extends)) | ||
profile = get_profile(profiles_files, profile_path, policies, profile) | ||
|
||
|
||
def _process_selections(file, profile, policies): | ||
selections = get_parameter_from_yaml(file, "selections") | ||
for selected in selections: | ||
if ":" in selected and "=" not in selected: | ||
profile.add_from_policy(policies, selected) | ||
else: | ||
profile.add_rule(selected) | ||
profile.clean_rules() | ||
|
||
|
||
def get_profile(profiles_files, file, policies, profile=None): | ||
if profile is None: | ||
title = get_parameter_from_yaml(file, "title") | ||
profile = Profile(file, title) | ||
|
||
_process_extends(profiles_files, file, policies, profile) | ||
|
||
_process_selections(file, profile, policies) | ||
return profile | ||
|
||
|
||
class Profile: | ||
def __init__(self, path, title): | ||
self.path = path | ||
self.title = title | ||
self.rules = [] | ||
self.unselected_rules = [] | ||
|
||
def add_rule(self, rule_id): | ||
if rule_id.startswith("!"): | ||
self.unselected_rules.append(rule_id) | ||
return | ||
if "=" not in rule_id: | ||
self.rules.append(rule_id) | ||
|
||
def add_rules(self, rules): | ||
for rule in rules: | ||
self.add_rule(rule) | ||
|
||
def clean_rules(self): | ||
for rule in self.unselected_rules: | ||
rule_ = rule.replace("!", "") | ||
if rule_ in self.rules: | ||
self.rules.remove(rule_) | ||
|
||
@staticmethod | ||
def _get_sel(selected): | ||
policy = None | ||
control = None | ||
level = None | ||
if selected.count(":") == 2: | ||
policy, control, level = selected.split(":") | ||
else: | ||
policy, control = selected.split(":") | ||
return policy, control, level | ||
|
||
@staticmethod | ||
def _get_levels(policy, level): | ||
levels = [level] | ||
if policy.levels_by_id.get(level).inherits_from is not None: | ||
levels.extend(policy.levels_by_id.get(level).inherits_from) | ||
return levels | ||
|
||
def add_from_policy(self, policies, selected): | ||
policy_id, control, level = self._get_sel(selected) | ||
policy = policies[policy_id] | ||
|
||
if control != "all": | ||
self.add_rules(policy.controls_by_id[control].rules) | ||
return | ||
|
||
if level is None: | ||
for control in policy.controls: | ||
self.add_rules(control.rules) | ||
return | ||
|
||
levels = self._get_levels(policy, level) | ||
for control in policy.controls: | ||
intersection = set(control.levels) & set(levels) | ||
if len(intersection) >= 1: | ||
self.add_rules(control.rules) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered to also count the variables? I think it is easy to extend and this information might be useful as well.
If so, the next function can also be renamed from
add_rule
toadd_rule_or_var
, for example. However, it would be probably better to extend this in another PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it can easily be expanded if necessary.