Island: Make Machine.network_interfaces immutable

This commit is contained in:
Mike Salvatore 2022-08-18 09:32:34 -04:00
parent a4a4613a66
commit b6e04074a4
3 changed files with 17 additions and 2 deletions

View File

@ -1,7 +1,7 @@
from ipaddress import IPv4Interface from ipaddress import IPv4Interface
from typing import Optional, Sequence from typing import MutableSequence, Optional, Sequence
from pydantic import Field, PositiveInt from pydantic import Field, PositiveInt, validator
from common import OperatingSystems from common import OperatingSystems
@ -15,3 +15,10 @@ class Machine(MutableBaseModel):
operating_system: OperatingSystems operating_system: OperatingSystems
operating_system_version: str operating_system_version: str
hostname: str hostname: str
@validator("network_interfaces", pre=True)
def _make_sequence_immutable(cls, sequence: Sequence):
if isinstance(sequence, MutableSequence):
return tuple(sequence)
return sequence

View File

@ -1,6 +1,7 @@
import uuid import uuid
from ipaddress import IPv4Interface from ipaddress import IPv4Interface
from types import MappingProxyType from types import MappingProxyType
from typing import MutableSequence
import pytest import pytest
@ -125,6 +126,12 @@ def test_network_interfaces_set_invalid_value():
m.network_interfaces = [IPv4Interface("172.1.2.3/24"), None] m.network_interfaces = [IPv4Interface("172.1.2.3/24"), None]
def test_network_interfaces_sequence_is_immutable():
m = Machine(**MACHINE_OBJECT_DICT)
assert not isinstance(m.network_interfaces, MutableSequence)
def test_operating_system_set_valid_value(): def test_operating_system_set_valid_value():
m = Machine(**MACHINE_OBJECT_DICT) m = Machine(**MACHINE_OBJECT_DICT)

View File

@ -210,6 +210,7 @@ _serialize_credentials # unused method (monkey/common/credentials/credentials:6
# Models # Models
_make_simulation # unused method (monkey/monkey_island/cc/models/simulation.py:19 _make_simulation # unused method (monkey/monkey_island/cc/models/simulation.py:19
operating_system_version operating_system_version
_make_sequence_immutable
# TODO DELETE AFTER RESOURCE REFACTORING # TODO DELETE AFTER RESOURCE REFACTORING