|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +"""Raw log search functionality for Chronicle.""" |
| 16 | + |
| 17 | +from datetime import datetime |
| 18 | +from typing import TYPE_CHECKING, Any |
| 19 | + |
| 20 | +from secops.chronicle.models import APIVersion |
| 21 | +from secops.chronicle.utils.request_utils import chronicle_request |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from secops.chronicle.client import ChronicleClient |
| 25 | + |
| 26 | + |
| 27 | +def search_raw_logs( |
| 28 | + client: "ChronicleClient", |
| 29 | + query: str, |
| 30 | + start_time: datetime, |
| 31 | + end_time: datetime, |
| 32 | + snapshot_query: str | None = None, |
| 33 | + case_sensitive: bool = False, |
| 34 | + log_types: list[str] | None = None, |
| 35 | + max_aggregations_per_field: int | None = None, |
| 36 | + page_size: int | None = None, |
| 37 | +) -> dict[str, Any]: |
| 38 | + """Search for raw logs in Chronicle. |
| 39 | +
|
| 40 | + Args: |
| 41 | + client: The ChronicleClient instance. |
| 42 | + query: Query to search for raw logs. |
| 43 | + start_time: Search start time (inclusive). |
| 44 | + end_time: Search end time (exclusive). |
| 45 | + snapshot_query: Optional. Query to filter results. |
| 46 | + case_sensitive: Optional. Whether search is case-sensitive. |
| 47 | + log_types: Optional. Limit results to specific log types |
| 48 | + (e.g. ["OKTA"]). |
| 49 | + max_aggregations_per_field: Optional. Max values for a UDM field. |
| 50 | + page_size: Optional. Maximum number of results to return. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + Dictionary containing search results. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + APIError: If the API request fails. |
| 57 | + """ |
| 58 | + search_query: dict[str, Any] = { |
| 59 | + "baselineQuery": query, |
| 60 | + "baselineTimeRange": { |
| 61 | + "startTime": start_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), |
| 62 | + "endTime": end_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), |
| 63 | + }, |
| 64 | + "caseSensitive": case_sensitive, |
| 65 | + } |
| 66 | + |
| 67 | + if snapshot_query: |
| 68 | + search_query["snapshotQuery"] = snapshot_query |
| 69 | + |
| 70 | + if log_types: |
| 71 | + # The API expects a list of LogType objects, filtering by displayName |
| 72 | + search_query["logTypes"] = [{"displayName": lt} for lt in log_types] |
| 73 | + |
| 74 | + if max_aggregations_per_field is not None: |
| 75 | + search_query["maxAggregationsPerField"] = max_aggregations_per_field |
| 76 | + |
| 77 | + if page_size is not None: |
| 78 | + search_query["pageSize"] = page_size |
| 79 | + |
| 80 | + return chronicle_request( |
| 81 | + client, |
| 82 | + method="POST", |
| 83 | + endpoint_path=":searchRawLogs", |
| 84 | + api_version=APIVersion.V1ALPHA, |
| 85 | + json=search_query, |
| 86 | + ) |
0 commit comments