Ejemplo n.º 1
0
    def take_action(self, alert: 'Alert', action: str, text: str, **kwargs) -> Any:

        if is_in_xloop(base_url()):
            http_origin = request.origin or '(unknown)'  # type: ignore
            raise ForwardingLoop('Action {} forwarded by {} already processed by {}'.format(
                action, http_origin, base_url())
            )

        for remote, auth, actions in self.get_config('FWD_DESTINATIONS', default=[], type=list, **kwargs):
            if is_in_xloop(remote):
                LOG.debug(f'Forward [action={action}]: {alert.id} ; Remote {remote} already processed action. Skip.')
                continue
            if not ('*' in actions or 'actions' in actions or action in actions):
                LOG.debug(f'Forward [action={action}]: {alert.id} ; Remote {remote} not configured for action. Skip.')
                continue

            headers = {X_LOOP_HEADER: append_to_header(base_url())}
            client = Client(endpoint=remote, headers=headers, **auth)

            LOG.info(f'Forward [action={action}]: {alert.id} ; {base_url()} -> {remote}')
            try:
                r = client.action(alert.id, action, text)
            except Exception as e:
                LOG.warning(f'Forward [action={action}]: {alert.id} ; Failed to action alert on {remote} - {str(e)}')
                continue
            LOG.debug(f'Forward [action={action}]: {alert.id} ; [{r.status_code}] {r.text}')

        return alert
Ejemplo n.º 2
0
    def delete(self, alert: 'Alert', **kwargs) -> bool:

        if is_in_xloop(base_url()):
            http_origin = request.origin or '(unknown)'  # type: ignore
            raise ForwardingLoop(f'Delete forwarded by {http_origin} already processed by {base_url()}')

        for remote, auth, actions in self.get_config('FWD_DESTINATIONS', default=[], type=list, **kwargs):
            if is_in_xloop(remote):
                LOG.debug(f'Forward [action=delete]: {alert.id} ; Remote {remote} already processed delete. Skip.')
                continue
            if not ('*' in actions or 'delete' in actions):
                LOG.debug(f'Forward [action=delete]: {alert.id} ; Remote {remote} not configured for deletes. Skip.')
                continue

            headers = {X_LOOP_HEADER: append_to_header(base_url())}
            client = Client(endpoint=remote, headers=headers, **auth)

            LOG.info(f'Forward [action=delete]: {alert.id} ; {base_url()} -> {remote}')
            try:
                r = client.delete_alert(alert.id)
            except Exception as e:
                LOG.warning(f'Forward [action=delete]: {alert.id} ; Failed to delete alert on {remote} - {str(e)}')
                continue
            LOG.debug(f'Forward [action=delete]: {alert.id} ; [{r.status_code}] {r.text}')

        return True  # always continue with local delete even if remote delete(s) fail
Ejemplo n.º 3
0
    def pre_receive(self, alert: 'Alert', **kwargs) -> 'Alert':

        if is_in_xloop(base_url()):
            http_origin = request.origin or '(unknown)'  # type: ignore
            raise ForwardingLoop(
                'Alert forwarded by {} already processed by {}'.format(
                    http_origin, base_url()))
        return alert