diff --git a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py index 7b359abb3..c8142404d 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py @@ -120,8 +120,8 @@ class MonkeyIslandClient(object): assert False def _reset_island_mode(self): - if self.requests.put("api/island/mode", data='{"mode": "unset"}').ok: - LOGGER.info("Resseting island mode after the test.") + if self.requests.put("api/island/mode", data='"unset"').ok: + LOGGER.info("Resetting island mode after the test.") else: LOGGER.error("Failed to reset island mode") assert False diff --git a/monkey/monkey_island/cc/resources/island_mode.py b/monkey/monkey_island/cc/resources/island_mode.py index 5bd695b3d..84d2858c5 100644 --- a/monkey/monkey_island/cc/resources/island_mode.py +++ b/monkey/monkey_island/cc/resources/island_mode.py @@ -2,7 +2,7 @@ import json import logging from http import HTTPStatus -from flask import make_response, request +from flask import request from monkey_island.cc.models import IslandMode as IslandModeEnum from monkey_island.cc.resources.AbstractResource import AbstractResource @@ -21,8 +21,7 @@ class IslandMode(AbstractResource): @jwt_required def put(self): try: - body = json.loads(request.data) - mode = IslandModeEnum(body.get("mode")) + mode = IslandModeEnum(request.json) self._island_mode_service.set_mode(mode) @@ -35,4 +34,4 @@ class IslandMode(AbstractResource): @jwt_required def get(self): island_mode = self._island_mode_service.get_mode() - return make_response({"mode": island_mode.value}, HTTPStatus.OK) + return island_mode.value, HTTPStatus.OK diff --git a/monkey/monkey_island/cc/ui/src/components/IslandHttpClient.tsx b/monkey/monkey_island/cc/ui/src/components/IslandHttpClient.tsx index ef8d8adf5..11e6387b7 100644 --- a/monkey/monkey_island/cc/ui/src/components/IslandHttpClient.tsx +++ b/monkey/monkey_island/cc/ui/src/components/IslandHttpClient.tsx @@ -20,7 +20,7 @@ class IslandHttpClient extends AuthComponent { headers: {'Content-Type': 'application/json'}, body: JSON.stringify(contents) }) - .then(res => {status = res.status; return res.json()}) + .then(res => {status = res.status; return res}) .then(res => new Response(res, status)); } diff --git a/monkey/monkey_island/cc/ui/src/components/Main.tsx b/monkey/monkey_island/cc/ui/src/components/Main.tsx index d65c71a67..fd36b85a1 100644 --- a/monkey/monkey_island/cc/ui/src/components/Main.tsx +++ b/monkey/monkey_island/cc/ui/src/components/Main.tsx @@ -115,7 +115,7 @@ class AppComponent extends AuthComponent { setMode = () => { return IslandHttpClient.get('/api/island/mode') .then(res => { - this.setState({islandMode: res.body.mode}); + this.setState({islandMode: res.body}); }); } diff --git a/monkey/monkey_island/cc/ui/src/components/pages/LandingPage.tsx b/monkey/monkey_island/cc/ui/src/components/pages/LandingPage.tsx index 3c2bf1037..79d511758 100644 --- a/monkey/monkey_island/cc/ui/src/components/pages/LandingPage.tsx +++ b/monkey/monkey_island/cc/ui/src/components/pages/LandingPage.tsx @@ -74,7 +74,7 @@ const LandingPageComponent = (props: Props) => { } function setScenario(scenario: string) { - IslandHttpClient.put('/api/island/mode', {'mode': scenario}) + IslandHttpClient.put('/api/island/mode', scenario) .then(() => { props.onStatusChange(); }); diff --git a/monkey/monkey_island/cc/ui/src/components/ui-components/IslandResetModal.tsx b/monkey/monkey_island/cc/ui/src/components/ui-components/IslandResetModal.tsx index 67f3256b4..f5b47ac04 100644 --- a/monkey/monkey_island/cc/ui/src/components/ui-components/IslandResetModal.tsx +++ b/monkey/monkey_island/cc/ui/src/components/ui-components/IslandResetModal.tsx @@ -111,9 +111,16 @@ const IslandResetModal = (props: Props) => { if (res.ok) { return auth.authFetch('/api/propagation-credentials/configured-credentials', {method: 'PUT', body:'[]'}) }}) - .then(res => { + .then(res => { if (res.ok) { - return auth.authFetch('/api/island/mode', {method: 'PUT', body: '{"mode": "unset"}'}) + return auth.authFetch( + '/api/island/mode', + { + method: 'PUT', + headers: {'Content-Type': 'application/json'}, + body: '"unset"' + } + ) }}) .then(res => { if (!res.ok) { diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py index 642c5c936..322f62502 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py @@ -1,4 +1,3 @@ -import json from http import HTTPStatus from unittest.mock import MagicMock @@ -38,24 +37,22 @@ def flask_client(build_flask_client): ) def test_island_mode_post(flask_client, mode): resp = flask_client.put( - IslandModeResource.urls[0], data=json.dumps({"mode": mode}), follow_redirects=True + IslandModeResource.urls[0], + json=mode, + follow_redirects=True, ) assert resp.status_code == HTTPStatus.NO_CONTENT def test_island_mode_post__invalid_mode(flask_client): resp = flask_client.put( - IslandModeResource.urls[0], data=json.dumps({"mode": "bogus mode"}), follow_redirects=True + IslandModeResource.urls[0], + json="bogus mode", + follow_redirects=True, ) assert resp.status_code == HTTPStatus.UNPROCESSABLE_ENTITY -@pytest.mark.parametrize("invalid_json", ["42", "{test"]) -def test_island_mode_post__invalid_json(flask_client, invalid_json): - resp = flask_client.put(IslandModeResource.urls[0], data="{test", follow_redirects=True) - assert resp.status_code == HTTPStatus.BAD_REQUEST - - def test_island_mode_post__internal_server_error(build_flask_client): mock_island_mode_service = MagicMock(spec=IslandModeService) mock_island_mode_service.set_mode = MagicMock(side_effect=RetrievalError) @@ -66,7 +63,7 @@ def test_island_mode_post__internal_server_error(build_flask_client): with build_flask_client(container) as flask_client: resp = flask_client.put( IslandModeResource.urls[0], - data=json.dumps({"mode": IslandMode.RANSOMWARE.value}), + json=IslandMode.RANSOMWARE.value, follow_redirects=True, ) @@ -76,17 +73,21 @@ def test_island_mode_post__internal_server_error(build_flask_client): @pytest.mark.parametrize("mode", [IslandMode.RANSOMWARE.value, IslandMode.ADVANCED.value]) def test_island_mode_endpoint(flask_client, mode): flask_client.put( - IslandModeResource.urls[0], data=json.dumps({"mode": mode}), follow_redirects=True + IslandModeResource.urls[0], + json=mode, + follow_redirects=True, ) resp = flask_client.get(IslandModeResource.urls[0], follow_redirects=True) assert resp.status_code == HTTPStatus.OK - assert json.loads(resp.data)["mode"] == mode + assert resp.json == mode def test_island_mode_endpoint__invalid_mode(flask_client): resp_post = flask_client.put( - IslandModeResource.urls[0], data=json.dumps({"mode": "bogus_mode"}), follow_redirects=True + IslandModeResource.urls[0], + json="bogus_mode", + follow_redirects=True, ) resp_get = flask_client.get(IslandModeResource.urls[0], follow_redirects=True) assert resp_post.status_code == HTTPStatus.UNPROCESSABLE_ENTITY - assert json.loads(resp_get.data)["mode"] == IslandMode.UNSET.value + assert resp_get.json == IslandMode.UNSET.value