Skip to content

Commit 5b91874

Browse files
committed
fix: Solve minor error
1 parent 41791c3 commit 5b91874

File tree

5 files changed

+14
-28
lines changed

5 files changed

+14
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Currently the only way to consume NVD data is through a public [API](https://nvd
1010

1111
## How does it work?
1212

13-
This repository solves this problem by creating an automatic clone on a MongoDB database that the developer has already initialised. This command also create an index for the own id extracted from NVD (cve:id, cpe_match:matchCriteriaId and cpe:cpeNameId). This is done using the command:
13+
This repository solves this problem by creating an automatic clone on a MongoDB database that the developer has already initialised, in approximately 30 minutes. This command also create an index for the own id extracted from NVD (cve:id, cpe_match:matchCriteriaId and cpe:cpeNameId). This is done using the command:
1414

1515
```
1616
python3 main.py clone [your_mongodb_uri] --nvd_api [your_nvd_api_key]

main.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,24 @@
44
from asyncio import run
55
from src import clone_cves, clone_cpe_matchs, clone_cpes, nvd_sync
66

7-
import time
8-
97

108
@command()
119
@argument('mongodb_uri')
1210
@option('--nvd_api', default='')
1311
async def clone(mongodb_uri, nvd_api):
1412
headers = {'apiKey': nvd_api}
15-
1613
client = AsyncIOMotorClient(mongodb_uri)
17-
1814
dbs = await client.list_databases()
1915
while dbs.alive:
2016
db = await dbs.next()
2117
if db['name'] == 'nvd':
22-
raise Exception('Database have been ready cloned. Delete it or run updater command.')
23-
18+
raise Exception('NVD database have been already cloned. Delete it or run sync command.')
2419
if nvd_api:
2520
delay = 1
2621
else:
2722
delay = 6
28-
2923
await clone_cves(client, delay, headers)
30-
3124
await clone_cpe_matchs(client, delay, headers)
32-
3325
await clone_cpes(client, delay, headers)
3426

3527

@@ -38,14 +30,11 @@ async def clone(mongodb_uri, nvd_api):
3830
@option('--nvd_api', default='')
3931
async def sync(mongodb_uri, nvd_api):
4032
headers = {'apiKey': nvd_api}
41-
4233
client = AsyncIOMotorClient(mongodb_uri)
43-
4434
if nvd_api:
4535
delay = 1.0
4636
else:
4737
delay = 6.0
48-
4938
scheduler = BackgroundScheduler()
5039
scheduler.add_job(nvd_sync, 'interval', args=[client, headers, delay], seconds=7200)
5140
scheduler.start()

src/cpe.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from requests import get, ConnectTimeout, ConnectionError
2-
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase, AsyncIOMotorCollection
2+
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
33
from time import sleep
44
from pymongo import InsertOne
55

@@ -8,22 +8,21 @@
88

99

1010
async def clone_cpes(client: AsyncIOMotorClient, delay: float, headers: dict[str, str]):
11-
nvd_clone_db: AsyncIOMotorDatabase = client.nvd
12-
cpes_collection: AsyncIOMotorCollection = nvd_clone_db.get_collection('cpes')
11+
cpes_collection: AsyncIOMotorCollection = client.nvd.get_collection('cpes')
1312
await cpes_collection.create_index('cpeNameId', unique=True)
1413
index: int = 0
1514
while True:
16-
actions: list[InsertOne] = []
1715
while True:
1816
try:
1917
response = get(cpe_uri + str(index), headers=headers).json()
2018
sleep(delay)
2119
break
2220
except (ConnectTimeout, ConnectionError):
2321
sleep(6)
22+
actions: list[InsertOne] = []
2423
for product in response['products']:
2524
actions.append(InsertOne(product['cpe']))
26-
index += response['resultsPerPage']
2725
await cpes_collection.bulk_write(actions, ordered=False)
26+
index += response['resultsPerPage']
2827
if index == response['totalResults']:
2928
break

src/cpe_match.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from requests import get, ConnectTimeout, ConnectionError
2-
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase, AsyncIOMotorCollection
2+
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
33
from time import sleep
44
from pymongo import InsertOne
55

@@ -8,22 +8,21 @@
88

99

1010
async def clone_cpe_matchs(client: AsyncIOMotorClient, delay: float, headers: dict[str, str]):
11-
nvd_clone_db: AsyncIOMotorDatabase = client.nvd
12-
cpe_match_collection: AsyncIOMotorCollection = nvd_clone_db.get_collection('cpe_matchs')
11+
cpe_match_collection: AsyncIOMotorCollection = client.nvd.get_collection('cpe_matchs')
1312
await cpe_match_collection.create_index('matchCriteriaId', unique=True)
1413
index: int = 0
1514
while True:
16-
actions: list[InsertOne] = []
1715
while True:
1816
try:
1917
response = get(cpe_match_uri + str(index), headers=headers).json()
2018
sleep(delay)
2119
break
2220
except (ConnectTimeout, ConnectionError):
2321
sleep(6)
22+
actions: list[InsertOne] = []
2423
for match_string in response['matchStrings']:
2524
actions.append(InsertOne(match_string['matchString']))
26-
index += response['resultsPerPage']
2725
await cpe_match_collection.bulk_write(actions, ordered=False)
26+
index += response['resultsPerPage']
2827
if index == response['totalResults']:
2928
break

src/cve.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from requests import get, ConnectTimeout, ConnectionError
2-
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase, AsyncIOMotorCollection
2+
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorCollection
33
from time import sleep
44
from pymongo import InsertOne
55

@@ -8,22 +8,21 @@
88

99

1010
async def clone_cves(client: AsyncIOMotorClient, delay: float, headers: dict[str, str]):
11-
nvd_clone_db: AsyncIOMotorDatabase = client.nvd
12-
cves_collection: AsyncIOMotorCollection = nvd_clone_db.get_collection('cves')
11+
cves_collection: AsyncIOMotorCollection = client.nvd.get_collection('cves')
1312
await cves_collection.create_index('id', unique=True)
1413
index: int = 0
1514
while True:
16-
actions: list[InsertOne] = []
1715
while True:
1816
try:
1917
response = get(cve_uri + str(index), headers=headers).json()
2018
sleep(delay)
2119
break
2220
except (ConnectTimeout, ConnectionError):
2321
sleep(6)
22+
actions: list[InsertOne] = []
2423
for vulnerability in response['vulnerabilities']:
2524
actions.append(InsertOne(vulnerability['cve']))
26-
index += response['resultsPerPage']
2725
await cves_collection.bulk_write(actions, ordered=False)
26+
index += response['resultsPerPage']
2827
if index == response['totalResults']:
2928
break

0 commit comments

Comments
 (0)