Skip to content

Commit 64bfa60

Browse files
authored
Omegaconf implementation (#347)
Closes #344, #340
1 parent 7284e34 commit 64bfa60

37 files changed

+613
-712
lines changed

.github/workflows/python.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ on:
77
push:
88
branches:
99
- main
10-
- test-fix-nlp
1110
pull_request:
1211
branches:
1312
- main
14-
- text-fix-nlp
1513

1614
jobs:
1715
build:
@@ -46,13 +44,11 @@ jobs:
4644
uses: actions/setup-python@v4
4745
with:
4846
python-version: 3.10.6
49-
- name: Setup virtual environment
47+
- name: Setup python environment
5048
run: |
5149
python -m pip install --upgrade pip
52-
pip install virtualenv
5350
cd prospector
54-
virtualenv ../venv
55-
source ../venv/bin/activate
51+
cp config-sample.yaml config.yaml
5652
pip install -r requirements.txt
5753
pip install -r requirements-dev.txt
5854
python -m spacy download en_core_web_sm
@@ -61,7 +57,7 @@ jobs:
6157
with:
6258
path: prospector
6359
max_line_length: 100
64-
ignore: E203,E501,W503,F401,F403
60+
ignore: E203,E501,W503,F401,F403,E999
6561
- name: Test with pytest
6662
env:
6763
GIT_CACHE: "/tmp/git-cache"
@@ -74,5 +70,4 @@ jobs:
7470
run: |
7571
cd prospector
7672
[ -d /tmp/git-cache ] || mkdir -p /tmp/git-cache
77-
source ../venv/bin/activate
7873
pytest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ prospector/data/nvd.csv
4343
.vscode/settings.json
4444
prospector/cov_html/*
4545
prospector/client/cli/cov_html/*
46+
prospector/config.yaml
4647
prospector/client/web/node-app/node_modules
4748
prospector/.coverage.*
4849
prospector/.coverage
@@ -53,6 +54,7 @@ prospector/prospector.code-workspace
5354
prospector/requests-cache.sqlite
5455
prospector/prospector-report.html
5556
prospector/test_report.html
57+
prospector/test_report.json
5658
prospector/.idea/*
5759
similarities.csv
5860
prospector/demo_ul.html

prospector/Makefile

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,22 @@ END='\033[0m]' # No Color
77
DONE="$(GREEN)DONE$(END)"
88
PROGRESS="$(YELLOW)....$(END)"
99

10-
test:
11-
@echo "$(PROGRESS) TEST"
12-
@echo "$(DONE) TEST"
1310

1411
setup: requirements.txt
1512
@echo "$(PROGRESS) Installing requirements"
1613
@pip install -r requirements.txt
1714
@echo "$(DONE) Installed requirements"
18-
@echo "$(PROGRESS) Installing pre-commit and other modules"
15+
@echo "$(PROGRESS) Installing pre-commit and spacy model"
1916
@pre-commit install
2017
@python -m spacy download en_core_web_sm
21-
@echo "$(DONE) Installed pre-commit and other modules"
22-
@mkdir -p $(GIT_CACHE)
23-
@echo "$(DONE) Created directory $(GIT_CACHE)"
18+
@echo "$(DONE) Installed pre-commit and spacy model"
2419

2520
dev-setup: setup requirements-dev.txt
26-
@mkdir -p $(CVE_DATA_PATH)
27-
@echo "$(DONE) Created directory $(CVE_DATA_PATH)"
2821
@echo "$(PROGRESS) Installing development requirements"
2922
@pip install -r requirements-dev.txt
3023
@echo "$(DONE) Installed development requirements"
3124

3225
docker-setup:
33-
mkdir -p $(GIT_CACHE)
34-
mkdir -p $(CVE_DATA_PATH)
3526
docker-compose up -d --build
3627

3728
docker-clean:
@@ -47,20 +38,15 @@ docker-clean:
4738
@docker system prune -a -f
4839
@echo "$(DONE) Cleaned residue"
4940

50-
run: client/cli/main.py
51-
#python client/cli/main.py CVE-2014-0050 --repository https://github.com/apache/commons-fileupload --use-nvd --tag-interval FILEUPLOAD_1_3_RC1:FILEUPLOAD_1_3_2_RC1
52-
python client/cli/main.py CVE-2022-29599 --repository https://github.com/apache/maven-shared-utils --use-nvd
53-
#--tag-interval maven-shared-utils-0.1:maven-shared-utils-3.3.4
54-
55-
select-run:
56-
python client/cli/main.py $(cve) --repository $(repository) --use-nvd
5741

5842
clean:
5943
@rm -f prospector.log
60-
@rm -rf $(GIT_CACHE)/*
6144
@rm -rf __pycache__
6245
@rm -rf */__pycache__
6346
@rm -rf */*/__pycache__
64-
@rm -rf *report.html
47+
@rm -rf *.log
48+
@rm -rf .pytest_cache
49+
@rm -rf *.html
6550
@rm -rf *.json
66-
@rm -rf requests-cache.sqlite
51+
@rm -rf *.sqlite
52+
#@rm -rf $(GIT_CACHE)/*

prospector/api/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import uvicorn
22
from fastapi import FastAPI
3-
43
from fastapi.middleware.cors import CORSMiddleware
54
from fastapi.responses import HTMLResponse, RedirectResponse
65

prospector/api/routers/jobs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
from rq import Connection, Queue
66
from rq.job import Job
77

8-
from log.logger import logger
98
from api.routers.nvd_feed_update import main
109
from git.git import do_clone
11-
10+
from log.logger import logger
1211

1312
redis_url = os.environ["REDIS_URL"]
1413

prospector/api/routers/nvd.py

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,98 @@
33
import json
44
import os
55

6+
import requests
67
from fastapi import APIRouter, HTTPException
7-
from fastapi.responses import JSONResponse
88

99
from log.logger import logger
1010

11-
1211
router = APIRouter(
1312
prefix="/nvd",
1413
tags=["nvd"],
1514
# dependencies=[Depends(oauth2_scheme)],
1615
responses={404: {"description": "Not found"}},
1716
)
1817

19-
DATA_PATH = os.environ.get("CVE_DATA_PATH") or os.path.join(
20-
os.path.realpath(os.path.dirname(__file__)), "..", "data"
21-
)
2218

19+
NVD_REST_ENDPOINT = "https://services.nvd.nist.gov/rest/json/cves/2.0"
20+
NVD_API_KEY = os.getenv("NVD_API_KEY")
21+
DATA_PATH = os.getenv("CVE_DATA_PATH")
22+
23+
24+
def get_from_nvd(cve_id: str):
25+
"""
26+
Get an advisory from the NVD dtabase
27+
"""
28+
try:
29+
if NVD_API_KEY is None:
30+
logger.warning("No NVD API key provided, rate liting may apply")
31+
32+
headers = {"apiKey": NVD_API_KEY} if NVD_API_KEY else {}
33+
params = {"cveId": cve_id}
2334

24-
@router.get("/vulnerabilities/by-year/{year}")
25-
async def get_vuln_list_by_year(year: str):
26-
logger.debug("Requested list of vulnerabilities for " + year)
35+
response = requests.get(NVD_REST_ENDPOINT, headers=headers, params=params)
2736

28-
if len(year) != 4 or not year.isdigit():
29-
return JSONResponse([])
37+
if response.status_code != 200:
38+
return False
3039

31-
data_dir = os.path.join(DATA_PATH, year)
32-
if not os.path.isdir(data_dir):
33-
logger.info("No data found for year " + year)
34-
raise HTTPException(
35-
status_code=404, detail="No vulnerabilities found for " + year
36-
)
40+
data = response.json()["vulnerabilities"]
41+
if len(data) == 0:
42+
return False
3743

38-
logger.debug("Serving data for year " + year)
39-
vuln_ids = [vid.rstrip(".json") for vid in os.listdir(data_dir)]
40-
results = {"count": len(vuln_ids), "data": vuln_ids}
41-
return JSONResponse(results)
44+
with open(f"{DATA_PATH}/{cve_id}.json", "w") as out:
45+
json.dump(data[0]["cve"], out)
46+
47+
return data[0]["cve"]
48+
49+
except Exception:
50+
return None
4251

4352

4453
@router.get("/vulnerabilities/{vuln_id}")
4554
async def get_vuln_data(vuln_id):
46-
logger.debug("Requested data for vulnerability " + vuln_id)
4755

48-
year = vuln_id.split("-")[1]
49-
json_file = os.path.join(DATA_PATH, year, vuln_id.upper() + ".json")
56+
json_file = os.path.join(DATA_PATH, f"{vuln_id.upper()}.json")
5057
if not os.path.isfile(json_file):
51-
logger.info("No file found: " + json_file)
52-
raise HTTPException(
53-
status_code=404, detail=json_file
54-
) # detail="Vulnerability data not found")
55-
56-
logger.debug("Serving file: " + json_file)
57-
with open(json_file) as f:
58-
data = json.loads(f.read())
59-
58+
logger.debug("Fallback to NVD")
59+
data = get_from_nvd(vuln_id.upper())
60+
else:
61+
logger.debug("Vulnerability data found locally " + vuln_id)
62+
with open(json_file) as f:
63+
data = json.load(f)
64+
65+
if data is None:
66+
raise HTTPException(status_code=404, detail="Vulnerability not found.")
67+
# TODO: check what happens if there's some error here
6068
return data
6169

6270

63-
@router.get("/status")
64-
async def status():
65-
logger.debug("Serving status page")
66-
out = dict()
67-
metadata_file = os.path.join(DATA_PATH, "metadata.json")
68-
if os.path.isfile(metadata_file):
69-
with open(metadata_file) as f:
70-
metadata = json.loads(f.read())
71-
out["metadata"] = metadata
72-
return JSONResponse(out)
73-
raise HTTPException(status_code=404, detail="Missing feed file")
71+
# @router.get("/status")
72+
# async def status():
73+
# logger.debug("Serving status page")
74+
# out = dict()
75+
# metadata_file = os.path.join(DATA_PATH, "metadata.json")
76+
# if os.path.isfile(metadata_file):
77+
# with open(metadata_file) as f:
78+
# metadata = json.loads(f.read())
79+
# out["metadata"] = metadata
80+
# return JSONResponse(out)
81+
# raise HTTPException(status_code=404, detail="Missing feed file")
82+
83+
# @router.get("/vulnerabilities/by-year/{year}")
84+
# async def get_vuln_list_by_year(year: str):
85+
# logger.debug("Requested list of vulnerabilities for " + year)
86+
87+
# if len(year) != 4 or not year.isdigit():
88+
# return JSONResponse([])
89+
90+
# data_dir = os.path.join(DATA_PATH, year)
91+
# if not os.path.isdir(data_dir):
92+
# logger.info("No data found for year " + year)
93+
# raise HTTPException(
94+
# status_code=404, detail="No vulnerabilities found for " + year
95+
# )
96+
97+
# logger.debug("Serving data for year " + year)
98+
# vuln_ids = [vid.rstrip(".json") for vid in os.listdir(data_dir)]
99+
# results = {"count": len(vuln_ids), "data": vuln_ids}
100+
# return JSONResponse(results)

prospector/api/routers/nvd_feed_update.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
from log.logger import logger
2828

29-
3029
NVD_API_KEY = os.getenv("NVD_API_KEY", "")
3130

3231
# note: The NVD has not data older than 2002

prospector/api/routers/preprocessed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Dict, List, Optional
22

3-
from fastapi import APIRouter
3+
from fastapi import APIRouter, HTTPException
44
from fastapi.responses import JSONResponse
55

66
from commitdb.postgres import PostgresCommitDB
@@ -23,7 +23,7 @@ async def get_commits(
2323
data = db.lookup(repository_url, commit_id)
2424

2525
if len(data) == 0:
26-
return JSONResponse(status_code=404, content={"message": "Not found"})
26+
raise HTTPException(status_code=404, detail="Commit not found")
2727

2828
return JSONResponse(data)
2929

prospector/api/routers/users.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from fastapi import APIRouter, Depends, HTTPException
22
from fastapi.security import OAuth2PasswordRequestForm
33

4-
# from http import HTTPStatus
5-
6-
74
from ..dependencies import (
85
User,
96
UserInDB,
@@ -13,6 +10,9 @@
1310
oauth2_scheme,
1411
)
1512

13+
# from http import HTTPStatus
14+
15+
1616
router = APIRouter(
1717
prefix="/users",
1818
tags=["users"],

0 commit comments

Comments
 (0)