Common: Add PasswordSchema

This commit is contained in:
Mike Salvatore 2022-07-06 10:46:05 -04:00
parent 3f3494e5d4
commit 0be43157cf
3 changed files with 69 additions and 0 deletions

View File

@ -1,8 +1,25 @@
from dataclasses import dataclass, field 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 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) @dataclass(frozen=True)
class Password(ICredentialComponent): class Password(ICredentialComponent):
credential_type: CredentialComponentType = field( credential_type: CredentialComponentType = field(

View File

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

View File

@ -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_network_scan_configuration # unused method (monkey/common/configuration/agent_configuration.py:110)
_make_propagation_configuration # unused method (monkey/common/configuration/agent_configuration.py:167) _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 # Models
_make_simulation # unused method (monkey/monkey_island/cc/models/simulation.py:19 _make_simulation # unused method (monkey/monkey_island/cc/models/simulation.py:19