10
10
import boto3
11
11
from sumologic import SumoLogic
12
12
13
+ TIMEFORMAT = '%Y-%m-%dT%H:%M:%S'
14
+
13
15
# Update the below values in case the template locations are changed.
14
16
15
17
GUARD_DUTY_BENCHMARK_TEMPLATE = "guarddutybenchmark/template_v2.yaml"
@@ -153,10 +155,18 @@ def delete_source(self, collector_id, source):
153
155
def fetch_logs (self ):
154
156
raw_messages = []
155
157
# fetch Last 10 Minutes logs
156
- to_time = int (time .time ()) * 1000
157
- from_time = to_time - self .delay * 60 * 1000
158
+ # Get the current time
159
+ to_time = datetime .datetime .now ()
160
+ from_time = to_time - datetime .timedelta (minutes = self .delay + 2 )
161
+ from_time = from_time .strftime (TIMEFORMAT )
162
+ to_time = to_time .strftime (TIMEFORMAT )
163
+ print ("Fetching records" )
164
+ print ("from_time" , from_time )
165
+ print ("to_time" , to_time )
166
+
158
167
search_query = '_sourceCategory=%s' % self .source_category
159
- search_job_response = self .sumo .search_job (search_query , fromTime = from_time , toTime = to_time , timeZone = "IST" )
168
+ search_job_response = self .sumo .search_job (search_query , fromTime = from_time ,
169
+ toTime = to_time , timeZone = "IST" )
160
170
print ("Search Jobs API success with JOB ID as %s." % search_job_response ["id" ])
161
171
state = "GATHERING RESULTS"
162
172
message_count = 0
@@ -180,9 +190,25 @@ def fetch_logs(self):
180
190
print ("Received message count as %s." % len (raw_messages ))
181
191
return raw_messages
182
192
193
+ def fetch_logs_with_retry (self , retries = 6 , delay = 60 ):
194
+ for attempt in range (1 , retries + 1 ):
195
+ result = self .fetch_logs ()
196
+ if len (result ) >= 3 :
197
+ return result
198
+ else :
199
+ print (f"Attempt { attempt } " )
200
+ if attempt == retries :
201
+ print ("All attempts failed." )
202
+ return result
203
+ else :
204
+ print (f"Retrying in { delay } seconds..." )
205
+ self .delay += 1
206
+ time .sleep (delay )
207
+
183
208
# Validate the specific findings generated
184
209
def assert_logs (self ):
185
- messages = self .fetch_logs ()
210
+ messages = self .fetch_logs_with_retry ()
211
+
186
212
for finding_type in self .findings :
187
213
try :
188
214
assert any ((("type" in d and d ["type" ] == finding_type )
@@ -300,7 +326,7 @@ def setUp(self):
300
326
self .source_name = "GuardDuty Benchmark"
301
327
self .source_category = "Labs/test/guard/duty/benchmark"
302
328
self .finding_types = ["Policy:S3/AccountBlockPublicAccessDisabled" , "Policy:S3/BucketPublicAccessGranted" ]
303
- self .delay = 7
329
+ self .delay = 4
304
330
305
331
# Get GuardDuty details
306
332
self .guard_duty = boto3 .client ('guardduty' , AWS_REGION )
0 commit comments