17
17
import os
18
18
import click
19
19
import logging
20
- import sys
21
20
import google .cloud .logging
22
- from google .auth .transport .requests import AuthorizedSession
23
- from google .oauth2 import service_account
24
21
from jinja2 import Template
25
22
from shared import utils
26
23
from google .cloud import dlp_v2
27
24
from google .cloud import storage
28
- from datetime import date , timedelta
29
- from shared import secops
25
+ from datetime import date , timedelta , datetime
26
+ from secops import SecOpsClient
30
27
31
28
client = google .cloud .logging .Client ()
32
29
client .setup_logging ()
37
34
format = '[%(levelname)-8s] - %(asctime)s - %(message)s' )
38
35
logging .root .setLevel (logging .DEBUG )
39
36
40
- SCOPES = [
41
- "https://www.googleapis.com/auth/chronicle-backstory" ,
42
- "https://www.googleapis.com/auth/malachite-ingestion"
43
- ]
44
-
45
37
SECOPS_REGION = os .environ .get ("SECOPS_REGION" )
46
38
GCP_PROJECT_ID = os .environ .get ("GCP_PROJECT" )
47
39
SECOPS_EXPORT_BUCKET = os .environ .get ("SECOPS_EXPORT_BUCKET" )
51
43
SECOPS_SOURCE_CUSTOMER_ID = os .environ .get ("SECOPS_SOURCE_CUSTOMER_ID" )
52
44
SECOPS_TARGET_CUSTOMER_ID = os .environ .get ("SECOPS_TARGET_CUSTOMER_ID" )
53
45
SECOPS_TARGET_FORWARDER_ID = os .environ .get ("SECOPS_TARGET_FORWARDER_ID" )
54
-
55
- SKIP_ANONYMIZATION = False if (os .environ .get (
56
- "SKIP_ANONYMIZATION" , "false" ).lower () == "false" ) else True
46
+ SKIP_ANONYMIZATION = False if (os .environ .get ("SKIP_ANONYMIZATION" , "false" ).lower () == "false" ) else True
57
47
DLP_DEIDENTIFY_TEMPLATE_ID = os .environ .get ("DLP_DEIDENTIFY_TEMPLATE_ID" )
58
48
DLP_INSPECT_TEMPLATE_ID = os .environ .get ("DLP_INSPECT_TEMPLATE_ID" )
59
49
DLP_REGION = os .environ .get ("DLP_REGION" )
60
50
61
51
62
52
def import_logs (export_date ):
63
- # Initialize with default credentials - will automatically use the service account
64
- # assigned to your Google Cloud resource
65
- client = secops .SecOpsClient ()
66
-
67
- # Initialize Chronicle client
68
- chronicle = client .chronicle (
69
- customer_id = SECOPS_TARGET_CUSTOMER_ID , # Your Chronicle instance ID
70
- project_id = SECOPS_TARGET_PROJECT , # Your GCP project ID
71
- region = SECOPS_REGION # Chronicle API region
72
- )
53
+ client = SecOpsClient ()
54
+ chronicle = client .chronicle (customer_id = SECOPS_TARGET_CUSTOMER_ID , project_id = SECOPS_TARGET_PROJECT , region = SECOPS_REGION )
73
55
74
56
storage_client = storage .Client ()
75
57
BUCKET = SECOPS_OUTPUT_BUCKET if not SKIP_ANONYMIZATION else SECOPS_EXPORT_BUCKET
@@ -87,13 +69,13 @@ def import_logs(export_date):
87
69
for line in f :
88
70
logs .append (line .rstrip ('\n ' ))
89
71
if len (logs ) == 1000 :
90
- response = chronicle .ingest_logs ( logs = logs , log_type = log_type , forwarder_id = SECOPS_TARGET_FORWARDER_ID )
72
+ response = chronicle .ingest_log ( log_message = logs , log_type = log_type , forwarder_id = SECOPS_TARGET_FORWARDER_ID )
91
73
LOGGER .debug (response )
92
74
logs = []
93
75
94
76
# Send any remaining entries
95
77
if len (logs ) > 0 :
96
- response = chronicle .ingest_logs ( logs = logs , log_type = log_type , forwarder_id = SECOPS_TARGET_FORWARDER_ID )
78
+ response = chronicle .ingest_log ( log_message = logs , log_type = log_type , forwarder_id = SECOPS_TARGET_FORWARDER_ID )
97
79
LOGGER .debug (response )
98
80
99
81
# delete both export and anonymized buckets after ingesting logs
@@ -119,35 +101,27 @@ def trigger_export(export_date: str, export_start_datetime: str,
119
101
:return:
120
102
"""
121
103
104
+ client = SecOpsClient ()
105
+ chronicle = client .chronicle (customer_id = SECOPS_SOURCE_CUSTOMER_ID , project_id = SECOPS_SOURCE_PROJECT , region = SECOPS_REGION )
122
106
123
- # Initialize with default credentials - will automatically use the service account
124
- # assigned to your Google Cloud resource
125
- client = secops .SecOpsClient ()
107
+ export_ids = []
126
108
127
- # Initialize Chronicle client
128
- chronicle = client .chronicle (
129
- customer_id = SECOPS_SOURCE_CUSTOMER_ID , # Your Chronicle instance ID
130
- project_id = SECOPS_SOURCE_PROJECT , # Your GCP project ID
131
- region = SECOPS_REGION # Chronicle API region
132
- )
109
+ if export_start_datetime and export_end_datetime :
110
+ start_time , end_time = datetime .strptime (export_start_datetime , "%Y-%m-%dT%H:%M:%SZ" ), datetime .strptime (export_end_datetime , "%Y-%m-%dT%H:%M:%SZ" )
111
+ else :
112
+ start_time , end_time = utils .format_date_time_range (date_input = export_date )
113
+ gcs_bucket = f"projects/{ GCP_PROJECT_ID } /buckets/{ SECOPS_EXPORT_BUCKET } "
133
114
134
- export_ids = []
135
115
try :
136
- if log_types is None :
137
- export_response = chronicle .create_data_export (
138
- project = GCP_PROJECT_ID , export_date = export_date ,
139
- export_start_datetime = export_start_datetime ,
140
- export_end_datetime = export_end_datetime )
116
+ if log_types is None or log_types == "" :
117
+ export_response = chronicle .create_data_export (start_time = start_time , end_time = end_time , gcs_bucket = gcs_bucket , export_all_logs = True )
141
118
LOGGER .info (export_response )
142
119
export_id = export_response ["dataExportStatus" ]["name" ].split ("/" )[- 1 ]
143
120
export_ids .append (export_id )
144
121
LOGGER .info (f"Triggered export with ID: { export_id } " )
145
122
else :
146
123
for log_type in log_types .split ("," ):
147
- export_response = chronicle .create_data_export (
148
- project = GCP_PROJECT_ID , export_date = export_date ,
149
- export_start_datetime = export_start_datetime ,
150
- export_end_datetime = export_end_datetime , log_type = log_type )
124
+ export_response = chronicle .create_data_export (start_time = start_time , end_time = end_time , gcs_bucket = gcs_bucket , log_type = log_type )
151
125
export_id = export_response ["dataExportStatus" ]["name" ].split ("/" )[- 1 ]
152
126
export_ids .append (export_id )
153
127
LOGGER .info (f"Triggered export with ID: { export_id } " )
@@ -164,22 +138,14 @@ def anonymize_data(export_date):
164
138
:param export_date: date for which data should be anonymized
165
139
:return:
166
140
"""
167
- # Initialize with default credentials - will automatically use the service account
168
- # assigned to your Google Cloud resource
169
- client = secops .SecOpsClient ()
170
-
171
- # Initialize Chronicle client
172
- chronicle = client .chronicle (
173
- customer_id = SECOPS_SOURCE_CUSTOMER_ID , # Your Chronicle instance ID
174
- project_id = SECOPS_SOURCE_PROJECT , # Your GCP project ID
175
- region = SECOPS_REGION # Chronicle API region
176
- )
177
- export_ids = utils .get_secops_export_folders_for_date (SECOPS_EXPORT_BUCKET ,
178
- export_date = export_date )
141
+
142
+ client = SecOpsClient ()
143
+ chronicle = client .chronicle (customer_id = SECOPS_SOURCE_CUSTOMER_ID , project_id = SECOPS_SOURCE_PROJECT , region = SECOPS_REGION )
144
+ export_ids = utils .get_secops_export_folders_for_date (SECOPS_EXPORT_BUCKET , export_date = export_date )
179
145
180
146
export_finished = True
181
147
for export_id in export_ids :
182
- export = chronicle .get_data_export (name = export_id )
148
+ export = chronicle .get_data_export (data_export_id = export_id )
183
149
LOGGER .info (f"Export response: { export } ." )
184
150
if "dataExportStatus" in export and export ["dataExportStatus" ]["stage" ] == "FINISHED_SUCCESS" :
185
151
export_state = export ["dataExportStatus" ]["stage" ]
@@ -261,21 +227,13 @@ def main(request):
261
227
262
228
263
229
@click .command ()
264
- @click .option ('--export-date' , '-d' , required = False , type = str ,
265
- help = 'Date for secops export and anonymization.' )
266
- @click .option ('--export-start-datetime' , '-d' , required = False , type = str ,
267
- help = 'Start datetime for secops export and anonymization.' )
268
- @click .option ('--export-end-datetime' , '-d' , required = False , type = str ,
269
- help = 'End datetime for secops export and anonymization.' )
230
+ @click .option ('--export-date' , '-d' , required = False , type = str , help = 'Date for secops export and anonymization.' )
231
+ @click .option ('--export-start-datetime' , '-d' , required = False , type = str , help = 'Start datetime for secops export and anonymization.' )
232
+ @click .option ('--export-end-datetime' , '-d' , required = False , type = str , help = 'End datetime for secops export and anonymization.' )
270
233
@click .option ('--log-type' , type = str , multiple = True )
271
- @click .option (
272
- '--action' ,
273
- type = click .Choice (['TRIGGER-EXPORT' , 'ANONYMIZE-DATA' ,
274
- 'IMPORT-DATA' ]), required = True )
275
- @click .option ('--debug' , is_flag = True , default = False ,
276
- help = 'Turn on debug logging.' )
277
- def main_cli (export_date , export_start_datetime , export_end_datetime ,
278
- log_type : list , action : str , debug = False ):
234
+ @click .option ('--action' , type = click .Choice (['TRIGGER-EXPORT' , 'ANONYMIZE-DATA' , 'IMPORT-DATA' ]), required = True )
235
+ @click .option ('--debug' , is_flag = True , default = False , help = 'Turn on debug logging.' )
236
+ def main_cli (export_date , export_start_datetime , export_end_datetime , log_type : list , action : str , debug = False ):
279
237
"""
280
238
CLI entry point.
281
239
:param date: date for secops export and anonymization
@@ -288,7 +246,7 @@ def main_cli(export_date, export_start_datetime, export_end_datetime,
288
246
trigger_export (export_date = export_date ,
289
247
export_start_datetime = export_start_datetime ,
290
248
export_end_datetime = export_end_datetime ,
291
- log_types = log_type )
249
+ log_types = ',' . join ( log_type ) )
292
250
case "ANONYMIZE-DATA" :
293
251
anonymize_data (export_date = export_date )
294
252
case "IMPORT-DATA" :
0 commit comments