Skip to content

Commit

Permalink
osbuild: use sort_keys=True when calculating the Context.id
Browse files Browse the repository at this point in the history
Since we support python3.6 we cannot assume that dicts are ordered
in any way. To ensure the `id` is still always valid we pass
sort_keys=True to json.dump().

Thanks to Simon!
  • Loading branch information
mvo5 authored and ondrejbudai committed Mar 12, 2024
1 parent 8701531 commit 27ac6dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion osbuild/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def set_stage(self, stage: osbuild.Stage):
@property
def id(self):
if self._id is None:
self._id = hashlib.sha256(json.dumps(self._dict()).encode()).hexdigest()
self._id = hashlib.sha256(
json.dumps(self._dict(), sort_keys=True).encode()).hexdigest()
return self._id

def _dict(self):
Expand Down
10 changes: 5 additions & 5 deletions test/mod/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ def test_context():
info = index.get_module_info("Stage", "org.osbuild.noop")
stage = osbuild.Stage(info, {}, None, None, {}, None)
ctx = Context("org.osbuild.test", pipeline, stage)
assert ctx.id == "e6305b7e8ccbc39ec88415ea955b89149faf6f51fb6c89831658068bc6850411"
assert ctx.id == "75bf3feab3d5662744c3ac38406ba73142aeb67666b1180bc1006f913b18f792"

ctx_dict = ctx.as_dict()
# should be a full dict
assert "origin" in ctx_dict
assert ctx_dict["id"] == "e6305b7e8ccbc39ec88415ea955b89149faf6f51fb6c89831658068bc6850411"
assert ctx_dict["id"] == "75bf3feab3d5662744c3ac38406ba73142aeb67666b1180bc1006f913b18f792"
assert "pipeline" in ctx_dict
assert ctx_dict["pipeline"]["name"] == "test-pipeline"
assert ctx_dict["pipeline"]["stage"]["name"] == "org.osbuild.noop"

ctx_dict = ctx.as_dict()
# should only have id
assert ctx_dict["id"] == "e6305b7e8ccbc39ec88415ea955b89149faf6f51fb6c89831658068bc6850411"
assert ctx_dict["id"] == "75bf3feab3d5662744c3ac38406ba73142aeb67666b1180bc1006f913b18f792"
assert len(ctx_dict) == 1

ctx.set_origin("org.osbuild.test-2")
Expand All @@ -154,7 +154,7 @@ def test_context():
ctx.set_origin("org.osbuild.test")
ctx_dict = ctx.as_dict()
# should only have id again (old context ID)
assert ctx_dict["id"] == "e6305b7e8ccbc39ec88415ea955b89149faf6f51fb6c89831658068bc6850411"
assert ctx_dict["id"] == "75bf3feab3d5662744c3ac38406ba73142aeb67666b1180bc1006f913b18f792"
assert len(ctx_dict) == 1


Expand Down Expand Up @@ -316,6 +316,6 @@ def test_log_line_with_entries():

def test_context_id():
ctx = Context()
assert ctx.id == "00d202e4fc9d917def414d1c9f284b137287144087ec275f2d146d9d47b3c8bb"
assert ctx.id == "20bf38c0723b15c2c9a52733c99814c298628526d8b8eabf7c378101cc9a9cf3"
ctx._origin = "foo" # pylint: disable=protected-access
assert ctx.id != "00d202e4fc9d917def414d1c9f284b137287144087ec275f2d146d9d47b3c8bb"

0 comments on commit 27ac6dd

Please sign in to comment.