Agent: Implement HTTPIslandAPIClient.send_log and use in ControlClient

This commit is contained in:
Shreya Malviya 2022-09-19 18:14:24 +05:30 committed by Mike Salvatore
parent e674f3ab24
commit 1b92ec78fb
2 changed files with 13 additions and 7 deletions

View File

@ -9,6 +9,7 @@ from urllib3 import disable_warnings
from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT
from common.network.network_utils import get_my_ip_addresses from common.network.network_utils import get_my_ip_addresses
from infection_monkey.config import GUID from infection_monkey.config import GUID
from infection_monkey.island_api_client import HTTPIslandAPIClient
from infection_monkey.network.info import get_host_subnets from infection_monkey.network.info import get_host_subnets
from infection_monkey.utils import agent_process from infection_monkey.utils import agent_process
@ -78,13 +79,7 @@ class ControlClient:
return return
try: try:
telemetry = {"monkey_guid": GUID, "log": json.dumps(log)} telemetry = {"monkey_guid": GUID, "log": json.dumps(log)}
requests.post( # noqa: DUO123 HTTPIslandAPIClient(self.server_address).send_log(json.dumps(telemetry))
"https://%s/api/log" % (self.server_address,),
data=json.dumps(telemetry),
headers={"content-type": "application/json"},
verify=False,
timeout=MEDIUM_REQUEST_TIMEOUT,
)
except Exception as exc: except Exception as exc:
logger.warning(f"Error connecting to control server {self.server_address}: {exc}") logger.warning(f"Error connecting to control server {self.server_address}: {exc}")

View File

@ -21,9 +21,20 @@ class HTTPIslandAPIClient(IIslandAPIClient):
verify=False, verify=False,
timeout=MEDIUM_REQUEST_TIMEOUT, timeout=MEDIUM_REQUEST_TIMEOUT,
) )
self._island_server = island_server
except requests.exceptions.ConnectionError as err: except requests.exceptions.ConnectionError as err:
raise IslandAPIConnectionError(err) raise IslandAPIConnectionError(err)
except TimeoutError as err: except TimeoutError as err:
raise IslandAPITimeoutError(err) raise IslandAPITimeoutError(err)
except Exception as err: except Exception as err:
raise IslandAPIError(err) raise IslandAPIError(err)
# TODO: set server address as object property when init is called in find_server and pass
# object around? won't need to pass island server and create object in every function
def send_log(self, data: str):
requests.post( # noqa: DUO123
"https://%s/api/log" % (self._island_server,),
json=data,
verify=False,
timeout=MEDIUM_REQUEST_TIMEOUT,
)