Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3078 logout agent #3210

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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