|
| 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 getting a rule. |
| 18 | +
|
| 19 | +Sample Commands (run from api_samples_python dir): |
| 20 | + python3 -m detect.v1alpha.get_rule -r=<region> -p=<project_id> \ |
| 21 | + -i=<instance_id> -rid=<rule_id> |
| 22 | +
|
| 23 | + python3 -m detect.v1alpha.get_rule -r=<region> -p=<project_id> \ |
| 24 | + -i=<instance_id> -rid=<rule_id>@v_<seconds>_<nanoseconds> |
| 25 | +
|
| 26 | +API reference: |
| 27 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.rules/get |
| 28 | + https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.rules#Rule |
| 29 | +""" |
| 30 | + |
| 31 | +import argparse |
| 32 | +import json |
| 33 | + |
| 34 | +from typing import Any, Mapping |
| 35 | +from common import chronicle_auth |
| 36 | +from common import project_id |
| 37 | +from common import project_instance |
| 38 | +from common import regions |
| 39 | +from google.auth.transport import requests |
| 40 | + |
| 41 | +CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com" |
| 42 | + |
| 43 | +SCOPES = [ |
| 44 | + "https://www.googleapis.com/auth/cloud-platform", |
| 45 | +] |
| 46 | + |
| 47 | + |
| 48 | +def get_rule( |
| 49 | + http_session: requests.AuthorizedSession, |
| 50 | + proj_region: str, |
| 51 | + proj_id: str, |
| 52 | + proj_instance: str, |
| 53 | + rule_id: str, |
| 54 | +) -> Mapping[str, Any]: |
| 55 | + """Get a rule. |
| 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>" or |
| 63 | + "ru_<UUID>@v_<seconds>_<nanoseconds>"). If a version suffix isn't |
| 64 | + specified we use the rule's latest version. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + a rule object containing relevant rule's information |
| 68 | + Raises: |
| 69 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 70 | + (response.status_code >= 400). |
| 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/{proj_id}/locations/{proj_region}/instances/{proj_instance}" |
| 78 | + url = f"{base_url_with_region}/v1alpha/{parent}/rules/{rule_id}" |
| 79 | + |
| 80 | + # See API reference links at top of this file, for response format. |
| 81 | + response = http_session.request("GET", url) |
| 82 | + if response.status_code >= 400: |
| 83 | + print(response.text) |
| 84 | + response.raise_for_status() |
| 85 | + return response.json() |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + parser = argparse.ArgumentParser() |
| 90 | + chronicle_auth.add_argument_credentials_file(parser) |
| 91 | + project_instance.add_argument_project_instance(parser) |
| 92 | + project_id.add_argument_project_id(parser) |
| 93 | + regions.add_argument_region(parser) |
| 94 | + parser.add_argument( |
| 95 | + "-rid", |
| 96 | + "--rule_id", |
| 97 | + type=str, |
| 98 | + required=True, |
| 99 | + help=( |
| 100 | + 'rule ID to get rule for. can use both "ru_<UUID>" or' |
| 101 | + ' "ru_<UUID>@v_<seconds>_<nanoseconds>"' |
| 102 | + ), |
| 103 | + ) |
| 104 | + args = parser.parse_args() |
| 105 | + auth_session = chronicle_auth.initialize_http_session( |
| 106 | + args.credentials_file, |
| 107 | + SCOPES |
| 108 | + ) |
| 109 | + print( |
| 110 | + json.dumps( |
| 111 | + get_rule( |
| 112 | + auth_session, |
| 113 | + args.region, |
| 114 | + args.project_id, |
| 115 | + args.project_instance, |
| 116 | + args.rule_id |
| 117 | + ), |
| 118 | + indent=2, |
| 119 | + ) |
| 120 | + ) |
0 commit comments