-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Adding exit codes #76
Conversation
Thanks for the contribution! Definitely something I wanted to tackle at some point. A few things:
|
I personally wouldn't object to adding Some examples:
But I'm open to modify the PR :) |
Changed to an exit code of maximum 1 based on potential malicious indicators. |
Thanks, would like to add a few things, can you tick "Allow maintainers to edit my pull request"? |
@christophetd That is already ticked :) I can add the things too if you're busy or want help. But by all means, go nuts! :) |
Weird, I can't seem to push it:
Anyway, this was my suggested set of changes, care to apply it yourself? Then we can merge it.Let me know if that looks good! commit fda55b962ad5f405b999f986f29256b61d1ca6db
Author: Christophe Tafani-Dereeper <christophe.tafanidereeper@datadoghq.com>
Date: Mon Nov 28 09:59:08 2022 +0100
Exit with non-zero status code when issues are identified and a specific CLI flag is provided
diff --git a/guarddog/cli.py b/guarddog/cli.py
index f068dba..56cd8a0 100644
--- a/guarddog/cli.py
+++ b/guarddog/cli.py
@@ -18,7 +18,7 @@ from .scanners.project_scanner import RequirementsScanner
analyzer = Analyzer()
ALL_RULES = analyzer.sourcecode_ruleset | analyzer.metadata_ruleset
-
+EXIT_CODE_ISSUES_FOUND = 1
@click.group
def cli():
@@ -29,7 +29,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:
@@ -46,6 +47,8 @@ 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")
@@ -53,7 +56,8 @@ def verify(path, json):
@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:
@@ -81,7 +85,8 @@ def scan(identifier, version, rules, exclude_rules, json):
else:
print_scan_results(results, identifier)
- exit(min(results.get('issues', 1), 1))
+ 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):
@@ -114,5 +119,13 @@ def print_scan_results(results, identifier):
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'])
\ No newline at end of file
+ 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)
\ No newline at end of file
|
I've had better success in the past by git cloning the contributors repo, and working directly in it by doing:
For whatever reason, that's what works for me. |
Thanks! |
No worries :) Happy to help! |
Description
This PR will add exit codes on
scan
.This will help automating and adding guarddog to runners or scripts, as there's no other way to determine if guarddog found issues or not in a machine friendly way (other than
grep [1-9]+
or some other shenanigans).This will use
results['issues']
counter, 0 results will exit 0 and anything above will exit with the number of issues.Usage
Assuming a runner similar to https://github.com/Torxed/archoffline/blob/797436de3999e73963af4b60364e0c9767afece9/.github/workflows/guarddog.yaml:
Normally, this runner could never exit with a bad exit code, leaving a green tick on github:
When in fact, there might have been 1+ issues.
And this is due to github runners checking for non-zero exit codes (by default) to indicate if the runner had any complaints.
This should allow people to incorporate guarddog in their runners in a natural way :)
Footnote
Cool project and fun approach to grabbing the common suspects.