Skip to content

Commit 689d443

Browse files
committed
arg for log_type
1 parent b875144 commit 689d443

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

ingestion/v1alpha/logs_import.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,29 @@
2828
from common import regions
2929
from google.auth.transport import requests
3030

31+
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
3132
SCOPES = [
3233
"https://www.googleapis.com/auth/cloud-platform",
3334
]
3435

3536

3637
def logs_import(
3738
http_session: requests.AuthorizedSession,
38-
logs_file,
3939
proj_id: str,
40-
region: str,
4140
proj_instance: str,
41+
proj_region: str,
42+
log_type: str,
43+
logs_file: str,
4244
forwarder_id: str) -> Mapping[str, Any]:
4345
"""Imports logs to Chronicle using the GCP CLOUDAUDIT log type.
4446
4547
Args:
4648
http_session: Authorized session for HTTP requests.
47-
logs_file: File-like object containing the logs to import.
4849
proj_id: Google Cloud project ID.
49-
region: Chronicle region.
5050
proj_instance: Chronicle instance.
51+
proj_region: Chronicle region.
52+
log_type: Log type.
53+
logs_file: File-like object containing the logs to import.
5154
forwarder_id: UUID4 of the forwarder.
5255
5356
Returns:
@@ -56,13 +59,16 @@ def logs_import(
5659
Raises:
5760
requests.HTTPError: If the request fails.
5861
"""
59-
log_type = "GCP_CLOUDAUDIT"
6062
parent = (f"projects/{proj_id}/"
61-
f"locations/{region}/"
63+
f"locations/{proj_region}/"
6264
f"instances/{proj_instance}/"
6365
f"logTypes/{log_type}")
64-
url = (f"https://{region}-chronicle.googleapis.com/"
65-
f"v1alpha/{parent}/logs:import")
66+
67+
base_url_with_region = regions.url_always_prepend_region(
68+
CHRONICLE_API_BASE_URL,
69+
proj_region
70+
)
71+
url = (f"{base_url_with_region}/v1alpha/{parent}/logs:import")
6672
logs = logs_file.read()
6773
# Reset file pointer to beginning in case it needs to be read again
6874
logs_file.seek(0)
@@ -75,10 +81,13 @@ def logs_import(
7581
"data": logs,
7682
"log_entry_time": now,
7783
"collection_time": now,
84+
"labels": {
85+
"forwarder_id": {"value": forwarder_id}
86+
}
7887
}
7988
],
8089
"forwarder": (f"projects/{proj_id}/"
81-
f"locations/{region}/"
90+
f"locations/{proj_region}/"
8291
f"instances/{proj_instance}/"
8392
f"forwarders/{forwarder_id}")
8493
}
@@ -112,6 +121,11 @@ def main():
112121
type=str,
113122
required=True,
114123
help="UUID4 of the forwarder")
124+
parser.add_argument(
125+
"--log_type",
126+
type=str,
127+
required=True,
128+
help="Log type")
115129
parser.add_argument(
116130
"--logs_file",
117131
type=argparse.FileType("r"),
@@ -125,10 +139,11 @@ def main():
125139
try:
126140
result = logs_import(
127141
auth_session,
128-
args.logs_file,
129142
args.project_id,
130-
args.region,
131143
args.project_instance,
144+
args.region,
145+
args.log_type,
146+
args.logs_file,
132147
args.forwarder_id
133148
)
134149
logger.info("Import operation completed successfully")

0 commit comments

Comments
 (0)