From 77143ee76588ed6decd26c471b0050666a0f21fc Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 7 Jul 2022 15:33:45 -0400 Subject: [PATCH] Island: Use common.credentials.Credentials in resource --- .../cc/resources/propagation_credentials.py | 5 ++- ...stub_propagation_credentials_repository.py | 40 ++++++------------- .../resources/test_propagation_credentials.py | 9 +---- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/monkey/monkey_island/cc/resources/propagation_credentials.py b/monkey/monkey_island/cc/resources/propagation_credentials.py index 8b14e9b1f..8d72fe427 100644 --- a/monkey/monkey_island/cc/resources/propagation_credentials.py +++ b/monkey/monkey_island/cc/resources/propagation_credentials.py @@ -1,5 +1,6 @@ -from flask import jsonify +from flask import make_response +from common.credentials import Credentials from monkey_island.cc.repository import ICredentialsRepository from monkey_island.cc.resources.AbstractResource import AbstractResource @@ -13,4 +14,4 @@ class PropagationCredentials(AbstractResource): def get(self): propagation_credentials = self._credentials_repository.get_all_credentials() - return jsonify(propagation_credentials) + return make_response(Credentials.to_json_array(propagation_credentials), 200) diff --git a/monkey/tests/monkey_island/stub_propagation_credentials_repository.py b/monkey/tests/monkey_island/stub_propagation_credentials_repository.py index 7a2375c6c..237d28b85 100644 --- a/monkey/tests/monkey_island/stub_propagation_credentials_repository.py +++ b/monkey/tests/monkey_island/stub_propagation_credentials_repository.py @@ -1,38 +1,25 @@ from typing import Sequence +from common.credentials import Credentials, LMHash, NTHash, Password, Username from monkey_island.cc.repository import ICredentialsRepository -from monkey_island.cc.services.telemetry.processing.credentials import Credentials fake_username = "m0nk3y_user" fake_special_username = "m0nk3y.user" -fake_nt_hash = "c1c58f96cdf212b50837bc11a00be47c" -fake_lm_hash = "299BD128C1101FD6" +fake_nt_hash = "C1C58F96CDF212B50837BC11A00BE47C" +fake_lm_hash = "299BD128C1101FD6299BD128C1101FD6" fake_password_1 = "trytostealthis" fake_password_2 = "password" fake_password_3 = "12345678" -PROPAGATION_CREDENTIALS_1 = { - "identities": [{"username": fake_username, "credential_type": "USERNAME"}], - "secrets": [ - {"nt_hash": fake_nt_hash, "credential_type": "NT_HASH"}, - {"lm_hash": fake_lm_hash, "credential_type": "LM_HASH"}, - {"password": fake_password_1, "credential_type": "PASSWORD"}, - ], -} - -PROPAGATION_CREDENTIALS_2 = { - "identities": [ - {"username": fake_username, "credential_type": "USERNAME"}, - {"username": fake_special_username, "credential_type": "USERNAME"}, - ], - "secrets": [ - {"password": fake_password_1, "credential_type": "PASSWORD"}, - {"password": fake_password_2, "credential_type": "PASSWORD"}, - {"password": fake_password_3, "credential_type": "PASSWORD"}, - ], -} +PROPAGATION_CREDENTIALS_1 = Credentials( + identities=(Username(fake_username),), + secrets=(NTHash(fake_nt_hash), LMHash(fake_lm_hash), Password(fake_password_1)), +) +PROPAGATION_CREDENTIALS_2 = Credentials( + identities=(Username(fake_username), Username(fake_special_username)), + secrets=(Password(fake_password_1), Password(fake_password_2), Password(fake_password_3)), +) -# TODO: Use Credentials from common.credentials when serialization is implemented class StubPropagationCredentialsRepository(ICredentialsRepository): def get_configured_credentials(self) -> Sequence[Credentials]: pass @@ -42,10 +29,7 @@ class StubPropagationCredentialsRepository(ICredentialsRepository): def get_all_credentials(self) -> Sequence[Credentials]: - return [ - Credentials.from_mapping(PROPAGATION_CREDENTIALS_1, monkey_guid="some_guid"), - Credentials.from_mapping(PROPAGATION_CREDENTIALS_2, monkey_guid="second_guid"), - ] + return [PROPAGATION_CREDENTIALS_1, PROPAGATION_CREDENTIALS_2] def save_configured_credentials(self, credentials: Sequence[Credentials]): pass diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py index 08b8e942f..728aef1c3 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py @@ -1,5 +1,3 @@ -import json - import pytest from tests.common import StubDIContainer from tests.monkey_island import ( @@ -9,6 +7,7 @@ from tests.monkey_island import ( ) from tests.unit_tests.monkey_island.conftest import get_url_for_resource +from common.credentials import Credentials from monkey_island.cc.repository import ICredentialsRepository from monkey_island.cc.resources.propagation_credentials import PropagationCredentials @@ -27,13 +26,9 @@ def test_propagation_credentials_endpoint_get(flask_client): propagation_credentials_url = get_url_for_resource(PropagationCredentials) resp = flask_client.get(propagation_credentials_url) + actual_propagation_credentials = Credentials.from_json_array(resp.text) assert resp.status_code == 200 - actual_propagation_credentials = json.loads(resp.data) assert len(actual_propagation_credentials) == 2 - - # TODO: delete the removal of monkey_guid key when the serialization of credentials - del actual_propagation_credentials[0]["monkey_guid"] assert actual_propagation_credentials[0] == PROPAGATION_CREDENTIALS_1 - del actual_propagation_credentials[1]["monkey_guid"] assert actual_propagation_credentials[1] == PROPAGATION_CREDENTIALS_2