Skip to content

Commit

Permalink
fix(OCSF): fix OCSF output when timestamp is UNIX format (#6626)
Browse files Browse the repository at this point in the history
Co-authored-by: Rubén De la Torre Vico <[email protected]>
  • Loading branch information
prowler-bot and puchy22 authored Jan 20, 2025
1 parent a591beb commit 0a9ea21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
13 changes: 11 additions & 2 deletions prowler/lib/outputs/ocsf/ocsf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from datetime import datetime
from typing import List

from py_ocsf_models.events.base_event import SeverityID, StatusID
Expand Down Expand Up @@ -68,7 +69,11 @@ def transform(self, findings: List[Finding]) -> None:
activity_name=finding_activity.name,
finding_info=FindingInformation(
created_time_dt=finding.timestamp,
created_time=int(finding.timestamp.timestamp()),
created_time=(
int(finding.timestamp.timestamp())
if isinstance(finding.timestamp, datetime)
else finding.timestamp
),
desc=finding.metadata.Description,
title=finding.metadata.CheckTitle,
uid=finding.uid,
Expand All @@ -77,7 +82,11 @@ def transform(self, findings: List[Finding]) -> None:
types=finding.metadata.CheckType,
),
time_dt=finding.timestamp,
time=int(finding.timestamp.timestamp()),
time=(
int(finding.timestamp.timestamp())
if isinstance(finding.timestamp, datetime)
else finding.timestamp
),
remediation=Remediation(
desc=finding.metadata.Remediation.Recommendation.Text,
references=list(
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/outputs/fixtures/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from typing import Union

from prowler.config.config import prowler_version
from prowler.lib.check.models import CheckMetadata, Code, Recommendation, Remediation
Expand All @@ -19,7 +20,7 @@ def generate_finding_output(
resource_name: str = "",
resource_tags: dict = {},
compliance: dict = {"test-compliance": "test-compliance"},
timestamp: datetime = None,
timestamp: Union[int, datetime] = None,
provider: str = "aws",
partition: str = "aws",
description: str = "check description",
Expand Down
20 changes: 18 additions & 2 deletions tests/lib/outputs/ocsf/ocsf_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from datetime import datetime
from datetime import datetime, timezone
from io import StringIO

import requests
Expand Down Expand Up @@ -36,7 +36,15 @@ def test_transform(self):
muted=False,
region=AWS_REGION_EU_WEST_1,
resource_tags={"Name": "test", "Environment": "dev"},
)
),
# Test with int timestamp (UNIX timestamp)
generate_finding_output(
status="FAIL",
severity="medium",
muted=False,
region=AWS_REGION_EU_WEST_1,
timestamp=1619600000,
),
]

ocsf = OCSF(findings)
Expand Down Expand Up @@ -100,6 +108,14 @@ def test_transform(self):
"compliance": findings[0].compliance,
}

# Test with int timestamp (UNIX timestamp)
output_data = ocsf.data[1]

assert output_data.time == 1619600000
assert output_data.time_dt == datetime.fromtimestamp(
1619600000, tz=timezone.utc
)

def test_validate_ocsf(self):
mock_file = StringIO()
findings = [
Expand Down

0 comments on commit 0a9ea21

Please sign in to comment.