Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify updater logic for downloading and verifying target files #1202

Merged

Conversation

joshuagl
Copy link
Member

@joshuagl joshuagl commented Nov 9, 2020

Description of the changes being introduced by the pull request:

The logic for downloading/verifying target files is fairly complex to follow and debug. This PR flattens several internal methods (including a callback) to make the logic simpler to follow.

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

This internal method isn't used by any code other than tests.

Signed-off-by: Joshua Lock <jlock@vmware.com>
The call stack and code for download_target() is more complex than
required:
* download_target() : builds target destination filepath, gets length
  and hashes
* _get_target_file() : fixes filenames if consistent snapshots enabled,
  defines verification callback
* _get_file() : iterates mirrors, tries to download files, verifies them

Remove the verification callback and collapse the call stack by a single
level to make the code easier to follow.

Signed-off-by: Joshua Lock <jlock@vmware.com>
try:
file_object = tuf.download.safe_download(file_mirror, file_length)

# Verify 'file_object' against the expected length and hashes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it is possible to explain why there is a duplicated check of file length here after the one in safe_download?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strictly speaking the checks that are done in functions that safe_download() calls are for the downloaded byte count, whereas _check_file_length() actually looks at the file object. I don't know how that could be of practical difference but...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, but I think better safe than sorry. I think redundant checks are fine as long as no real impact on performance.

Rather than read to the end of the file in order to determin its size, use
the whence value of seek() to move the file object's position to the end
of the file, then the tell() method of the file object to read the current
position in bytes.

Co-authored-by: Jussi Kukkonen <jkukkonen@vmware.com>
Signed-off-by: Joshua Lock <jlock@vmware.com>
@joshuagl joshuagl closed this Nov 12, 2020
@joshuagl
Copy link
Member Author

Trying close/re-open to handle the CI move

tuf/client/updater.py Outdated Show resolved Hide resolved
tests/test_updater.py Show resolved Hide resolved
Simplify the loop exit logic in _get_target_file() to simply return a
verified file_object, once we have it, rather than breaking from the loop
and then returning the file_object.

This converts a use of a try/except/else to a try/except and is a little
easier to read.

Signed-off-by: Joshua Lock <jlock@vmware.com>
@trishankatdatadog
Copy link
Member

Why is it called _get_target_file()? How are metadata downloaded and verified?

@joshuagl
Copy link
Member Author

Why is it called _get_target_file()? How are metadata downloaded and verified?

with _get_metadata_file(). It does things slightly differently: doesn't prefix the file with a hash, has different/additional checks for metadata fields (version and spec_version), checks for expiry, verifies signature, etc.

Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impeccable work, @joshuagl! 🎉 Thanks for deobfuscating. :)

@lukpueh lukpueh merged commit e061bc6 into theupdateframework:develop Nov 26, 2020
@joshuagl joshuagl deleted the joshuagl/updater-simplify branch November 26, 2020 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants