UT: export credential testing infrastructure to conftest

This commit is contained in:
vakarisz 2022-02-23 11:31:45 +02:00
parent 8dd033c212
commit 1fe1293405
2 changed files with 34 additions and 27 deletions

View File

@ -0,0 +1,23 @@
from datetime import datetime
import mongoengine
import pytest
from monkey_island.cc.services.config import ConfigService
@pytest.fixture
def fake_mongo(monkeypatch):
mongo = mongoengine.connection.get_connection()
monkeypatch.setattr("monkey_island.cc.services.config.mongo", mongo)
config = ConfigService.get_default_config()
ConfigService.update_config(config, should_encrypt=True)
CREDENTIAL_TELEM_TEMPLATE = {
"monkey_guid": "272405690278083",
"telem_category": "credentials",
"timestamp": datetime(2022, 2, 18, 11, 51, 15, 338953),
"command_control_channel": {"src": "10.2.2.251", "dst": "10.2.2.251:5000"},
"data": None,
}

View File

@ -1,9 +1,10 @@
from copy import deepcopy from copy import deepcopy
from datetime import datetime
import dpath.util import dpath.util
import mongoengine
import pytest import pytest
from tests.unit_tests.monkey_island.cc.services.telemetry.processing.credentials.conftest import (
CREDENTIAL_TELEM_TEMPLATE,
)
from common.config_value_paths import ( from common.config_value_paths import (
LM_HASH_LIST_PATH, LM_HASH_LIST_PATH,
@ -16,14 +17,6 @@ from monkey_island.cc.services.telemetry.processing.credentials.credentials_pars
parse_credentials, parse_credentials,
) )
CREDENTIAL_TELEM_TEMPLATE = {
"monkey_guid": "272405690278083",
"telem_category": "credentials",
"timestamp": datetime(2022, 2, 18, 11, 51, 15, 338953),
"command_control_channel": {"src": "10.2.2.251", "dst": "10.2.2.251:5000"},
"data": None,
}
fake_username = "m0nk3y_user" fake_username = "m0nk3y_user"
cred_telem_usernames = deepcopy(CREDENTIAL_TELEM_TEMPLATE) cred_telem_usernames = deepcopy(CREDENTIAL_TELEM_TEMPLATE)
cred_telem_usernames["data"] = [ cred_telem_usernames["data"] = [
@ -58,31 +51,22 @@ cred_empty_telem = deepcopy(CREDENTIAL_TELEM_TEMPLATE)
cred_empty_telem["data"] = [{"identities": [], "secrets": []}] cred_empty_telem["data"] = [{"identities": [], "secrets": []}]
@pytest.fixture @pytest.mark.usefixtures("uses_database", "fake_mongo")
def fake_mongo(monkeypatch): def test_cred_username_parsing():
mongo = mongoengine.connection.get_connection()
monkeypatch.setattr("monkey_island.cc.services.config.mongo", mongo)
config = ConfigService.get_default_config()
ConfigService.update_config(config, should_encrypt=True)
return mongo
@pytest.mark.usefixtures("uses_database")
def test_cred_username_parsing(fake_mongo):
parse_credentials(cred_telem_usernames) parse_credentials(cred_telem_usernames)
config = ConfigService.get_config(should_decrypt=True) config = ConfigService.get_config(should_decrypt=True)
assert fake_username in dpath.util.get(config, USER_LIST_PATH) assert fake_username in dpath.util.get(config, USER_LIST_PATH)
@pytest.mark.usefixtures("uses_database") @pytest.mark.usefixtures("uses_database", "fake_mongo")
def test_cred_special_username_parsing(fake_mongo): def test_cred_special_username_parsing():
parse_credentials(cred_telem_special_usernames) parse_credentials(cred_telem_special_usernames)
config = ConfigService.get_config(should_decrypt=True) config = ConfigService.get_config(should_decrypt=True)
assert fake_special_username in dpath.util.get(config, USER_LIST_PATH) assert fake_special_username in dpath.util.get(config, USER_LIST_PATH)
@pytest.mark.usefixtures("uses_database") @pytest.mark.usefixtures("uses_database", "fake_mongo")
def test_cred_telemetry_parsing(fake_mongo): def test_cred_telemetry_parsing():
parse_credentials(cred_telem) parse_credentials(cred_telem)
config = ConfigService.get_config(should_decrypt=True) config = ConfigService.get_config(should_decrypt=True)
assert fake_username in dpath.util.get(config, USER_LIST_PATH) assert fake_username in dpath.util.get(config, USER_LIST_PATH)
@ -91,8 +75,8 @@ def test_cred_telemetry_parsing(fake_mongo):
assert fake_password in dpath.util.get(config, PASSWORD_LIST_PATH) assert fake_password in dpath.util.get(config, PASSWORD_LIST_PATH)
@pytest.mark.usefixtures("uses_database") @pytest.mark.usefixtures("uses_database", "fake_mongo")
def test_empty_cred_telemetry_parsing(fake_mongo): def test_empty_cred_telemetry_parsing():
default_config = deepcopy(ConfigService.get_config(should_decrypt=True)) default_config = deepcopy(ConfigService.get_config(should_decrypt=True))
default_usernames = dpath.util.get(default_config, USER_LIST_PATH) default_usernames = dpath.util.get(default_config, USER_LIST_PATH)
default_nt_hashes = dpath.util.get(default_config, NTLM_HASH_LIST_PATH) default_nt_hashes = dpath.util.get(default_config, NTLM_HASH_LIST_PATH)