Island: Use common.credentials.Credentials in resource

This commit is contained in:
Mike Salvatore 2022-07-07 15:33:45 -04:00
parent 938aec9d49
commit 77143ee765
3 changed files with 17 additions and 37 deletions

View File

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

View File

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

View File

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