mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 21:11:53 +00:00
fix: allow raising exceptions from validate_mutelist (#8086)
This commit is contained in:
@@ -439,12 +439,13 @@ class Mutelist(ABC):
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def validate_mutelist(mutelist: dict) -> dict:
|
||||
def validate_mutelist(mutelist: dict, raise_on_exception: bool = False) -> dict:
|
||||
"""
|
||||
Validate the mutelist against the schema.
|
||||
|
||||
Args:
|
||||
mutelist (dict): The mutelist to be validated.
|
||||
raise_on_exception (bool): Whether to raise an exception if the mutelist is invalid.
|
||||
|
||||
Returns:
|
||||
dict: The mutelist itself.
|
||||
@@ -453,7 +454,10 @@ class Mutelist(ABC):
|
||||
validate(mutelist, schema=mutelist_schema)
|
||||
return mutelist
|
||||
except Exception as error:
|
||||
logger.error(
|
||||
f"{error.__class__.__name__} -- Mutelist YAML is malformed - {error}[{error.__traceback__.tb_lineno}]"
|
||||
)
|
||||
if raise_on_exception:
|
||||
raise error
|
||||
else:
|
||||
logger.error(
|
||||
f"{error.__class__.__name__} -- Mutelist YAML is malformed - {error}[{error.__traceback__.tb_lineno}]"
|
||||
)
|
||||
return {}
|
||||
|
||||
@@ -7,6 +7,7 @@ import yaml
|
||||
from boto3 import client, resource
|
||||
from mock import MagicMock, patch
|
||||
from moto import mock_aws
|
||||
import pytest
|
||||
|
||||
from prowler.config.config import encoding_format_utf_8
|
||||
from prowler.providers.aws.lib.mutelist.mutelist import AWSMutelist
|
||||
@@ -321,6 +322,30 @@ class TestAWSMutelist:
|
||||
assert mutelist.mutelist == {}
|
||||
assert mutelist.mutelist_file_path is None
|
||||
|
||||
def test_validate_mutelist_raise_on_exception(self):
|
||||
mutelist_path = MUTELIST_FIXTURE_PATH
|
||||
with open(mutelist_path) as f:
|
||||
mutelist_fixture = yaml.safe_load(f)["Mutelist"]
|
||||
|
||||
# Create an invalid mutelist by adding an invalid key
|
||||
invalid_mutelist = mutelist_fixture.copy()
|
||||
invalid_mutelist["Accounts1"] = invalid_mutelist["Accounts"]
|
||||
del invalid_mutelist["Accounts"]
|
||||
|
||||
mutelist = AWSMutelist(mutelist_content=mutelist_fixture)
|
||||
|
||||
# Test that it raises an exception when raise_on_exception=True
|
||||
with pytest.raises(Exception):
|
||||
mutelist.validate_mutelist(invalid_mutelist, raise_on_exception=True)
|
||||
|
||||
# Test that it doesn't raise an exception when raise_on_exception=False (default)
|
||||
result = mutelist.validate_mutelist(invalid_mutelist, raise_on_exception=False)
|
||||
assert result == {}
|
||||
|
||||
# Test that it doesn't raise an exception when raise_on_exception is not specified
|
||||
result = mutelist.validate_mutelist(invalid_mutelist)
|
||||
assert result == {}
|
||||
|
||||
def test_mutelist_findings_only_wildcard(self):
|
||||
# Mutelist
|
||||
mutelist_content = {
|
||||
|
||||
Reference in New Issue
Block a user