def test_compress_and_encrypt(self, files_data):
        """
        Test that files are correctly compressed.
        """
        files, total_original_size = self.create_files(files_data)

        password = b'frodo-baggins'
        compressed_file = utils.compress_and_encrypt(
            [file['file'] for file in files], password)

        # Verify file is compressed.
        compressed_file_size = os.path.getsize(compressed_file)
        self.assertTrue(compressed_file_size < total_original_size)

        zipfile = ZipFile(compressed_file, 'r')

        for file in files:
            # Verify text file is present in zip file.
            self.assertIn(file['file'].name.split('/')[-1], zipfile.namelist())

            # Verify file content is readable is correct password.
            content = zipfile.read(file['file'].name.split('/')[-1], password)
            self.assertEqual(len(content), file['size'])

            # Also verify file is only accessible with correct password.
            with self.assertRaises(RuntimeError):
                zipfile.read(file['file'].name.split('/')[-1], b'gollum')
Exemple #2
0
    def send(self, enterprise_report_file_name):
        """
        Send the given file with the configured information.
        """
        # create a password encrypted zip file
        LOGGER.info('Encrypting data report for {}'.format(
            self.enterprise_customer_name))
        data_report_zip_name = compress_and_encrypt(
            enterprise_report_file_name, self.password)

        data_report_subject = self.REPORT_EMAIL_SUBJECT.format(
            enterprise_name=self.enterprise_customer_name)

        # email the file to the email address(es) in the configuration
        LOGGER.info('Sending encrypted data to {}'.format(
            self.enterprise_customer_name))
        try:
            send_email_with_attachment(data_report_subject,
                                       self.REPORT_EMAIL_BODY,
                                       self.REPORT_EMAIL_FROM_EMAIL,
                                       self.email, data_report_zip_name)
            LOGGER.info(
                'Email report with encrypted zip successfully sent to {} for {}'
                .format(', '.join(self.email), self.enterprise_customer_name))
        except SMTPException:
            LOGGER.exception('Failed to send email report to {} for {}'.format(
                self.email, self.enterprise_customer_name))
 def send(self, files):
     """Base method for sending files, to perform common sending logic."""
     LOGGER.info(
         f'Encrypting data report for {self.enterprise_customer_name}')
     zip_password = decrypt_string(
         self.encrypted_password
     ) if self.encrypted_password else self.encrypted_password
     return compress_and_encrypt(files, zip_password,
                                 self.pgp_encryption_key)
Exemple #4
0
    def test_compress_and_encrypt(self, files_data):
        """
        Test that files are correctly compressed.
        """
        files, total_original_size = create_files(files_data)

        password = b'frodo-baggins'
        compressed_file = utils.compress_and_encrypt(
            [file['file'] for file in files], password)

        verify_compressed(self, compressed_file, files, total_original_size,
                          password)
Exemple #5
0
    def send_enterprise_report(self):
        """
        Query the data warehouse (vertica) and export data to a csv file.

        This file will get encrypted and emailed to the Enterprise Customer.
        """
        enterprise_customer_name = self.reporting_config[
            'enterprise_customer']['name']

        LOGGER.info('Starting process to send email report to {}'.format(
            enterprise_customer_name))

        # initialize base csv file and file writer
        data_report_file_name, data_report_file_writer = self._create_data_report_csv_writer(
        )

        # query vertica and write each row to the file
        LOGGER.debug('Querying Vertica for data for {}'.format(
            enterprise_customer_name))
        data_report_file_writer.writerows(self._query_vertica())

        # create a password encrypted zip file
        LOGGER.debug(
            'Encrypting data report for {}'.format(enterprise_customer_name))
        data_report_zip_name = compress_and_encrypt(
            data_report_file_name,
            decrypt_string(self.reporting_config['password'],
                           self.reporting_config['initialization_vector']))

        # email the file to the email address in the configuration
        LOGGER.debug(
            'Sending encrypted data to {}'.format(enterprise_customer_name))
        try:
            send_email_with_attachment(self.REPORT_EMAIL_SUBJECT,
                                       self.REPORT_EMAIL_BODY,
                                       self.REPORT_EMAIL_FROM_EMAIL,
                                       self.reporting_config['email'],
                                       data_report_zip_name)
        except SMTPException:
            LOGGER.exception(
                'Failed to send email for {}'.format(enterprise_customer_name))

        self._cleanup()
    def test_encryption(self, files_data):
        """
        Test the successful decryption with a valid key
        and an unsuccessful decryption when an incorrect key.
        """
        files, size = self.create_files(files_data)
        password = '******'
        correct_key = self.pgpy_create_key('JohnDoe')
        wrong_key = self.pgpy_create_key('JohnDoe2')

        encrypted_file_name = utils.compress_and_encrypt(
            [file['file'] for file in files], password,
            str(correct_key.pubkey))
        message = pgpy.PGPMessage.from_file(encrypted_file_name)
        decrypted_message = correct_key.decrypt(message)
        self.assertIsInstance(decrypted_message, pgpy.PGPMessage)

        with self.assertRaises(PGPError):
            wrong_key.decrypt(message)
Exemple #7
0
 def send(self, files):
     """Base method for sending files, to perform common sending logic."""
     LOGGER.info('Encrypting data report for {}'.format(self.enterprise_customer_name))
     return compress_and_encrypt(files, self.password, self.pgp_encryption_key)