feat(checks): Select checks to run from provider using -c/--checks (#1197)

* feat(checks): Select checks to run

* Update providers/aws/services/iam/iam_disable_30_days_credentials/iam_disable_30_days_credentials.py

Co-authored-by: Sergio Garcia <38561120+sergargar@users.noreply.github.com>
This commit is contained in:
Pepe Fagoaga
2022-06-16 12:20:03 +02:00
committed by GitHub
parent 33c6801501
commit 162852634e
4 changed files with 95 additions and 58 deletions

View File

@@ -1,7 +1,57 @@
import importlib
import json
import pkgutil
from abc import ABC, abstractmethod
from dataclasses import dataclass
from lib.logger import logger
from lib.outputs import report
def load_checks_to_execute(check_list, provider):
checks_to_execute = set()
# LOADER
# Handle if there are checks passed using -c/--checks
if check_list:
for check_name in check_list:
checks_to_execute.add(check_name)
# If there are no checks passed as argument
else:
# Get all check modules to run with the specific provider
modules = recover_modules_from_provider(provider)
for check_module in modules:
# Recover check name from import path (last part)
# Format: "providers.{provider}.services.{service}.{check_name}.{check_name}"
check_name = check_module.split(".")[-1]
checks_to_execute.add(check_name)
return checks_to_execute
def recover_modules_from_provider(provider):
modules = []
for module_name in pkgutil.walk_packages(
importlib.import_module(f"providers.{provider}.services").__path__,
importlib.import_module(f"providers.{provider}.services").__name__ + ".",
):
# Format: "providers.{provider}.services.{service}.{check_name}.{check_name}"
if module_name.name.count(".") == 5:
modules.append(module_name.name)
return modules
def run_check(check):
print(f"\nCheck Name: {check.CheckName}")
logger.debug(f"Executing check: {check.CheckName}")
findings = check.execute()
report(findings)
def import_check(check_path):
lib = importlib.import_module(f"{check_path}")
return lib
@dataclass
class Check_Report:
@@ -9,6 +59,11 @@ class Check_Report:
region: str
result_extended: str
def __init__(self):
self.status = ""
self.region = ""
self.result_extended = ""
class Check(ABC):
def __init__(self):

14
lib/check_test.py Normal file
View File

@@ -0,0 +1,14 @@
import importlib
class Test_Check:
def test_import_check(self):
test_cases = [
{
"name": "Test valid check path",
"input": "providers.aws.services.iam.iam_disable_30_days_credentials.iam_disable_30_days_credentials",
"expected": "providers.aws.services.iam.iam_disable_30_days_credentials.iam_disable_30_days_credentials",
}
]
for test in test_cases:
assert importlib.import_module(test["input"]).__name__ == test["expected"]