From 160d2d11cf0918ab58067c52e146f73f0483e5a9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 22 Aug 2022 14:02:18 -0400 Subject: [PATCH] Agent: Add get_agent_id() --- monkey/infection_monkey/utils/agent_id.py | 17 +++++++++++++++++ .../infection_monkey/utils/test_agent_id.py | 10 ++++++++++ 2 files changed, 27 insertions(+) create mode 100644 monkey/infection_monkey/utils/agent_id.py create mode 100644 monkey/tests/unit_tests/infection_monkey/utils/test_agent_id.py diff --git a/monkey/infection_monkey/utils/agent_id.py b/monkey/infection_monkey/utils/agent_id.py new file mode 100644 index 000000000..0a889a114 --- /dev/null +++ b/monkey/infection_monkey/utils/agent_id.py @@ -0,0 +1,17 @@ +from uuid import UUID, uuid4 + + +def get_agent_id() -> UUID: + """ + Get the agent ID for the current running agent + + Each time an agent process starts, the return value of this function will be unique. Subsequent + calls to this function from within the same process will have the same return value. + """ + if get_agent_id._id is None: + get_agent_id._id = uuid4() + + return get_agent_id._id + + +get_agent_id._id = None diff --git a/monkey/tests/unit_tests/infection_monkey/utils/test_agent_id.py b/monkey/tests/unit_tests/infection_monkey/utils/test_agent_id.py new file mode 100644 index 000000000..c4a95b73c --- /dev/null +++ b/monkey/tests/unit_tests/infection_monkey/utils/test_agent_id.py @@ -0,0 +1,10 @@ +from uuid import UUID + +from infection_monkey.utils.agent_id import get_agent_id + + +def test_get_agent_id(): + agent_id = get_agent_id() + + assert isinstance(agent_id, UUID) + assert agent_id == get_agent_id()