Skip to content

Commit

Permalink
cli: add spinning wheel for vulnerability list
Browse files Browse the repository at this point in the history
Add a spinning wheel when users are running the pro vulnerability list
command. This should better inform users that some processing is
happening in the background
  • Loading branch information
lucasmoura committed Oct 15, 2024
1 parent 547601a commit 2378c82
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 22 deletions.
12 changes: 6 additions & 6 deletions features/cli/vulnerability_list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Feature: CLI vulnerability list command
And I run `pro vulnerability list --data-file=/tmp/security_issues_xenial.json --manifest-file=/tmp/manifest` as non-root
And I remove colors from output
And I remove links from output
Then I will see the following on stdout:
Then stdout contains substring:
"""
Common vulnerabilities and exposures (CVE):
VULNERABILITY PRIORITY FIX AVAILABLE FROM AFFECTED INSTALLED PACKAGES
Expand All @@ -47,7 +47,7 @@ Feature: CLI vulnerability list command
When I run `pro vulnerability list --all --data-file=/tmp/security_issues_xenial.json --manifest-file=/tmp/manifest` as non-root
And I remove colors from output
And I remove links from output
Then I will see the following on stdout:
Then stdout contains substring:
"""
Common vulnerabilities and exposures (CVE):
VULNERABILITY PRIORITY FIX AVAILABLE FROM AFFECTED INSTALLED PACKAGES
Expand Down Expand Up @@ -83,7 +83,7 @@ Feature: CLI vulnerability list command
When I run `pro vulnerability list --unfixable --data-file=/tmp/security_issues_xenial.json --manifest-file=/tmp/manifest` as non-root
And I remove colors from output
And I remove links from output
Then I will see the following on stdout:
Then stdout contains substring:
"""
Common vulnerabilities and exposures (CVE):
VULNERABILITY PRIORITY FIX AVAILABLE FROM AFFECTED INSTALLED PACKAGES
Expand All @@ -106,7 +106,7 @@ Feature: CLI vulnerability list command
When I run `pro vulnerability list --usns --data-file=/tmp/security_issues_xenial.json --manifest-file=/tmp/manifest` as non-root
And I remove colors from output
And I remove links from output
Then I will see the following on stdout:
Then stdout contains substring:
"""
Ubuntu Security Notices (USN):
VULNERABILITY FIX AVAILABLE FROM AFFECTED INSTALLED PACKAGES
Expand All @@ -128,13 +128,13 @@ Feature: CLI vulnerability list command
"""
When I run `pro vulnerability list --data-file=/tmp/security_issues_xenial.json --manifest-file=/tmp/manifest` as non-root
And I remove colors from output
Then I will see the following on stdout:
Then stdout contains substring:
"""
No CVEs found that affects this system
"""
When I run `pro vulnerability list --usns --data-file=/tmp/security_issues_xenial.json --manifest-file=/tmp/manifest` as non-root
And I remove colors from output
Then I will see the following on stdout:
Then stdout contains substring:
"""
No USNs found that affects this system
"""
Expand Down
92 changes: 76 additions & 16 deletions uaclient/cli/vulnerability/list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import itertools
import re
import sys
import threading
import time
from typing import Any, Dict # noqa: F401

from uaclient import config, exceptions, messages
Expand Down Expand Up @@ -363,6 +367,52 @@ def _create_list_header(
return msg


def run_spinner(stop_spinner):
spinner = itertools.cycle(["|", "/", "-", "\\"])
while not stop_spinner.is_set():
sys.stdout.write(next(spinner))
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write("\b")


def _get_vulnerability_data_with_spinner(
cfg: config.UAConfig,
options,
get_vuln_func,
):
print(messages.CLI_VULNERABILITY_LIST_SPINNER_MSG)
if not sys.stdout.isatty():
return get_vuln_func(
options=options,
cfg=cfg,
)

stop_spinner = threading.Event()

spinner_thread = threading.Thread(target=run_spinner, args=(stop_spinner,))
spinner_thread.start()

vulnerabilities_result, applied_fixes_count = get_vuln_func(
options=options,
cfg=cfg,
)

stop_spinner.set()
spinner_thread.join()
sys.stdout.write("\b")

return vulnerabilities_result, applied_fixes_count


def _get_cve_data(cfg: config.UAConfig, cve_options):
return _get_vulnerability_data_with_spinner(
cfg=cfg,
options=cve_options,
get_vuln_func=cve_vulnerabilities,
)


def _list_cves(
cfg: config.UAConfig,
show_all: bool,
Expand All @@ -371,15 +421,16 @@ def _list_cves(
manifest_file: str,
series: str,
):
cve_vulnerabilities_result, applied_fixes_count = cve_vulnerabilities(
options=CVEVulnerabilitiesOptions(
all=show_all,
unfixable=show_unfixable,
data_file=data_file,
manifest_file=manifest_file,
series=series,
),
cve_options = CVEVulnerabilitiesOptions(
all=show_all,
unfixable=show_unfixable,
data_file=data_file,
manifest_file=manifest_file,
series=series,
)
cve_vulnerabilities_result, applied_fixes_count = _get_cve_data(
cfg=cfg,
cve_options=cve_options,
)

if cve_vulnerabilities_result.cves:
Expand All @@ -402,6 +453,14 @@ def _list_cves(
print(_create_already_fixed_cves_count(applied_fixes_count))


def _get_usn_data(cfg: config.UAConfig, usn_options):
return _get_vulnerability_data_with_spinner(
cfg=cfg,
options=usn_options,
get_vuln_func=usn_vulnerabilities,
)


def _list_usns(
cfg: config.UAConfig,
show_all: bool,
Expand All @@ -410,15 +469,16 @@ def _list_usns(
manifest_file: str,
series: str,
):
usn_vulnerabilities_result, applied_fixes_count = usn_vulnerabilities(
options=USNVulnerabilitiesOptions(
all=show_all,
unfixable=show_unfixable,
data_file=data_file,
manifest_file=manifest_file,
series=series,
),
usn_options = USNVulnerabilitiesOptions(
all=show_all,
unfixable=show_unfixable,
data_file=data_file,
manifest_file=manifest_file,
series=series,
)
usn_vulnerabilities_result, applied_fixes_count = _get_usn_data(
cfg=cfg,
usn_options=usn_options,
)

if usn_vulnerabilities_result.usns:
Expand Down
4 changes: 4 additions & 0 deletions uaclient/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,10 @@ class TxtColor:
By default, the command will only list CVEs. To display
USNs instead, run the command with the --usns flag."""
)
CLI_VULNERABILITY_LIST_SPINNER_MSG = t.gettext(
"""\
Processing vulnerability data..."""
)
CLI_VULNERABILITY_LIST_CVE_HEADER = (
TxtColor.DISABLEGREY
+ t.gettext("Common vulnerabilities and exposures (CVE):")
Expand Down

0 comments on commit 2378c82

Please sign in to comment.