From 8d5c124debcdcc42ebfdde3922c1750b548dbb40 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Oct 2020 09:37:01 -0500 Subject: [PATCH] Improve performance of generating non-cryptographically secure uuids (#41314) --- homeassistant/config_entries.py | 2 +- homeassistant/core.py | 2 +- homeassistant/helpers/area_registry.py | 2 +- homeassistant/helpers/device_registry.py | 2 +- homeassistant/util/uuid.py | 15 ++++++--------- tests/util/test_uuid.py | 8 ++++---- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 139e2066d17..38593badf2f 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -139,7 +139,7 @@ class ConfigEntry: ) -> None: """Initialize a config entry.""" # Unique id of the config entry - self.entry_id = entry_id or uuid_util.uuid_v1mc_hex() + self.entry_id = entry_id or uuid_util.random_uuid_hex() # Version of the configuration. self.version = version diff --git a/homeassistant/core.py b/homeassistant/core.py index 50dae06acf1..436580a7858 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -511,7 +511,7 @@ class Context: user_id: str = attr.ib(default=None) parent_id: Optional[str] = attr.ib(default=None) - id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex) + id: str = attr.ib(factory=uuid_util.random_uuid_hex) def as_dict(self) -> dict: """Return a dictionary representation of the context.""" diff --git a/homeassistant/helpers/area_registry.py b/homeassistant/helpers/area_registry.py index 3e3a3e03f6a..e97b613ab16 100644 --- a/homeassistant/helpers/area_registry.py +++ b/homeassistant/helpers/area_registry.py @@ -26,7 +26,7 @@ class AreaEntry: """Area Registry Entry.""" name: Optional[str] = attr.ib(default=None) - id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex) + id: str = attr.ib(factory=uuid_util.random_uuid_hex) class AreaRegistry: diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 1dff2ef9483..fcb9b4ddcb8 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -73,7 +73,7 @@ class DeviceEntry: area_id: str = attr.ib(default=None) name_by_user: str = attr.ib(default=None) entry_type: str = attr.ib(default=None) - id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex) + id: str = attr.ib(factory=uuid_util.random_uuid_hex) # This value is not stored, just used to keep track of events to fire. is_new: bool = attr.ib(default=False) diff --git a/homeassistant/util/uuid.py b/homeassistant/util/uuid.py index c91cfe0dd12..d924eab934d 100644 --- a/homeassistant/util/uuid.py +++ b/homeassistant/util/uuid.py @@ -1,15 +1,12 @@ """Helpers to generate uuids.""" -import random -import uuid +from random import getrandbits -def uuid_v1mc_hex() -> str: - """Generate a uuid1 with a random multicast MAC address. +def random_uuid_hex() -> str: + """Generate a random UUID hex. - The uuid1 uses a random multicast MAC address instead of the real MAC address - of the machine without the overhead of calling the getrandom() system call. - - This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function + This uuid should not be used for cryptographically secure + operations. """ - return uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex + return "%032x" % getrandbits(32 * 4) diff --git a/tests/util/test_uuid.py b/tests/util/test_uuid.py index 0debb867341..6e85f638d1b 100644 --- a/tests/util/test_uuid.py +++ b/tests/util/test_uuid.py @@ -5,7 +5,7 @@ import uuid import homeassistant.util.uuid as uuid_util -async def test_uuid_v1mc_hex(): - """Verify we can generate a uuid_v1mc and return hex.""" - assert len(uuid_util.uuid_v1mc_hex()) == 32 - assert uuid.UUID(uuid_util.uuid_v1mc_hex()) +async def test_uuid_util_random_uuid_hex(): + """Verify we can generate a random uuid.""" + assert len(uuid_util.random_uuid_hex()) == 32 + assert uuid.UUID(uuid_util.random_uuid_hex())