-
Notifications
You must be signed in to change notification settings - Fork 20
ENT-12997: Strict checksums and insecure flag #141
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
Conversation
…y. Seperated lockfiles to other tempfiles. Wrong checksum on pre-existing file now triggers warning, deletion of pre-existing file and re-download rather than raising of error.
…dded flags for supplying checksums for installing packages and masterfiles specified with url. All calls for download_package now allows for specifying checksums, and ignoring them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work so far 😄 Added some comments for you to address
cf_remote/utils.py
Outdated
|
||
with open(filepath, "rb") as f: | ||
content = f.read() | ||
return is_different_checksum(checksum, content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No big deal. But this return statement does not have to be indented
mkdir(tempfolder) | ||
# Check if other process is handeling this - lockfile | ||
lockfile = os.path.join(tempfolder, f"{filename}.lock") | ||
with open(lockfile, "w") as lf: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opening the file with in "w"
mode may truncate the file while another process is writing to it.
) | ||
|
||
# Copy over the tempfile and remove it | ||
copy_file(tempfilename, path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just use rename
, because it is atomic. The copy_file
function introduces a new race condition where two processes may be writing to ".{}.tmp".format(filename)
at the same time (see CFE-4549). Furthermore, there is no need to copy the file, because we don't want to keep the original.
log.warning("Pre-existing file did not match existing checksum. File is now deleted.") | ||
else: | ||
# Checksum was correct, skip download | ||
return path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably release the lock before returning
cf_remote/web.py
Outdated
tempfilename, header = urllib.request.urlretrieve(url) | ||
if file_has_different_checksum(tempfilename, checksum): | ||
urllib.request.urlcleanup() | ||
raise ChecksumError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably release the lock here as well
# Find the checksum | ||
name = os.path.basename(package_url) | ||
checksum = None | ||
if package_url in package_checksums: | ||
# Checksum provided for the specified url | ||
checksum = package_checksums[package_url] | ||
elif name in package_checksums: | ||
# Checksum provided for the specified name | ||
checksum = package_checksums[name] | ||
elif "any" in package_checksums: | ||
# Single checksum or no checksum privided | ||
checksum = package_checksums["any"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be in a separate function, and you could add some unit tests testing different checksum files
return SHA256_RE.match(checksum) | ||
|
||
|
||
def parse_checksums_txt(checksums): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some unit tests for this function
tempfolder = os.path.join(tempfile.gettempdir(), "cf-engine") | ||
tempfolder = os.path.join(tempfile.gettempdir(), "cf-remote") | ||
mkdir(tempfolder) | ||
# Check if other process is handeling this - lockfile | ||
# Wait for other process if lockfile is taken. Otherwise lock it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like these changes belong in one of the previous commits
# Unlock the lockfile | ||
fcntl.flock(lf.fileno(), fcntl.LOCK_UN) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this part?
Closing until August. |
No description provided.