Skip to content

Commit

Permalink
Merge branch '3078-logout-agent' into develop
Browse files Browse the repository at this point in the history
Issue #3078
PR #3210
  • Loading branch information
mssalvatore committed Apr 7, 2023
2 parents 3f0cc32 + d936b20 commit dbef149
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __init__(self, island_api_client: IIslandAPIClient):
def login(self, otp: str):
return self._island_api_client.login(otp)

def logout(self):
return self._island_api_client.logout()

def get_agent_binary(self, operating_system: OperatingSystem) -> bytes:
return self._island_api_client.get_agent_binary(operating_system)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def _update_token_from_response(self, response: Response):
self._http_client.additional_headers[HTTPIslandAPIClient.TOKEN_HEADER_KEY] = auth_token
self._token_timer.set(token_ttl_sec * TOKEN_TTL_FACTOR)

@handle_response_parsing_errors
def logout(self):
self._http_client.post("/logout")

@handle_response_parsing_errors
def _refresh_token(self):
response = self._http_client.post("/refresh-authentication-token", {})
Expand Down
17 changes: 17 additions & 0 deletions monkey/infection_monkey/island_api_client/i_island_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ def login(self, otp: str):
Island API
"""

@abstractmethod
def logout(self):
"""
Disconnect from the island's API
:raises IslandAPIAuthenticationError: If the client is not authorized to access this
endpoint
:raises IslandAPIConnectionError: If the client cannot successfully connect to the Island
:raises IslandAPIRequestError: If an error occurs while attempting to logout from the
Island due to an issue in the request sent from the client
:raises IslandAPIRequestFailedError: If an error occurs while attempting to logout from the
Island API due to an error on the server
:raises IslandAPITimeoutError: If a timeout occurs while attempting to connect to the Island
:raises IslandAPIError: If an unexpected error occurs while attempting to logout from the
Island API
"""

@abstractmethod
def get_agent_binary(self, operating_system: OperatingSystem) -> bytes:
"""
Expand Down
9 changes: 9 additions & 0 deletions monkey/infection_monkey/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
AbstractIslandAPIClientFactory,
HTTPIslandAPIClientFactory,
IIslandAPIClient,
IslandAPIAuthenticationError,
)
from infection_monkey.master import AutomatedMaster
from infection_monkey.master.control_channel import ControlChannel
Expand Down Expand Up @@ -506,6 +507,8 @@ def cleanup(self):

self._heart.stop()

self._logout()

self._close_tunnel()

except Exception as e:
Expand Down Expand Up @@ -633,3 +636,9 @@ def _get_startup_info():
@staticmethod
def _self_delete_linux():
os.remove(sys.executable)

def _logout(self):
try:
self._island_api_client.logout()
except IslandAPIAuthenticationError:
logger.info("Agent is already logged out")
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class BaseIslandAPIClient(IIslandAPIClient):
def login(self, otp: str):
return

def logout(self):
return

def get_agent_binary(self, operating_system: OperatingSystem) -> bytes:
return b""

Expand Down

0 comments on commit dbef149

Please sign in to comment.