Skip to content

Commit a848d0e

Browse files
Fixed testcase TestGuardDutyBenchmark
1 parent b94e234 commit a848d0e

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

cloudwatchevents/test/test-guardduty-benchmark.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import boto3
1111
from sumologic import SumoLogic
1212

13+
TIMEFORMAT = '%Y-%m-%dT%H:%M:%S'
14+
1315
# Update the below values in case the template locations are changed.
1416

1517
GUARD_DUTY_BENCHMARK_TEMPLATE = "guarddutybenchmark/template_v2.yaml"
@@ -153,10 +155,18 @@ def delete_source(self, collector_id, source):
153155
def fetch_logs(self):
154156
raw_messages = []
155157
# 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+
158167
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")
160170
print("Search Jobs API success with JOB ID as %s." % search_job_response["id"])
161171
state = "GATHERING RESULTS"
162172
message_count = 0
@@ -180,9 +190,25 @@ def fetch_logs(self):
180190
print("Received message count as %s." % len(raw_messages))
181191
return raw_messages
182192

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+
183208
# Validate the specific findings generated
184209
def assert_logs(self):
185-
messages = self.fetch_logs()
210+
messages = self.fetch_logs_with_retry()
211+
186212
for finding_type in self.findings:
187213
try:
188214
assert any((("type" in d and d["type"] == finding_type)
@@ -300,7 +326,7 @@ def setUp(self):
300326
self.source_name = "GuardDuty Benchmark"
301327
self.source_category = "Labs/test/guard/duty/benchmark"
302328
self.finding_types = ["Policy:S3/AccountBlockPublicAccessDisabled", "Policy:S3/BucketPublicAccessGranted"]
303-
self.delay = 7
329+
self.delay = 4
304330

305331
# Get GuardDuty details
306332
self.guard_duty = boto3.client('guardduty', AWS_REGION)

0 commit comments

Comments
 (0)