Skip to content

Commit 4328861

Browse files
jason-wgcopybara-github
authored andcommitted
Add v1alpha sample for BatchUpdateCuratedRuleSetDeployment
PiperOrigin-RevId: 607058857
1 parent 4f9068f commit 4328861

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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 batch updating curated rule sets deployments.
18+
19+
Sample Commands (run from api_samples_python dir):
20+
# Modify the script to update the constants that point to deployments.
21+
python3 -m detect.v1alpha.batch_update_curated_rule_set_deployments \
22+
-r=<region> -p=<project_id> -i=<instance_id>
23+
24+
API reference:
25+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.curatedRuleSetCategories.curatedRuleSets.curatedRuleSetDeployments/batchUpdate
26+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.curatedRuleSetCategories.curatedRuleSets.curatedRuleSetDeployments#CuratedRuleSetDeployment
27+
"""
28+
import argparse
29+
import json
30+
from typing import Any, Mapping
31+
from common import chronicle_auth
32+
from common import project_id
33+
from common import project_instance
34+
from common import regions
35+
from google.auth.transport import requests
36+
37+
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
38+
39+
SCOPES = [
40+
"https://www.googleapis.com/auth/cloud-platform",
41+
]
42+
43+
44+
def batch_update_curated_rule_set_deployments(
45+
http_session: requests.AuthorizedSession,
46+
proj_region: str,
47+
proj_id: str,
48+
proj_instance: str,
49+
) -> Mapping[str, Any]:
50+
"""Batch update curated rule set deployments.
51+
52+
Args:
53+
http_session: Authorized session for HTTP requests.
54+
proj_region: region in which the target project is located
55+
proj_id: GCP project id or number which the target instance belongs to
56+
proj_instance: uuid of the instance (with dashes)
57+
58+
Returns:
59+
an object with information about the modified deployments
60+
61+
Raises:
62+
requests.exceptions.HTTPError: HTTP request resulted in an error
63+
(response.status_code >= 400).
64+
"""
65+
66+
base_url_with_region = regions.url_always_prepend_region(
67+
CHRONICLE_API_BASE_URL,
68+
args.region
69+
)
70+
# pylint: disable-next=line-too-long
71+
parent = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}"
72+
73+
# We use "-" in the URL because we provide category and rule_set IDs
74+
# in the request data.
75+
url = f"{base_url_with_region}/v1alpha/{parent}/curatedRuleSetCategories/-/curatedRuleSets/-/curatedRuleSetDeployments:batchUpdate"
76+
77+
# Helper function for making a deployment name. Use this as the
78+
# curated_rule_set_deployment.name field in the request data below.
79+
def make_deployment_name(category, rule_set, precision):
80+
return f"{parent}/curatedRuleSetCategories/{category}/curatedRuleSets/{rule_set}/curatedRuleSetDeployments/{precision}"
81+
82+
# Note that IDs are hard-coded below, as examples.
83+
print("\nCategories, rule sets, and precisions are hard-coded as " +
84+
"examples. Update the script to provide actual IDs.\n"
85+
)
86+
87+
# Modify the category/rule_set/precision for each deployment below.
88+
# Deployment A.
89+
category_a = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
90+
rule_set_a = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
91+
precision_a = "broad"
92+
93+
# Deployment B.
94+
category_b = "cccccccc-cccc-cccc-cccc-cccccccccccc"
95+
rule_set_b = "dddddddd-dddd-dddd-dddd-dddddddddddd"
96+
precision_b = "precise"
97+
98+
# Modify the data below to change the behavior of the request.
99+
# - Add elements to `requests` to batch update multiple deployments
100+
# - Change the enabled and alerting fields as needed
101+
# - Change the update_mask to modify only certain properties
102+
json_data = {
103+
"parent": f"{parent}/curatedRuleSetCategories/-/curatedRuleSets/-",
104+
"requests": [
105+
{
106+
"curated_rule_set_deployment": {
107+
"name": make_deployment_name(
108+
category_a,
109+
rule_set_a,
110+
precision_a,
111+
),
112+
"enabled": True,
113+
"alerting": False,
114+
},
115+
"update_mask": {
116+
"paths": ["alerting", "enabled"],
117+
},
118+
},
119+
{
120+
"curated_rule_set_deployment": {
121+
"name": make_deployment_name(
122+
category_b,
123+
rule_set_b,
124+
precision_b,
125+
),
126+
"enabled": True,
127+
"alerting": True,
128+
},
129+
"update_mask": {
130+
"paths": ["alerting", "enabled"],
131+
},
132+
},
133+
],
134+
}
135+
136+
# See API reference links at top of this file, for response format.
137+
response = http_session.request("POST", url, json=json_data)
138+
139+
if response.status_code >= 400:
140+
print(response.text)
141+
response.raise_for_status()
142+
return response.json()
143+
144+
145+
if __name__ == "__main__":
146+
parser = argparse.ArgumentParser()
147+
chronicle_auth.add_argument_credentials_file(parser)
148+
regions.add_argument_region(parser)
149+
project_instance.add_argument_project_instance(parser)
150+
project_id.add_argument_project_id(parser)
151+
152+
args = parser.parse_args()
153+
auth_session = chronicle_auth.initialize_http_session(
154+
args.credentials_file,
155+
SCOPES
156+
)
157+
session = chronicle_auth.initialize_http_session(args.credentials_file)
158+
print(
159+
json.dumps(
160+
batch_update_curated_rule_set_deployments(
161+
auth_session,
162+
args.region,
163+
args.project_id,
164+
args.project_instance,
165+
),
166+
indent=2,
167+
)
168+
)

0 commit comments

Comments
 (0)