Example #1
0
 async def receive(request):
     Log.debug(f"Received GET request for component " \
         f"{request.rel_url.query['component']}")
     try:
         component = request.rel_url.query['component']
         EventMessage.subscribe(component=component)
         alert = EventMessage.receive()
     except EventMessageError as e:
         status_code = e.rc
         error_message = e.desc
         Log.error(f"Unable to receive event message for component: " \
             f"{component}, status code: {status_code}," \
             f" error: {error_message}")
         response_obj = {'error_code': status_code, 'exception': \
             ['EventMessageError', {'message': error_message}]}
     except Exception as e:
         exception_key = type(e).__name__
         exception = RestServerError(exception_key).http_error()
         status_code = exception[0]
         error_message = exception[1]
         Log.error(f"Internal error while receiving event messages for " \
             f"component: {component}, status code: " \
             f"{status_code}, error: {error_message}")
         response_obj = {'error_code': status_code, 'exception': \
             [exception_key, {'message': error_message}]}
         raise EventMessageError(status_code, error_message) from e
     else:
         status_code = 200  # No exception, Success
         response_obj = {'alert': alert}
         Log.debug(f"GET method finished with status code: {status_code}" \
             f"for component {component} and received event message " \
             f"alert info. - {alert['iem']['info']}.")
     finally:
         return web.Response(text=json.dumps(response_obj), \
             status=status_code)
 async def send_webhook_info(request):
     Log.debug("Received POST request for webhook information")
     try:
         external_server_info = await request.json()
         # write webhook info to the external server
         AuditLogRequestHandler.webhook_info = external_server_info
         # TODO store webhook_info to persistent storage
     except AuditLogError as e:
         status_code = e.rc
         error_message = e.desc
         Log.error(f"Unable to receive audit webhook information, status code: " \
             f"{status_code}, error: {error_message}")
         response_obj = {'error_code': status_code, 'exception': \
             ['AuditLogError', {'message': error_message}]}
     except Exception as e:
         exception_key = type(e).__name__
         exception = RestServerError(exception_key).http_error()
         status_code = exception[0]
         error_message = exception[1]
         Log.error(f"Internal error while receiving webhook info." \
             f"status code: {status_code}, error: {error_message}")
         response_obj = {'error_code': status_code, 'exception': \
             [exception_key, {'message': error_message}]}
         raise AuditLogError(status_code, error_message) from e
     else:
         status_code = 200  # No exception, Success
         response_obj = {}
         Log.debug(f"Receiving webhook info using POST method finished with status " \
             f"code: {status_code}")
         response_obj = {'status_code': status_code, 'status': 'success'}
     finally:
         return web.Response(text=json.dumps(response_obj), \
             status=status_code)
    async def send(request):
        Log.debug("Received POST request for audit message")
        try:
            payload = await request.json()
            messages = payload['messages']
            external_server_info = AuditLogRequestHandler.webhook_info
            # send audit logs messages to to external server provided by webhook
            if external_server_info:
                # write Audit Log messages to the webhook server
                import requests

                data = json.dumps({'messages': messages})
                headers = {'content-type': 'application/json'}
                response = requests.post(url=external_server_info, data=data,\
                    headers=headers)
                if response.status_code != 200:
                    # if status code from webhok server is not 200 then store audit
                    # log message to message bus
                    producer.send(messages)
            else:
                # write the audit log message to message bus
                producer.send(messages)
        except AuditLogError as e:
            status_code = e.rc
            error_message = e.desc
            Log.error(f"Unable to receive audit messages, status code: {status_code}," \
                f" error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                ['AuditLogError', {'message': error_message}]}
        except Exception as e:
            exception_key = type(e).__name__
            exception = RestServerError(exception_key).http_error()
            status_code = exception[0]
            error_message = exception[1]
            Log.error(f"Internal error while receiving audit messages." \
                f"status code: {status_code}, error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                [exception_key, {'message': error_message}]}
            raise AuditLogError(status_code, error_message) from e
        else:
            status_code = 200  # No exception, Success
            response_obj = {}
            Log.debug(f"Receiving audit messages using POST method finished with status"\
                f" code: {status_code}")
            response_obj = {'status_code': status_code, 'status': 'success'}
        finally:
            return web.Response(text=json.dumps(response_obj), \
                status=status_code)
Example #4
0
    async def send(request):
        try:
            payload = await request.json()

            component = payload['component']
            source = payload['source']
            EventMessage.init(component=component, source=source,\
                cluster_id=IemRequestHandler.cluster_id,\
                message_server_endpoints=IemRequestHandler.message_server_endpoints)

            del payload['component']
            del payload['source']
            EventMessage.send(**payload)
        except EventMessageError as e:
            status_code = e.rc
            error_message = e.desc
            Log.error(f"Unable to send event message for component: " \
                      f"{component}, status code: {status_code}," \
                      f" error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                ['EventMessageError', {'message': error_message}]}
        except Exception as e:
            exception_key = type(e).__name__
            exception = RestServerError(exception_key).http_error()
            status_code = exception[0]
            error_message = exception[1]
            Log.error(f"Internal error while sending event messages for " \
                f"component: {component}, status code: " \
                f"{status_code}, error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                [exception_key, {'message': error_message}]}
            raise EventMessageError(status_code, error_message) from e
        else:
            status_code = 200  # No exception, Success
            response_obj = {'status_code': status_code, 'status': 'success'}
            Log.debug(f"POST method finished for component: {component} "
                      f"and source: {source}, with status code: {status_code}")
        finally:
            return web.Response(text=json.dumps(response_obj), \
                status=status_code)
Example #5
0
    async def receive(request):
        Log.debug(f"Received GET request for message type " \
            f"{request.match_info['message_type']}. Getting message")
        try:
            message_types = str(request.match_info['message_type']).split('&')
            consumer_group = request.rel_url.query['consumer_group']
            consumer = MessageConsumer(consumer_id='rest_consumer', \
                consumer_group=consumer_group, message_types=message_types, \
                auto_ack=True, offset='latest')

            message = consumer.receive()
        except MessageBusError as e:
            status_code = e.rc
            error_message = e.desc
            Log.error(f"Unable to receive message for message_type: " \
                f"{message_types} using consumer group {consumer_group}, " \
                f"status code: {status_code}, error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                ['MessageBusError', {'message': error_message}]}
        except Exception as e:
            exception_key = type(e).__name__
            exception = RestServerError(exception_key).http_error()
            status_code = exception[0]
            error_message = exception[1]
            Log.error(f"Internal error while receiving messages for " \
                f"message_type: {message_types}, status code: " \
                f"{status_code}, error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                [exception_key, {'message': error_message}]}
            raise MessageBusError(status_code, error_message) from e
        else:
            status_code = 200  # No exception, Success
            response_obj = {'messages': str(message)}
            Log.debug(f"Received message - {message} for message_type "
                f"{message_types} using GET method finished with " \
                f"status code: {status_code}")
        finally:
            return web.Response(text=json.dumps(response_obj), \
                status=status_code)
Example #6
0
    async def send(request):
        Log.debug(f"Received POST request for message type " \
            f"{request.match_info['message_type']}. Processing message")
        try:
            message_type = request.match_info['message_type']
            payload = await request.json()
            messages = payload['messages']
            producer = MessageProducer(producer_id='rest_producer', \
                message_type=message_type, method='sync')

            producer.send(messages)
        except MessageBusError as e:
            status_code = e.rc
            error_message = e.desc
            Log.error(f"Unable to send message for message_type: " \
                f"{message_type}, status code: {status_code}," \
                f" error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                ['MessageBusError', {'message': error_message}]}
        except Exception as e:
            exception_key = type(e).__name__
            exception = RestServerError(exception_key).http_error()
            status_code = exception[0]
            error_message = exception[1]
            Log.error(f"Internal error while sending messages for " \
                f"message_type: {message_type}, status code: " \
                f"{status_code}, error: {error_message}")
            response_obj = {'error_code': status_code, 'exception': \
                [exception_key, {'message': error_message}]}
            raise MessageBusError(status_code, error_message) from e
        else:
            status_code = 200  # No exception, Success
            Log.debug(f"Sending messages for message_type  " \
                f"{message_type} using POST method finished with status " \
                f"code: {status_code}")
            response_obj = {'status_code': status_code, 'status': 'success'}
        finally:
            return web.Response(text=json.dumps(response_obj), \
                status=status_code)