From 0be43157cfbb4a2bd941665746109147b2c11ed1 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 6 Jul 2022 10:46:05 -0400 Subject: [PATCH] Common: Add PasswordSchema --- monkey/common/credentials/password.py | 17 +++++++ .../common/credentials/test_password.py | 49 +++++++++++++++++++ vulture_allowlist.py | 3 ++ 3 files changed, 69 insertions(+) create mode 100644 monkey/tests/unit_tests/common/credentials/test_password.py diff --git a/monkey/common/credentials/password.py b/monkey/common/credentials/password.py index ff6da0eae..1fee478aa 100644 --- a/monkey/common/credentials/password.py +++ b/monkey/common/credentials/password.py @@ -1,8 +1,25 @@ from dataclasses import dataclass, field +from marshmallow import Schema, fields, post_load, validate +from marshmallow_enum import EnumField + +from common.utils.code_utils import del_key + from . import CredentialComponentType, ICredentialComponent +class PasswordSchema(Schema): + credential_type = EnumField( + CredentialComponentType, validate=validate.Equal(CredentialComponentType.PASSWORD) + ) + password = fields.Str() + + @post_load + def _strip_credential_type(self, data, **kwargs): + del_key(data, "credential_type") + return data + + @dataclass(frozen=True) class Password(ICredentialComponent): credential_type: CredentialComponentType = field( diff --git a/monkey/tests/unit_tests/common/credentials/test_password.py b/monkey/tests/unit_tests/common/credentials/test_password.py new file mode 100644 index 000000000..deb9ca594 --- /dev/null +++ b/monkey/tests/unit_tests/common/credentials/test_password.py @@ -0,0 +1,49 @@ +from copy import deepcopy + +import pytest +from marshmallow.exceptions import ValidationError + +from common.credentials import CredentialComponentType, Password +from common.credentials.password import PasswordSchema + +PASSWORD_VALUE = "123456" +PASSWORD_DICT = { + "credential_type": CredentialComponentType.PASSWORD.name, + "password": PASSWORD_VALUE, +} + + +def test_password_serialize(): + schema = PasswordSchema() + password = Password(PASSWORD_VALUE) + + serialized_password = schema.dump(password) + + assert serialized_password == PASSWORD_DICT + + +def test_password_deserialize(): + schema = PasswordSchema() + + password = Password(**schema.load(PASSWORD_DICT)) + + assert password.credential_type == CredentialComponentType.PASSWORD + assert password.password == PASSWORD_VALUE + + +def test_invalid_credential_type(): + invalid_password_dict = deepcopy(PASSWORD_DICT) + invalid_password_dict["credential_type"] = "INVALID" + schema = PasswordSchema() + + with pytest.raises(ValidationError): + Password(**schema.load(invalid_password_dict)) + + +def test_incorrect_credential_type(): + invalid_password_dict = deepcopy(PASSWORD_DICT) + invalid_password_dict["credential_type"] = CredentialComponentType.USERNAME.name + schema = PasswordSchema() + + with pytest.raises(ValidationError): + Password(**schema.load(invalid_password_dict)) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index a3393325e..b04ec8cca 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -196,6 +196,9 @@ _make_tcp_scan_configuration # unused method (monkey/common/configuration/agent _make_network_scan_configuration # unused method (monkey/common/configuration/agent_configuration.py:110) _make_propagation_configuration # unused method (monkey/common/configuration/agent_configuration.py:167) +# Credentials +_strip_credential_type # unused method(monkey/common/credentials/password.py:18) + # Models _make_simulation # unused method (monkey/monkey_island/cc/models/simulation.py:19