|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2025 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 | +# pylint: disable=line-too-long |
| 18 | +r"""Executable and reusable v1alpha API sample for getting a search query in Chronicle. |
| 19 | +
|
| 20 | +API reference: |
| 21 | +https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.users.searchQueries/get |
| 22 | +""" |
| 23 | +# pylint: enable=line-too-long |
| 24 | + |
| 25 | +import argparse |
| 26 | + |
| 27 | +from common import chronicle_auth |
| 28 | +from common import project_id |
| 29 | +from common import project_instance |
| 30 | +from common import regions |
| 31 | +from google.auth.transport import requests |
| 32 | + |
| 33 | +CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com" |
| 34 | +SCOPES = [ |
| 35 | + "https://www.googleapis.com/auth/cloud-platform", |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +def get_search_query(http_session: requests.AuthorizedSession, |
| 40 | + proj_id: str, |
| 41 | + proj_instance: str, |
| 42 | + proj_region: str, |
| 43 | + user_id: str, |
| 44 | + query_id: str) -> None: |
| 45 | + """Get a search query by ID from Chronicle. |
| 46 | +
|
| 47 | + Args: |
| 48 | + http_session: Authorized session for HTTP requests. |
| 49 | + proj_id: GCP project id or number to which the target instance belongs. |
| 50 | + proj_instance: Customer ID (uuid with dashes) for the instance. |
| 51 | + proj_region: region in which the target project is located. |
| 52 | + user_id: ID of the user who owns the search query. |
| 53 | + query_id: ID of the search query to retrieve. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 57 | + (response.status_code >= 400). |
| 58 | +
|
| 59 | + Requires the following IAM permission on the instance resource: |
| 60 | + chronicle.searchQueries.get |
| 61 | + """ |
| 62 | + base_url_with_region = regions.url_always_prepend_region( |
| 63 | + CHRONICLE_API_BASE_URL, proj_region) |
| 64 | + instance = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}" |
| 65 | + url = f"{base_url_with_region}/v1alpha/{instance}/users/{user_id}/searchQueries/{query_id}" |
| 66 | + |
| 67 | + response = http_session.request("GET", url) |
| 68 | + if response.status_code >= 400: |
| 69 | + print(response.text) |
| 70 | + response.raise_for_status() |
| 71 | + print(response.text) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + parser = argparse.ArgumentParser() |
| 76 | + # common |
| 77 | + chronicle_auth.add_argument_credentials_file(parser) |
| 78 | + project_instance.add_argument_project_instance(parser) |
| 79 | + project_id.add_argument_project_id(parser) |
| 80 | + regions.add_argument_region(parser) |
| 81 | + # local |
| 82 | + parser.add_argument( |
| 83 | + "--user_id", |
| 84 | + type=str, |
| 85 | + required=True, |
| 86 | + help="ID of the user who owns the search query" |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + "--query_id", |
| 90 | + type=str, |
| 91 | + required=True, |
| 92 | + help="ID of the search query to retrieve" |
| 93 | + ) |
| 94 | + |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + auth_session = chronicle_auth.initialize_http_session( |
| 98 | + args.credentials_file, |
| 99 | + SCOPES, |
| 100 | + ) |
| 101 | + get_search_query(auth_session, args.project_id, args.project_instance, |
| 102 | + args.region, args.user_id, args.query_id) |
0 commit comments