|
| 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 sample for creating a retrohunt. |
| 18 | +
|
| 19 | +Sample Commands (run from api_samples_python dir): |
| 20 | + python3 -m detect.v1alpha.create_retrohunt \ |
| 21 | + -r=<region> -p=<project_id> -i=<instance_id> -rid=<rule_id> \ |
| 22 | + -st="2023-10-02T18:00:00Z" -et="2023-10-02T20:00:00Z" |
| 23 | +
|
| 24 | +API reference: |
| 25 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.rules.retrohunts/create |
| 26 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.operations#Operation |
| 27 | +""" |
| 28 | +import argparse |
| 29 | +import datetime |
| 30 | +import json |
| 31 | +from typing import Any, Mapping |
| 32 | +from common import chronicle_auth |
| 33 | +from common import datetime_converter |
| 34 | +from common import project_id |
| 35 | +from common import project_instance |
| 36 | +from common import regions |
| 37 | +from google.auth.transport import requests |
| 38 | + |
| 39 | +CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com" |
| 40 | + |
| 41 | +SCOPES = [ |
| 42 | + "https://www.googleapis.com/auth/cloud-platform", |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +def create_retrohunt( |
| 47 | + http_session: requests.AuthorizedSession, |
| 48 | + proj_region: str, |
| 49 | + proj_id: str, |
| 50 | + proj_instance: str, |
| 51 | + rule_id: str, |
| 52 | + start_time: datetime.datetime, |
| 53 | + end_time: datetime.datetime, |
| 54 | +) -> Mapping[str, Any]: |
| 55 | + """Creates a retrohunt. |
| 56 | +
|
| 57 | + Args: |
| 58 | + http_session: Authorized session for HTTP requests. |
| 59 | + proj_region: region in which the target project is located |
| 60 | + proj_id: GCP project id or number which the target instance belongs to |
| 61 | + proj_instance: uuid of the instance (with dashes) |
| 62 | + rule_id: Unique ID of the detection rule to retrieve ("ru_<UUID>"). |
| 63 | + start_time: the start time of the event time range this retrohunt will be |
| 64 | + executed over |
| 65 | + end_time: the end time of the event time range this retrohunt will be |
| 66 | + executed over |
| 67 | +
|
| 68 | + Returns: |
| 69 | + an Operation resource object containing relevant retrohunt's information |
| 70 | +
|
| 71 | + Raises: |
| 72 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 73 | + (response.status_code >= 400). |
| 74 | + """ |
| 75 | + base_url_with_region = regions.url_always_prepend_region( |
| 76 | + CHRONICLE_API_BASE_URL, |
| 77 | + args.region |
| 78 | + ) |
| 79 | + # pylint: disable-next=line-too-long |
| 80 | + parent = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}" |
| 81 | + url = f"{base_url_with_region}/v1alpha/{parent}/rules/{rule_id}/retrohunts" |
| 82 | + body = { |
| 83 | + "process_interval": { |
| 84 | + "start_time": datetime_converter.strftime(start_time), |
| 85 | + "end_time": datetime_converter.strftime(end_time), |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + # See API reference links at top of this file, for response format. |
| 90 | + response = http_session.request("POST", url, json=body) |
| 91 | + if response.status_code >= 400: |
| 92 | + print(response.text) |
| 93 | + response.raise_for_status() |
| 94 | + return response.json() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + parser = argparse.ArgumentParser() |
| 99 | + chronicle_auth.add_argument_credentials_file(parser) |
| 100 | + regions.add_argument_region(parser) |
| 101 | + project_instance.add_argument_project_instance(parser) |
| 102 | + project_id.add_argument_project_id(parser) |
| 103 | + parser.add_argument( |
| 104 | + "-rid", |
| 105 | + "--rule_id", |
| 106 | + type=str, |
| 107 | + required=True, |
| 108 | + help='rule ID to create retrohunt for. In the form of "ru_<UUID>"', |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "-st", |
| 112 | + "--start_time", |
| 113 | + type=datetime_converter.iso8601_datetime_utc, |
| 114 | + required=True, |
| 115 | + help="Retrohunt start time in UTC ('yyyy-mm-ddThh:mm:ssZ')", |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + "-et", |
| 119 | + "--end_time", |
| 120 | + type=datetime_converter.iso8601_datetime_utc, |
| 121 | + required=True, |
| 122 | + help="Retrohunt end time in UTC ('yyyy-mm-ddThh:mm:ssZ')", |
| 123 | + ) |
| 124 | + args = parser.parse_args() |
| 125 | + auth_session = chronicle_auth.initialize_http_session( |
| 126 | + args.credentials_file, |
| 127 | + SCOPES |
| 128 | + ) |
| 129 | + print( |
| 130 | + json.dumps( |
| 131 | + create_retrohunt( |
| 132 | + auth_session, |
| 133 | + args.region, |
| 134 | + args.project_id, |
| 135 | + args.project_instance, |
| 136 | + args.rule_id, |
| 137 | + args.start_time, |
| 138 | + args.end_time, |
| 139 | + ), |
| 140 | + indent=2, |
| 141 | + ) |
| 142 | + ) |
0 commit comments