|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2024 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +r"""Executable and reusable sample for ingesting events in UDM format. |
| 18 | +
|
| 19 | + Usage: |
| 20 | + python3 -m ingestion.v1alpha.create_udm_events \ |
| 21 | + --project_instance $PROJECT_INSTANCE \ |
| 22 | + --project_id $PROJECT_ID \ |
| 23 | + --json_events_file=./ingestion/example_input/sample_udm_events.json |
| 24 | +
|
| 25 | + API reference: |
| 26 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.events/import |
| 27 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.events/import#EventsInlineSource |
| 28 | + https://cloud.google.com/chronicle/docs/reference/udm-field-list |
| 29 | + https://cloud.google.com/chronicle/docs/unified-data-model/udm-usage |
| 30 | +""" |
| 31 | + |
| 32 | +import argparse |
| 33 | +import json |
| 34 | + |
| 35 | +from google.auth.transport import requests |
| 36 | + |
| 37 | +from common import chronicle_auth |
| 38 | +from common import project_id |
| 39 | +from common import project_instance |
| 40 | +from common import regions |
| 41 | + |
| 42 | +CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com" |
| 43 | +SCOPES = [ |
| 44 | + "https://www.googleapis.com/auth/cloud-platform", |
| 45 | +] |
| 46 | + |
| 47 | + |
| 48 | +def create_udm_events( |
| 49 | + http_session: requests.AuthorizedSession, json_events: str |
| 50 | +) -> None: |
| 51 | + """Sends a collection of UDM events to the Google SecOps backend for ingestion. |
| 52 | +
|
| 53 | + A Unified Data Model (UDM) event is a structured representation of an event |
| 54 | + regardless of the log source. |
| 55 | +
|
| 56 | + Args: |
| 57 | + http_session: Authorized session for HTTP requests. |
| 58 | + json_events: A collection of UDM events in (serialized) JSON format. |
| 59 | +
|
| 60 | + Raises: |
| 61 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 62 | + (response.status_code >= 400). |
| 63 | +
|
| 64 | + Requires the following IAM permission on the parent resource: |
| 65 | + chronicle.events.import |
| 66 | +
|
| 67 | + POST https://chronicle.googleapis.com/v1alpha/{parent}/events:import |
| 68 | +
|
| 69 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.events/import |
| 70 | + """ |
| 71 | + |
| 72 | + base_url_with_region = regions.url_always_prepend_region( |
| 73 | + CHRONICLE_API_BASE_URL, |
| 74 | + args.region |
| 75 | + ) |
| 76 | + # pylint: disable-next=line-too-long |
| 77 | + parent = f"projects/{args.project_id}/locations/{args.region}/instances/{args.project_instance}" |
| 78 | + url = f"{base_url_with_region}/v1alpha/{parent}/events:import" |
| 79 | + body = {"inline_source": {"events": [{"udm": json.loads(json_events)[0],}]}} |
| 80 | + |
| 81 | + response = http_session.request("POST", url, json=body) |
| 82 | + print(response) |
| 83 | + if response.status_code >= 400: |
| 84 | + print(response.text) |
| 85 | + response.raise_for_status() |
| 86 | + return None |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + parser = argparse.ArgumentParser() |
| 91 | + # common |
| 92 | + chronicle_auth.add_argument_credentials_file(parser) |
| 93 | + project_instance.add_argument_project_instance(parser) |
| 94 | + project_id.add_argument_project_id(parser) |
| 95 | + regions.add_argument_region(parser) |
| 96 | + # local |
| 97 | + parser.add_argument( |
| 98 | + "--json_events_file", |
| 99 | + type=argparse.FileType("r"), |
| 100 | + required=True, |
| 101 | + help=( |
| 102 | + "path to a file containing a list of UDM events in json format" |
| 103 | + ), |
| 104 | + ) |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + auth_session = chronicle_auth.initialize_http_session( |
| 108 | + args.credentials_file, |
| 109 | + SCOPES, |
| 110 | + ) |
| 111 | + create_udm_events(auth_session, args.json_events_file.read()) |
0 commit comments