-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(aws): add AWS Well-Architected output class
- Loading branch information
Showing
14 changed files
with
294 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
101 changes: 101 additions & 0 deletions
101
prowler/lib/outputs/compliance/aws_well_architected/aws_well_architected.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from csv import DictWriter | ||
from venv import logger | ||
|
||
from prowler.lib.check.compliance_models import ComplianceBaseModel | ||
from prowler.lib.outputs.compliance.aws_well_architected.models import ( | ||
AWSWellArchitected as AWSWellArchitectedModel, | ||
) | ||
from prowler.lib.outputs.compliance.compliance_output import ComplianceOutput | ||
from prowler.lib.outputs.finding import Finding | ||
|
||
|
||
class AWSWellArchitected(ComplianceOutput): | ||
""" | ||
This class represents the AWS Well-Architected compliance output. | ||
Attributes: | ||
- _data (list): A list to store transformed data from findings. | ||
- _file_descriptor (TextIOWrapper): A file descriptor to write data to a file. | ||
Methods: | ||
- transform: Transforms findings into AWS Well-Architected compliance format. | ||
- batch_write_data_to_file: Writes the findings data to a CSV file in AWS Well-Architected compliance format. | ||
""" | ||
|
||
def transform( | ||
self, | ||
findings: list[Finding], | ||
compliance: ComplianceBaseModel, | ||
compliance_name: str, | ||
) -> None: | ||
""" | ||
Transforms a list of findings into AWS Well-Architected compliance format. | ||
Parameters: | ||
- findings (list): A list of findings. | ||
- compliance (ComplianceBaseModel): A compliance model. | ||
- compliance_name (str): The name of the compliance model. | ||
Returns: | ||
- None | ||
""" | ||
for finding in findings: | ||
# Get the compliance requirements for the finding | ||
finding_requirements = finding.compliance.get(compliance_name, []) | ||
for requirement in compliance.Requirements: | ||
if requirement.Id in finding_requirements: | ||
for attribute in requirement.Attributes: | ||
compliance_row = AWSWellArchitectedModel( | ||
Provider=finding.provider, | ||
Description=compliance.Description, | ||
AccountId=finding.account_uid, | ||
Region=finding.region, | ||
AssessmentDate=str(finding.timestamp), | ||
Requirements_Id=requirement.Id, | ||
Requirements_Description=requirement.Description, | ||
Requirements_Attributes_Name=attribute.Name, | ||
Requirements_Attributes_WellArchitectedQuestionId=attribute.WellArchitectedQuestionId, | ||
Requirements_Attributes_WellArchitectedPracticeId=attribute.WellArchitectedPracticeId, | ||
Requirements_Attributes_Section=attribute.Section, | ||
Requirements_Attributes_SubSection=attribute.SubSection, | ||
Requirements_Attributes_LevelOfRisk=attribute.LevelOfRisk, | ||
Requirements_Attributes_AssessmentMethod=attribute.AssessmentMethod, | ||
Requirements_Attributes_Description=attribute.Description, | ||
Requirements_Attributes_ImplementationGuidanceUrl=attribute.ImplementationGuidanceUrl, | ||
Status=finding.status, | ||
StatusExtended=finding.status_extended, | ||
ResourceId=finding.resource_uid, | ||
ResourceName=finding.resource_name, | ||
CheckId=finding.check_id, | ||
Muted=finding.muted, | ||
) | ||
self._data.append(compliance_row) | ||
|
||
def batch_write_data_to_file(self) -> None: | ||
""" | ||
Writes the findings data to a CSV file in AWS Well-Architected compliance format. | ||
Returns: | ||
- None | ||
""" | ||
try: | ||
if ( | ||
getattr(self, "_file_descriptor", None) | ||
and not self._file_descriptor.closed | ||
and self._data | ||
): | ||
csv_writer = DictWriter( | ||
self._file_descriptor, | ||
fieldnames=[field.upper() for field in self._data[0].dict().keys()], | ||
delimiter=";", | ||
) | ||
csv_writer.writeheader() | ||
for finding in self._data: | ||
csv_writer.writerow( | ||
{k.upper(): v for k, v in finding.dict().items()} | ||
) | ||
self._file_descriptor.close() | ||
except Exception as error: | ||
logger.error( | ||
f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" | ||
) |
32 changes: 32 additions & 0 deletions
32
prowler/lib/outputs/compliance/aws_well_architected/models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class AWSWellArchitected(BaseModel): | ||
""" | ||
AWSWellArchitected generates a finding's output in AWS Well-Architected Framework format. | ||
""" | ||
|
||
Provider: str | ||
Description: str | ||
AccountId: str | ||
Region: str | ||
AssessmentDate: str | ||
Requirements_Id: str | ||
Requirements_Description: str | ||
Requirements_Attributes_Name: str | ||
Requirements_Attributes_WellArchitectedQuestionId: str | ||
Requirements_Attributes_WellArchitectedPracticeId: str | ||
Requirements_Attributes_Section: str | ||
Requirements_Attributes_SubSection: Optional[str] | ||
Requirements_Attributes_LevelOfRisk: str | ||
Requirements_Attributes_AssessmentMethod: str | ||
Requirements_Attributes_Description: str | ||
Requirements_Attributes_ImplementationGuidanceUrl: str | ||
Status: str | ||
StatusExtended: str | ||
ResourceId: str | ||
CheckId: str | ||
Muted: bool | ||
ResourceName: str |
60 changes: 0 additions & 60 deletions
60
prowler/lib/outputs/compliance/aws_well_architected_framework.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.