|
| 1 | +from sys import exit |
| 2 | +from re import search |
| 3 | +from zipfile import ZipFile |
| 4 | +from libs.utils import * |
| 5 | +from logging import getLogger |
| 6 | +from libs.github import get_latest_github_repo_version |
| 7 | + |
| 8 | +logger = getLogger('codeql-container') |
| 9 | + |
| 10 | +class CodeQL: |
| 11 | + """ |
| 12 | + Codeql related logic, downloading, installing etc |
| 13 | + """ |
| 14 | + CODEQL_HOME = None |
| 15 | + |
| 16 | + CODEQL_GITHUB_URL = 'https://github.com/github/codeql-cli-binaries' |
| 17 | + CODEQL_QUERIES_URL = 'https://github.com/github/codeql' |
| 18 | + CODEQL_GO_QUERIES_URL = 'https://github.com/github/codeql-go' |
| 19 | + |
| 20 | + TEMP_DIR ='/tmp' |
| 21 | + |
| 22 | + # Error codes |
| 23 | + ERROR_EXECUTING_COMMAND = 1 |
| 24 | + ERROR_EXECUTING_CODEQL = 2 |
| 25 | + ERROR_UNKNOWN_OS = 3 |
| 26 | + ERROR_GIT_COMMAND = 4 |
| 27 | + |
| 28 | + def __init__(self, codeql_base_dir): |
| 29 | + self.CODEQL_HOME = codeql_base_dir |
| 30 | + |
| 31 | + def download_and_install_latest_codeql(self, github_version): |
| 32 | + """ |
| 33 | + Download and install the latest codeql-cli from the github repo releases |
| 34 | + """ |
| 35 | + download_url = None |
| 36 | + download_path = None |
| 37 | + if os_name == 'posix': |
| 38 | + download_url = f'https://github.com/github/codeql-cli-binaries/releases/download/{github_version.title}/codeql-linux64.zip' |
| 39 | + download_path = f'{self.TEMP_DIR}/codeql_linux.zip' |
| 40 | + elif os_name == 'nt': |
| 41 | + download_url = f'https://github.com/github/codeql-cli-binaries/releases/download/{github_version.title}/codeql-win64.zip' |
| 42 | + download_path = f'{self.TEMP_DIR}/codeql_windows.zip' |
| 43 | + else: |
| 44 | + exit(ERROR_UNKNOWN_OS) |
| 45 | + |
| 46 | + logger.info(f'Downloading codeql-cli version {github_version.title}...') |
| 47 | + check_output_wrapper(f"wget -q {download_url} -O {download_path}", shell=True).decode("utf-8") |
| 48 | + self.install_codeql_cli(download_path) |
| 49 | + #rm /tmp/codeql_linux.zip |
| 50 | + |
| 51 | + def download_and_install_latest_codeql_queries(self): |
| 52 | + """ |
| 53 | + Download and install the latest codeql queries from the github repo |
| 54 | + """ |
| 55 | + logger.info("Downloading codeql queries...") |
| 56 | + codeql_repo_dir = f'{self.CODEQL_HOME}/codeql-repo' |
| 57 | + wipe_and_create_dir(codeql_repo_dir) |
| 58 | + ret1 = check_output_wrapper(f'git clone {self.CODEQL_QUERIES_URL} {codeql_repo_dir}', shell=True) |
| 59 | + |
| 60 | + codeql_go_repo_dir = f'{self.CODEQL_HOME}/codeql-go-repo' |
| 61 | + wipe_and_create_dir(codeql_go_repo_dir) |
| 62 | + ret2 = check_output_wrapper(f'git clone {self.CODEQL_GO_QUERIES_URL} {codeql_go_repo_dir}', shell=True) |
| 63 | + if ret1 is CalledProcessError or ret2 is CalledProcessError: |
| 64 | + logger.error("Could not run git command") |
| 65 | + exit(ERROR_GIT_COMMAND) |
| 66 | + |
| 67 | + def get_current_local_version(self): |
| 68 | + ret_string = check_output_wrapper(f'{self.CODEQL_HOME}/codeql-cli/codeql/codeql version', shell=True).decode("utf-8") |
| 69 | + if ret_string is CalledProcessError: |
| 70 | + logger.error("Could not run codeql command") |
| 71 | + exit(ERROR_EXECUTING_CODEQL) |
| 72 | + version_match = search("Version: ([0-9.]+)\.", ret_string) |
| 73 | + if not version_match: |
| 74 | + logger.error("Could not determine existing codeql version") |
| 75 | + exit(ERROR_EXECUTING_CODEQL) |
| 76 | + version = f'v{version_match.group(1)}' |
| 77 | + return version |
| 78 | + |
| 79 | + def get_latest_codeql_github_version(self): |
| 80 | + return get_latest_github_repo_version("github/codeql-cli-binaries") |
| 81 | + |
| 82 | + def install_codeql_cli(self, download_path): |
| 83 | + logger.info("Installing codeql-cli...") |
| 84 | + codeql_dir = f'{self.CODEQL_HOME}/codeql-cli' |
| 85 | + wipe_and_create_dir(codeql_dir) |
| 86 | + ret1 = check_output_wrapper(f'unzip {download_path} -d {codeql_dir}', shell=True) |
0 commit comments