-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11439 from Honny1/most-used-rules
Tool for identifying the most used rules
- Loading branch information
Showing
7 changed files
with
265 additions
and
5 deletions.
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) |