def test_sendgrid_handler(self, mock_send): sendgrid_delivery = SendGridDelivery(MAILER_CONFIG_AZURE, logger) sendgrid_messages = \ sendgrid_delivery.get_to_addrs_sendgrid_messages_map(self.loaded_message) result = sendgrid_delivery.sendgrid_handler(self.loaded_message, sendgrid_messages) self.assertTrue(result) mock_send.assert_called()
def test_sendgrid_handler(self, mock_send): sendgrid_delivery = SendGridDelivery(MAILER_CONFIG_AZURE, Mock(), logger) sendgrid_messages = \ sendgrid_delivery.get_to_addrs_sendgrid_messages_map(self.loaded_message) result = sendgrid_delivery.sendgrid_handler(self.loaded_message, sendgrid_messages) self.assertTrue(result) mock_send.assert_called_once() mail_contents = mock_send.call_args[0][0].contents[0].content self.assertIn('The following azure.keyvault resources', mail_contents)
def test_sendgrid_handler_multiple_to_addrs(self, mock_send): sendgrid_delivery = SendGridDelivery(MAILER_CONFIG_AZURE, Mock(), logger) sendgrid_messages = \ sendgrid_delivery.get_to_addrs_sendgrid_messages_map(self.multiple_addrs_message) result = sendgrid_delivery.sendgrid_handler( self.multiple_addrs_message, sendgrid_messages) self.assertTrue(result) self.assertEqual(2, mock_send.call_count) mail_contents = mock_send.call_args[0][0].contents[0].content self.assertIn('The following azure.keyvault resources', mail_contents) address_one = mock_send.call_args_list[0][0][0].personalizations[ 0].tos[0]['email'] self.assertEqual("*****@*****.**", address_one) address_two = mock_send.call_args_list[1][0][0].personalizations[ 0].tos[0]['email'] self.assertEqual("*****@*****.**", address_two)
def _deliver_email(self, queue_message): try: sendgrid_delivery = SendGridDelivery(self.config, self.session, self.logger) email_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map(queue_message) if 'smtp_server' in self.config: smtp_delivery = SmtpDelivery(config=self.config, session=self.session, logger=self.logger) for to_addrs, message in email_messages.items(): self.logger.info( 'Sending message to SMTP server, {}.'.format(self.config['smtp_server'])) smtp_delivery.send_message(message=message, to_addrs=list(to_addrs)) else: self.logger.info('Sending message to Sendgrid.') return sendgrid_delivery.sendgrid_handler(queue_message, email_messages) except Exception as error: self.logger.exception(error)
def process_azure_queue_message(self, encoded_azure_queue_message): queue_message = json.loads( zlib.decompress( base64.b64decode(encoded_azure_queue_message.content))) self.logger.debug( "Got account:%s message:%s %s:%d policy:%s recipients:%s" % (queue_message.get('account', 'na'), encoded_azure_queue_message.id, queue_message['policy']['resource'], len(queue_message['resources']), queue_message['policy']['name'], ', '.join(queue_message['action'].get('to')))) if any( e.startswith('slack') or e.startswith('https://hooks.slack.com/') for e in queue_message.get('action', ()).get('to')): from c7n_mailer.slack_delivery import SlackDelivery slack_delivery = SlackDelivery( self.config, self.logger, SendGridDelivery(self.config, self.logger)) slack_messages = slack_delivery.get_to_addrs_slack_messages_map( queue_message) try: slack_delivery.slack_handler(queue_message, slack_messages) except Exception: traceback.print_exc() pass # this section gets the map of metrics to send to datadog and delivers it if any( e.startswith('datadog') for e in queue_message.get('action', ()).get('to')): from c7n_mailer.datadog_delivery import DataDogDelivery datadog_delivery = DataDogDelivery(self.config, self.session, self.logger) datadog_message_packages = datadog_delivery.get_datadog_message_packages( queue_message) try: datadog_delivery.deliver_datadog_messages( datadog_message_packages, queue_message) except Exception: traceback.print_exc() pass # this section sends a notification to the resource owner via SendGrid try: sendgrid_delivery = SendGridDelivery(self.config, self.logger) email_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map( queue_message) if 'smtp_server' in self.config: smtp_delivery = SmtpDelivery(config=self.config, session=self.session, logger=self.logger) for to_addrs, message in six.iteritems(email_messages): smtp_delivery.send_message(message=message, to_addrs=list(to_addrs)) else: return sendgrid_delivery.sendgrid_handler( queue_message, email_messages) except Exception: traceback.print_exc() return True