Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API endpoint for registration status #2149

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions monkey/monkey_island/cc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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 (
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions monkey/monkey_island/cc/resources/auth/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions monkey/monkey_island/cc/resources/auth/registration_status.py
Original file line number Diff line number Diff line change
@@ -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()}
3 changes: 2 additions & 1 deletion monkey/monkey_island/cc/ui/src/services/AuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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