diff --git a/CHANGELOG.md b/CHANGELOG.md index c8a1738f2..442c6c86d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/). - The ability to download the Monkey Island logs from the Infection Map page. #1640 - `/api/reset-agent-configuration` endpoint. #2036 - `/api/clear-simulation-data` endpoint. #2036 +- `/api/registration-status` endpoint. #2149 ### Changed - Reset workflow. Now it's possible to delete data gathered by agents without diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index d583ebc1a..2afbb685e 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -23,6 +23,7 @@ from monkey_island.cc.resources.agent_controls import StopAgentCheck, StopAllAge from monkey_island.cc.resources.attack.attack_report import AttackReport from monkey_island.cc.resources.auth.authenticate import Authenticate, init_jwt from monkey_island.cc.resources.auth.register import Registration +from monkey_island.cc.resources.auth.registration_status import RegistrationStatus from monkey_island.cc.resources.blackbox.log_blackbox_endpoint import LogBlackboxEndpoint from monkey_island.cc.resources.blackbox.monkey_blackbox_endpoint import MonkeyBlackboxEndpoint from monkey_island.cc.resources.blackbox.telemetry_blackbox_endpoint import ( @@ -153,6 +154,7 @@ def init_api_resources(api: FlaskDIWrapper): def init_restful_endpoints(api: FlaskDIWrapper): api.add_resource(Root) api.add_resource(Registration) + api.add_resource(RegistrationStatus) api.add_resource(Authenticate) api.add_resource(Monkey) api.add_resource(LocalRun) diff --git a/monkey/monkey_island/cc/resources/auth/register.py b/monkey/monkey_island/cc/resources/auth/register.py index 5101a1836..8f4814858 100644 --- a/monkey/monkey_island/cc/resources/auth/register.py +++ b/monkey/monkey_island/cc/resources/auth/register.py @@ -18,9 +18,6 @@ class Registration(AbstractResource): def __init__(self, authentication_service: AuthenticationService): self._authentication_service = authentication_service - def get(self): - return {"needs_registration": self._authentication_service.needs_registration()} - def post(self): username, password = get_username_password_from_request(request) diff --git a/monkey/monkey_island/cc/resources/auth/registration_status.py b/monkey/monkey_island/cc/resources/auth/registration_status.py new file mode 100644 index 000000000..040c202f2 --- /dev/null +++ b/monkey/monkey_island/cc/resources/auth/registration_status.py @@ -0,0 +1,13 @@ +from monkey_island.cc.resources.AbstractResource import AbstractResource +from monkey_island.cc.services import AuthenticationService + + +class RegistrationStatus(AbstractResource): + + urls = ["/api/registration-status"] + + def __init__(self, authentication_service: AuthenticationService): + self._authentication_service = authentication_service + + def get(self): + return {"needs_registration": self._authentication_service.needs_registration()} diff --git a/monkey/monkey_island/cc/ui/src/services/AuthService.js b/monkey/monkey_island/cc/ui/src/services/AuthService.js index f8d7b0754..202d204a6 100644 --- a/monkey/monkey_island/cc/ui/src/services/AuthService.js +++ b/monkey/monkey_island/cc/ui/src/services/AuthService.js @@ -4,6 +4,7 @@ export default class AuthService { SECONDS_BEFORE_JWT_EXPIRES = 20; AUTHENTICATION_API_ENDPOINT = '/api/authenticate'; REGISTRATION_API_ENDPOINT = '/api/register'; + REGISTRATION_STATUS_API_ENDPOINT = '/api/registration-status'; login = (username, password) => { return this._login(username, password); @@ -91,7 +92,7 @@ export default class AuthService { }; needsRegistration = () => { - return fetch(this.REGISTRATION_API_ENDPOINT, + return fetch(this.REGISTRATION_STATUS_API_ENDPOINT, {method: 'GET'}) .then(response => response.json()) .then(res => { diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_register.py b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_register.py index 67d7478d9..49419ee4f 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_register.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_register.py @@ -62,12 +62,3 @@ def test_internal_error(make_registration_request, mock_authentication_service): response = make_registration_request(registration_request_body) assert response.status_code == 500 - - -@pytest.mark.parametrize("needs_registration", [True, False]) -def test_needs_registration(flask_client, mock_authentication_service, needs_registration): - mock_authentication_service.needs_registration = MagicMock(return_value=needs_registration) - response = flask_client.get(REGISTRATION_URL, follow_redirects=True) - - assert response.status_code == 200 - assert response.json["needs_registration"] is needs_registration diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration_status.py b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration_status.py new file mode 100644 index 000000000..350590df8 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/auth/test_registration_status.py @@ -0,0 +1,16 @@ +from unittest.mock import MagicMock + +import pytest + +from monkey_island.cc.resources.auth.registration_status import RegistrationStatus + +REGISTRATION_STATUS_URL = RegistrationStatus.urls[0] + + +@pytest.mark.parametrize("needs_registration", [True, False]) +def test_needs_registration(flask_client, mock_authentication_service, needs_registration): + mock_authentication_service.needs_registration = MagicMock(return_value=needs_registration) + response = flask_client.get(REGISTRATION_STATUS_URL, follow_redirects=True) + + assert response.status_code == 200 + assert response.json["needs_registration"] is needs_registration