diff --git a/monkey/monkey_island/cc/services/reporting/format_credentials.py b/monkey/monkey_island/cc/services/reporting/format_credentials.py index 9d01bde25..721868cdc 100644 --- a/monkey/monkey_island/cc/services/reporting/format_credentials.py +++ b/monkey/monkey_island/cc/services/reporting/format_credentials.py @@ -17,19 +17,22 @@ def format_creds_for_reporting(credentials: Sequence[Credentials]) -> Sequence[M CredentialComponentType.SSH_KEYPAIR: "Clear SSH private key", } for cred in credentials: - for secret_type in cred.secrets: - if secret_type.credential_type not in cred_type_dict: - continue - username = _get_username(cred) - cred_row = { - "username": username, - "_type": secret_type.credential_type.name, - "type": cred_type_dict[secret_type.credential_type], - } - if cred_row not in formatted_creds: - formatted_creds.append(cred_row) + secret = cred.secret + if secret is None: + continue + + if secret.credential_type not in cred_type_dict: + continue + username = _get_username(cred) + cred_row = { + "username": username, + "_type": secret.credential_type.name, + "type": cred_type_dict[secret.credential_type], + } + if cred_row not in formatted_creds: + formatted_creds.append(cred_row) return formatted_creds def _get_username(credentials: Credentials) -> str: - return credentials.identities[0].username if credentials.identities else "" + return credentials.identity.username if credentials.identity else "" diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_format_credentials.py b/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_format_credentials.py index 089dc5d5d..bb51b89dd 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_format_credentials.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_format_credentials.py @@ -23,7 +23,14 @@ fake_ssh_key = SSHKeypair(fake_ssh_private_key, fake_ssh_public_key) identities = (fake_username,) secrets = (fake_nt_hash, fake_lm_hash, fake_password, fake_ssh_key) -fake_credentials = [Credentials(identities, secrets)] +fake_credentials = [ + Credentials(fake_username, fake_nt_hash), + Credentials(fake_username, fake_lm_hash), + Credentials(fake_username, fake_password), + Credentials(fake_username, fake_ssh_key), + Credentials(None, fake_ssh_key), + Credentials(fake_username, None), +] def test_formatting_credentials_for_report(): @@ -50,7 +57,13 @@ def test_formatting_credentials_for_report(): "type": "Clear SSH private key", "username": fake_username.username, } + result5 = { + "_type": CredentialComponentType.SSH_KEYPAIR.name, + "type": "Clear SSH private key", + "username": "", + } assert result1 in credentials assert result2 in credentials assert result3 in credentials assert result4 in credentials + assert result5 in credentials