def setup(): with suppress(OSError): remove('cve_cpe.db') sr.QUERIES.update(TEST_QUERIES) cpe_base = sr.setup_cpe_table(dp.extract_cpe(PATH_TO_TEST + EXTRACT_CPE_XML)) cve_base, summary_base = dp.extract_cve(PATH_TO_TEST + EXTRACT_CVE_JSON) cve_base = sr.setup_cve_feeds_table(cve_list=cve_base) summary_base = sr.setup_cve_summary_table(summary_list=summary_base) with DatabaseInterface(PATH_TO_TEST + 'test_update.db') as db: db.execute_query(query=QUERIES['create_cpe_table'].format('cpe_table')) db.insert_rows(query=QUERIES['insert_cpe'].format('cpe_table'), input_data=cpe_base) db.execute_query(query=QUERIES['create_cve_table'].format('cve_table')) db.execute_query(query=QUERIES['create_summary_table'].format('summary_table')) db.insert_rows(query=QUERIES['insert_cve'].format('cve_table'), input_data=cve_base) db.insert_rows(query=QUERIES['insert_summary'].format('summary_table'), input_data=summary_base) db.execute_query(query=TEST_QUERIES['test_create_update'].format('outdated')) db.execute_query(query=TEST_QUERIES['test_create_update'].format('new')) db.insert_rows(query=TEST_QUERIES['test_insert_cve_id'].format('outdated'), input_data=[('CVE-2018-0001', 2018), ('CVE-2018-0002', 2018)]) db.insert_rows(query=TEST_QUERIES['test_insert_cve_id'].format('new'), input_data=[('CVE-2018-0002', 2018), ('CVE-2018-0003', 2018)]) yield with suppress(OSError): remove(PATH_TO_TEST + 'test_update.db') remove(PATH_TO_TEST + 'test_import.db') remove(PATH_TO_TEST + 'test_output.db')
def test_insert_functionality(): with DatabaseInterface(TEST_DB_PATH) as db: db.insert_rows(TEST_QUERIES['test_insert'].format('test_table'), [(34, )]) test_insert_output = list( db.fetch_multiple( query=QUERIES['select_all'].format('test_table'))) assert test_insert_output == [(23, ), (34, )]
def __init__(self): readxml_obj = XmlReading() self.ip = readxml_obj.get_cluster_ip() self.src_path = readxml_obj.get_src_path() self.dst_path = readxml_obj.get_dst_path() self.tar_type = readxml_obj.get_tar_type() self.database_obj = DatabaseInterface() self.tar_obj = Tar()
def test_execute_query(): with DatabaseInterface(TEST_DB_PATH) as db: db.execute_query( query=TEST_QUERIES['test_create'].format('test_table_2')) assert list( db.fetch_multiple( query=QUERIES['exist'].format('test_table_2'))) == [ ('test_table_2', ) ] db.execute_query(query=QUERIES['drop'].format('test_table_2')) assert list( db.fetch_multiple( query=QUERIES['exist'].format('test_table_2'))) == []
def look_up_vulnerabilities(product_name: str, requested_version: str) -> Optional[dict]: with DatabaseInterface() as db: product_terms, version = replace_characters_and_wildcards(generate_search_terms(product_name)), replace_characters_and_wildcards([requested_version])[0] matched_cpe = match_cpe(db, product_terms) if len(matched_cpe) == 0: logging.debug(f'No CPEs were found for product {product_name}') return None try: matched_product = find_matching_cpe_product(matched_cpe, version) except IndexError: return None cve_candidates = search_cve(db, matched_product) cve_candidates.update(search_cve_summary(db, matched_product)) return cve_candidates
from typing import List, Tuple try: from ..internal import data_parsing as dp from ..internal.database_interface import DB_PATH, QUERIES, DatabaseInterface from ..internal.helper_functions import (CveEntry, CveLookupException, CveSummaryEntry, replace_characters_and_wildcards) except (ImportError, ValueError, SystemError): sys.path.append(str(Path(__file__).parent.parent / 'internal')) import data_parsing as dp from database_interface import DB_PATH, QUERIES, DatabaseInterface from helper_functions import CveEntry, CveLookupException, CveSummaryEntry, replace_characters_and_wildcards CURRENT_YEAR = datetime.now().year DATABASE = DatabaseInterface() CPE_SPLIT_REGEX = r'(?<![\\:]):(?!:)|(?<=\\:):' # don't split on '::' or '\:' but split on '\::' Years = namedtuple('Years', 'start_year end_year') def overlap(requested_years: namedtuple, years_in_cve_database: list) -> list: set_of_requested_years = set( range(requested_years.start_year, requested_years.end_year + 1)) return list(set_of_requested_years.difference(set(years_in_cve_database))) def table_exists(table_name: str) -> bool: return bool( list(DATABASE.fetch_multiple(QUERIES['exist'].format(table_name))))
def test_select_functionality(): with DatabaseInterface(TEST_DB_PATH) as db: assert list( db.fetch_multiple( query=QUERIES['select_all'].format('test_table'))) == [(23, )]
def test_db_connection(): with DatabaseInterface(TEST_DB_PATH) as db: assert db.connection is not None
def __init__(self): self.joblist = [] self.upload_obj = Upload() self.dcider_flag = True self.database_obj = DatabaseInterface()