|
| 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 updating 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 | + parser.add_argument( |
| 46 | + "-t", |
| 47 | + "--type", |
| 48 | + type=str, |
| 49 | + required=True, |
| 50 | + help=("the subject's type (ANALYST or IDP_GROUP)")) |
| 51 | + parser.add_argument( |
| 52 | + "-rs", |
| 53 | + "--roles", |
| 54 | + type=str, |
| 55 | + required=True, |
| 56 | + help=("the role(s) the subject must have after the update")) |
| 57 | + |
| 58 | + # Sanity checks for the command-line arguments. |
| 59 | + |
| 60 | + # No need for a sanity check for the subject name and roles because these |
| 61 | + # arguments convert the provided input into strings and accept a wide range |
| 62 | + # of values. If the subject name isn't passed in, the error will be thrown |
| 63 | + # from the argparse library. |
| 64 | + |
| 65 | + # Check the subject type. |
| 66 | + parsed_args = parser.parse_args(args) |
| 67 | + if parsed_args.type not in ("ANALYST", "IDP_GROUP"): |
| 68 | + print("Error: invalid subject type") |
| 69 | + return None |
| 70 | + |
| 71 | + return parser.parse_args(args) |
| 72 | + |
| 73 | + |
| 74 | +def update_subject(http_session: requests.AuthorizedSession, name: str, |
| 75 | + subject_type: str, |
| 76 | + roles: Sequence[str]) -> Mapping[str, Sequence[Any]]: |
| 77 | + """Updates information about a subject. |
| 78 | +
|
| 79 | + Args: |
| 80 | + http_session: Authorized session for HTTP requests. |
| 81 | + name: The ID of the subject to retrieve information about. |
| 82 | + subject_type: The subject's type (ANALYST or IDP_GROUP). |
| 83 | + roles: The role(s) the subject must have after the update. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Information about the requested subject in the form: |
| 87 | + { |
| 88 | + "subject": { |
| 89 | + "name": "test@test.com", |
| 90 | + "type": "SUBJECT_TYPE_ANALYST", |
| 91 | + "roles": [ |
| 92 | + { |
| 93 | + "name": "Test", |
| 94 | + "title": "Test role", |
| 95 | + "description": "The Test role", |
| 96 | + "createTime": "yyyy-mm-ddThh:mm:ss.ssssssZ", |
| 97 | + "isDefault": false, |
| 98 | + "permissions": [ |
| 99 | + { |
| 100 | + "name": "Test", |
| 101 | + "title": "Test permission", |
| 102 | + "description": "The Test permission", |
| 103 | + "createTime": "yyyy-mm-ddThh:mm:ss.ssssssZ", |
| 104 | + }, |
| 105 | + ... |
| 106 | + ] |
| 107 | + }, |
| 108 | + ... |
| 109 | + ] |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + Raises: |
| 114 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 115 | + (response.status_code >= 400). |
| 116 | + """ |
| 117 | + url = f"{CHRONICLE_API_BASE_URL}/v1/subjects/{name}" |
| 118 | + body = { |
| 119 | + "name": name, |
| 120 | + "type": subject_type, |
| 121 | + "roles": roles, |
| 122 | + } |
| 123 | + update_fields = ["subject.roles"] |
| 124 | + params = {"update_mask": ",".join(update_fields)} |
| 125 | + response = http_session.request("PATCH", url, params=params, json=body) |
| 126 | + |
| 127 | + if response.status_code >= 400: |
| 128 | + print(response.text) |
| 129 | + response.raise_for_status() |
| 130 | + return response.json() |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + cli = initialize_command_line_args() |
| 135 | + if not cli: |
| 136 | + sys.exit(1) # A sanity check failed. |
| 137 | + |
| 138 | + CHRONICLE_API_BASE_URL = regions.url(CHRONICLE_API_BASE_URL, cli.region) |
| 139 | + session = chronicle_auth.initialize_http_session(cli.credentials_file) |
| 140 | + print( |
| 141 | + json.dumps( |
| 142 | + update_subject(session, cli.name, cli.type, cli.roles.split(",")), |
| 143 | + indent=2)) |
0 commit comments