From 1b562e723f3e5884e71f1e2e746acdf5af2170ef Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 1 Aug 2022 12:55:25 +0200 Subject: [PATCH] Island: Change `POST` to `PUT` in `api/island/mode` --- .../blackbox/island_client/monkey_island_client.py | 2 +- .../blackbox/island_client/monkey_island_requests.py | 6 ++++++ monkey/monkey_island/cc/resources/island_mode.py | 12 ++++++------ .../cc/ui/src/components/IslandHttpClient.tsx | 4 ++-- .../cc/ui/src/components/pages/LandingPage.tsx | 2 +- .../components/ui-components/IslandResetModal.tsx | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) 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 e287c8998..7b359abb3 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py @@ -120,7 +120,7 @@ class MonkeyIslandClient(object): assert False def _reset_island_mode(self): - if self.requests.post("api/island/mode", data='{"mode": "unset"}').ok: + if self.requests.put("api/island/mode", data='{"mode": "unset"}').ok: LOGGER.info("Resseting island mode after the test.") else: LOGGER.error("Failed to reset island mode") 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 ff3b32e92..f26dc9e8a 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py @@ -82,6 +82,12 @@ class MonkeyIslandRequests(object): self.addr + url, data=data, headers=self.get_jwt_header(), verify=False ) + @_Decorators.refresh_jwt_token + def put(self, url, data): + return requests.put( # noqa: DUO123 + self.addr + url, data=data, headers=self.get_jwt_header(), verify=False + ) + @_Decorators.refresh_jwt_token def post_json(self, url, json: Dict): return requests.post( # noqa: DUO123 diff --git a/monkey/monkey_island/cc/resources/island_mode.py b/monkey/monkey_island/cc/resources/island_mode.py index 047320306..2eb1ba627 100644 --- a/monkey/monkey_island/cc/resources/island_mode.py +++ b/monkey/monkey_island/cc/resources/island_mode.py @@ -1,5 +1,6 @@ import json import logging +from http import HTTPStatus from flask import make_response, request @@ -17,9 +18,8 @@ class IslandMode(AbstractResource): def __init__(self, island_mode_service: IslandModeService): self._island_mode_service = island_mode_service - # API Spec: Instead of POST, this should be PUT @jwt_required - def post(self): + def put(self): try: body = json.loads(request.data) mode = IslandModeEnum(body.get("mode")) @@ -29,13 +29,13 @@ class IslandMode(AbstractResource): # TODO: Do any of these returns need a body and make_response? What happens if we just # return the response code? # API Spec: This should be 204 (NO CONTENT) - return make_response({}, 200) + return make_response({}, HTTPStatus.NO_CONTENT) except (AttributeError, json.decoder.JSONDecodeError): - return make_response({}, 400) + return make_response({}, HTTPStatus.BAD_REQUEST) except ValueError: - return make_response({}, 422) + return make_response({}, HTTPStatus.UNPROCESSABLE_ENTITY) @jwt_required def get(self): island_mode = self._island_mode_service.get_mode() - return make_response({"mode": island_mode.value}, 200) + return make_response({"mode": 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 1c9571011..ef8d8adf5 100644 --- a/monkey/monkey_island/cc/ui/src/components/IslandHttpClient.tsx +++ b/monkey/monkey_island/cc/ui/src/components/IslandHttpClient.tsx @@ -12,11 +12,11 @@ export class Response{ } class IslandHttpClient extends AuthComponent { - post(endpoint: string, contents: any): Promise{ + put(endpoint: string, contents: any): Promise{ let status = null; return this.authFetch(endpoint, { - method: 'POST', + method: 'PUT', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(contents) }) 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 8f89189ec..3c2bf1037 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.post('/api/island/mode', {'mode': scenario}) + IslandHttpClient.put('/api/island/mode', {'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 9717bb079..e3f798799 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 @@ -113,7 +113,7 @@ const IslandResetModal = (props: Props) => { }}) .then(res => { if (res.status === 200) { - return auth.authFetch('/api/island/mode', {method: 'POST', body: '{"mode": "unset"}'}) + return auth.authFetch('/api/island/mode', {method: 'PUT', body: '{"mode": "unset"}'}) }}) .then(res => { if (res.status !== 200) {