Skip to content

Commit

Permalink
Added checksum checking in download_package
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Moene <[email protected]>
  • Loading branch information
victormlg committed Dec 12, 2024
1 parent 385f9d6 commit e40ec6e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cf_remote/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _iterate_over_packages(tags=None, version=None, edition=None, download=False
else:
for artifact in artifacts:
if download:
download_package(artifact.url, checksum=artifact.data.get("SHA256"))
download_package(artifact.url, checksum=artifact.checksum)
else:
print(artifact.url)
return 0
Expand Down
2 changes: 2 additions & 0 deletions cf_remote/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def __init__(self, data, filename=None):
self.tags = ["any"]
self.create_tags()

self.checksum = data.get("SHA256")

def create_tags(self):
if self.arch:
self.add_tag(self.arch)
Expand Down
3 changes: 2 additions & 1 deletion cf_remote/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def get_info(host, *, users=None, connection=None):
def install_package(host, pkg, data, *, connection=None):

print("Installing: '{}' on '{}'".format(pkg, host))
output = None
if ".deb" in pkg:
output = ssh_sudo(connection, 'dpkg -i "{}"'.format(pkg), True)
elif ".msi" in pkg:
Expand Down Expand Up @@ -391,7 +392,7 @@ def _package_from_releases(tags, extension, version, edition, remote_download):
if remote_download:
return artifact.url
else:
return download_package(artifact.url)
return download_package(artifact.url, checksum=artifact.checksum)


def get_package_from_host_info(
Expand Down
19 changes: 9 additions & 10 deletions cf_remote/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ def get_json(url):

def download_package(url, path=None, checksum=None):

if not checksum :
user_error("No checksum found")

if not SHA256_RE.match(checksum):
user_error("Invalid checksum or unsupported checksum algorithm: '%s'" % checksum)
if checksum and not SHA256_RE.match(checksum):
user_error("Invalid checksum or unsupOkported checksum algorithm: '%s'" % checksum)

if not path:
filename = os.path.basename(url)
Expand All @@ -52,13 +50,14 @@ def download_package(url, path=None, checksum=None):
print("Downloading package: '{}'".format(path))

answer = urllib.request.urlopen(url).read()
digest = hashlib.sha256(answer).digest().hex()

if checksum == digest :
f.write(answer)
f.flush()
else :
user_error("Mismatching checksums")
if checksum :
digest = hashlib.sha256(answer).digest().hex()
if checksum != digest :
user_error("Mismatching checksums")

f.write(answer)
f.flush()

fcntl.flock(f.fileno(), fcntl.LOCK_UN)

Expand Down

0 comments on commit e40ec6e

Please sign in to comment.