Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from RickBarretto/issue1
Browse files Browse the repository at this point in the history
Issue #1
  • Loading branch information
RickBarretto committed May 21, 2022
2 parents df96c30 + c80cb18 commit 02336ab
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
30 changes: 22 additions & 8 deletions src/commands/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""This is the entry of terminal commands"""

import click
from requests.exceptions import HTTPError, Timeout, ConnectionError

from src.core.fetch_api import fetch_api
from src.core.fetch_api import OsvApi
from src.core.filter_report import Filter
from src.core.utils.osv_model import OsvModel
from src.core.utils.exceptions import PackageNotFound


@click.command(name="package")
Expand All @@ -13,11 +15,23 @@
def audit_package(package, version):
"""Fetches in OSV for vulnerabilities"""

data = fetch_api(OsvModel(package, version))
f = Filter(data)
click.secho("Vulnerabilities founded!\n", fg="red", bold=True)
info = f.get_main_info()
pkg = OsvModel(package, version)

for i in info:
click.secho("{}: ".format(i[0]), fg="red", bold=True)
click.secho("{}\n".format(i[1]), fg="blue")
try:
data = OsvApi(pkg).fetch()

f = Filter(data)
click.secho("Vulnerabilities founded!\n", fg="red", bold=True)
info = f.get_main_info()

for i in info:
click.secho("{}: ".format(i[0]), fg="red", bold=True)
click.secho("{}\n".format(i[1]), fg="blue")
except PackageNotFound:
click.secho("Package isn't in OSV's DataBase!\n", fg="red", bold=True)
except HTTPError as err:
click.secho("Http Error: {}\n".format(err), fg="red", bold=True)
except ConnectionError:
click.secho("Connection Error!\n".format(err), fg="red", bold=True)
except Timeout:
click.secho("Request Timeout! :(\n", fg="red", bold=True)
40 changes: 34 additions & 6 deletions src/core/fetch_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
"""Fetch the OSV API"""

import requests
from requests.exceptions import HTTPError

from src.core.utils.osv_model import OsvModel, OsvUrl
from src.core.utils.exceptions import PackageNotFound


def fetch_api(osv_model: OsvModel) -> dict:
"""Fetch the OSV API and return a Json"""
import requests


class OsvApi:
"""Deal with OsvApi to get package Vulnerabilities
usage:
>>> django_pkg = OsvModel("Django", "3.0")
>>> response = OsvApi(django_pkg).fetch()
{...}
"""

def __init__(self, osv_parameters: OsvModel):

# Attributes
self.api_parameters = osv_parameters.get_data()

def fetch(self):
json: dict = self.__request()
if json:
return json
else:
raise PackageNotFound

def __request(self) -> dict:
"""Fetch the OSV API and return a Json"""

osv_link = "https://api.osv.dev/v1/query"

osv_link = OsvUrl().get_url()
osv_model = osv_model.get_data()
response = requests.post(osv_link, data=self.api_parameters, timeout=3.05)
response.raise_for_status()

response = requests.post(osv_link, data=osv_model)
json = response.json()

return response.json()
return json
2 changes: 2 additions & 0 deletions src/core/utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class PackageNotFound(Exception):
pass

0 comments on commit 02336ab

Please sign in to comment.