Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 75b0d89

Browse files
committed
Do not scan tagged files
1 parent 0e86c59 commit 75b0d89

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ the table below for reference.
8989
| AV_SCAN_START_SNS_ARN | SNS topic ARN to publish notification about start of scan | | No |
9090
| AV_SCAN_START_METADATA | The tag/metadata indicating the start of the scan | av-scan-start | No |
9191
| AV_SIGNATURE_METADATA | The tag/metadata name representing file's AV type | av-signature | No |
92+
| AV_STATUS_DO_NOT_SCAN | The value assigned to block scanning of items inside of tags/metadata | DO_NOT_SCAN | No |
9293
| AV_STATUS_CLEAN | The value assigned to clean items inside of tags/metadata | CLEAN | No |
9394
| AV_STATUS_INFECTED | The value assigned to clean items inside of tags/metadata | INFECTED | No |
9495
| AV_STATUS_METADATA | The tag/metadata name representing file's AV status | av-status | No |

common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
AV_SIGNATURE_METADATA = os.getenv("AV_SIGNATURE_METADATA", "av-signature")
2727
AV_SIGNATURE_OK = "OK"
2828
AV_SIGNATURE_UNKNOWN = "UNKNOWN"
29+
AV_STATUS_DO_NOT_SCAN = os.getenv("AV_STATUS_DO_NOT_SCAN", "DO_NOT_SCAN")
2930
AV_STATUS_CLEAN = os.getenv("AV_STATUS_CLEAN", "CLEAN")
3031
AV_STATUS_INFECTED = os.getenv("AV_STATUS_INFECTED", "INFECTED")
3132
AV_STATUS_METADATA = os.getenv("AV_STATUS_METADATA", "av-status")

scan.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from common import AV_SCAN_START_SNS_ARN
3232
from common import AV_SIGNATURE_METADATA
3333
from common import AV_STATUS_CLEAN
34+
from common import AV_STATUS_DO_NOT_SCAN
3435
from common import AV_STATUS_INFECTED
3536
from common import AV_STATUS_METADATA
3637
from common import AV_STATUS_SNS_ARN
@@ -213,6 +214,14 @@ def lambda_handler(event, context):
213214
print("Script starting at %s\n" % (start_time))
214215
s3_object = event_object(event, event_source=EVENT_SOURCE)
215216

217+
if not object_have_to_be_scanned(s3_client, s3_object):
218+
set_av_tags(s3_client, s3_object, AV_STATUS_DO_NOT_SCAN, 'NC', get_timestamp())
219+
print(
220+
"Skipp of s3://%s file is tagged DO_NOT_SCAN \n"
221+
% (os.path.join(s3_object.bucket_name, s3_object.key))
222+
)
223+
return
224+
216225
if str_to_bool(AV_PROCESS_ORIGINAL_VERSION_ONLY):
217226
verify_s3_object_version(s3, s3_object)
218227

@@ -274,3 +283,15 @@ def lambda_handler(event, context):
274283

275284
def str_to_bool(s):
276285
return bool(strtobool(str(s)))
286+
287+
288+
# Determine if an object have to be scanned (tagged DO_NOT_CLEAN)
289+
def object_have_to_be_scanned(s3_client, s3_object):
290+
s3_object_tags = s3_client.get_object_tagging(Bucket=s3_object.bucket_name, Key=s3_object.key)
291+
if "TagSet" not in s3_object_tags:
292+
return True
293+
for tag in s3_object_tags["TagSet"]:
294+
if tag["Key"] in [AV_STATUS_METADATA] and tag["Value"] in [AV_STATUS_DO_NOT_SCAN]:
295+
return False
296+
return True
297+

scan_bucket.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from common import AV_TIMESTAMP_METADATA
2626
from common import S3_ENDPOINT
2727

28-
2928
# Get all objects in an S3 bucket that have not been previously scanned
3029
def get_objects(s3_client, s3_bucket_name):
3130

@@ -43,12 +42,24 @@ def get_objects(s3_client, s3_bucket_name):
4342
for key in s3_list_objects_result["Contents"]:
4443
key_name = key["Key"]
4544
# Don't include objects that have been scanned
46-
if not object_previously_scanned(s3_client, s3_bucket_name, key_name):
45+
if not object_previously_scanned(s3_client, s3_bucket_name, key_name) and \
46+
object_have_to_be_scanned(s3_client, s3_bucket_name, key_name):
4747
s3_object_list.append(key_name)
4848

4949
return s3_object_list
5050

5151

52+
# Determine if an object have to be scanned (tagged DO_NOT_CLEAN)
53+
def object_have_to_be_scanned(s3_client, s3_bucket_name, key_name):
54+
s3_object_tags = s3_client.get_object_tagging(Bucket=s3_bucket_name, Key=key_name)
55+
if "TagSet" not in s3_object_tags:
56+
return True
57+
for tag in s3_object_tags["TagSet"]:
58+
if tag["Key"] in [AV_STATUS_METADATA] and tag["Value"] in [AV_STATUS_DO_NOT_SCAN]:
59+
return False
60+
return True
61+
62+
5263
# Determine if an object has been previously scanned for viruses
5364
def object_previously_scanned(s3_client, s3_bucket_name, key_name):
5465
s3_object_tags = s3_client.get_object_tagging(Bucket=s3_bucket_name, Key=key_name)

0 commit comments

Comments
 (0)