From fdb7fb659464a199972e4948e6dd544088e6ac1a Mon Sep 17 00:00:00 2001 From: Rebecca Hsieh Date: Wed, 28 Feb 2024 09:17:59 -0800 Subject: [PATCH] Biz logic --- .../generate_current_metadata_report.py | 28 ++++++++-- src/registrar/utility/email.py | 54 +++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/registrar/management/commands/generate_current_metadata_report.py b/src/registrar/management/commands/generate_current_metadata_report.py index d8f5a4693..69d111f4c 100644 --- a/src/registrar/management/commands/generate_current_metadata_report.py +++ b/src/registrar/management/commands/generate_current_metadata_report.py @@ -2,10 +2,12 @@ import logging import os +import pyzipper from django.core.management import BaseCommand from registrar.utility import csv_export from registrar.utility.s3_bucket import S3ClientHelper +from ...utility.email import send_templated_email, EmailSendingError logger = logging.getLogger(__name__) @@ -59,9 +61,27 @@ class Command(BaseCommand): We want to make sure to upload to s3 for back up And now we also want to get the file and encrypt it so we can send it in an email """ - # metadata_file = s3_client.get_file(file_name) - # metadata_file.encryptthisthingherewithpyzipper - # email.blasend_email(metadata_file) + unencrypted_metadata_input = s3_client.get_file(file_name) + + # Encrypt metadata into a zip file + + # pre-setting zip file name + encrypted_metadata_output = 'encrypted_metadata.zip' + # set this to be an env var somewhere + password = b'somepasswordhere' + # encrypted_metadata is the encrypted output + encrypted_metadata = _encrypt_metadata(unencrypted_metadata_input, encrypted_metadata_output, password) + print("encrypted_metadata is:", encrypted_metadata) + + # Send the metadata file that is zipped + # Q: Would we set the vars I set in email.py here to pass in to the helper function or best way to invoke + # send_templated_email(encrypted_metadata, attachment=True) - + def _encrypt_metadata(input_file, output_file, password): + with open(input_file, 'rb') as f_in: + with pyzipper.AESZipFile(output_file, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as f_out: + f_out.setpassword(password) + f_out.writestr(input_file, f_in.read()) + return output_file + diff --git a/src/registrar/utility/email.py b/src/registrar/utility/email.py index e4e997d9d..199a6c304 100644 --- a/src/registrar/utility/email.py +++ b/src/registrar/utility/email.py @@ -40,6 +40,8 @@ def send_templated_email(template_name: str, subject_template_name: str, to_addr except Exception as exc: raise EmailSendingError("Could not access the SES client.") from exc + # Are we okay with passing in "attachment" var in as boolean parameter + # If so, TODO: add attachment boolean to other functions try: #if not attachment: ses_client.send_email( @@ -55,5 +57,57 @@ def send_templated_email(template_name: str, subject_template_name: str, to_addr # else: # has attachment # same as above but figure out how to attach a file # via boto3 "boto3 SES file attachment" + # we also want this to only send to the help email + + # from email.mime.multipart import MIMEMultipart + # from email.mime.text import MIMEText + # from email.mime.application import MIMEApplication + + # sender_email = 'sender@example.com' + # recipient_email = 'help@get.gov' + # subject = 'DOTGOV-Full Domain Metadata' + # body = 'Domain metadata email, should have an attachment included change here later.' + # attachment_path = 'path/to/attachment/file.pdf' + # aws_region = 'sesv2' + + # response = send_email_with_attachment(sender_email, recipient_email, subject, body, attachment_path, aws_region) + # print(response) except Exception as exc: raise EmailSendingError("Could not send SES email.") from exc + + +# def send_email_with_attachment(sender, recipient, subject, body, attachment_path, aws_region): + # # Create a multipart/mixed parent container + # msg = MIMEMultipart('mixed') + # msg['Subject'] = subject + # msg['From'] = sender_email + # msg['To'] = recipient_email + + # # Add the text part + # text_part = MIMEText(body, 'plain') + # msg.attach(text_part) + + # # Add the attachment part + # with open(attachment_path, 'rb') as attachment_file: + # attachment_data = attachment_file.read() + # attachment_part = MIMEApplication(attachment_data) + # attachment_part.add_header('Content-Disposition', f'attachment; filename="{attachment_path}"') + # msg.attach(attachment_part) + + # # Send the email + # response = ses_client.send_raw_email( + # Source=sender, + # Destinations=[recipient], + # RawMessage={'Data': msg.as_string()} + # ) + + # ses_client.send_email( + # FromEmailAddress=settings.DEFAULT_FROM_EMAIL, + # Destination={"ToAddresses": [to_address]}, + # Content={ + # "Simple": { + # "Subject": {"Data": subject}, + # "Body": {"Text": {"Data": email_body}}, + # }, + # }, + # ) \ No newline at end of file