|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # |
3 | | -# Copyright 2024 Google LLC |
| 3 | +# Copyright 2025 Google LLC |
4 | 4 | # |
5 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
6 | 6 | # you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | import uuid |
18 | 18 |
|
19 | | -from google.api_core.exceptions import GoogleAPICallError, NotFound |
| 19 | +from google.api_core.exceptions import GoogleAPICallError, NotFound, RetryError |
20 | 20 | from google.cloud import securitycentermanagement_v1 |
21 | 21 | from google.protobuf.field_mask_pb2 import FieldMask |
22 | 22 | from google.protobuf.struct_pb2 import Struct |
@@ -220,3 +220,173 @@ def delete_event_threat_detection_custom_module(parent: str, module_id: str): |
220 | 220 | print(f"Custom Module not found: {module_id}") |
221 | 221 | raise e |
222 | 222 | # [END securitycenter_delete_event_threat_detection_custom_module] |
| 223 | + |
| 224 | + |
| 225 | +# [START securitycenter_get_effective_event_threat_detection_custom_module] |
| 226 | +def get_effective_event_threat_detection_custom_module(parent: str, module_id: str): |
| 227 | + """ |
| 228 | + Retrieves an Event Threat Detection custom module using parent and module id as parameters. |
| 229 | + Args: |
| 230 | + parent: Use any one of the following options: |
| 231 | + - organizations/{organization_id}/locations/{location_id} |
| 232 | + - folders/{folder_id}/locations/{location_id} |
| 233 | + - projects/{project_id}/locations/{location_id} |
| 234 | + Returns: |
| 235 | + The retrieved Event Threat Detection custom module. |
| 236 | + Raises: |
| 237 | + NotFound: If the specified custom module does not exist. |
| 238 | + """ |
| 239 | + client = securitycentermanagement_v1.SecurityCenterManagementClient() |
| 240 | + |
| 241 | + try: |
| 242 | + request = securitycentermanagement_v1.GetEffectiveEventThreatDetectionCustomModuleRequest( |
| 243 | + name=f"{parent}/effectiveEventThreatDetectionCustomModules/{module_id}", |
| 244 | + ) |
| 245 | + |
| 246 | + response = client.get_effective_event_threat_detection_custom_module(request=request) |
| 247 | + print(f"Retrieved Effective Event Threat Detection Custom Module: {response.name}") |
| 248 | + return response |
| 249 | + except NotFound as e: |
| 250 | + print(f"Custom Module not found: {e.message}") |
| 251 | + raise e |
| 252 | +# [END securitycenter_get_effective_event_threat_detection_custom_module] |
| 253 | + |
| 254 | + |
| 255 | +# [START securitycenter_list_effective_event_threat_detection_custom_module] |
| 256 | +def list_effective_event_threat_detection_custom_module(parent: str): |
| 257 | + """ |
| 258 | + Retrieves list of Event Threat Detection custom module. |
| 259 | + This includes resident modules defined at the scope of the parent, |
| 260 | + and inherited modules, inherited from ancestor organizations, folders, and projects (no descendants). |
| 261 | +
|
| 262 | + Args: |
| 263 | + parent: Use any one of the following options: |
| 264 | + - organizations/{organization_id}/locations/{location_id} |
| 265 | + - folders/{folder_id}/locations/{location_id} |
| 266 | + - projects/{project_id}/locations/{location_id} |
| 267 | + Returns: |
| 268 | + List of retrieved all Event Threat Detection custom modules. |
| 269 | + Raises: |
| 270 | + NotFound: If the parent resource is not found. |
| 271 | + """ |
| 272 | + |
| 273 | + client = securitycentermanagement_v1.SecurityCenterManagementClient() |
| 274 | + |
| 275 | + try: |
| 276 | + request = securitycentermanagement_v1.ListEffectiveEventThreatDetectionCustomModulesRequest( |
| 277 | + parent=parent, |
| 278 | + ) |
| 279 | + |
| 280 | + response = client.list_effective_event_threat_detection_custom_modules(request=request) |
| 281 | + |
| 282 | + custom_modules = [] |
| 283 | + for custom_module in response: |
| 284 | + print(f"Custom Module: {custom_module.name}") |
| 285 | + custom_modules.append(custom_module) |
| 286 | + return custom_modules |
| 287 | + except NotFound as e: |
| 288 | + print(f"Parent resource not found: {parent}") |
| 289 | + raise e |
| 290 | + except Exception as e: |
| 291 | + print(f"An error occurred while listing custom modules: {e}") |
| 292 | + raise e |
| 293 | + |
| 294 | +# [END securitycenter_list_effective_event_threat_detection_custom_module] |
| 295 | + |
| 296 | + |
| 297 | +# [START securitycenter_list_descendant_event_threat_detection_custom_module] |
| 298 | +def list_descendant_event_threat_detection_custom_module(parent: str): |
| 299 | + """ |
| 300 | + Retrieves list of all resident Event Threat Detection custom modules and all of its descendants. |
| 301 | +
|
| 302 | + Args: |
| 303 | + parent: Use any one of the following options: |
| 304 | + - organizations/{organization_id}/locations/{location_id} |
| 305 | + - folders/{folder_id}/locations/{location_id} |
| 306 | + - projects/{project_id}/locations/{location_id} |
| 307 | + Returns: |
| 308 | + List of retrieved all Event Threat Detection custom modules. |
| 309 | + Raises: |
| 310 | + NotFound: If the parent resource is not found. |
| 311 | + """ |
| 312 | + |
| 313 | + client = securitycentermanagement_v1.SecurityCenterManagementClient() |
| 314 | + |
| 315 | + try: |
| 316 | + request = securitycentermanagement_v1.ListDescendantEventThreatDetectionCustomModulesRequest( |
| 317 | + parent=parent, |
| 318 | + ) |
| 319 | + |
| 320 | + response = client.list_descendant_event_threat_detection_custom_modules(request=request) |
| 321 | + |
| 322 | + custom_modules = [] |
| 323 | + for custom_module in response: |
| 324 | + print(f"Custom Module: {custom_module.name}") |
| 325 | + custom_modules.append(custom_module) |
| 326 | + return custom_modules |
| 327 | + except NotFound as e: |
| 328 | + print(f"Parent resource not found: {parent}") |
| 329 | + raise e |
| 330 | + except Exception as e: |
| 331 | + print(f"An error occurred while listing custom modules: {e}") |
| 332 | + raise e |
| 333 | + |
| 334 | +# [END securitycenter_list_descendant_event_threat_detection_custom_module] |
| 335 | + |
| 336 | + |
| 337 | +# [START securitycenter_validate_event_threat_detection_custom_module] |
| 338 | +def validate_event_threat_detection_custom_module(parent: str): |
| 339 | + """ |
| 340 | + Validates a custom module for Event Threat Detection. |
| 341 | +
|
| 342 | + Args: |
| 343 | + parent (str): Use any one of the following options: |
| 344 | + - organizations/{organization_id}/locations/{location_id} |
| 345 | + - folders/{folder_id}/locations/{location_id} |
| 346 | + - projects/{project_id}/locations/{location_id} |
| 347 | + """ |
| 348 | + try: |
| 349 | + # Define the raw JSON configuration for the Event Threat Detection custom module |
| 350 | + raw_text = """ |
| 351 | + { |
| 352 | + "ips": ["192.0.2.1"], |
| 353 | + "metadata": { |
| 354 | + "properties": { |
| 355 | + "someProperty": "someValue" |
| 356 | + }, |
| 357 | + "severity": "MEDIUM" |
| 358 | + } |
| 359 | + } |
| 360 | + """ |
| 361 | + |
| 362 | + # Initialize the client |
| 363 | + client = securitycentermanagement_v1.SecurityCenterManagementClient() |
| 364 | + |
| 365 | + # Create the request |
| 366 | + request = securitycentermanagement_v1.ValidateEventThreatDetectionCustomModuleRequest( |
| 367 | + parent=parent, |
| 368 | + raw_text=raw_text, |
| 369 | + type="CONFIGURABLE_BAD_IP" |
| 370 | + ) |
| 371 | + |
| 372 | + # Perform validation |
| 373 | + response = client.validate_event_threat_detection_custom_module(request=request) |
| 374 | + |
| 375 | + # Handle the response and output validation results |
| 376 | + if response.errors: |
| 377 | + print("Validation errors:") |
| 378 | + for error in response.errors: |
| 379 | + print(f"Field: {error.field_path}, Description: {error.description}") |
| 380 | + return response |
| 381 | + else: |
| 382 | + print("Validation successful: No errors found.") |
| 383 | + return response |
| 384 | + |
| 385 | + except GoogleAPICallError as api_error: |
| 386 | + print(f"API call failed: {api_error}") |
| 387 | + except RetryError as retry_error: |
| 388 | + print(f"Retry error occurred: {retry_error}") |
| 389 | + except Exception as e: |
| 390 | + print(f"An unexpected error occurred: {e}") |
| 391 | + |
| 392 | +# [END securitycenter_validate_event_threat_detection_custom_module] |
0 commit comments