Skip to content

Commit

Permalink
Adding exit codes (#76)
Browse files Browse the repository at this point in the history
* Adding exit codes

* Limits the exit code to 1 if it's >=1

* Added patch from @christophetd adding --exit-non-zero-on-finding
  • Loading branch information
Torxed authored Nov 28, 2022
1 parent 1c517ff commit 14b3858
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions guarddog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

analyzer = Analyzer()
ALL_RULES = analyzer.sourcecode_ruleset | analyzer.metadata_ruleset

EXIT_CODE_ISSUES_FOUND = 1

@click.group
def cli():
Expand All @@ -27,7 +27,8 @@ def cli():
@cli.command("verify")
@click.argument("path")
@click.option("--json", default=False, is_flag=True, help="Dump the output as JSON to standard out")
def verify(path, json):
@click.option("--exit-non-zero-on-finding", default=False, is_flag=True, help="Exit with a non-zero status code if at least one issue is identified")
def verify(path, json, exit_non_zero_on_finding):
"""Verify a requirements.txt file
Args:
Expand All @@ -44,14 +45,17 @@ def verify(path, json):
import json as js
print(js.dumps(results))

if exit_non_zero_on_finding:
exit_with_status_code(results)

@cli.command("scan")
@click.argument("identifier")
@click.option("-v", "--version", default=None, help="Specify a version to scan")
@click.option("-r", "--rules", multiple=True, type=click.Choice(ALL_RULES, case_sensitive=False))
@click.option("-x", "--exclude-rules", multiple=True, type=click.Choice(ALL_RULES, case_sensitive=False))
@click.option("--json", default=False, is_flag=True, help="Dump the output as JSON to standard out")
def scan(identifier, version, rules, exclude_rules, json):
@click.option("--exit-non-zero-on-finding", default=False, is_flag=True, help="Exit with a non-zero status code if at least one issue is identified")
def scan(identifier, version, rules, exclude_rules, json, exit_non_zero_on_finding):
"""Scan a package
Args:
Expand Down Expand Up @@ -84,6 +88,9 @@ def scan(identifier, version, rules, exclude_rules, json):
else:
print_scan_results(results, identifier)

if exit_non_zero_on_finding:
exit_with_status_code(results)

# Determines if the input passed to the 'scan' command is a local package name
def is_local_package(input):
identifier_is_path = re.search(r"(.{0,2}\/)+.+", input)
Expand All @@ -93,6 +100,7 @@ def is_local_package(input):
# Pretty prints scan results for the console
def print_scan_results(results, identifier):
num_issues = results.get('issues')

if num_issues == 0:
print("Found " + colored('0 potentially malicious indicators', 'green', attrs=['bold']) + " scanning " + colored(identifier, None, attrs=['bold']))
print()
Expand All @@ -113,5 +121,14 @@ def print_scan_results(results, identifier):
for finding in source_code_findings:
print(' * ' + finding['message'] + ' at ' + finding['location'] + '\n ' + format_code_line_for_output(finding['code']))
print()


def format_code_line_for_output(code):
return ' ' + colored(code.strip().replace('\n', '\n ').replace('\t', ' '), None, 'on_red', attrs=['bold'])
return ' ' + colored(code.strip().replace('\n', '\n ').replace('\t', ' '), None, 'on_red', attrs=['bold'])


# Given the results, exit with the appropriate status code
def exit_with_status_code(results):
num_issues = results.get('issues', 0)
if num_issues > 0:
exit(EXIT_CODE_ISSUES_FOUND)

0 comments on commit 14b3858

Please # to comment.