Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/secops/chronicle/log_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand Down
75 changes: 75 additions & 0 deletions tests/chronicle/test_log_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Loading