From 488143b1d3a052289365099c21fe7aede31c7382 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 20 May 2021 08:42:51 +0300 Subject: [PATCH 1/4] BB tests: added the ability for BB tests to "register". If they need registration to run monkeys, BB tests selects passwordless option --- .../island_client/monkey_island_requests.py | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py b/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py index f7be1b3cf..8e8392b9e 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py @@ -12,6 +12,10 @@ NO_AUTH_CREDS = "1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()" LOGGER = logging.getLogger(__name__) +class AuthenticationFailedError(Exception): + pass + + # noinspection PyArgumentList class MonkeyIslandRequests(object): def __init__(self, server_address): @@ -43,6 +47,9 @@ class MonkeyIslandRequests(object): def try_get_jwt_from_server(self): try: return self.get_jwt_from_server() + except AuthenticationFailedError: + self.try_set_island_to_no_password() + return self.get_jwt_from_server() except requests.ConnectionError as err: LOGGER.error( "Unable to connect to island, aborting! Error information: {}. Server: {}".format( @@ -51,6 +58,21 @@ class MonkeyIslandRequests(object): ) assert False + def get_jwt_from_server(self): + resp = requests.post( # noqa: DUO123 + self.addr + "api/auth", + json={"username": NO_AUTH_CREDS, "password": NO_AUTH_CREDS}, + verify=False, + ) + if resp.status_code == 401: + raise AuthenticationFailedError + return resp.json()["access_token"] + + def try_set_island_to_no_password(self): + requests.patch( # noqa: DUO123 + self.addr + "api/environment", json={"server_config": "standard"}, verify=False + ) + class _Decorators: @classmethod def refresh_jwt_token(cls, request_function): @@ -62,14 +84,6 @@ class MonkeyIslandRequests(object): return request_function_wrapper - def get_jwt_from_server(self): - resp = requests.post( # noqa: DUO123 - self.addr + "api/auth", - json={"username": NO_AUTH_CREDS, "password": NO_AUTH_CREDS}, - verify=False, - ) - return resp.json()["access_token"] - @_Decorators.refresh_jwt_token def get(self, url, data=None): return requests.get( # noqa: DUO123 From af049b468b4c4156f69dc8d84952a47ba89ffce3 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 20 May 2021 08:44:12 +0300 Subject: [PATCH 2/4] BB tests: removed island connectivity test. Now the connection is tested in fixture and if anything goes wrong tests are not launched --- envs/monkey_zoo/blackbox/test_blackbox.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index 20f495151..2fe39a399 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -98,7 +98,15 @@ def wait_machine_bootup(): @pytest.fixture(scope="class") def island_client(island, quick_performance_tests): - island_client_object = MonkeyIslandClient(island) + client_established = False + try: + island_client_object = MonkeyIslandClient(island) + client_established = island_client_object.get_api_status() + except Exception: + logging.exception("message") + finally: + if not client_established: + pytest.exit("BB tests couldn't establish communication to the island.") if not quick_performance_tests: island_client_object.reset_env() yield island_client_object @@ -158,10 +166,6 @@ class TestMonkeyBlackbox: def get_log_dir_path(): return os.path.abspath(LOG_DIR_PATH) - def test_server_online(self, island_client): - if not island_client.get_api_status(): - pytest.exit("BB tests couldn't reach the Island server, quiting.") - def test_ssh_exploiter(self, island_client): TestMonkeyBlackbox.run_exploitation_test(island_client, Ssh, "SSH_exploiter_and_keys") From cc365a74c54d484c5af450d3907d68653aac86b0 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 20 May 2021 16:45:26 +0300 Subject: [PATCH 3/4] Added a CHANGELOG.md entry about BB tests being able to self-register --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3021263d4..aa2a96cb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Authentication mechanism to use bcrypt on server side. #1139 - `server_config.json` puts environment config options in a separate section named "environment". #1161 +- BlackBox tests can now register if they are ran on a fresh installation. #1180 - Improved the structure of unit tests by scoping fixtures only to relevant modules instead of having a one huge fixture file, improved and renamed the directory structure of unit tests and unit test infrastructure. #1178 From 49e63fcf1b77f7efd56fce4c0710018569f71f97 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 21 May 2021 08:41:28 +0300 Subject: [PATCH 4/4] Improve exception message, thrown when trying to establish connection to island in BB tests --- envs/monkey_zoo/blackbox/test_blackbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index 2fe39a399..5ee5f63c7 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -103,7 +103,7 @@ def island_client(island, quick_performance_tests): island_client_object = MonkeyIslandClient(island) client_established = island_client_object.get_api_status() except Exception: - logging.exception("message") + logging.exception("Got an exception while trying to establish connection to the Island.") finally: if not client_established: pytest.exit("BB tests couldn't establish communication to the island.")