Skip to content

Commit a1c7226

Browse files
authored
feat: checker-experiment for CI-Pre-Checker (#3873)
--------- Signed-off-by: Joydeep Tripathy <[email protected]> Co-authored-by: Joydeep Tripathy <[email protected]>
1 parent e4239bd commit a1c7226

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

experiments/checker-experiment.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
"""
5+
This experiment is an extension of the CI-Pre-Checker github action.(https://github.com/intel/cve-bin-tool/pull/3840)
6+
7+
This script aims to print any and all the checkers which have {product,version} pairs in their VENDOR_PRODUCT which do NOT have any associated,reported CVEs
8+
After this experiment is done and all the pre-existing checkers are taken care of , we can proceed to add the CI-Pre-checker github action for any newly added/updated checkers.
9+
10+
-- Joydeep Tripathy (www.github.com/joydeep049)
11+
"""
12+
13+
import ast
14+
import os
15+
import sqlite3
16+
import sys
17+
from pathlib import Path
18+
19+
OLD_CACHE_DIR = Path("~").expanduser() / ".cache" / "cve-bin-tool" / "cve.db"
20+
21+
22+
def extract_vendor_product(file_path):
23+
"""Extract {vendor,product} pairs from given checker file"""
24+
vendor_product = None
25+
with open(file_path) as file:
26+
inside_vendor_product = False
27+
vendor_product_str = ""
28+
for line in file:
29+
if "VENDOR_PRODUCT" in line:
30+
inside_vendor_product = True
31+
if inside_vendor_product:
32+
vendor_product_str += line.strip()
33+
if line.strip().endswith("]"):
34+
break
35+
if vendor_product_str:
36+
vendor_product = ast.literal_eval(vendor_product_str.split("=")[1].strip())
37+
return vendor_product
38+
39+
40+
def query_database(file_path):
41+
"""Query the database and check whether all the {vendor,product} pairs have associated CVEs"""
42+
vendor_product = extract_vendor_product(file_path)
43+
dbcon = sqlite3.connect(OLD_CACHE_DIR)
44+
cursor = dbcon.cursor()
45+
for vendor, product in vendor_product:
46+
cursor.execute(
47+
"SELECT count(*) FROM cve_range WHERE vendor = ? AND product = ?",
48+
(vendor, product),
49+
)
50+
result = cursor.fetchall()
51+
# Failing
52+
if result[0] == 0:
53+
return False
54+
# Success
55+
return True
56+
57+
58+
directory = "/home/joydeep/dev/cve-bin-tool/cve_bin_tool/checkers"
59+
value = None
60+
# Iterate through the files in the directory
61+
for filename in os.listdir(directory):
62+
# Check if the file is a Python file and not __init__.py
63+
if filename.endswith(".py") and filename != "__init__.py":
64+
file_path = os.path.join(directory, filename)
65+
value = query_database(file_path)
66+
if value is False:
67+
print("WARNING::")
68+
sys.exit(1)
69+
print(f"For {filename}: {value}")
70+
71+
72+
"""
73+
74+
Result: All the pre-existing checkers are in the clear.
75+
We can go ahead and add the github action.
76+
77+
"""

0 commit comments

Comments
 (0)