Skip to content

Commit c926a55

Browse files
authored
Doug/add non blocking mode (#16)
* Add support to disable blocking with --disable-blocking * Fixed issue with support for no_change causing the scan to not run * Added full scan ID to results
1 parent 94a6520 commit c926a55

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ If you don't want to provide the Socket API Token every time then you can use th
3737
| --disable-security-issue | | False | False | If enabled will disable Security Issue Comments |
3838
| --files | | False | | If provided in the format of `["file1", "file2"]` it will only look for those files and not glob the path |
3939
| --ignore-commit-files | | False | False | If enabled then the CLI will ignore what files are changed in the commit and look for all manifest files |
40+
| --disable-blocking | | False | False | Disables failing checks and will only exit with an exit code of 0 |

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__author__ = 'socket.dev'
2-
__version__ = '1.0.3'
2+
__version__ = '1.0.7'

socketsecurity/core/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
"pyproject.toml": {
8383
"pattern": "pyproject.toml"
8484
},
85+
"poetry.lock": {
86+
"pattern": "poetry.lock"
87+
},
8588
"requirements.txt": {
8689
"pattern": "*requirements.txt"
8790
},
@@ -394,12 +397,15 @@ def find_files(path: str, files: list = None) -> list:
394397
:return:
395398
"""
396399
all_files = []
400+
files_provided = False
401+
if files is not None and len(files) > 0:
402+
files_provided = True
397403
for ecosystem in socket_globs:
398404
patterns = socket_globs[ecosystem]
399405
for file_name in patterns:
400406
pattern = patterns[file_name]["pattern"]
401407
file_path = f"{path}/**/{pattern}"
402-
if files is None or len(files) == 0:
408+
if not files_provided:
403409
files = glob(file_path, recursive=True)
404410
else:
405411
files = Core.match_supported_files(path, files)

socketsecurity/core/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def __init__(self):
111111
@staticmethod
112112
def check_event_type() -> str:
113113
if github_event_name.lower() == "push":
114-
if pr_number is None or pr_number == "":
114+
if pr_number is None or pr_number == "" or pr_number == "0":
115115
event_type = "main"
116116
else:
117117
event_type = "diff"

socketsecurity/core/messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def create_security_comment_json(diff: Diff) -> dict:
1818
break
1919
output = {
2020
"scan_failed": scan_failed,
21-
"new_alerts": []
21+
"new_alerts": [],
22+
"full_scan_id": diff.id
2223
}
2324
for alert in diff.new_alerts:
2425
alert: Issue

socketsecurity/socketcli.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import json
33
from socketsecurity.core import Core, __version__
4-
from socketsecurity.core.classes import FullScanParams, Diff, Package, Alert
4+
from socketsecurity.core.classes import FullScanParams, Diff, Package, Issue
55
from socketsecurity.core.messages import Messages
66
from socketsecurity.core.scm_comments import Comments
77
from socketsecurity.core.git_interface import Git
@@ -12,6 +12,7 @@
1212

1313
logging.basicConfig(level=logging.INFO)
1414
log = logging.getLogger("socketcli")
15+
blocking_disabled = False
1516

1617
parser = argparse.ArgumentParser(
1718
prog="socketcli",
@@ -142,15 +143,24 @@
142143
default=False
143144
)
144145

146+
parser.add_argument(
147+
'--disable-blocking',
148+
help='Disables failing checks and will only exit with an exit code of 0',
149+
action='store_true',
150+
default=False
151+
)
152+
145153

146154
def output_console_comments(diff_report: Diff, sbom_file_name: str = None) -> None:
147155
console_security_comment = Messages.create_console_security_alert_table(diff_report)
148156
save_sbom_file(diff_report, sbom_file_name)
157+
log.info(f"Socket Full Scan ID: {diff_report.id}")
149158
if not report_pass(diff_report):
150159
log.info("Security issues detected by Socket Security")
151160
msg = f"\n{console_security_comment}"
152161
log.info(msg)
153-
sys.exit(1)
162+
if not blocking_disabled:
163+
sys.exit(1)
154164
else:
155165
log.info("No New Security issues detected by Socket Security")
156166

@@ -159,15 +169,15 @@ def output_console_json(diff_report: Diff, sbom_file_name: str = None) -> None:
159169
console_security_comment = Messages.create_security_comment_json(diff_report)
160170
save_sbom_file(diff_report, sbom_file_name)
161171
print(json.dumps(console_security_comment))
162-
if not report_pass(diff_report):
172+
if not report_pass(diff_report) and not blocking_disabled:
163173
sys.exit(1)
164174

165175

166176
def report_pass(diff_report: Diff) -> bool:
167177
report_passed = True
168178
if len(diff_report.new_alerts) > 0:
169179
for alert in diff_report.new_alerts:
170-
alert: Alert
180+
alert: Issue
171181
if report_passed and alert.error:
172182
report_passed = False
173183
break
@@ -184,11 +194,17 @@ def cli():
184194
main_code()
185195
except KeyboardInterrupt:
186196
log.info("Keyboard Interrupt detected, exiting")
187-
sys.exit(2)
197+
if not blocking_disabled:
198+
sys.exit(2)
199+
else:
200+
sys.exit(0)
188201
except Exception as error:
189202
log.error("Unexpected error when running the cli")
190203
log.error(error)
191-
sys.exit(3)
204+
if not blocking_disabled:
205+
sys.exit(3)
206+
else:
207+
sys.exit(0)
192208

193209

194210
def main_code():
@@ -214,6 +230,10 @@ def main_code():
214230
disable_overview = arguments.disable_overview
215231
disable_security_issue = arguments.disable_security_issue
216232
ignore_commit_files = arguments.ignore_commit_files
233+
disable_blocking = arguments.disable_blocking
234+
if disable_blocking:
235+
global blocking_disabled
236+
blocking_disabled = True
217237
files = arguments.files
218238
log.info(f"Starting Socket Security Scan version {__version__}")
219239
api_token = os.getenv("SOCKET_SECURITY_API_KEY") or arguments.api_token
@@ -244,6 +264,7 @@ def main_code():
244264
is_repo = True
245265
except InvalidGitRepositoryError:
246266
is_repo = False
267+
ignore_commit_files = True
247268
pass
248269
except NoSuchPathError:
249270
raise Exception(f"Unable to find path {target_path}")
@@ -265,12 +286,15 @@ def main_code():
265286
if scm is not None:
266287
default_branch = scm.is_default_branch
267288

268-
if is_repo and files is not None and len(files) == 0 and not ignore_commit_files:
269-
no_change = True
270-
else:
271-
no_change = False
272289
base_api_url = os.getenv("BASE_API_URL") or None
273290
core = Core(token=api_token, request_timeout=6000, base_api_url=base_api_url)
291+
no_change = True
292+
if ignore_commit_files:
293+
no_change = False
294+
elif is_repo and files is not None and len(files) > 0:
295+
if len(core.match_supported_files(target_path, files)) > 0:
296+
no_change = False
297+
274298
set_as_pending_head = False
275299
if default_branch:
276300
set_as_pending_head = True
@@ -295,7 +319,9 @@ def main_code():
295319
log.info("Push initiated flow")
296320
diff: Diff
297321
diff = core.create_new_diff(target_path, params, workspace=target_path, new_files=files, no_change=no_change)
298-
if scm.check_event_type() == "diff":
322+
if no_change:
323+
log.info("No dependency changes")
324+
elif scm.check_event_type() == "diff":
299325
log.info("Starting comment logic for PR/MR event")
300326
log.debug(f"Getting comments for Repo {scm.repository} for PR {scm.pr_number}")
301327
comments = scm.get_comments_for_pr(repo, str(pr_number))
@@ -307,14 +333,24 @@ def main_code():
307333
security_comment = Messages.security_comment_template(diff)
308334
new_security_comment = True
309335
new_overview_comment = True
336+
update_old_security_comment = (
337+
security_comment is None or
338+
security_comment == "" or
339+
(len(comments) != 0 and comments.get("security") is not None)
340+
)
341+
update_old_overview_comment = (
342+
overview_comment is None or
343+
overview_comment == "" or
344+
(len(comments) != 0 and comments.get("overview") is not None)
345+
)
310346
if len(diff.new_alerts) == 0 or disable_security_issue:
311-
if security_comment is None or security_comment == "":
347+
if not update_old_security_comment:
312348
new_security_comment = False
313349
log.debug("No new alerts or security issue comment disabled")
314350
else:
315351
log.debug("Updated security comment with no new alerts")
316352
if (len(diff.new_packages) == 0 and len(diff.removed_packages) == 0) or disable_overview:
317-
if overview_comment is None or overview_comment == "":
353+
if not update_old_overview_comment:
318354
new_overview_comment = False
319355
log.debug("No new/removed packages or Dependency Overview comment disabled")
320356
else:

0 commit comments

Comments
 (0)