diff --git a/CHANGELOG.md b/CHANGELOG.md index 34f581cef34..ff791c5495f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -133,6 +133,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - "POST /api/pba/upload/?load=" endpoint. #2442 - "POST /api/pba/upload/?restore=" endpoint. #2442 - The TelemetryLog component from the Infection Map page. #2500 +- "GET /api/telemetry-feed" endpoint. #2502 ### Fixed - A bug in network map page that caused delay of telemetry log loading. #1545 diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index 478284df7a2..efac4d5ab42 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -42,7 +42,6 @@ from monkey_island.cc.resources.root import Root from monkey_island.cc.resources.security_report import SecurityReport from monkey_island.cc.resources.telemetry import Telemetry -from monkey_island.cc.resources.telemetry_feed import TelemetryFeed from monkey_island.cc.resources.version import Version from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.services.representations import output_json @@ -171,7 +170,6 @@ def init_restful_endpoints(api: FlaskDIWrapper): api.add_resource(ManualExploitation) api.add_resource(MonkeyExploitation) - api.add_resource(TelemetryFeed) api.add_resource(Log) api.add_resource(AgentLogs) api.add_resource(IslandLog) diff --git a/monkey/monkey_island/cc/resources/telemetry_feed.py b/monkey/monkey_island/cc/resources/telemetry_feed.py deleted file mode 100644 index efb572b2dd7..00000000000 --- a/monkey/monkey_island/cc/resources/telemetry_feed.py +++ /dev/null @@ -1,81 +0,0 @@ -import logging -from datetime import datetime - -import dateutil -import flask_pymongo -from flask import request - -from common.common_consts.telem_categories import TelemCategoryEnum -from monkey_island.cc.database import mongo -from monkey_island.cc.resources.AbstractResource import AbstractResource -from monkey_island.cc.resources.request_authentication import jwt_required -from monkey_island.cc.services.node import NodeService - -logger = logging.getLogger(__name__) - - -class TelemetryFeed(AbstractResource): - urls = ["/api/telemetry-feed"] - - @jwt_required - def get(self, **kw): - timestamp = request.args.get("timestamp") - if "null" == timestamp or timestamp is None: # special case to avoid ugly JS code... - telemetries = mongo.db.telemetry.find({}) - else: - telemetries = mongo.db.telemetry.find( - {"timestamp": {"$gt": dateutil.parser.parse(timestamp)}} - ) - telemetries = telemetries.sort([("timestamp", flask_pymongo.ASCENDING)]) - - try: - return { - "telemetries": [ - TelemetryFeed.get_displayed_telemetry(telem) - for telem in telemetries - if TelemetryFeed.should_show_brief(telem) - ], - "timestamp": datetime.now().isoformat(), - } - except KeyError as err: - logger.error("Failed parsing telemetries. Error: {0}.".format(err)) - # API Spec: Should return HTTP status code 404 (?) - return {"telemetries": [], "timestamp": datetime.now().isoformat()} - - @staticmethod - def get_displayed_telemetry(telem): - monkey = NodeService.get_monkey_by_guid(telem["monkey_guid"]) - default_hostname = "GUID-" + telem["monkey_guid"] - return { - "id": telem["_id"], - "timestamp": telem["timestamp"].strftime("%d/%m/%Y %H:%M:%S"), - "hostname": monkey.get("hostname", default_hostname) if monkey else default_hostname, - "brief": TelemetryFeed.get_telem_brief(telem), - } - - @staticmethod - def get_telem_brief(telem): - telem_brief_parser = TelemetryFeed.get_telem_brief_parser_by_category( - telem["telem_category"] - ) - return telem_brief_parser(telem) - - @staticmethod - def get_telem_brief_parser_by_category(telem_category): - return TELEM_PROCESS_DICT[telem_category] - - @staticmethod - def get_state_telem_brief(telem): - if telem["data"]["done"]: - return """Monkey finishing its execution.""" - else: - return "Monkey started." - - @staticmethod - def should_show_brief(telem) -> bool: - return telem["telem_category"] in TELEM_PROCESS_DICT - - -TELEM_PROCESS_DICT = { - TelemCategoryEnum.STATE: TelemetryFeed.get_state_telem_brief, -}