Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions tests/test_updater_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,22 @@ def test_persist_metadata_fails(
self, wrapped_remove: MagicMock, wrapped_replace: MagicMock
) -> None:
# Testing that when write succeeds (the file is created) and replace
# fails by throwing OSError, then the file will be deleted.
# fails by throwing OSError, any existing target file is preserved and
# the temporary file is deleted.
root_path = os.path.join(self.updater._dir, "root.json")
with open(root_path, "rb") as f:
original_root = f.read()

wrapped_replace.side_effect = OSError()
with self.assertRaises(OSError):
self.updater._persist_metadata("target", b"data")
self.updater._persist_metadata("root", b"data")

wrapped_replace.assert_called_once()
wrapped_remove.assert_called_once()

with open(root_path, "rb") as f:
self.assertEqual(f.read(), original_root)

# Assert that the created tempfile during writing is eventually deleted
# or in other words, there is no temporary file left in the folder.
for filename in os.listdir(self.updater._dir):
Expand Down Expand Up @@ -362,29 +370,6 @@ def test_user_agent(self) -> None:

self.assertEqual(ua[:23], "MyApp/1.2.3 python-tuf/")

@patch("os.symlink")
def test_update_root_symlink_oserror_fallback(
self, mock_symlink: MagicMock
) -> None:
"""Test fallback to os.link when os.symlink raises OSError."""
err = OSError("A required privilege is not held by the client")
err.winerror = 1314
mock_symlink.side_effect = err

self.updater._update_root_symlink()
mock_symlink.assert_called_once()

linkname = os.path.join(self.updater._dir, "root.json")
self.assertTrue(os.path.isfile(linkname))
self.assertFalse(os.path.islink(linkname))

version = self.updater._trusted_set.root.version
target = os.path.join(
self.updater._dir, "root_history", f"{version}.root.json"
)
with open(linkname, "rb") as f1, open(target, "rb") as f2:
self.assertEqual(f1.read(), f2.read())


if __name__ == "__main__":
utils.configure_test_logging(sys.argv)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_updater_top_level_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ def test_intermediate_root_cache(self, wrapped_open: MagicMock) -> None:
)
expected_calls = [("root", 4), ("timestamp", None)]
self.assertListEqual(self.sim.fetch_tracker.metadata, expected_calls)
self._assert_content_equals(Root.type, 3)

def test_intermediate_root_cache_poisoning(self) -> None:
"""Test that refresh works as expected when intermediate roots in cache are poisoned"""
Expand Down Expand Up @@ -802,6 +803,7 @@ def test_intermediate_root_cache_poisoning(self) -> None:

expected_calls = [("root", 4), ("timestamp", None)]
self.assertListEqual(self.sim.fetch_tracker.metadata, expected_calls)
self._assert_content_equals(Root.type, 3)

def test_expired_metadata(self) -> None:
"""Verifies that expired local timestamp/snapshot can be used for
Expand Down
31 changes: 10 additions & 21 deletions tuf/ngclient/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ def __init__( # noqa: PLR0917
# if no root was provided, use the cached non-versioned root.json
bootstrap = self._load_local_metadata(Root.type)

# Load the initial root, make sure it's cached
# Load the initial root, make sure it's cached (versioned and not)
self._trusted_set = TrustedMetadataSet(
bootstrap, self.config.envelope_type
)
self._persist_root(self._trusted_set.root.version, bootstrap)
self._update_root_symlink()
self._persist_metadata(Root.type, bootstrap)

def refresh(self) -> None:
"""Refresh top-level metadata.
Expand Down Expand Up @@ -362,22 +362,6 @@ def _persist_file(self, filename: str, data: bytes) -> None:
os.remove(temp_file_name)
raise e

def _update_root_symlink(self) -> None:
"""Symlink root.json to current trusted root version in root_history/"""
linkname = os.path.join(self._dir, "root.json")
version = self._trusted_set.root.version
current = os.path.join("root_history", f"{version}.root.json")
with contextlib.suppress(FileNotFoundError):
os.remove(linkname)
try:
os.symlink(current, linkname)
except OSError as e:
if getattr(e, "winerror", None) == 1314:
# Fallback for NTFS "required privilege is not held by client"
os.link(os.path.join(self._dir, current), linkname)
else:
raise

def _load_root(self) -> None:
"""Load root metadata.

Expand All @@ -391,6 +375,7 @@ def _load_root(self) -> None:
lower_bound = self._trusted_set.root.version + 1
upper_bound = lower_bound + self.config.max_root_rotations

new_root_bytes: bytes | None = None
try:
for next_version in range(lower_bound, upper_bound):
# look for next_version in local cache
Expand All @@ -399,7 +384,9 @@ def _load_root(self) -> None:
self._dir, "root_history", f"{next_version}.root.json"
)
with open(root_path, "rb") as f:
self._trusted_set.update_root(f.read())
data = f.read()
self._trusted_set.update_root(data)
new_root_bytes = data
continue
except (OSError, exceptions.RepositoryError) as e:
# this root did not exist locally or is invalid
Expand All @@ -414,15 +401,17 @@ def _load_root(self) -> None:
)
self._trusted_set.update_root(data)
self._persist_root(next_version, data)
new_root_bytes = data

except exceptions.DownloadHTTPError as exception:
if exception.status_code not in {403, 404}:
raise
# 404/403 means current root is newest available
break
finally:
# Make sure the non-versioned root.json links to current version
self._update_root_symlink()
# Make sure the non-versioned root.json matches current version
if new_root_bytes is not None:
self._persist_metadata(Root.type, new_root_bytes)

def _load_timestamp(self) -> None:
"""Load local and remote timestamp metadata."""
Expand Down
Loading