Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(check_report): Do not allow empty values #6732

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
104 changes: 65 additions & 39 deletions prowler/lib/check/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def execute(self) -> list:


@dataclass
class Check_Report:
class CheckReport:
"""Contains the Check's finding information."""

status: str
Expand Down Expand Up @@ -440,30 +440,43 @@ def __init__(self, metadata: Dict, resource: Any) -> None:


@dataclass
class Check_Report_AWS(Check_Report):
"""Contains the AWS Check's finding information."""
class Check_Report_AWS(CheckReport):
"""
Contains the AWS Check's finding information.

Attributes:
resource_id (str): The resource ID.
resource_arn (str): The resource ARN.
region (str): The region of the resource.
Raises:
AttributeError: If the any of the required attributes are not present.
"""

resource_id: str
resource_arn: str
region: str

def __init__(self, metadata: Dict, resource: Any) -> None:
super().__init__(metadata, resource)
self.resource_id = (
getattr(resource, "id", None) or getattr(resource, "name", None) or ""
)
self.resource_arn = getattr(resource, "arn", "")
self.region = getattr(resource, "region", "")
# Use resource.name as fallback if no resource.id
# If no name or id, an exception is raised
try:
self.resource_id = getattr(resource, "id")
except AttributeError:
self.resource_id = getattr(resource, "name")
# ARN and Region must be present
self.resource_arn = getattr(resource, "arn")
self.region = getattr(resource, "region")


@dataclass
class Check_Report_Azure(Check_Report):
class Check_Report_Azure(CheckReport):
"""Contains the Azure Check's finding information."""

resource_name: str
resource_id: str
subscription: str
location: str
subscription: str = None

def __init__(self, metadata: Dict, resource: Any) -> None:
"""Initialize the Azure Check's finding information.
Expand All @@ -473,16 +486,21 @@ def __init__(self, metadata: Dict, resource: Any) -> None:
resource: Basic information about the resource. Defaults to None.
"""
super().__init__(metadata, resource)
self.resource_name = getattr(
resource, "name", getattr(resource, "resource_name", "")
)
self.resource_id = getattr(resource, "id", getattr(resource, "resource_id", ""))
self.subscription = ""
try:
self.resource_id = getattr(resource, "id")
except AttributeError:
self.resource_id = getattr(resource, "resource_id")

try:
self.resource_name = getattr(resource, "name")
except AttributeError:
self.resource_name = getattr(resource, "resource_name")

self.location = getattr(resource, "location", "global")


@dataclass
class Check_Report_GCP(Check_Report):
class Check_Report_GCP(CheckReport):
"""Contains the GCP Check's finding information."""

resource_name: str
Expand All @@ -500,23 +518,30 @@ def __init__(
project_id=None,
) -> None:
super().__init__(metadata, resource)
self.resource_id = (
resource_id
or getattr(resource, "id", None)
or getattr(resource, "name", None)
or ""
)
self.resource_name = resource_name or getattr(resource, "name", "")
self.project_id = project_id or getattr(resource, "project_id", "")
self.location = (
location
or getattr(resource, "location", "")
or getattr(resource, "region", "")
)

self.resource_name = resource_name or getattr(resource, "name")

if resource_id:
self.resource_id = resource_id
else:
try:
self.resource_id = getattr(resource, "id")
except AttributeError:
self.resource_id = self.resource_name

self.project_id = project_id or getattr(resource, "project_id")

if location:
self.location = location
else:
try:
self.location = getattr(resource, "location")
except AttributeError:
self.location = getattr(resource, "region")


@dataclass
class Check_Report_Kubernetes(Check_Report):
class Check_Report_Kubernetes(CheckReport):
# TODO change class name to CheckReportKubernetes
"""Contains the Kubernetes Check's finding information."""

Expand All @@ -526,17 +551,18 @@ class Check_Report_Kubernetes(Check_Report):

def __init__(self, metadata: Dict, resource: Any) -> None:
super().__init__(metadata, resource)
self.resource_id = (
getattr(resource, "uid", None) or getattr(resource, "name", None) or ""
)
self.resource_name = getattr(resource, "name", "")
self.resource_name = getattr(resource, "name")
try:
self.resource_id = getattr(resource, "uid")
except AttributeError:
self.resource_id = self.resource_name

self.namespace = getattr(resource, "namespace", "cluster-wide")
if not self.namespace:
self.namespace = "cluster-wide"


# TODO: review this logic and add tests
@dataclass
class Check_Report_Microsoft365(Check_Report):
class Check_Report_Microsoft365(CheckReport):
"""Contains the Microsoft365 Check's finding information."""

resource_name: str
Expand All @@ -552,9 +578,9 @@ def __init__(self, metadata: Dict, resource: Any) -> None:
"""
super().__init__(metadata, resource)
self.resource_name = getattr(
resource, "name", getattr(resource, "resource_name", "")
resource, "name", getattr(resource, "resource_name")
)
self.resource_id = getattr(resource, "id", getattr(resource, "resource_id", ""))
self.resource_id = getattr(resource, "id", getattr(resource, "resource_id"))
self.location = getattr(resource, "location", "global")


Expand Down
6 changes: 3 additions & 3 deletions prowler/lib/outputs/compliance/compliance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from prowler.lib.check.models import Check_Report
from prowler.lib.check.models import CheckReport
from prowler.lib.logger import logger
from prowler.lib.outputs.compliance.cis.cis import get_cis_table
from prowler.lib.outputs.compliance.ens.ens import get_ens_table
Expand Down Expand Up @@ -90,7 +90,7 @@ def display_compliance_table(

# TODO: this should be in the Check class
def get_check_compliance(
finding: Check_Report, provider_type: str, bulk_checks_metadata: dict
finding: CheckReport, provider_type: str, bulk_checks_metadata: dict
) -> dict:
"""get_check_compliance returns a map with the compliance framework as key and the requirements where the finding's check is present.

Expand All @@ -102,7 +102,7 @@ def get_check_compliance(
}

Args:
finding (Any): The Check_Report finding
finding (Any): The CheckReport finding
provider_type (str): The provider type
bulk_checks_metadata (dict): The bulk checks metadata

Expand Down
6 changes: 3 additions & 3 deletions prowler/lib/outputs/finding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import BaseModel, Field, ValidationError

from prowler.config.config import prowler_version
from prowler.lib.check.models import Check_Report, CheckMetadata
from prowler.lib.check.models import CheckMetadata, CheckReport
from prowler.lib.logger import logger
from prowler.lib.outputs.common import Status, fill_common_finding_data
from prowler.lib.outputs.compliance.compliance import get_check_compliance
Expand Down Expand Up @@ -91,13 +91,13 @@ def get_metadata(self) -> dict:

@classmethod
def generate_output(
cls, provider: Provider, check_output: Check_Report, output_options
cls, provider: Provider, check_output: CheckReport, output_options
) -> "Finding":
"""Generates the output for a finding based on the provider and output options

Args:
provider (Provider): the provider object
check_output (Check_Report): the check output object
check_output (CheckReport): the check output object
output_options: the output options object, depending on the provider
Returns:
finding_output (Finding): the finding output object
Expand Down
Loading
Loading