"""Invoke av-scan lambda on upload approval file.""" import json from hooks.lambda_hook import OrchLambdaHook from lib import config from lib.constants import PAYMENT_GROUP_PAYMENT_REPORT_TYPES from lib.utils import aws from lib.utils import ows from tasks.payments_upload_approval import helpers def invoke_lambda_av_scan_task(dag_run, *arg, **kwargs): """Invoke lambda to scan upload approval file.""" event = helpers.get_event_from_params(dag_run, **kwargs) reports = ows.get_payment_group_payment_reports(event.target_id) upload_approval_report = next( ( report for report in reports if report['report_type'] == PAYMENT_GROUP_PAYMENT_REPORT_TYPES.APPROVAL ), None ) assert upload_approval_report is not None, 'Missing upload file' s3_object = aws.split_path(upload_approval_report['report_export_url']) av_scan_event = { 'detail': { 'requestParameters': { 'bucketName': s3_object.bucket, 'key': s3_object.key }, }, 'delete': True } # Note: This is the only invocation using $LATEST qualifier # ABACUS team is not the owner of AV Scan and the owners have not configured # Provisioned lambda. hook = OrchLambdaHook( config.AV_SCAN_LAMBDA_NAME, qualifier='$LATEST' ) response = hook.invoke_lambda(json.dumps(av_scan_event)) if not response.function_response.succeeded: raise Exception(response.error_message) if response.function_response.payload == {'has_virus': True}: raise Exception('Virus detected')