From 0526140d050b052d382a2b59f691f495abd58db5 Mon Sep 17 00:00:00 2001 From: 1AbdullahButt <168034895+1AbdullahButt@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:39:30 +0500 Subject: [PATCH] fix(chronicle): send UTC timestamps from ingest_log ingest_log built its default log_entry_time and collection_time from datetime.now(), a naive local time, and then formatted it with a literal Z suffix. On machines outside UTC every ingested log was shifted by the local offset. Passing an aware log_entry_time with the default collection_time also raised TypeError when the two were compared. Default to datetime.now(timezone.utc) and normalise both values to UTC before the ordering check and formatting. Naive inputs are still treated as UTC, so explicit naive timestamps are sent unchanged. Fixes #229 --- src/secops/chronicle/log_ingest.py | 18 +++++-- tests/chronicle/test_log_ingest.py | 75 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/secops/chronicle/log_ingest.py b/src/secops/chronicle/log_ingest.py index 0cd0ba8b..b5ea23ca 100644 --- a/src/secops/chronicle/log_ingest.py +++ b/src/secops/chronicle/log_ingest.py @@ -20,7 +20,7 @@ import re import uuid from collections.abc import Callable -from datetime import datetime +from datetime import datetime, timezone from typing import Any from secops.chronicle.log_types import is_valid_log_type @@ -756,6 +756,13 @@ def extract_forwarder_id(forwarder_name: str) -> str: return segments[-1] +def _to_utc(value: datetime) -> datetime: + """Return ``value`` in UTC, treating a naive datetime as UTC.""" + if value.tzinfo is None: + return value.replace(tzinfo=timezone.utc) + return value.astimezone(timezone.utc) + + def ingest_log( client: "ChronicleClient", log_type: str, @@ -807,8 +814,8 @@ def ingest_log( "Use force_log_type=True to override." ) - # Get current time as default for log_entry_time and collection_time - now = datetime.now() + # Get current UTC time as default for log_entry_time and collection_time + now = datetime.now(timezone.utc) # If log_entry_time is not provided, use current time if log_entry_time is None: @@ -818,6 +825,11 @@ def ingest_log( if collection_time is None: collection_time = now + # The API expects UTC. Naive datetimes are treated as UTC (as before); + # aware ones are converted, so they can be compared and formatted with Z. + log_entry_time = _to_utc(log_entry_time) + collection_time = _to_utc(collection_time) + # Validate that collection_time is not before log_entry_time if collection_time < log_entry_time: raise ValueError("Collection time must be same or after log entry time") diff --git a/tests/chronicle/test_log_ingest.py b/tests/chronicle/test_log_ingest.py index b20666eb..bf16f027 100644 --- a/tests/chronicle/test_log_ingest.py +++ b/tests/chronicle/test_log_ingest.py @@ -419,6 +419,81 @@ def test_ingest_log_invalid_timestamps(chronicle_client): ) +def _sent_log(mock_request): + """Return the first log entry sent in the ingest request payload.""" + call = mock_request.call_args_list[-1] + return call.kwargs["json"]["inline_source"]["logs"][0] + + +def test_ingest_log_default_timestamps_are_utc( + chronicle_client, mock_forwarders_list_response, mock_ingest_response +): + """Default timestamps use UTC, not the machine's local time (#229).""" + utc_now = datetime(2026, 1, 2, 12, 0, 0, tzinfo=timezone.utc) + local_tz = timezone(timedelta(hours=-8)) + + class FakeDatetime(datetime): + """datetime whose local clock is 8 hours behind UTC.""" + + @classmethod + def now(cls, tz=None): + if tz is None: + return utc_now.astimezone(local_tz).replace(tzinfo=None) + return utc_now.astimezone(tz) + + with ( + patch.object( + chronicle_client.session, + "request", + side_effect=[mock_forwarders_list_response, mock_ingest_response], + ) as mock_request, + patch( + "secops.chronicle.log_ingest.is_valid_log_type", return_value=True + ), + patch("secops.chronicle.log_ingest.datetime", FakeDatetime), + ): + ingest_log( + client=chronicle_client, + log_type="OKTA", + log_message="{}", + ) + + sent = _sent_log(mock_request) + assert sent["log_entry_time"] == "2026-01-02T12:00:00.000000Z" + assert sent["collection_time"] == "2026-01-02T12:00:00.000000Z" + + +def test_ingest_log_aware_timestamp_converted_to_utc( + chronicle_client, mock_forwarders_list_response, mock_ingest_response +): + """An aware log_entry_time works with the default collection_time and + is converted to UTC before it is sent (#229).""" + plus_one = timezone(timedelta(hours=1)) + log_entry_time = datetime.now(plus_one) - timedelta(minutes=5) + + with ( + patch.object( + chronicle_client.session, + "request", + side_effect=[mock_forwarders_list_response, mock_ingest_response], + ) as mock_request, + patch( + "secops.chronicle.log_ingest.is_valid_log_type", return_value=True + ), + ): + ingest_log( + client=chronicle_client, + log_type="OKTA", + log_message="{}", + log_entry_time=log_entry_time, + ) + + expected = log_entry_time.astimezone(timezone.utc).strftime( + "%Y-%m-%dT%H:%M:%S.%fZ" + ) + assert _sent_log(mock_request)["log_entry_time"] == expected + + def test_ingest_log_invalid_log_type(chronicle_client): """Test log ingestion with invalid log type.""" test_log = {"test": "log", "message": "Test message"}