Skip to content

Commit 0b5c98d

Browse files
author
sourabh
committed
adding test case for guardduty
1 parent 1c07789 commit 0b5c98d

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

cloudwatchevents/test/test-guardduty-benchmark.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _run(command, input=None, check=False, **kwargs):
7171

7272
def run_command(cmdargs):
7373
resp = _run(cmdargs)
74-
if len(resp.stderr.decode()) > 0:
74+
if resp.returncode != 0:
7575
# traceback.print_exc()
7676
raise Exception("Error in run command %s cmd: %s" % (resp, cmdargs))
7777
return resp.stdout
@@ -98,6 +98,56 @@ def api_endpoint(self):
9898
else:
9999
return 'https://%s-api.sumologic.net/api' % SUMO_DEPLOYMENT
100100

101+
def create_collector(self, collector_name):
102+
collector = {
103+
'collector': {
104+
'collectorType': "Hosted",
105+
'name': collector_name,
106+
'description': "This is a test collector."
107+
}
108+
}
109+
response_collector = self.sumo.create_collector(collector, headers=None)
110+
return json.loads(response_collector.text)
111+
112+
def create_source(self, collector_id, source_name):
113+
source_json = {
114+
"source":
115+
{
116+
"name": source_name,
117+
"category": self.source_category,
118+
"automaticDateParsing": True,
119+
"multilineProcessingEnabled": True,
120+
"useAutolineMatching": True,
121+
"forceTimeZone": False,
122+
"defaultDateFormats": [{
123+
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
124+
"locator": ".*\"updatedAt\":\"(.*)\".*"
125+
}],
126+
"filters": [],
127+
"cutoffTimestamp": 0,
128+
"encoding": "UTF-8",
129+
"fields": {
130+
131+
},
132+
"messagePerRequest": False,
133+
"sourceType": "HTTP"
134+
}
135+
}
136+
response_source = self.sumo.create_source(collector_id, source_json)
137+
return json.loads(response_source.text)
138+
139+
def delete_collector(self, collector):
140+
try:
141+
self.sumo.delete_collector(collector)
142+
except Exception as e:
143+
print(e)
144+
145+
def delete_source(self, collector_id, source):
146+
try:
147+
self.sumo.delete_source(collector_id, source)
148+
except Exception as e:
149+
print(e)
150+
101151
def fetch_logs(self):
102152
raw_messages = []
103153
# fetch Last 10 Minutes logs
@@ -315,5 +365,67 @@ def test_guard_duty_benchmark(self):
315365
assert len(self.sumo_resource.verificationErrors) == 0
316366

317367

368+
class TestGuardDuty(unittest.TestCase):
369+
370+
@classmethod
371+
def setUpClass(cls):
372+
super(TestGuardDuty, cls).setUpClass()
373+
create_sam_package_and_upload(GUARD_DUTY_TEMPLATE, GUARD_DUTY_SAM_TEMPLATE,
374+
"guardduty")
375+
print("Completed SetUp for All test Cases.")
376+
377+
def setUp(self):
378+
# Parameters
379+
self.collector_name = "Test GuardDuty Lambda"
380+
self.source_name = "GuardDuty"
381+
self.source_category = "Labs/test/guard/duty"
382+
self.finding_types = ["Policy:S3/AccountBlockPublicAccessDisabled", "Policy:S3/BucketPublicAccessGranted"]
383+
self.delay = 7
384+
385+
# Get GuardDuty details
386+
self.guard_duty = boto3.client('guardduty', AWS_REGION)
387+
response = self.guard_duty.list_detectors()
388+
if "DetectorIds" in response:
389+
self.detector_id = response["DetectorIds"][0]
390+
391+
# Get Sumo Logic Client
392+
self.sumo_resource = SumoLogicResource(self.source_category, self.finding_types, self.delay)
393+
# Create a collector and http source for testing
394+
self.collector = self.sumo_resource.create_collector(self.collector_name)
395+
self.collector_id = self.collector['collector']['id']
396+
self.source = self.sumo_resource.create_source(self.collector_id, self.source_name)
397+
self.source_id = self.source['source']['id']
398+
399+
# Get CloudFormation client
400+
self.cf = CloudFormation("TestGuardDuty", GUARD_DUTY_SAM_TEMPLATE)
401+
self.parameters = {
402+
"SumoEndpointUrl": self.source['source']['url'],
403+
}
404+
405+
def tearDown(self):
406+
if self.cf.stack_exists():
407+
self.cf.delete_stack()
408+
self.sumo_resource.delete_source(self.collector_id, self.source)
409+
self.sumo_resource.delete_collector(self.collector)
410+
411+
def test_guard_duty(self):
412+
self.cf.create_stack(self.parameters)
413+
print("Testing Stack Creation.")
414+
self.assertTrue(self.cf.stack_exists())
415+
# Generate some specific sample findings
416+
print("Generating sample GuardDuty findings.")
417+
self.guard_duty.create_sample_findings(DetectorId=self.detector_id, FindingTypes=self.finding_types)
418+
print("Waiting for %s minutes for logs to appear in Sumo Logic." % self.delay)
419+
time.sleep(self.delay * 60)
420+
# Go to SumoLogic and check if you received the logs
421+
# Assert one of the log for JSON format to check correctness
422+
print("Validate Logs in Sumo Logic.")
423+
self.sumo_resource.assert_logs()
424+
425+
if len(self.sumo_resource.verificationErrors) > 0:
426+
print("Assertions failures are:- %s." % '\n'.join(self.sumo_resource.verificationErrors))
427+
assert len(self.sumo_resource.verificationErrors) == 0
428+
429+
318430
if __name__ == '__main__':
319431
unittest.main()

0 commit comments

Comments
 (0)