Merge pull request #2149 from guardicore/2105-new-api-endpoint-registration-status

New API endpoint for registration status
This commit is contained in:
Shreya Malviya 2022-08-02 14:46:07 +05:30 committed by GitHub
commit dc4749da63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 13 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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()}

View File

@ -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 => {

View File

@ -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

View File

@ -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