|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2021 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 | +"""Executable and reusable sample for retrieving information about a subject. |
| 18 | +
|
| 19 | +A subject can be an analyst or an Identity Provider (IdP) group. |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import json |
| 24 | +import sys |
| 25 | +from typing import Any, Mapping |
| 26 | +from typing import Optional |
| 27 | +from typing import Sequence |
| 28 | + |
| 29 | +from google.auth.transport import requests |
| 30 | + |
| 31 | +from common import chronicle_auth |
| 32 | +from common import regions |
| 33 | + |
| 34 | +CHRONICLE_API_BASE_URL = "https://backstory.googleapis.com" |
| 35 | + |
| 36 | + |
| 37 | +def initialize_command_line_args( |
| 38 | + args: Optional[Sequence[str]] = None) -> Optional[argparse.Namespace]: |
| 39 | + """Initializes and checks all the command-line arguments.""" |
| 40 | + parser = argparse.ArgumentParser() |
| 41 | + chronicle_auth.add_argument_credentials_file(parser) |
| 42 | + regions.add_argument_region(parser) |
| 43 | + parser.add_argument( |
| 44 | + "-n", "--name", type=str, required=True, help="subject ID") |
| 45 | + |
| 46 | + # No sanity checks for the command-line arguments here. The subject name |
| 47 | + # argument converts the provide input into a string and accepts a wide range |
| 48 | + # of values. If the subject name isn't passed in, the error will be thrown |
| 49 | + # from the argparse library. |
| 50 | + |
| 51 | + return parser.parse_args(args) |
| 52 | + |
| 53 | + |
| 54 | +def get_subject(http_session: requests.AuthorizedSession, |
| 55 | + name: str) -> Mapping[str, Sequence[Any]]: |
| 56 | + """Retrieves information about a subject. |
| 57 | +
|
| 58 | + Args: |
| 59 | + http_session: Authorized session for HTTP requests. |
| 60 | + name: The ID of the subject to retrieve information about. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Information about the requested subject in the form: |
| 64 | + { |
| 65 | + "subject": { |
| 66 | + "name": "test@test.com", |
| 67 | + "type": "SUBJECT_TYPE_ANALYST", |
| 68 | + "roles": [ |
| 69 | + { |
| 70 | + "name": "Test", |
| 71 | + "title": "Test role", |
| 72 | + "description": "The Test role", |
| 73 | + "createTime": "yyyy-mm-ddThh:mm:ss.ssssssZ", |
| 74 | + "isDefault": false, |
| 75 | + "permissions": [ |
| 76 | + { |
| 77 | + "name": "Test", |
| 78 | + "title": "Test permission", |
| 79 | + "description": "The Test permission", |
| 80 | + "createTime": "yyyy-mm-ddThh:mm:ss.ssssssZ", |
| 81 | + }, |
| 82 | + ... |
| 83 | + ] |
| 84 | + }, |
| 85 | + ... |
| 86 | + ] |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + Raises: |
| 91 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 92 | + (response.status_code >= 400). |
| 93 | + """ |
| 94 | + url = f"{CHRONICLE_API_BASE_URL}/v1/subjects/{name}" |
| 95 | + response = http_session.request("GET", url) |
| 96 | + |
| 97 | + if response.status_code >= 400: |
| 98 | + print(response.text) |
| 99 | + response.raise_for_status() |
| 100 | + return response.json() |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + cli = initialize_command_line_args() |
| 105 | + if not cli: |
| 106 | + sys.exit(1) # A sanity check failed. |
| 107 | + |
| 108 | + CHRONICLE_API_BASE_URL = regions.url(CHRONICLE_API_BASE_URL, cli.region) |
| 109 | + session = chronicle_auth.initialize_http_session(cli.credentials_file) |
| 110 | + print(json.dumps(get_subject(session, cli.name), indent=2)) |
0 commit comments