Skip to content

Commit e973da4

Browse files
committed
use hash 2562
1 parent 3a7793a commit e973da4

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

backend/infrahub/lock_getter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import hashlib
2+
13
from infrahub.core.branch import Branch
24
from infrahub.core.constants import InfrahubKind
35
from infrahub.core.node import Node
@@ -60,6 +62,12 @@ def _should_kind_be_locked_on_any_branch(kind: str, schema_branch: SchemaBranch)
6062
return False
6163

6264

65+
def _hash(value: str) -> str:
66+
# Do not use builtin `hash` for lock names as due to randomization results would differ between
67+
# different processes.
68+
return hashlib.sha256(value.encode()).hexdigest()
69+
70+
6371
def get_lock_names_on_object_mutation(node: Node, branch: Branch, schema_branch: SchemaBranch) -> list[str]:
6472
"""
6573
Return lock names for object on which we want to avoid concurrent mutation (create/update). Except for some specific kinds,
@@ -99,9 +107,9 @@ def get_lock_names_on_object_mutation(node: Node, branch: Branch, schema_branch:
99107
if attr is None or attr.value is None:
100108
# `attr.value` being None corresponds to optional unique attribute.
101109
# `attr` being None is not supposed to happen.
102-
value_hashed = str(hash(""))
110+
value_hashed = _hash("")
103111
else:
104-
value_hashed = str(hash(str(attr.value)))
112+
value_hashed = _hash(str(attr.value))
105113

106114
uc_attributes_values.append(value_hashed)
107115

backend/tests/unit/core/test_get_kinds_lock.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from infrahub.database import InfrahubDatabase
66
from infrahub.lock_getter import (
77
_get_kinds_to_lock_on_object_mutation,
8+
_hash,
89
_should_kind_be_locked_on_any_branch,
910
get_lock_names_on_object_mutation,
1011
)
@@ -78,7 +79,7 @@ async def test_lock_names_only_attributes(
7879
person = await create_and_save(db=db, schema="TestPerson", name="John")
7980
car = await create_and_save(db=db, schema="TestCar", name="mercedes", color="blue", owner=person)
8081
assert get_lock_names_on_object_mutation(car, branch=default_branch, schema_branch=schema_branch) == [
81-
"global.object.TestCar." + str(hash("mercedes")) + "." + str(hash("blue"))
82+
"global.object.TestCar." + _hash("mercedes") + "." + _hash("blue")
8283
]
8384

8485
async def test_lock_names_optional_empty_attribute(
@@ -95,5 +96,5 @@ async def test_lock_names_optional_empty_attribute(
9596
schema_branch = registry.schema.get_schema_branch(name=default_branch.name)
9697
person = await create_and_save(db=db, schema="TestPerson", name="John")
9798
assert get_lock_names_on_object_mutation(person, branch=default_branch, schema_branch=schema_branch) == [
98-
"global.object.TestPerson." + str(hash("")) + "." + str(hash("John"))
99+
"global.object.TestPerson." + _hash("") + "." + _hash("John")
99100
]

0 commit comments

Comments
 (0)