diff --git a/monkey/common/credentials/credentials.py b/monkey/common/credentials/credentials.py index 732818783..e04be27a5 100644 --- a/monkey/common/credentials/credentials.py +++ b/monkey/common/credentials/credentials.py @@ -77,7 +77,10 @@ class CredentialsSchema(Schema): credential_component_type ] - return credential_component_class(**credential_component_schema.load(data)) + try: + return credential_component_class(**credential_component_schema.load(data)) + except MarshmallowError as err: + raise InvalidCredentialComponentError(credential_component_class, str(err)) @pre_dump def _serialize_credentials( diff --git a/monkey/tests/unit_tests/common/credentials/test_credentials.py b/monkey/tests/unit_tests/common/credentials/test_credentials.py index d6fd55ef5..c68e1c813 100644 --- a/monkey/tests/unit_tests/common/credentials/test_credentials.py +++ b/monkey/tests/unit_tests/common/credentials/test_credentials.py @@ -4,6 +4,7 @@ import pytest from common.credentials import ( Credentials, + InvalidCredentialComponentError, InvalidCredentialsError, LMHash, NTHash, @@ -77,3 +78,12 @@ def test_credentials_deserialization__invalid_component_type(): invalid_data = {"secrets": [], "identities": [{"credential_type": "FAKE", "username": "user1"}]} with pytest.raises(InvalidCredentialsError): Credentials.from_mapping(invalid_data) + + +def test_credentials_deserialization__invalid_component(): + invalid_data = { + "secrets": [], + "identities": [{"credential_type": "USERNAME", "unknown_field": "user1"}], + } + with pytest.raises(InvalidCredentialComponentError): + Credentials.from_mapping(invalid_data)