示例#1
0
    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()
    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()
示例#3
0
    def test_map_sending_to_tag_channel_with_hash(self):
        self.target_channel = '#tag-channel'
        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_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