import sys
from io import StringIO
from prowler.config.config import timestamp
from prowler.lib.outputs.html.html import HTML
from tests.lib.outputs.fixtures.fixtures import generate_finding_output
from tests.providers.aws.utils import AWS_REGION_EU_WEST_1, set_mocked_aws_provider
from tests.providers.azure.azure_fixtures import set_mocked_azure_provider
from tests.providers.gcp.gcp_fixtures import GCP_PROJECT_ID, set_mocked_gcp_provider
from tests.providers.kubernetes.kubernetes_fixtures import (
set_mocked_kubernetes_provider,
)
html_stats = {
"total_pass": 0,
"total_fail": 1,
"resources_count": 1,
"findings_count": 1,
}
pass_html_finding = """
| PASS |
high |
test-service |
eu-west-1 |
test-check-id |
test-check-id |
|
|
|
test-risk |
|
•test-compliance: test-compliance
|
"""
fail_html_finding = """
| FAIL |
high |
test-service |
eu-west-1 |
test-check-id |
test-check-id |
|
|
|
test-risk |
|
•test-compliance: test-compliance
|
"""
muted_html_finding = """
| MUTED (PASS) |
high |
test-service |
eu-west-1 |
test-check-id |
test-check-id |
|
|
|
test-risk |
|
•test-compliance: test-compliance
|
"""
manual_html_finding = """
| MANUAL |
high |
test-service |
eu-west-1 |
test-check-id |
test-check-id |
|
|
|
test-risk |
|
•test-compliance: test-compliance
|
"""
aws_html_assessment_summary = """
-
AWS Account: 123456789012
-
AWS-CLI Profile: default
-
Audited Regions: eu-west-1
-
User Id: None
-
Caller Identity ARN: None
"""
azure_html_assessment_summary = """
-
Azure Tenant IDs: 00000000-0000-0000-0000-000000000000
-
Azure Tenant Domain: Unknown tenant domain (missing AAD permissions)
-
Azure Subscriptions: 4f647f43-15d2-4e3a-a7f0-8517cc4d977b : Subscription Name
-
Azure Identity Type: Service Principal
-
Azure Identity ID: 00000000-0000-0000-0000-000000000000
"""
gcp_html_assessment_summary = """
-
GCP Project IDs: 123456789012
-
GCP Account: test@test.com
"""
kubernetes_html_assessment_summary = """
"""
aws_html_header = (
"""
Prowler - The Handy Cloud Security Tool
-
-
Parameters used: """
+ " ".join(sys.argv[1:])
+ """
-
Date: """
+ timestamp.isoformat()
+ """
"""
+ aws_html_assessment_summary
+ """
-
Total Findings: 1
-
Passed: 0
-
Failed: 1
-
Total Resources: 1
| Status |
Severity |
Service Name |
Region |
Check ID |
Check Title |
Resource ID |
Resource Tags |
Status Extended |
Risk |
Recomendation |
Compliance |
"""
)
html_footer = """
"""
class TestHTML:
def test_transform_fail_finding(self):
findings = [generate_finding_output(status="FAIL")]
html = HTML(findings)
output_data = html.data[0]
assert isinstance(output_data, str)
assert output_data == fail_html_finding
def test_transform_pass_finding(self):
findings = [generate_finding_output()]
html = HTML(findings)
output_data = html.data[0]
assert isinstance(output_data, str)
assert output_data == pass_html_finding
def test_transform_muted_finding(self):
findings = [generate_finding_output(muted=True)]
html = HTML(findings)
output_data = html.data[0]
assert isinstance(output_data, str)
assert output_data == muted_html_finding
def test_transform_manual_finding(self):
findings = [generate_finding_output(status="MANUAL")]
html = HTML(findings)
output_data = html.data[0]
assert isinstance(output_data, str)
assert output_data == manual_html_finding
def test_batch_write_data_to_file(self):
mock_file = StringIO()
findings = [generate_finding_output()]
output = HTML(findings)
output._file_descriptor = mock_file
provider = set_mocked_aws_provider(audited_regions=[AWS_REGION_EU_WEST_1])
output.batch_write_data_to_file(provider, html_stats)
mock_file.seek(0)
content = mock_file.read()
assert content == aws_html_header + pass_html_finding + html_footer
def test_write_header(self):
mock_file = StringIO()
findings = [generate_finding_output()]
output = HTML(findings)
output._file_descriptor = mock_file
provider = set_mocked_aws_provider(audited_regions=[AWS_REGION_EU_WEST_1])
output.write_header(mock_file, provider, html_stats)
mock_file.seek(0)
content = mock_file.read()
assert content == aws_html_header
def test_write_footer(self):
mock_file = StringIO()
findings = [generate_finding_output()]
output = HTML(findings)
output._file_descriptor = mock_file
output.write_footer(mock_file)
mock_file.seek(0)
content = mock_file.read()
assert content == html_footer
def test_aws_get_assessment_summary(self):
findings = [generate_finding_output()]
output = HTML(findings)
provider = set_mocked_aws_provider(audited_regions=[AWS_REGION_EU_WEST_1])
summary = output.get_assessment_summary(provider)
assert summary == aws_html_assessment_summary
def test_azure_get_assessment_summary(self):
findings = [generate_finding_output()]
output = HTML(findings)
provider = set_mocked_azure_provider()
summary = output.get_assessment_summary(provider)
assert summary == summary
def test_gcp_get_assessment_summary(self):
findings = [generate_finding_output()]
output = HTML(findings)
provider = set_mocked_gcp_provider(project_ids=[GCP_PROJECT_ID])
summary = output.get_assessment_summary(provider)
assert summary == gcp_html_assessment_summary
def test_kubernetes_get_assessment_summary(self):
findings = [generate_finding_output()]
output = HTML(findings)
provider = set_mocked_kubernetes_provider()
summary = output.get_assessment_summary(provider)
assert summary == kubernetes_html_assessment_summary