Skip to content

Commit

Permalink
feat: removed terminaltables and replaced with richs table implem…
Browse files Browse the repository at this point in the history
…entation

Signed-off-by: Paul Horton <phorton@sonatype.com>
  • Loading branch information
madpah committed Dec 9, 2021
1 parent 76c4a54 commit 416b03c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 50 deletions.
70 changes: 34 additions & 36 deletions jake/command/oss.py
Expand Up @@ -31,15 +31,20 @@
from ossindex.model import OssIndexComponent, Vulnerability
from ossindex.ossindex import OssIndex
from packageurl import PackageURL
from rich.console import Console
from rich.progress import Progress
from terminaltables import DoubleTable
from rich.table import Table

from . import BaseCommand


class OssCommand(BaseCommand):

_console: Console

def handle_args(self) -> int:
self._console = Console()

exit_code: int = 0

with Progress() as progress:
Expand Down Expand Up @@ -114,7 +119,6 @@ def handle_args(self) -> int:

return exit_code


def setup_argument_parser(self, subparsers: argparse._SubParsersAction):
parser = subparsers.add_parser('ddt', help='perform a scan backed by OSS Index')

Expand All @@ -131,7 +135,6 @@ def setup_argument_parser(self, subparsers: argparse._SubParsersAction):
choices={'1.3', '1.2', '1.1', '1.0'}, default='1.3',
dest='oss_schema_version')


def _build_bom(self, oss_index_results: List[OssIndexComponent]) -> Bom:
bom = Bom()
oic: OssIndexComponent = None
Expand All @@ -156,7 +159,6 @@ def _build_bom(self, oss_index_results: List[OssIndexComponent]) -> Bom:

return bom


def _print_oss_index_report(self, oss_index_results: List[OssIndexComponent]):
total_vulnerabilities = 0
total_packages = len(oss_index_results)
Expand All @@ -181,40 +183,37 @@ def _print_oss_index_report(self, oss_index_results: List[OssIndexComponent]):
i += 1

print('')
table_data = [
["Audited Dependencies", len(oss_index_results)],
["Vulnerablities Found", total_vulnerabilities],

]

table_instance = DoubleTable(table_data, "Summary")
print(table_instance.table)


@staticmethod
def _print_vulnerability_as_table(v: Vulnerability) -> None:
table_data = [
["ID", v.get_id()],
["Title", v.get_title()],
["Description", '\n'.join(wrap(v.get_description(), 100))],
["CVSS Score", f"{v.get_cvss_score()} - {OssCommand._get_severity_for_cvss_score(v.get_cvss_score())}"],
]
if v.get_cvss_vector():
table_data.append(
["CVSS Vector", v.get_cvss_vector()]
)

table_data.extend(
[
["CWE", v.get_cwe()],
["Reference", v.get_oss_index_reference_url()]
]
table = Table(title='Summary')
table.add_column("Audited Dependencies", justify="left", no_wrap=True)
table.add_column("Vulnerabilities Found", justify="left", no_wrap=True)
table.add_row('{}'.format(len(oss_index_results)), f'{total_vulnerabilities}')

self._console.print(table)

def _print_vulnerability_as_table(self, v: Vulnerability) -> None:
table = Table(title='Vulnerability Details')
table.add_column("ID", justify="center", no_wrap=True)
table.add_column("Title", justify="left", no_wrap=False)
table.add_column("Description", justify="left", no_wrap=False)
table.add_column("CVSS Score", justify="center", no_wrap=True)
table.add_column("CVSS Vector", justify="right", no_wrap=True)
table.add_column("CWE", justify="center", no_wrap=True)
table.add_column("Ref.", justify="left", no_wrap=True)

table.add_row(
v.get_id(),
v.get_title(),
v.get_description(), # '\n'.join(wrap(v.get_description(), 100)),
f"{v.get_cvss_score()} - {OssCommand._get_severity_for_cvss_score(v.get_cvss_score())}",
v.get_cvss_vector() if v.get_cvss_vector() else 'Unknown',
v.get_cwe(),
v.get_oss_index_reference_url()
)
table_instance = DoubleTable(table_data)
table_instance.inner_heading_row_border = False
table_instance.inner_row_border = True
print(OssCommand._get_color_for_cvss_score(cvss_score=v.get_cvss_score()) + table_instance.table + Fore.RESET)

print(OssCommand._get_color_for_cvss_score(cvss_score=v.get_cvss_score()))
self._console.print(table)
print(Fore.RESET)

@staticmethod
def _get_color_for_cvss_score(cvss_score: float = 0.0):
Expand All @@ -229,7 +228,6 @@ def _get_color_for_cvss_score(cvss_score: float = 0.0):
else:
return Fore.GREEN


@staticmethod
def _get_severity_for_cvss_score(cvss_score: float = None) -> str:
if cvss_score >= 9.0:
Expand Down
14 changes: 1 addition & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Expand Up @@ -48,7 +48,6 @@ colorama = "^0.4.4"
tinydb = "^4.5.1"
PyYAML = "^5.4.1"
requests = "^2.26.0"
terminaltables = "^3.1.7"
cyclonedx-python-lib = "^0.10.2"
polling2 = "^0.5.0"
ossindex-lib = "^0.2.1"
Expand Down

0 comments on commit 416b03c

Please sign in to comment.