Island: Add initialize_machine_repository()

This commit is contained in:
Mike Salvatore 2022-09-20 14:24:30 -04:00
parent fca3a1357e
commit ff0a57aa93
2 changed files with 36 additions and 0 deletions

View File

@ -27,3 +27,5 @@ from .mongo_machine_repository import MongoMachineRepository
from .mongo_agent_repository import MongoAgentRepository
from .mongo_node_repository import MongoNodeRepository
from .mongo_agent_event_repository import MongoAgentEventRepository
from .utils import initialize_machine_repository

View File

@ -0,0 +1,34 @@
import platform
from socket import gethostname
from uuid import getnode
from common import OperatingSystem
from common.network.network_utils import get_network_interfaces
from monkey_island.cc.models import Machine
from . import IMachineRepository, UnknownRecordError
def initialize_machine_repository(machine_repository: IMachineRepository):
"""
Populate an IMachineRepository with island machine data
If the island is not already present in the IMachineRepository, add it.
:param machine_repository: The repository to populate
:raises StorageError: If an error occurs while attempting to store data in the repository
"""
hardware_id = getnode()
try:
machine_repository.get_machine_by_hardware_id(hardware_id)
except UnknownRecordError:
machine = Machine(
id=machine_repository.get_new_id(),
hardware_id=hardware_id,
network_interfaces=get_network_interfaces(),
operating_system=OperatingSystem(platform.system().lower()),
operating_system_version=platform.version(),
hostname=gethostname(),
)
machine_repository.upsert_machine(machine)