Skip to content

Commit

Permalink
Add processing of controlfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Honny1 committed Jan 17, 2024
1 parent 8f44246 commit e9e7849
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions utils/profile_tool/most_used_rules.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import json

from ssg.build_profile import XCCDFBenchmark
from .profile import get_profile
from ..controleval import (
load_controls_manager,
get_available_products,
get_product_profiles_files,
)


def _count_rules_per_profile(profile, rules):
for rule in profile.get("rules", []):
def _count_rules_per_rules_list(rules_list, rules):
for rule in rules_list:
if rule in rules:
rules[rule] += 1
else:
Expand All @@ -14,15 +20,39 @@ def _count_rules_per_profile(profile, rules):
def _count_rules_per_benchmark(benchmark, rules):
benchmark = XCCDFBenchmark(benchmark)
for profile in benchmark.get_all_profile_stats():
_count_rules_per_profile(profile, rules)
_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(product, profiles_files, file, ctrls_mgr.policies))
return profiles


def _process_all_products_from_controls(rules):
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 command_most_used_rules(args):
rules = {}
for benchmark in args.benchmarks:
_count_rules_per_benchmark(benchmark, rules)

sorted_rules = {k: v for k, v in sorted(rules.items(), key=lambda x: x[1], reverse=True)}
if len(args.BENCHMARKS) == 0:
_process_all_products_from_controls(rules)
else:
for benchmark in args.BENCHMARKS:
_count_rules_per_benchmark(benchmark, rules)

sorted_alphabetically_rules = dict(sorted(rules.items()))
sorted_rules = {
k: v
for k, v in sorted(sorted_alphabetically_rules.items(), key=lambda x: x[1], reverse=True)
}

f_string = "{}: {}"

Expand Down

0 comments on commit e9e7849

Please sign in to comment.