def send_c7n_email(self, sqs_message, email_to_addrs, mimetext_msg): try: # if smtp_server is set in mailer.yml, send through smtp if 'smtp_server' in self.config: smtp_delivery = SmtpDelivery(config=self.config, session=self.session, logger=self.logger) smtp_delivery.send_message(message=mimetext_msg, to_addrs=email_to_addrs) elif 'sendgrid_api_key' in self.config: sendgrid_delivery = sendgrid.SendGridDelivery( config=self.config, session=self.session, logger=self.logger) sendgrid_delivery.sendgrid_handler( sqs_message, self.get_to_addrs_email_messages_map(sqs_message)) # if smtp_server or sendgrid_api_key isn't set in mailer.yml, use aws ses normally. else: self.aws_ses.send_raw_email( RawMessage={'Data': mimetext_msg.as_string()}) except Exception as error: self.logger.warning( "Error policy:%s account:%s sending to:%s \n\n error: %s\n\n mailer.yml: %s" % (sqs_message['policy'], sqs_message.get( 'account', ''), email_to_addrs, error, self.config)) self.logger.info( "Sending account:%s policy:%s %s:%s email:%s to %s" % (sqs_message.get('account', ''), sqs_message['policy']['name'], sqs_message['policy']['resource'], str(len(sqs_message['resources'])), sqs_message['action'].get( 'template', 'default'), email_to_addrs))
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
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 test_send_message(self, mock_smtp): config = { 'smtp_server': 'server', 'smtp_port': 25, 'smtp_ssl': False, 'smtp_username': None, 'smtp_password': None } d = SmtpDelivery(config, MagicMock(), MagicMock()) message_mock = MagicMock() message_mock.__getitem__.side_effect = lambda x: '*****@*****.**' if x == 'From' else None message_mock.as_string.return_value = 'mock_text' d.send_message(message_mock, ['*****@*****.**']) del d mock_smtp.assert_has_calls([ call('server', 25), call().sendmail('*****@*****.**', ['*****@*****.**'], 'mock_text'), call().quit() ])
def test_no_ssl(self, mock_smtp): config = { 'smtp_server': 'server', 'smtp_port': 25, 'smtp_ssl': False, 'smtp_username': None, 'smtp_password': None } d = SmtpDelivery(config, MagicMock(), MagicMock()) del d mock_smtp.assert_has_calls([call('server', 25), call().quit()])
def test_no_ssl_with_credentials(self, mock_smtp, decrypt_mock): config = { 'smtp_server': 'server', 'smtp_port': 25, 'smtp_ssl': False, 'smtp_username': '******', 'smtp_password': '******' } d = SmtpDelivery(config, MagicMock(), MagicMock()) del d mock_smtp.assert_has_calls([ call('server', 25), call().login('username', 'password'), call().quit() ])