Skip to content

Commit ab28830

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 3798002 commit ab28830

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
@@ -120,11 +120,12 @@ def __init__(
120120
# if no root was provided, use the cached non-versioned root.json
121121
bootstrap = self._load_local_metadata(Root.type)
122122

123-
# Load the initial root, make sure it's cached in root_history/
123+
# Load the initial root, make sure it's cached
124124
self._trusted_set = TrustedMetadataSet(
125125
bootstrap, self.config.envelope_type
126126
)
127127
self._persist_root(self._trusted_set.root.version, bootstrap)
128+
self._update_root_symlink()
128129

129130
def refresh(self) -> None:
130131
"""Refresh top-level metadata.
@@ -314,7 +315,8 @@ def _persist_metadata(self, rolename: str, data: bytes) -> None:
314315
def _persist_root(self, version: int, data: bytes) -> None:
315316
"""Write root metadata to disk atomically to avoid data loss.
316317
317-
Use a filename prefixed with version (e.g. "1.root.json").
318+
The metadata is stored with version prefix (e.g.
319+
"root_history/1.root.json").
318320
"""
319321
rootdir = os.path.join(self._dir, "root_history")
320322
with contextlib.suppress(FileExistsError):
@@ -340,6 +342,15 @@ def _persist_file(self, filename: str, data: bytes) -> None:
340342
os.remove(temp_file_name)
341343
raise e
342344

345+
def _update_root_symlink(self) -> None:
346+
"""Symlink root.json to current trusted root version in root_history/"""
347+
linkname = os.path.join(self._dir, "root.json")
348+
version = self._trusted_set.root.version
349+
current = os.path.join("root_history", f"{version}.root.json")
350+
with contextlib.suppress(FileNotFoundError):
351+
os.remove(linkname)
352+
os.symlink(current, linkname)
353+
343354
def _load_root(self) -> None:
344355
"""Load root metadata.
345356
@@ -384,12 +395,7 @@ def _load_root(self) -> None:
384395
break
385396
finally:
386397
# Make sure the non-versioned root.json links to current version
387-
linkname = os.path.join(self._dir, "root.json")
388-
version = self._trusted_set.root.version
389-
current = os.path.join("root_history", f"{version}.root.json")
390-
with contextlib.suppress(FileNotFoundError):
391-
os.remove(linkname)
392-
os.symlink(current, linkname)
398+
self._update_root_symlink()
393399

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

0 commit comments

Comments
 (0)