Skip to content

Commit 7c65f2b

Browse files
committed
updater: Update root.json symlink on initialize
When application initializes an Updater with bootstrap, it should be considered the trusted version from that point onwards: Update the symlink "root.json" already here (even if refresh is never called). n that Updater instance). Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent e5f8c6e commit 7c65f2b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

tuf/ngclient/updater.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ def __init__(
126126
# if no root was provided, use the cached non-versioned root.json
127127
bootstrap = self._load_local_metadata(Root.type)
128128

129-
# Load the initial root, make sure it's cached in root_history/
129+
# Load the initial root, make sure it's cached
130130
self._trusted_set = TrustedMetadataSet(
131131
bootstrap, self.config.envelope_type
132132
)
133133
self._persist_root(self._trusted_set.root.version, bootstrap)
134+
self._update_root_symlink()
134135

135136
def refresh(self) -> None:
136137
"""Refresh top-level metadata.
@@ -320,7 +321,8 @@ def _persist_metadata(self, rolename: str, data: bytes) -> None:
320321
def _persist_root(self, version: int, data: bytes) -> None:
321322
"""Write root metadata to disk atomically to avoid data loss.
322323
323-
Use a filename prefixed with version (e.g. "1.root.json").
324+
The metadata is stored with version prefix (e.g.
325+
"root_history/1.root.json").
324326
"""
325327
rootdir = os.path.join(self._dir, "root_history")
326328
with contextlib.suppress(FileExistsError):
@@ -346,6 +348,15 @@ def _persist_file(self, filename: str, data: bytes) -> None:
346348
os.remove(temp_file_name)
347349
raise e
348350

351+
def _update_root_symlink(self) -> None:
352+
"""Symlink root.json to current trusted root version in root_history/"""
353+
linkname = os.path.join(self._dir, "root.json")
354+
version = self._trusted_set.root.version
355+
current = os.path.join("root_history", f"{version}.root.json")
356+
with contextlib.suppress(FileNotFoundError):
357+
os.remove(linkname)
358+
os.symlink(current, linkname)
359+
349360
def _load_root(self) -> None:
350361
"""Load root metadata.
351362
@@ -390,12 +401,7 @@ def _load_root(self) -> None:
390401
break
391402
finally:
392403
# Make sure the non-versioned root.json links to current version
393-
linkname = os.path.join(self._dir, "root.json")
394-
version = self._trusted_set.root.version
395-
current = os.path.join("root_history", f"{version}.root.json")
396-
with contextlib.suppress(FileNotFoundError):
397-
os.remove(linkname)
398-
os.symlink(current, linkname)
404+
self._update_root_symlink()
399405

400406
def _load_timestamp(self) -> None:
401407
"""Load local and remote timestamp metadata."""

0 commit comments

Comments
 (0)