Skip to content

Commit

Permalink
get_url: Verify checksum using tmpsrc, not dest
Browse files Browse the repository at this point in the history
Previously, if the checksum of the downloaded file did not match the
specified checksum, the *destination* file was removed. This possibly
leaves the system that is being provisioned in an invalid state.

Instead, the checksum should be calculated on the temporary file only.
If there's a mismatch, delete the *temporary* file, not the destination
file.

This requires checking the checksum before moving the file.
  • Loading branch information
dbrgn committed Oct 30, 2019
1 parent df6b7bf commit 34a2b64
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/ansible/modules/net_tools/basics/get_url.py
Expand Up @@ -614,6 +614,18 @@ def main():
result['checksum_src'] != result['checksum_dest'])
module.exit_json(msg=info.get('msg', ''), **result)

# If a checksum was provided, ensure that the temporary file matches this checksum
# before moving it to the destination.
if checksum != '':
tmpsrc_checksum = module.digest_from_file(tmpsrc, algorithm)

if checksum != tmpsrc_checksum:
os.remove(tmpsrc)
module.fail_json(msg="The checksum for %s did not match %s; it was %s." % (
tmpsrc, checksum, tmpsrc_checksum
), **result)

# Copy temporary file to destination if necessary
backup_file = None
if result['checksum_src'] != result['checksum_dest']:
try:
Expand All @@ -632,13 +644,6 @@ def main():
if os.path.exists(tmpsrc):
os.remove(tmpsrc)

if checksum != '':
destination_checksum = module.digest_from_file(dest, algorithm)

if checksum != destination_checksum:
os.remove(dest)
module.fail_json(msg="The checksum for %s did not match %s; it was %s." % (dest, checksum, destination_checksum), **result)

# allow file attribute changes
module.params['path'] = dest
file_args = module.load_file_common_arguments(module.params)
Expand Down

0 comments on commit 34a2b64

Please sign in to comment.