From c61737abc0c029f6dd6106a3d04de476c8887f85 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Fri, 12 Aug 2022 09:32:50 +0300 Subject: [PATCH] Island: Send analytics on island startup instead of object creation This change makes the logic more evident, because sending the analytics is not done during the initialization of an object --- monkey/monkey_island/cc/server_setup.py | 26 +++++++++++++++++ monkey/monkey_island/cc/services/analytics.py | 28 ------------------- .../monkey_island/cc/services/initialize.py | 2 -- 3 files changed, 26 insertions(+), 30 deletions(-) delete mode 100644 monkey/monkey_island/cc/services/analytics.py diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index eed949a02..c8d9985fe 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -5,8 +5,11 @@ import sys from pathlib import Path import gevent.hub +import requests from gevent.pywsgi import WSGIServer +from monkey_island.cc import Version +from monkey_island.cc.deployment import Deployment from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.setup.config_setup import get_server_config @@ -150,6 +153,7 @@ def _start_island_server( error_log=logger, ) _log_init_info() + _send_analytics(container) http_server.serve_forever() @@ -173,3 +177,25 @@ def _log_web_interface_access_urls(): "To access the web interface, navigate to one of the the following URLs using your " f"browser: {web_interface_urls}" ) + + +ANALYTICS_URL = ( + "https://m15mjynko3.execute-api.us-east-1.amazonaws.com/default?version={" + "version}&deployment={deployment}" +) + + +def _send_analytics(di_container): + version = di_container.resolve(Version) + deployment = di_container.resolve(Deployment) + url = ANALYTICS_URL.format(deployment=deployment.value, version=version.version_number) + try: + response = requests.get(url).json() + logger.info( + f"Version number and deployment type was sent to analytics server." + f" The response is: {response}" + ) + except requests.exceptions.ConnectionError as err: + logger.info( + f"Failed to send deployment type and version " f"number to the analytics server: {err}" + ) diff --git a/monkey/monkey_island/cc/services/analytics.py b/monkey/monkey_island/cc/services/analytics.py deleted file mode 100644 index 55fc09d0c..000000000 --- a/monkey/monkey_island/cc/services/analytics.py +++ /dev/null @@ -1,28 +0,0 @@ -import logging - -import requests - -from monkey_island.cc import Version -from monkey_island.cc.deployment import Deployment - -logger = logging.getLogger(__name__) - -ANALYTICS_URL = ( - "https://m15mjynko3.execute-api.us-east-1.amazonaws.com/default?version={" - "version}&deployment={deployment}" -) - - -class Analytics: - def __init__(self, version: Version, deployment: Deployment): - self.version = version.version_number - self.deployment = deployment.value - self._send_analytics() - - def _send_analytics(self): - url = ANALYTICS_URL.format(deployment=self.deployment, version=self.version) - response = requests.get(url).json() - logger.info( - f"Version number and deployment type was sent to analytics server." - f" The response is: {response}" - ) diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 3cfd456c8..fc7b6e432 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -53,7 +53,6 @@ from monkey_island.cc.services.utils.network_utils import get_ip_addresses from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from . import AuthenticationService -from .analytics import Analytics from .reporting.report import ReportService logger = logging.getLogger(__name__) @@ -74,7 +73,6 @@ def initialize_services(data_dir: Path) -> DIContainer: ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME) ) container.register_instance(Version, container.resolve(Version)) - container.register_instance(Analytics, container.resolve(Analytics)) container.register(Publisher, Publisher) container.register_instance(IEventQueue, container.resolve(PyPubSubEventQueue))