From 677e2755b9f5a58ff53c4db670542429ad800176 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 2 Aug 2022 14:36:39 +0530 Subject: [PATCH 1/6] Island: Add docstrings to AuthenticationService --- .../cc/services/authentication_service.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/monkey/monkey_island/cc/services/authentication_service.py b/monkey/monkey_island/cc/services/authentication_service.py index 7b32e4f1c..225f57e50 100644 --- a/monkey/monkey_island/cc/services/authentication_service.py +++ b/monkey/monkey_island/cc/services/authentication_service.py @@ -18,6 +18,10 @@ from monkey_island.cc.setup.mongo.database_initializer import reset_database class AuthenticationService: + """ + A service for user authentication + """ + def __init__( self, data_dir: Path, @@ -29,9 +33,21 @@ class AuthenticationService: self._repository_encryptor = repository_encryptor def needs_registration(self) -> bool: + """ + Checks if a user is already registered on the Island + + :return: Whether registration is required on the Island + """ return not self._user_repository.has_registered_users() def register_new_user(self, username: str, password: str): + """ + Registers a new user on the Island, then resets the encryptor and database + + :param username: Username to register + :param password: Password to register + :raises InvalidRegistrationCredentialsError: If username or password is empty + """ if not username or not password: raise InvalidRegistrationCredentialsError("Username or password can not be empty.") From 9f02fd4e4401ced748a166f0f03e2dc18b1fdd52 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 2 Aug 2022 14:42:58 +0530 Subject: [PATCH 2/6] Island: Add docstrings to Authenticate resource --- .../cc/resources/auth/authenticate.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/monkey/monkey_island/cc/resources/auth/authenticate.py b/monkey/monkey_island/cc/resources/auth/authenticate.py index a4b4468ec..f7ffe251b 100644 --- a/monkey/monkey_island/cc/resources/auth/authenticate.py +++ b/monkey/monkey_island/cc/resources/auth/authenticate.py @@ -22,10 +22,7 @@ def init_jwt(app): class Authenticate(AbstractResource): """ - Resource for user authentication. The user provides the username and password and we \ - give them a JWT. \ - See `AuthService.js` file for the frontend counterpart for this code. \ - + A resource for user authentication """ urls = ["/api/authenticate"] @@ -35,13 +32,13 @@ class Authenticate(AbstractResource): def post(self): """ - Example request: \ - { \ - "username": "my_user", \ - "password": "my_password" \ - } \ + Gets a username and password from the request sent from the client, authenticates, and + returns an access token + :return: Access token in the response body + :raises IncorrectCredentialsError: If credentials are invalid """ + username, password = get_username_password_from_request(request) try: From ac34eb56e9b9d6ffad791f12ad1a07f488fc67fa Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 2 Aug 2022 14:45:31 +0530 Subject: [PATCH 3/6] Island: Add docstrings to Registration resource --- monkey/monkey_island/cc/resources/auth/register.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/monkey/monkey_island/cc/resources/auth/register.py b/monkey/monkey_island/cc/resources/auth/register.py index 8f4814858..d3938ec7b 100644 --- a/monkey/monkey_island/cc/resources/auth/register.py +++ b/monkey/monkey_island/cc/resources/auth/register.py @@ -12,6 +12,9 @@ logger = logging.getLogger(__name__) class Registration(AbstractResource): + """ + A resource for user registration + """ urls = ["/api/register"] @@ -19,6 +22,14 @@ class Registration(AbstractResource): self._authentication_service = authentication_service def post(self): + """ + Gets a username and password from the request sent from the client, + and registers a new user + + :raises InvalidRegistrationCredentialsError: If username or password is empty + :raises AlreadyRegisteredError: If a user has already been registered + """ + username, password = get_username_password_from_request(request) try: From 5826ef17678ab965923ec767fee7ed12603f5756 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 2 Aug 2022 18:24:00 +0530 Subject: [PATCH 4/6] Island: Fixes docstrings in Authenticate and Registration resources according to PEP 257 --- monkey/monkey_island/cc/resources/auth/authenticate.py | 2 ++ monkey/monkey_island/cc/resources/auth/register.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/monkey/monkey_island/cc/resources/auth/authenticate.py b/monkey/monkey_island/cc/resources/auth/authenticate.py index f7ffe251b..9ea06f307 100644 --- a/monkey/monkey_island/cc/resources/auth/authenticate.py +++ b/monkey/monkey_island/cc/resources/auth/authenticate.py @@ -32,6 +32,8 @@ class Authenticate(AbstractResource): def post(self): """ + Authenticates a user + Gets a username and password from the request sent from the client, authenticates, and returns an access token diff --git a/monkey/monkey_island/cc/resources/auth/register.py b/monkey/monkey_island/cc/resources/auth/register.py index d3938ec7b..56178ea20 100644 --- a/monkey/monkey_island/cc/resources/auth/register.py +++ b/monkey/monkey_island/cc/resources/auth/register.py @@ -23,6 +23,8 @@ class Registration(AbstractResource): def post(self): """ + Registers a new user + Gets a username and password from the request sent from the client, and registers a new user From a5f71a3ff5c8d3c9104db9ccfe5c8089dab6a2e3 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 2 Aug 2022 18:25:12 +0530 Subject: [PATCH 5/6] Island: Rename Registration -> Register --- monkey/monkey_island/cc/app.py | 4 ++-- monkey/monkey_island/cc/resources/auth/register.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index 2afbb685e..778f0fc1f 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -22,7 +22,7 @@ from monkey_island.cc.resources.agent_configuration import AgentConfiguration from monkey_island.cc.resources.agent_controls import StopAgentCheck, StopAllAgents 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.register import Register 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 @@ -153,7 +153,7 @@ def init_api_resources(api: FlaskDIWrapper): def init_restful_endpoints(api: FlaskDIWrapper): api.add_resource(Root) - api.add_resource(Registration) + api.add_resource(Register) api.add_resource(RegistrationStatus) api.add_resource(Authenticate) api.add_resource(Monkey) diff --git a/monkey/monkey_island/cc/resources/auth/register.py b/monkey/monkey_island/cc/resources/auth/register.py index 56178ea20..07bb67796 100644 --- a/monkey/monkey_island/cc/resources/auth/register.py +++ b/monkey/monkey_island/cc/resources/auth/register.py @@ -11,7 +11,7 @@ from monkey_island.cc.services import AuthenticationService logger = logging.getLogger(__name__) -class Registration(AbstractResource): +class Register(AbstractResource): """ A resource for user registration """ From c7f9a5c90a967308831a3531c6daee1d6c5e8cf3 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 2 Aug 2022 18:25:39 +0530 Subject: [PATCH 6/6] UT: Use renamed resource (Registration -> Register) --- .../monkey_island/cc/resources/auth/test_register.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 49419ee4f..aee054f84 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 @@ -4,9 +4,9 @@ from unittest.mock import MagicMock import pytest from common.utils.exceptions import AlreadyRegisteredError, InvalidRegistrationCredentialsError -from monkey_island.cc.resources.auth.register import Registration +from monkey_island.cc.resources.auth.register import Register -REGISTRATION_URL = Registration.urls[0] +REGISTRATION_URL = Register.urls[0] USERNAME = "test_user" PASSWORD = "test_password"