-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with
allow_mismatch: false
flag (#3078)
* Fix issue with `allow_mismatch: false` flag * An exception will be raised when `allow_mismatch` flag is set to `false` while downloading images
- Loading branch information
Showing
6 changed files
with
130 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
ansible/playbooks/roles/repository/files/download-requirements/src/downloader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import logging | ||
from os import chmod | ||
from pathlib import Path | ||
from shutil import move | ||
from tempfile import mkstemp | ||
from typing import Callable, Dict | ||
|
||
from src.command.toolchain import Toolchain, TOOLCHAINS | ||
from src.config import Config, OSArch | ||
from src.crypt import SHA_ALGORITHMS | ||
from src.error import CriticalError, ChecksumMismatch | ||
|
||
|
||
class Downloader: | ||
""" | ||
Wrapper for downloading requirements with checksum validation. | ||
""" | ||
|
||
def __init__(self, requirements: Dict[str, Dict], | ||
sha_algorithm: str, | ||
download_func: Callable, | ||
download_func_args: Dict = None): | ||
""" | ||
:param requirements: data from parsed requirements file | ||
:param sha_algorithm: which algorithm will be used in validating the requirements | ||
:param download_func: back end function used for downloading the requirements | ||
:param download_func_args: optional args passed to the `download_func` | ||
""" | ||
self.__requirements: Dict[str, Dict] = requirements | ||
self.__sha_algorithm: str = sha_algorithm | ||
self.__download: Callable = download_func | ||
self.__download_args: Dict = download_func_args or {} | ||
|
||
def __is_checksum_valid(self, requirement: str, requirement_file: Path) -> bool: | ||
""" | ||
Check if checksum matches with `requirement` and downloaded file `requirement_file`. | ||
:param requirement: an entry from the requirements corresponding to the downloaded file | ||
:param requirement_file: existing requirement file | ||
:returns: True - checksum ok, False - otherwise | ||
:raises: | ||
:class:`ChecksumMismatch`: can be raised on failed checksum and missing allow_mismatch flag | ||
""" | ||
req = self.__requirements[requirement] | ||
if req[self.__sha_algorithm] != SHA_ALGORITHMS[self.__sha_algorithm](requirement_file): | ||
try: | ||
if req['allow_mismatch']: | ||
logging.warning(f'{requirement} - allow_mismatch flag used') | ||
return True | ||
|
||
return False | ||
except KeyError: | ||
return False | ||
|
||
return True | ||
|
||
def download(self, requirement: str, requirement_file: Path, sub_key: str = None): | ||
""" | ||
Download `requirement` as `requirement_file` and compare checksums. | ||
:param requirement: an entry from the requirements corresponding to the downloaded file | ||
:param requirement_file: existing requirement file | ||
:param sub_key: optional key for the `requirement` such as `url` | ||
:raises: | ||
:class:`ChecksumMismatch`: can be raised on failed checksum | ||
""" | ||
req = self.__requirements[requirement][sub_key] if sub_key else requirement | ||
|
||
if requirement_file.exists(): | ||
|
||
if self.__is_checksum_valid(requirement, requirement_file): | ||
logging.debug(f'- {requirement} - checksum ok, skipped') | ||
return | ||
else: | ||
logging.info(f'- {requirement}') | ||
|
||
tmpfile = Path(mkstemp()[1]) | ||
chmod(tmpfile, 0o0644) | ||
|
||
self.__download(req, tmpfile, **self.__download_args) | ||
|
||
if not self.__is_checksum_valid(requirement, tmpfile): | ||
tmpfile.unlink() | ||
raise ChecksumMismatch(f'- {requirement}') | ||
|
||
move(str(tmpfile), str(requirement_file)) | ||
return | ||
|
||
logging.info(f'- {requirement}') | ||
self.__download(req, requirement_file, **self.__download_args) | ||
|
||
if not self.__is_checksum_valid(requirement, requirement_file): | ||
requirement_file.unlink() | ||
raise ChecksumMismatch(f'- {requirement}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters