Skip to content

Commit 6f73d23

Browse files
committed
feat(securitycenter): Add Resource SCC Mgt API Org ETD Cust Modules (GetEff, ListEff, ListDesc, Validate)
1 parent 4536af5 commit 6f73d23

File tree

5 files changed

+494
-0
lines changed

5 files changed

+494
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#!/usr/bin/env python
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+
# https://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+
from google.api_core.exceptions import GoogleAPICallError, NotFound, RetryError
18+
from google.cloud import securitycentermanagement_v1
19+
20+
21+
# [START securitycenter_get_effective_event_threat_detection_custom_module]
22+
def get_effective_event_threat_detection_custom_module(parent: str, module_id: str):
23+
"""
24+
Retrieves an Event Threat Detection custom module using parent and module id as parameters.
25+
Args:
26+
parent: Use any one of the following options:
27+
- organizations/{organization_id}/locations/{location_id}
28+
- folders/{folder_id}/locations/{location_id}
29+
- projects/{project_id}/locations/{location_id}
30+
Returns:
31+
The retrieved Event Threat Detection custom module.
32+
Raises:
33+
NotFound: If the specified custom module does not exist.
34+
"""
35+
client = securitycentermanagement_v1.SecurityCenterManagementClient()
36+
37+
try:
38+
request = securitycentermanagement_v1.GetEffectiveEventThreatDetectionCustomModuleRequest(
39+
name=f"{parent}/effectiveEventThreatDetectionCustomModules/{module_id}",
40+
)
41+
42+
response = client.get_effective_event_threat_detection_custom_module(request=request)
43+
print(f"Retrieved Effective Event Threat Detection Custom Module: {response.name}")
44+
return response
45+
except NotFound as e:
46+
print(f"Custom Module not found: {e.message}")
47+
raise e
48+
# [END securitycenter_get_effective_event_threat_detection_custom_module]
49+
50+
51+
# [START securitycenter_list_effective_event_threat_detection_custom_module]
52+
def list_effective_event_threat_detection_custom_module(parent: str):
53+
"""
54+
Retrieves list of Event Threat Detection custom module.
55+
This includes resident modules defined at the scope of the parent,
56+
and inherited modules, inherited from ancestor organizations, folders, and projects (no descendants).
57+
58+
Args:
59+
parent: Use any one of the following options:
60+
- organizations/{organization_id}/locations/{location_id}
61+
- folders/{folder_id}/locations/{location_id}
62+
- projects/{project_id}/locations/{location_id}
63+
Returns:
64+
List of retrieved all Event Threat Detection custom modules.
65+
Raises:
66+
NotFound: If the parent resource is not found.
67+
"""
68+
69+
client = securitycentermanagement_v1.SecurityCenterManagementClient()
70+
71+
try:
72+
request = securitycentermanagement_v1.ListEffectiveEventThreatDetectionCustomModulesRequest(
73+
parent=parent,
74+
)
75+
76+
response = client.list_effective_event_threat_detection_custom_modules(request=request)
77+
78+
custom_modules = []
79+
for custom_module in response:
80+
print(f"Custom Module: {custom_module.name}")
81+
custom_modules.append(custom_module)
82+
return custom_modules
83+
except NotFound as e:
84+
print(f"Parent resource not found: {parent}")
85+
raise e
86+
except Exception as e:
87+
print(f"An error occurred while listing custom modules: {e}")
88+
raise e
89+
90+
# [END securitycenter_list_effective_event_threat_detection_custom_module]
91+
92+
93+
# [START securitycenter_list_descendant_event_threat_detection_custom_module]
94+
def list_descendant_event_threat_detection_custom_module(parent: str):
95+
"""
96+
Retrieves list of all resident Event Threat Detection custom modules and all of its descendants.
97+
98+
Args:
99+
parent: Use any one of the following options:
100+
- organizations/{organization_id}/locations/{location_id}
101+
- folders/{folder_id}/locations/{location_id}
102+
- projects/{project_id}/locations/{location_id}
103+
Returns:
104+
List of retrieved all Event Threat Detection custom modules.
105+
Raises:
106+
NotFound: If the parent resource is not found.
107+
"""
108+
109+
client = securitycentermanagement_v1.SecurityCenterManagementClient()
110+
111+
try:
112+
request = securitycentermanagement_v1.ListDescendantEventThreatDetectionCustomModulesRequest(
113+
parent=parent,
114+
)
115+
116+
response = client.list_descendant_event_threat_detection_custom_modules(request=request)
117+
118+
custom_modules = []
119+
for custom_module in response:
120+
print(f"Custom Module: {custom_module.name}")
121+
custom_modules.append(custom_module)
122+
return custom_modules
123+
except NotFound as e:
124+
print(f"Parent resource not found: {parent}")
125+
raise e
126+
except Exception as e:
127+
print(f"An error occurred while listing custom modules: {e}")
128+
raise e
129+
130+
# [END securitycenter_list_descendant_event_threat_detection_custom_module]
131+
132+
133+
# [START securitycenter_validate_event_threat_detection_custom_module]
134+
def validate_event_threat_detection_custom_module(parent: str):
135+
"""
136+
Validates a custom module for Event Threat Detection.
137+
138+
Args:
139+
parent (str): Use any one of the following options:
140+
- organizations/{organization_id}/locations/{location_id}
141+
- folders/{folder_id}/locations/{location_id}
142+
- projects/{project_id}/locations/{location_id}
143+
"""
144+
try:
145+
# Define the raw JSON configuration for the Event Threat Detection custom module
146+
raw_text = """
147+
{
148+
"ips": ["192.0.2.1"],
149+
"metadata": {
150+
"properties": {
151+
"someProperty": "someValue"
152+
},
153+
"severity": "MEDIUM"
154+
}
155+
}
156+
"""
157+
158+
# Initialize the client
159+
client = securitycentermanagement_v1.SecurityCenterManagementClient()
160+
161+
# Create the request
162+
request = securitycentermanagement_v1.ValidateEventThreatDetectionCustomModuleRequest(
163+
parent=parent,
164+
raw_text=raw_text,
165+
type="CONFIGURABLE_BAD_IP"
166+
)
167+
168+
# Perform validation
169+
response = client.validate_event_threat_detection_custom_module(request=request)
170+
171+
# Handle the response and output validation results
172+
if response.errors:
173+
print("Validation errors:")
174+
for error in response.errors:
175+
print(f"Field: {error.field_path}, Description: {error.description}")
176+
return response
177+
else:
178+
print("Validation successful: No errors found.")
179+
return response
180+
181+
except GoogleAPICallError as api_error:
182+
print(f"API call failed: {api_error}")
183+
except RetryError as retry_error:
184+
print(f"Retry error occurred: {retry_error}")
185+
except Exception as e:
186+
print(f"An unexpected error occurred: {e}")
187+
188+
# [END securitycenter_validate_event_threat_detection_custom_module]

0 commit comments

Comments
 (0)