Skip to content

Commit

Permalink
Merge branch '3244-sha256-agent-registration' into develop
Browse files Browse the repository at this point in the history
Issue #3244
PR #3346
  • Loading branch information
mssalvatore committed May 16, 2023
2 parents 1b20771 + b3a9507 commit 105fff2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion monkey/common/agent_registration_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional, Sequence
from uuid import UUID

from pydantic import validator
from pydantic import Field, validator

from .base_models import InfectionMonkeyBaseModel
from .transforms import make_immutable_sequence
Expand All @@ -17,6 +17,7 @@ class AgentRegistrationData(InfectionMonkeyBaseModel):
parent_id: Optional[UUID]
cc_server: SocketAddress
network_interfaces: Sequence[IPv4Interface]
sha256: str = Field(regex=r"^[0-9a-fA-F]{64}$")

_make_immutable_sequence = validator("network_interfaces", pre=True, allow_reuse=True)(
make_immutable_sequence
Expand Down
1 change: 1 addition & 0 deletions monkey/infection_monkey/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def _register_agent(self):
parent_id=self._opts.parent,
cc_server=self._island_address,
network_interfaces=get_network_interfaces(),
sha256="0000000000000000000000000000000000000000000000000000000000000000",
)
self._island_api_client.register_agent(agent_registration_data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
SERVER = SocketAddress(ip="1.1.1.1", port=9999)
WINDOWS = "windows"
AGENT_ID = UUID("80988359-a1cd-42a2-9b47-5b94b37cd673")
AGENT_SHA256 = "7f295ebba73b2f549f98b9c35ae588f153283476ac8f087cc75d9c4788795d82"
AGENT_REGISTRATION = AgentRegistrationData(
id=AGENT_ID,
machine_hardware_id=1,
start_time=0,
parent_id=None,
cc_server=SERVER,
network_interfaces=[],
sha256=AGENT_SHA256,
)

TIMESTAMP = 123456789
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

AGENT_ID = UUID("860aff5b-d2af-43ea-afb5-62bac3d30b7e")
AGENT_SHA256 = "a4beff584bc18ef48f64874e2f57ef2c8088d6947d4e15490120730401640dbc"

SEED_ID = 10

Expand All @@ -38,6 +39,7 @@
parent_id=None,
cc_server=SocketAddress.from_string(IP),
network_interfaces=[IPv4Interface("192.168.1.2/24")],
sha256=AGENT_SHA256,
)


Expand Down Expand Up @@ -67,7 +69,6 @@ def node_repository() -> INodeRepository:

@pytest.fixture
def handler(machine_repository, agent_repository, node_repository) -> handle_agent_registration:

return handle_agent_registration(
machine_repository, agent_repository, node_repository, get_current_datetime=lambda: NOW
)
Expand Down Expand Up @@ -125,6 +126,7 @@ def test_existing_machine_updated__find_by_ip(handler, machine_repository):
IPv4Interface("192.168.1.4/24"),
IPv4Interface("192.168.1.5/24"),
],
sha256=AGENT_SHA256,
)

existing_machine = Machine(
Expand Down Expand Up @@ -230,6 +232,7 @@ def test_machine_interfaces_updated(handler, machine_repository):
IPv4Interface("192.168.1.3/16"),
IPv4Interface("192.168.1.4/24"),
],
sha256=AGENT_SHA256,
)
expected_network_interfaces = sorted(
(*agent_registration_data.network_interfaces, existing_machine.network_interfaces[-1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

AGENT_ID = UUID("012e7238-7b81-4108-8c7f-0787bc3f3c10")
PARENT_ID = UUID("0fc9afcb-1902-436b-bd5c-1ad194252484")
AGENT_SHA256 = "c6fef92294c27bbe372696bd58f35d1117e41088edff3ffd3c73df865297ac88"
SOCKET_ADDRESS = SocketAddress(ip="192.168.1.1", port=5000)

AGENT_REGISTRATION_MESSAGE_OBJECT_DICT = {
Expand All @@ -19,6 +20,7 @@
"parent_id": PARENT_ID,
"cc_server": SOCKET_ADDRESS,
"network_interfaces": [IPv4Interface("10.0.0.1/24"), IPv4Interface("192.168.5.32/16")],
"sha256": AGENT_SHA256,
}

AGENT_REGISTRATION_MESSAGE_SIMPLE_DICT = {
Expand All @@ -28,6 +30,7 @@
"parent_id": str(PARENT_ID),
"cc_server": SOCKET_ADDRESS.dict(simplify=True),
"network_interfaces": ["10.0.0.1/24", "192.168.5.32/16"],
"sha256": AGENT_SHA256,
}


Expand All @@ -54,6 +57,7 @@ def test_from_serialized():
("parent_id", 2.1),
("cc_server", [1]),
("network_interfaces", "not-a-list"),
("sha256", []),
],
)
def test_construct_invalid_field__type_error(key, value):
Expand All @@ -71,6 +75,9 @@ def test_construct_invalid_field__type_error(key, value):
("start_time", "not-a-date-time"),
("network_interfaces", [1, "stuff", 3]),
("cc_server", []),
("sha256", "not-a-hex-string-although-it-is-of-the-correct-length-for-sha256"),
("sha256", "12345678123456781234567812345678123456781234567812345678123456780"),
("sha256", "12345678"),
],
)
def test_construct_invalid_field__value_error(key, value):
Expand All @@ -90,6 +97,7 @@ def test_construct_invalid_field__value_error(key, value):
("parent_id", AGENT_ID),
("cc_server", SOCKET_ADDRESS),
("network_interfaces", ["10.0.0.1/24"]),
("sha256", "fdf59a0b2fc2b49b9e245da81a44654e97fa5be566cc719deeb77aaf74fe6013"),
],
)
def test_fields_immutable(key, value):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"parent_id": UUID("9d55ba33-95c2-417d-bd86-d3d11e47daeb"),
"cc_server": {"ip": "10.0.0.1", "port": "5000"},
"network_interfaces": ["10.1.1.2/24"],
"sha256": "cf5c10a8073aa923877ee66df8c1912cac2dbb4b85a97d09cb95d57bde4d2876",
}

AGENTS = (
Expand Down

0 comments on commit 105fff2

Please sign in to comment.