Skip to content

Commit 7e897d5

Browse files
committed
refactor; lint
1 parent 239ff30 commit 7e897d5

File tree

2 files changed

+71
-65
lines changed

2 files changed

+71
-65
lines changed
Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
# Copyright 2025 Google LLC
3+
# Copyright 2024 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -14,48 +14,56 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
# pylint: disable=line-too-long
18-
r"""Executable and reusable v1alpha API sample for bulk updating alerts.
17+
r"""Executable and reusable sample for bulk updating alerts.
1918
19+
The file provided to the --alert_ids_file parameter should have one alert
20+
ID per line like so:
21+
```
22+
de_ad9d2771-a567-49ee-6452-1b2db13c1d33
23+
de_3c2e2556-aba1-a253-7518-b4ddb666cc32
24+
```
2025
Usage:
21-
python -m detect.v1alpha.bulk_update_alerts \
22-
--project_id=<PROJECT_ID> \
26+
python -m alerts.v1alpha.bulk_update_alerts \
27+
--project_id=<PROJECT_ID> \
2328
--project_instance=<PROJECT_INSTANCE> \
24-
--region=<REGION> \
2529
--alert_ids_file=<PATH_TO_FILE> \
26-
--status=CLOSED \
27-
--reason=REASON_MAINTENANCE
28-
29-
# The alert_ids_file should contain one alert ID per line:
30-
# de_ad9d2771-a567-49ee-6452-1b2db13c1d33
31-
# de_3c2e2556-aba1-a253-7518-b4ddb666cc32
30+
--confidence_score=<CONFIDENCE_SCORE> \
31+
--priority=<PRIORITY> \
32+
--reason=<REASON> \
33+
--reputation=<REPUTATION> \
34+
--priority=<PRIORITY> \
35+
--status=<STATUS> \
36+
--verdict=<VERDICT> \
37+
--risk_score=<RISK_SCORE> \
38+
--disregarded=<DISREGARDED> \
39+
--severity=<SEVERITY> \
40+
--comment=<COMMENT> \
41+
--root_cause=<ROOT_CAUSE> \
42+
--severity_display=<SEVERITY_DISPLAY>
3243
44+
# pylint: disable=line-too-long
3345
API reference:
34-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.legacy/legacyUpdateAlert
35-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Priority
36-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reason
37-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reputation
38-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Status
39-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Verdict
46+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.legacy/legacyUpdateAlert
47+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Priority
48+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reason
49+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reputation
50+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Priority
51+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Status
52+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Verdict
4053
"""
4154
# pylint: enable=line-too-long
4255

43-
import argparse
4456
import json
4557

4658
from common import chronicle_auth
47-
from common import project_id
48-
from common import project_instance
49-
from common import regions
50-
from google.auth.transport import requests
5159

5260
from . import update_alert
5361

62+
5463
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
5564
SCOPES = [
5665
"https://www.googleapis.com/auth/cloud-platform",
5766
]
58-
5967
DEFAULT_FEEDBACK = {
6068
"comment": "automated cleanup",
6169
"reason": "REASON_MAINTENANCE",
@@ -65,15 +73,13 @@
6573
"verdict": "VERDICT_UNSPECIFIED",
6674
}
6775

76+
6877
if __name__ == "__main__":
6978
parser = update_alert.get_update_parser()
70-
# local
71-
parser.add_argument("--alert_ids_file",
72-
type=str,
73-
required=True,
74-
help="Path to file containing one alert ID per line")
75-
76-
# Set default values from DEFAULT_FEEDBACK
79+
parser.add_argument(
80+
"--alert_ids_file", type=str, required=True,
81+
help="File with one alert ID per line."
82+
)
7783
parser.set_defaults(
7884
comment=DEFAULT_FEEDBACK["comment"],
7985
reason=DEFAULT_FEEDBACK["reason"],
@@ -82,18 +88,18 @@
8288
status=DEFAULT_FEEDBACK["status"],
8389
verdict=DEFAULT_FEEDBACK["verdict"],
8490
)
85-
8691
args = parser.parse_args()
8792

88-
# Validate required arguments
93+
# raise error if required args are not present
8994
update_alert.check_args(parser, args)
9095

91-
auth_session = chronicle_auth.initialize_http_session(args.credentials_file,
92-
SCOPES)
93-
94-
with open(args.alert_ids_file) as alert_file:
95-
for alert_id in alert_file:
96-
result = update_alert.update_alert(
96+
auth_session = chronicle_auth.initialize_http_session(
97+
args.credentials_file,
98+
SCOPES,
99+
)
100+
with open(args.alert_ids_file) as fh:
101+
for alert_id in fh:
102+
a_list = update_alert.update_alert(
97103
auth_session,
98104
args.project_id,
99105
args.project_instance,
@@ -111,4 +117,4 @@
111117
args.comment,
112118
args.root_cause,
113119
)
114-
print(json.dumps(result, indent=2))
120+
print(json.dumps(a_list, indent=2))

iocs/v1alpha/get_ioc.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,35 @@ def get_ioc(
2828
proj_region: str,
2929
ioc_value: str,
3030
ioc_type: str,
31-
) -> dict:
31+
) -> Mapping[str, Any]:
3232
"""Get a single IoC by its value from Chronicle.
3333
34-
Args:
35-
http_session: Authorized session for HTTP requests.
36-
proj_id: GCP project id or number to which the target instance belongs.
37-
proj_instance: Customer ID (uuid with dashes) for the instance.
38-
proj_region: region in which the target project is located.
39-
ioc_value: Value of the IoC to retrieve.
40-
ioc_type: Type of IoC being requested. One of:
41-
IOC_TYPE_UNSPECIFIED
42-
DOMAIN
43-
IP
44-
FILE_HASH
45-
URL
46-
USER_EMAIL
47-
MUTEX
48-
FILE_HASH_MD5
49-
FILE_HASH_SHA1
50-
FILE_HASH_SHA256
51-
IOC_TYPE_RESOURCE
34+
Args:
35+
http_session: Authorized session for HTTP requests.
36+
proj_id: GCP project id or number to which the target instance belongs.
37+
proj_instance: Customer ID (uuid with dashes) for the instance.
38+
proj_region: region in which the target project is located.
39+
ioc_value: Value of the IoC to retrieve.
40+
ioc_type: Type of IoC being requested. One of:
41+
IOC_TYPE_UNSPECIFIED
42+
DOMAIN
43+
IP
44+
FILE_HASH
45+
URL
46+
USER_EMAIL
47+
MUTEX
48+
FILE_HASH_MD5
49+
FILE_HASH_SHA1
50+
FILE_HASH_SHA256
51+
IOC_TYPE_RESOURCE
5252
53-
Returns:
54-
Dict containing the requested IoC.
53+
Returns:
54+
Mapping containing the requested IoC.
5555
56-
Raises:
57-
requests.exceptions.HTTPError: HTTP request resulted in an error
58-
(response.status_code >= 400).
59-
"""
56+
Raises:
57+
requests.exceptions.HTTPError: HTTP request resulted in an error
58+
(response.status_code >= 400).
59+
"""
6060
base_url_with_region = regions.url_always_prepend_region(
6161
CHRONICLE_API_BASE_URL, proj_region)
6262
instance = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}"

0 commit comments

Comments
 (0)