def test_map_sending_to_channel(self): slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) assert self.target_channel in result assert json.loads( result[self.target_channel])['channel'] == self.target_channel
def test_send_slack_msg_not_ok_response(self, mock_post): mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = {'ok': False, 'error': "failed"} slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) slack.send_slack_msg(self.target_channel, result[self.target_channel]) self.logger.info.assert_called_with('Error in sending Slack message. Status:%s, ' 'response:%s', 200, 'failed')
def test_send_slack_msg_not_200_response(self, mock_post): mock_post.return_value.status_code = 404 mock_post.return_value.text = "channel_not_found" slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) slack.send_slack_msg(self.target_channel, result[self.target_channel]) self.logger.info.assert_called_with('Error in sending Slack message status:%s response: %s', 404, 'channel_not_found')
def _deliver_slack_message(self, queue_message): from c7n_mailer.slack_delivery import SlackDelivery slack_delivery = SlackDelivery(self.config, self.logger, SendGridDelivery(self.config, self.session, self.logger)) slack_messages = slack_delivery.get_to_addrs_slack_messages_map(queue_message) try: self.logger.info('Sending message to Slack.') slack_delivery.slack_handler(queue_message, slack_messages) except Exception as error: self.logger.exception(error)
def test_slack_handler(self, mock_post): mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = {'ok': True} slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) slack.slack_handler(self.message, result) self.logger.info.assert_called_with("Sending account:core-services-dev " "policy:ebs-mark-unattached-deletion ebs:1 slack:slack" "_default to test-channel")
def test_map_sending_to_tag_channel_no_tag(self): slack = SlackDelivery(self.config, self.logger, self.email_delivery) message_destination = ['slack://tag/SlackChannel'] self.message['action']['to'] = message_destination self.message['policy']['actions'][1]['to'] = message_destination result = slack.get_to_addrs_slack_messages_map(self.message) assert result == {} self.logger.debug.assert_called_with("No SlackChannel tag found in resource.")
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 test_send_slack_msg_retry_after(self, mock_post): retry_after_delay = 1 mock_post.return_value.status_code = 429 mock_post.return_value.headers = {'Retry-After': retry_after_delay} slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) slack.send_slack_msg(self.target_channel, result[self.target_channel]) args, kwargs = mock_post.call_args self.logger.info.assert_called_with("Slack API rate limiting. Waiting %d seconds", retry_after_delay)
def test_send_slack_msg(self, mock_post): mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = {'ok': True} slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) slack.send_slack_msg(self.target_channel, result[self.target_channel]) args, kwargs = mock_post.call_args assert self.target_channel == json.loads(kwargs['data'])['channel'] assert SLACK_POST_MESSAGE_API == kwargs['url'] assert kwargs['data'] == result[self.target_channel]
def test_map_sending_to_webhook(self): webhook = "https://hooks.slack.com/this-is-a-webhook" slack = SlackDelivery(self.config, self.logger, self.email_delivery) message_destination = [webhook] self.message['action']['to'] = message_destination self.message['policy']['actions'][1]['to'] = message_destination result = slack.get_to_addrs_slack_messages_map(self.message) assert webhook in result assert 'channel' not in json.loads(result[webhook])
def test_map_sending_to_tag_channel(self): self.target_channel = 'tag-channel' print(self.config['templates_folders']) slack = SlackDelivery(self.config, self.logger, self.email_delivery) message_destination = ['slack://tag/SlackChannel'] self.resource['Tags'].append({"Key": "SlackChannel", "Value": self.target_channel}) self.message['action']['to'] = message_destination self.message['policy']['actions'][1]['to'] = message_destination result = slack.get_to_addrs_slack_messages_map(self.message) assert self.target_channel in result assert json.loads(result[self.target_channel])['channel'] == self.target_channel self.logger.debug.assert_called_with("Generating message for specified Slack channel.")
def test_send_slack_msg_webhook(self, mock_post): mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = {'ok': True} webhook = "https://hooks.slack.com/this-is-a-webhook" message_destination = [webhook] self.message['action']['to'] = message_destination self.message['policy']['actions'][1]['to'] = message_destination slack = SlackDelivery(self.config, self.logger, self.email_delivery) result = slack.get_to_addrs_slack_messages_map(self.message) slack.send_slack_msg(webhook, result[webhook]) args, kwargs = mock_post.call_args assert webhook == kwargs['url'] assert kwargs['data'] == result[webhook]
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) sendgrid_messages = sendgrid_delivery.get_to_addrs_sendgrid_messages_map(queue_message) return sendgrid_delivery.sendgrid_handler(queue_message, sendgrid_messages) except Exception: traceback.print_exc()