refactor(mutelist): use jsonschema on mutelist (#6264)

This commit is contained in:
Pedro Martín
2025-01-10 15:19:20 +01:00
committed by GitHub
parent 42dbefbb31
commit b0fe696935
2 changed files with 76 additions and 26 deletions
-24
View File
@@ -1,24 +0,0 @@
from schema import Optional, Schema
mutelist_schema = Schema(
{
"Accounts": {
str: {
"Checks": {
str: {
"Regions": list,
"Resources": list,
Optional("Tags"): list,
Optional("Exceptions"): {
Optional("Accounts"): list,
Optional("Regions"): list,
Optional("Resources"): list,
Optional("Tags"): list,
},
Optional("Description"): str,
}
}
}
}
}
)
+76 -2
View File
@@ -2,12 +2,86 @@ import re
from abc import ABC, abstractmethod
import yaml
from jsonschema import validate
from prowler.lib.logger import logger
from prowler.lib.mutelist.models import mutelist_schema
from prowler.lib.outputs.common import Status
from prowler.lib.outputs.utils import unroll_dict, unroll_tags
mutelist_schema = {
"type": "object",
"properties": {
"Accounts": {
"type": "object",
"patternProperties": {
".*": { # Match any account
"type": "object",
"properties": {
"Checks": {
"type": "object",
"patternProperties": {
".*": { # Match any check
"type": "object",
"properties": {
"Regions": {
"type": "array",
"items": {"type": "string"},
},
"Resources": {
"type": "array",
"items": {"type": "string"},
},
"Tags": { # Optional field
"type": "array",
"items": {"type": "string"},
},
"Exceptions": { # Optional field
"type": "object",
"properties": {
"Accounts": { # Optional field
"type": "array",
"items": {"type": "string"},
},
"Regions": { # Optional field
"type": "array",
"items": {"type": "string"},
},
"Resources": { # Optional field
"type": "array",
"items": {"type": "string"},
},
"Tags": { # Optional field
"type": "array",
"items": {"type": "string"},
},
},
"additionalProperties": False,
},
"Description": { # Optional field
"type": "string",
},
},
"required": [
"Regions",
"Resources",
], # Mandatory within a check
"additionalProperties": False,
}
},
"additionalProperties": False,
},
},
"required": ["Checks"], # Mandatory within an account
"additionalProperties": False,
}
},
"additionalProperties": False,
}
},
"required": ["Accounts"], # Accounts is mandatory at the root level
"additionalProperties": False,
}
class Mutelist(ABC):
"""
@@ -70,7 +144,7 @@ class Mutelist(ABC):
def validate_mutelist(self) -> bool:
try:
self._mutelist = mutelist_schema.validate(self._mutelist)
validate(self._mutelist, schema=mutelist_schema)
return True
except Exception as error:
logger.error(