Skip to content

Commit

Permalink
☣ Add quarantine notifications (#15)
Browse files Browse the repository at this point in the history
* Add Slack SDK
Add mode checking
Add functions
Signed-off-by: GitHub <noreply@github.com>

---------

Signed-off-by: GitHub <noreply@github.com>
Co-authored-by: Gary H <26419401+Gary-H9@users.noreply.github.com>
  • Loading branch information
Jacob Woffenden and Gary-H9 authored Apr 9, 2024
1 parent 2a96a9b commit 95b3397
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 29 deletions.
143 changes: 114 additions & 29 deletions src/var/task/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import boto3
from notifications_python_client.notifications import NotificationsAPIClient
from slack_sdk import WebClient

sm_client = boto3.client("secretsmanager")
govuk_notify_api_key_secret = os.environ["GOVUK_NOTIFY_API_KEY_SECRET"]
Expand All @@ -14,39 +15,123 @@
sm_client.get_secret_value(SecretId=govuk_notify_templates_secret)["SecretString"]
)
notifications_client = NotificationsAPIClient(govuk_notify_api_key)
slack_token_secret = os.environ["SLACK_TOKEN_SECRET"]
slack_token = sm_client.get_secret_value(SecretId=slack_token_secret)["SecretString"]
slack_client = WebClient(token=slack_token)


def handler(event, context):
def supplier_configuration(supplier):
data_contact = sm_client.get_secret_value(
SecretId=f"ingestion/sftp/{supplier}/data-contact"
)["SecretString"]

technical_contact = sm_client.get_secret_value(
SecretId=f"ingestion/sftp/{supplier}/technical-contact"
)["SecretString"]

slack_channel = sm_client.get_secret_value(
SecretId=f"ingestion/sftp/{supplier}/slack-channel"
)["SecretString"]

target_bucket = sm_client.get_secret_value(
SecretId=f"ingestion/sftp/{supplier}/target-bucket"
)["SecretString"]

return data_contact, technical_contact, slack_channel, target_bucket


def send_slack(slack_channel, message):
response = slack_client.chat_postMessage(
channel=slack_channel,
text=message,
)
return response


def send_gov_uk_notify(template, email_address, personalisation):
response = notifications_client.send_email_notification(
template_id=template,
email_address=email_address,
personalisation=personalisation,
)
return response


def handler(event, context): # pylint: disable=unused-argument
print("Received event:", event)
print("Received context:", context)
try:
mode = os.environ.get("MODE")
if mode == "quarantined":
# This mode expects S3 bucket notifications via SNS
object_key = event["Records"][0]["s3"]["object"]["key"]
supplier, file_name = object_key.split("/")[:2]
supplier_config = supplier_configuration(supplier=supplier)

s3_bucket = event["Records"][0]["s3"]["bucket"]["name"]
object_key = event["Records"][0]["s3"]["object"]["key"]
# GOV.UK Notify Data Contact
send_gov_uk_notify(
template=govuk_notify_templates["sftp_quarantined_file_data_contact"],
email_address=supplier_config[0],
personalisation={"filename": file_name},
)

supplier, file_name = object_key.split("/")[:2]
print(f"Supplier: {supplier}")
print(f"File name: {file_name}")
# GOV.UK Notify Technical Contact
send_gov_uk_notify(
template=govuk_notify_templates[
"sftp_quarantined_file_technical_contact"
],
email_address=supplier_config[1],
personalisation={
"filename": file_name,
"supplier": supplier,
},
)

supplier_data_contact = sm_client.get_secret_value(
SecretId=f"ingestion/sftp/{supplier}/data-contact"
)["SecretString"]
# supplier_data_owner = sm_client.get_secret_value(
# SecretId=f"ingestion/sftp/{supplier}/technical-contact"
# )["SecretString"]

if s3_bucket == "analytical-platform-quarantine":
print("File quarantined")
# send data owner
# notifications_client.send_email_notification(
# template_id=govuk_notify_templates["sftp_quarantined_file_data_owner"],
# email_address=supplier_data_owner,
# personalisation={"supplier": supplier, "filename": file_name},
# )
# send data contact
notifications_client.send_email_notification(
template_id=govuk_notify_templates["sftp_quarantined_file_supplier"],
email_address=supplier_data_contact,
personalisation={"filename": file_name},
)
else:
print(s3_bucket)
# Slack Technical Contact
if supplier_config[2]:
send_slack(
slack_channel=supplier_config[2],
message=f"File {file_name} from {supplier} has been quarantined.",
)
else:
print(f"No Slack channel configured for {supplier}")

elif mode == "transferred":
# This mode expects CSV style notifications from
# the transfer Lambda
# e.g, "transferred,${supplier}/${file_name},${timestamp}"
state, object_key, timestamp = event[ # pylint: disable=unused-variable
"message"
].split(",")
supplier, file_name = object_key.split("/")[:2]
supplier_config = supplier_configuration(supplier=supplier)

# GOV.UK Notify Technical Contact
send_gov_uk_notify(
template=govuk_notify_templates[
"sftp_transferred_file_technical_contact"
],
email_address=supplier_config[1],
personalisation={
"filename": file_name,
"supplier": supplier,
"targetlocation": supplier_config[3],
},
)

# Slack Technical Contact
if supplier_config[2]:
send_slack(
slack_channel=supplier_config[2],
message=f"File {file_name} from {supplier} has been transferred to {supplier_config[3]}.",
)
else:
print(f"No Slack channel configured for {supplier}")

else:
raise ValueError(f"Invalid mode: {mode}")

return {"statusCode": 200, "body": json.dumps({"message": "Success"})}
except ValueError as e:
print(f"Configuration Error: {e}")
return {"statusCode": 400, "body": json.dumps({"message": str(e)})}
1 change: 1 addition & 0 deletions src/var/task/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
boto3==1.34.80
botocore==1.34.80
notifications-python-client==9.0.0
slack_sdk==3.27.1

0 comments on commit 95b3397

Please sign in to comment.