forked from Georepublic/bucket-antivirus-function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_test.py
432 lines (394 loc) · 15.9 KB
/
scan_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# -*- coding: utf-8 -*-
# Upside Travel, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import json
import unittest
import boto3
import botocore.session
from botocore.stub import Stubber
from common import AV_SCAN_START_METADATA
from common import AV_SIGNATURE_METADATA
from common import AV_SIGNATURE_OK
from common import AV_STATUS_METADATA
from common import AV_TIMESTAMP_METADATA
from common import get_timestamp
from scan import delete_s3_object
from scan import event_object
from scan import get_local_path
from scan import set_av_metadata
from scan import set_av_tags
from scan import sns_start_scan
from scan import sns_scan_results
from scan import verify_s3_object_version
class TestScan(unittest.TestCase):
def setUp(self):
# Common data
self.s3_bucket_name = "test_bucket"
self.s3_key_name = "test_key"
# Clients and Resources
self.s3 = boto3.resource("s3")
self.s3_client = botocore.session.get_session().create_client("s3")
self.sns_client = botocore.session.get_session().create_client(
"sns", region_name="us-west-2"
)
def test_sns_event_object(self):
event = {
"Records": [
{
"s3": {
"bucket": {"name": self.s3_bucket_name},
"object": {"key": self.s3_key_name},
}
}
]
}
sns_event = {"Records": [{"Sns": {"Message": json.dumps(event)}}]}
s3_obj = event_object(sns_event, event_source="sns")
expected_s3_object = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
self.assertEqual(s3_obj, expected_s3_object)
def test_s3_event_object(self):
event = {
"Records": [
{
"s3": {
"bucket": {"name": self.s3_bucket_name},
"object": {"key": self.s3_key_name},
}
}
]
}
s3_obj = event_object(event)
expected_s3_object = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
self.assertEqual(s3_obj, expected_s3_object)
def test_s3_event_object_missing_bucket(self):
event = {"Records": [{"s3": {"object": {"key": self.s3_key_name}}}]}
with self.assertRaises(Exception) as cm:
event_object(event)
self.assertEqual(cm.exception.message, "No bucket found in event!")
def test_s3_event_object_missing_key(self):
event = {"Records": [{"s3": {"bucket": {"name": self.s3_bucket_name}}}]}
with self.assertRaises(Exception) as cm:
event_object(event)
self.assertEqual(cm.exception.message, "No key found in event!")
def test_s3_event_object_bucket_key_missing(self):
event = {"Records": [{"s3": {"bucket": {}, "object": {}}}]}
with self.assertRaises(Exception) as cm:
event_object(event)
self.assertEqual(
cm.exception.message,
"Unable to retrieve object from event.\n{}".format(event),
)
def test_s3_event_object_no_records(self):
event = {"Records": []}
with self.assertRaises(Exception) as cm:
event_object(event)
self.assertEqual(cm.exception.message, "No records found in event!")
def test_verify_s3_object_version(self):
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
# Set up responses
get_bucket_versioning_response = {"Status": "Enabled"}
get_bucket_versioning_expected_params = {"Bucket": self.s3_bucket_name}
s3_stubber_resource = Stubber(self.s3.meta.client)
s3_stubber_resource.add_response(
"get_bucket_versioning",
get_bucket_versioning_response,
get_bucket_versioning_expected_params,
)
list_object_versions_response = {
"Versions": [
{
"ETag": "string",
"Size": 123,
"StorageClass": "STANDARD",
"Key": "string",
"VersionId": "string",
"IsLatest": True,
"LastModified": datetime.datetime(2015, 1, 1),
"Owner": {"DisplayName": "string", "ID": "string"},
}
]
}
list_object_versions_expected_params = {
"Bucket": self.s3_bucket_name,
"Prefix": self.s3_key_name,
}
s3_stubber_resource.add_response(
"list_object_versions",
list_object_versions_response,
list_object_versions_expected_params,
)
try:
with s3_stubber_resource:
verify_s3_object_version(self.s3, s3_obj)
except Exception as e:
self.fail("verify_s3_object_version() raised Exception unexpectedly!")
raise e
def test_verify_s3_object_versioning_not_enabled(self):
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
# Set up responses
get_bucket_versioning_response = {"Status": "Disabled"}
get_bucket_versioning_expected_params = {"Bucket": self.s3_bucket_name}
s3_stubber_resource = Stubber(self.s3.meta.client)
s3_stubber_resource.add_response(
"get_bucket_versioning",
get_bucket_versioning_response,
get_bucket_versioning_expected_params,
)
with self.assertRaises(Exception) as cm:
with s3_stubber_resource:
verify_s3_object_version(self.s3, s3_obj)
self.assertEqual(
cm.exception.message,
"Object versioning is not enabled in bucket {}".format(
self.s3_bucket_name
),
)
def test_verify_s3_object_version_multiple_versions(self):
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
# Set up responses
get_bucket_versioning_response = {"Status": "Enabled"}
get_bucket_versioning_expected_params = {"Bucket": self.s3_bucket_name}
s3_stubber_resource = Stubber(self.s3.meta.client)
s3_stubber_resource.add_response(
"get_bucket_versioning",
get_bucket_versioning_response,
get_bucket_versioning_expected_params,
)
list_object_versions_response = {
"Versions": [
{
"ETag": "string",
"Size": 123,
"StorageClass": "STANDARD",
"Key": "string",
"VersionId": "string",
"IsLatest": True,
"LastModified": datetime.datetime(2015, 1, 1),
"Owner": {"DisplayName": "string", "ID": "string"},
},
{
"ETag": "string",
"Size": 123,
"StorageClass": "STANDARD",
"Key": "string",
"VersionId": "string",
"IsLatest": True,
"LastModified": datetime.datetime(2015, 1, 1),
"Owner": {"DisplayName": "string", "ID": "string"},
},
]
}
list_object_versions_expected_params = {
"Bucket": self.s3_bucket_name,
"Prefix": self.s3_key_name,
}
s3_stubber_resource.add_response(
"list_object_versions",
list_object_versions_response,
list_object_versions_expected_params,
)
with self.assertRaises(Exception) as cm:
with s3_stubber_resource:
verify_s3_object_version(self.s3, s3_obj)
self.assertEqual(
cm.exception.message,
"Detected multiple object versions in {}.{}, aborting processing".format(
self.s3_bucket_name, self.s3_key_name
),
)
def test_sns_start_scan(self):
sns_stubber = Stubber(self.sns_client)
s3_stubber_resource = Stubber(self.s3.meta.client)
sns_arn = "some_arn"
version_id = "version-id"
timestamp = get_timestamp()
message = {
"bucket": self.s3_bucket_name,
"key": self.s3_key_name,
"version": version_id,
AV_SCAN_START_METADATA: True,
AV_TIMESTAMP_METADATA: timestamp,
}
publish_response = {"MessageId": "message_id"}
publish_expected_params = {
"TargetArn": sns_arn,
"Message": json.dumps({"default": json.dumps(message)}),
"MessageStructure": "json",
}
sns_stubber.add_response("publish", publish_response, publish_expected_params)
head_object_response = {"VersionId": version_id}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber_resource.add_response(
"head_object", head_object_response, head_object_expected_params
)
with sns_stubber, s3_stubber_resource:
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
sns_start_scan(self.sns_client, s3_obj, sns_arn, timestamp)
def test_get_local_path(self):
local_prefix = "/tmp"
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
file_path = get_local_path(s3_obj, local_prefix)
expected_file_path = "/tmp/test_bucket/test_key"
self.assertEqual(file_path, expected_file_path)
def test_set_av_metadata(self):
scan_result = "CLEAN"
scan_signature = AV_SIGNATURE_OK
timestamp = get_timestamp()
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
s3_stubber_resource = Stubber(self.s3.meta.client)
# First head call is done to get content type and meta data
head_object_response = {"ContentType": "content", "Metadata": {}}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber_resource.add_response(
"head_object", head_object_response, head_object_expected_params
)
# Next two calls are done when copy() is called
head_object_response_2 = {
"ContentType": "content",
"Metadata": {},
"ContentLength": 200,
}
head_object_expected_params_2 = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber_resource.add_response(
"head_object", head_object_response_2, head_object_expected_params_2
)
copy_object_response = {"VersionId": "version_id"}
copy_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
"ContentType": "content",
"CopySource": {"Bucket": self.s3_bucket_name, "Key": self.s3_key_name},
"Metadata": {
AV_SIGNATURE_METADATA: scan_signature,
AV_STATUS_METADATA: scan_result,
AV_TIMESTAMP_METADATA: timestamp,
},
"MetadataDirective": "REPLACE",
}
s3_stubber_resource.add_response(
"copy_object", copy_object_response, copy_object_expected_params
)
with s3_stubber_resource:
set_av_metadata(s3_obj, scan_result, scan_signature, timestamp)
def test_set_av_tags(self):
scan_result = "CLEAN"
scan_signature = AV_SIGNATURE_OK
timestamp = get_timestamp()
tag_set = {
"TagSet": [
{"Key": AV_SIGNATURE_METADATA, "Value": scan_signature},
{"Key": AV_STATUS_METADATA, "Value": scan_result},
{"Key": AV_TIMESTAMP_METADATA, "Value": timestamp},
]
}
s3_stubber = Stubber(self.s3_client)
get_object_tagging_response = tag_set
get_object_tagging_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber.add_response(
"get_object_tagging",
get_object_tagging_response,
get_object_tagging_expected_params,
)
put_object_tagging_response = {}
put_object_tagging_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
"Tagging": tag_set,
}
s3_stubber.add_response(
"put_object_tagging",
put_object_tagging_response,
put_object_tagging_expected_params,
)
with s3_stubber:
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
set_av_tags(self.s3_client, s3_obj, scan_result, scan_signature, timestamp)
def test_sns_scan_results(self):
sns_stubber = Stubber(self.sns_client)
s3_stubber_resource = Stubber(self.s3.meta.client)
sns_arn = "some_arn"
version_id = "version-id"
scan_result = "CLEAN"
scan_signature = AV_SIGNATURE_OK
timestamp = get_timestamp()
message = {
"bucket": self.s3_bucket_name,
"key": self.s3_key_name,
"version": version_id,
AV_SIGNATURE_METADATA: scan_signature,
AV_STATUS_METADATA: scan_result,
AV_TIMESTAMP_METADATA: timestamp,
}
publish_response = {"MessageId": "message_id"}
publish_expected_params = {
"TargetArn": sns_arn,
"Message": json.dumps({"default": json.dumps(message)}),
"MessageAttributes": {
"av-status": {"DataType": "String", "StringValue": scan_result},
"av-signature": {"DataType": "String", "StringValue": scan_signature},
},
"MessageStructure": "json",
}
sns_stubber.add_response("publish", publish_response, publish_expected_params)
head_object_response = {"VersionId": version_id}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber_resource.add_response(
"head_object", head_object_response, head_object_expected_params
)
with sns_stubber, s3_stubber_resource:
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
sns_scan_results(
self.sns_client, s3_obj, sns_arn, scan_result, scan_signature, timestamp
)
def test_delete_s3_object(self):
s3_stubber = Stubber(self.s3.meta.client)
delete_object_response = {}
delete_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber.add_response(
"delete_object", delete_object_response, delete_object_expected_params
)
with s3_stubber:
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
delete_s3_object(s3_obj)
def test_delete_s3_object_exception(self):
s3_stubber = Stubber(self.s3.meta.client)
with self.assertRaises(Exception) as cm:
with s3_stubber:
s3_obj = self.s3.Object(self.s3_bucket_name, self.s3_key_name)
delete_s3_object(s3_obj)
self.assertEqual(
cm.exception.message,
"Failed to delete infected file: {}.{}".format(
self.s3_bucket_name, self.s3_key_name
),
)