forked from bluesentry/bucket-antivirus-function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clamav_test.py
352 lines (315 loc) · 12.6 KB
/
clamav_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
# -*- 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 os
import re
import textwrap
import unittest
import boto3
import botocore.session
from botocore.stub import Stubber
import mock
from clamav import RE_SEARCH_DIR
from clamav import scan_output_to_json
from clamav import md5_from_s3_tags
from clamav import time_from_s3
from clamav import update_defs_from_s3
from common import AV_DEFINITION_FILE_PREFIXES
from common import AV_DEFINITION_FILE_SUFFIXES
from common import AV_DEFINITION_S3_PREFIX
from common import AV_SIGNATURE_OK
class TestClamAV(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_current_library_search_path(self):
# Calling `ld --verbose` returns a lot of text but the line to check is this one:
search_path = """SEARCH_DIR("=/usr/x86_64-redhat-linux/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/x86_64-redhat-linux/lib"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");""" # noqa
rd_ld = re.compile(RE_SEARCH_DIR)
all_search_paths = rd_ld.findall(search_path)
expected_search_paths = [
"/usr/x86_64-redhat-linux/lib64",
"/usr/lib64",
"/usr/local/lib64",
"/lib64",
"/usr/x86_64-redhat-linux/lib",
"/usr/local/lib",
"/lib",
"/usr/lib",
]
self.assertEqual(all_search_paths, expected_search_paths)
def test_scan_output_to_json_clean(self):
file_path = "/tmp/test.txt"
signature = AV_SIGNATURE_OK
output = textwrap.dedent(
"""\
Scanning {0}
{0}: {1}
----------- SCAN SUMMARY -----------
Known viruses: 6305127
Engine version: 0.101.4
Scanned directories: 0
Scanned files: 1
Infected files: 0
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 80.299 sec (1 m 20 s)
""".format(
file_path, signature
)
)
summary = scan_output_to_json(output)
self.assertEqual(summary[file_path], signature)
self.assertEqual(summary["Infected files"], "0")
def test_scan_output_to_json_infected(self):
file_path = "/tmp/eicar.com.txt"
signature = "Eicar-Test-Signature FOUND"
output = textwrap.dedent(
"""\
Scanning {0}
{0}: {1}
{0}!(0): {1}
----------- SCAN SUMMARY -----------
Known viruses: 6305127
Engine version: 0.101.4
Scanned directories: 0
Scanned files: 1
Infected files: 1
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 80.299 sec (1 m 20 s)
""".format(
file_path, signature
)
)
summary = scan_output_to_json(output)
self.assertEqual(summary[file_path], signature)
self.assertEqual(summary["Infected files"], "1")
def test_md5_from_s3_tags_no_md5(self):
tag_set = {"TagSet": []}
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,
)
with s3_stubber:
md5_hash = md5_from_s3_tags(
self.s3_client, self.s3_bucket_name, self.s3_key_name
)
self.assertEquals("", md5_hash)
def test_md5_from_s3_tags_has_md5(self):
expected_md5_hash = "d41d8cd98f00b204e9800998ecf8427e"
tag_set = {"TagSet": [{"Key": "md5", "Value": expected_md5_hash}]}
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,
)
with s3_stubber:
md5_hash = md5_from_s3_tags(
self.s3_client, self.s3_bucket_name, self.s3_key_name
)
self.assertEquals(expected_md5_hash, md5_hash)
def test_time_from_s3(self):
expected_s3_time = datetime.datetime(2019, 1, 1)
s3_stubber = Stubber(self.s3_client)
head_object_response = {"LastModified": expected_s3_time}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": self.s3_key_name,
}
s3_stubber.add_response(
"head_object", head_object_response, head_object_expected_params
)
with s3_stubber:
s3_time = time_from_s3(
self.s3_client, self.s3_bucket_name, self.s3_key_name
)
self.assertEquals(expected_s3_time, s3_time)
@mock.patch("clamav.md5_from_file")
@mock.patch("common.os.path.exists")
def test_update_defs_from_s3(self, mock_exists, mock_md5_from_file):
expected_md5_hash = "d41d8cd98f00b204e9800998ecf8427e"
different_md5_hash = "d41d8cd98f00b204e9800998ecf8427f"
mock_md5_from_file.return_value = different_md5_hash
tag_set = {"TagSet": [{"Key": "md5", "Value": expected_md5_hash}]}
expected_s3_time = datetime.datetime(2019, 1, 1)
s3_stubber = Stubber(self.s3_client)
key_names = []
side_effect = []
for file_prefix in AV_DEFINITION_FILE_PREFIXES:
for file_suffix in AV_DEFINITION_FILE_SUFFIXES:
side_effect.extend([True, True])
filename = file_prefix + "." + file_suffix
key_names.append(os.path.join(AV_DEFINITION_S3_PREFIX, filename))
mock_exists.side_effect = side_effect
for s3_key_name in key_names:
get_object_tagging_response = tag_set
get_object_tagging_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": s3_key_name,
}
s3_stubber.add_response(
"get_object_tagging",
get_object_tagging_response,
get_object_tagging_expected_params,
)
head_object_response = {"LastModified": expected_s3_time}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": s3_key_name,
}
s3_stubber.add_response(
"head_object", head_object_response, head_object_expected_params
)
expected_to_download = {
"bytecode": {
"local_path": "/tmp/clamav_defs/bytecode.cvd",
"s3_path": "clamav_defs/bytecode.cvd",
},
"daily": {
"local_path": "/tmp/clamav_defs/daily.cvd",
"s3_path": "clamav_defs/daily.cvd",
},
"main": {
"local_path": "/tmp/clamav_defs/main.cvd",
"s3_path": "clamav_defs/main.cvd",
},
}
with s3_stubber:
to_download = update_defs_from_s3(
self.s3_client, self.s3_bucket_name, AV_DEFINITION_S3_PREFIX
)
self.assertEquals(expected_to_download, to_download)
@mock.patch("clamav.md5_from_file")
@mock.patch("common.os.path.exists")
def test_update_defs_from_s3_same_hash(self, mock_exists, mock_md5_from_file):
expected_md5_hash = "d41d8cd98f00b204e9800998ecf8427e"
different_md5_hash = expected_md5_hash
mock_md5_from_file.return_value = different_md5_hash
tag_set = {"TagSet": [{"Key": "md5", "Value": expected_md5_hash}]}
expected_s3_time = datetime.datetime(2019, 1, 1)
s3_stubber = Stubber(self.s3_client)
key_names = []
side_effect = []
for file_prefix in AV_DEFINITION_FILE_PREFIXES:
for file_suffix in AV_DEFINITION_FILE_SUFFIXES:
side_effect.extend([True, True])
filename = file_prefix + "." + file_suffix
key_names.append(os.path.join(AV_DEFINITION_S3_PREFIX, filename))
mock_exists.side_effect = side_effect
for s3_key_name in key_names:
get_object_tagging_response = tag_set
get_object_tagging_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": s3_key_name,
}
s3_stubber.add_response(
"get_object_tagging",
get_object_tagging_response,
get_object_tagging_expected_params,
)
head_object_response = {"LastModified": expected_s3_time}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": s3_key_name,
}
s3_stubber.add_response(
"head_object", head_object_response, head_object_expected_params
)
expected_to_download = {}
with s3_stubber:
to_download = update_defs_from_s3(
self.s3_client, self.s3_bucket_name, AV_DEFINITION_S3_PREFIX
)
self.assertEquals(expected_to_download, to_download)
@mock.patch("clamav.md5_from_file")
@mock.patch("common.os.path.exists")
def test_update_defs_from_s3_old_files(self, mock_exists, mock_md5_from_file):
expected_md5_hash = "d41d8cd98f00b204e9800998ecf8427e"
different_md5_hash = "d41d8cd98f00b204e9800998ecf8427f"
mock_md5_from_file.return_value = different_md5_hash
tag_set = {"TagSet": [{"Key": "md5", "Value": expected_md5_hash}]}
expected_s3_time = datetime.datetime(2019, 1, 1)
s3_stubber = Stubber(self.s3_client)
key_names = []
side_effect = []
for file_prefix in AV_DEFINITION_FILE_PREFIXES:
for file_suffix in AV_DEFINITION_FILE_SUFFIXES:
side_effect.extend([True, True])
filename = file_prefix + "." + file_suffix
key_names.append(os.path.join(AV_DEFINITION_S3_PREFIX, filename))
mock_exists.side_effect = side_effect
count = 0
for s3_key_name in key_names:
get_object_tagging_response = tag_set
get_object_tagging_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": s3_key_name,
}
s3_stubber.add_response(
"get_object_tagging",
get_object_tagging_response,
get_object_tagging_expected_params,
)
head_object_response = {
"LastModified": expected_s3_time - datetime.timedelta(hours=count)
}
head_object_expected_params = {
"Bucket": self.s3_bucket_name,
"Key": s3_key_name,
}
s3_stubber.add_response(
"head_object", head_object_response, head_object_expected_params
)
count += 1
expected_to_download = {
"bytecode": {
"local_path": "/tmp/clamav_defs/bytecode.cld",
"s3_path": "clamav_defs/bytecode.cld",
},
"daily": {
"local_path": "/tmp/clamav_defs/daily.cld",
"s3_path": "clamav_defs/daily.cld",
},
"main": {
"local_path": "/tmp/clamav_defs/main.cld",
"s3_path": "clamav_defs/main.cld",
},
}
with s3_stubber:
to_download = update_defs_from_s3(
self.s3_client, self.s3_bucket_name, AV_DEFINITION_S3_PREFIX
)
self.assertEquals(expected_to_download, to_download)