1
1
import argparse
2
2
import json
3
3
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
5
5
from socketsecurity .core .messages import Messages
6
6
from socketsecurity .core .scm_comments import Comments
7
7
from socketsecurity .core .git_interface import Git
12
12
13
13
logging .basicConfig (level = logging .INFO )
14
14
log = logging .getLogger ("socketcli" )
15
+ blocking_disabled = False
15
16
16
17
parser = argparse .ArgumentParser (
17
18
prog = "socketcli" ,
142
143
default = False
143
144
)
144
145
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
+
145
153
146
154
def output_console_comments (diff_report : Diff , sbom_file_name : str = None ) -> None :
147
155
console_security_comment = Messages .create_console_security_alert_table (diff_report )
148
156
save_sbom_file (diff_report , sbom_file_name )
157
+ log .info (f"Socket Full Scan ID: { diff_report .id } " )
149
158
if not report_pass (diff_report ):
150
159
log .info ("Security issues detected by Socket Security" )
151
160
msg = f"\n { console_security_comment } "
152
161
log .info (msg )
153
- sys .exit (1 )
162
+ if not blocking_disabled :
163
+ sys .exit (1 )
154
164
else :
155
165
log .info ("No New Security issues detected by Socket Security" )
156
166
@@ -159,15 +169,15 @@ def output_console_json(diff_report: Diff, sbom_file_name: str = None) -> None:
159
169
console_security_comment = Messages .create_security_comment_json (diff_report )
160
170
save_sbom_file (diff_report , sbom_file_name )
161
171
print (json .dumps (console_security_comment ))
162
- if not report_pass (diff_report ):
172
+ if not report_pass (diff_report ) and not blocking_disabled :
163
173
sys .exit (1 )
164
174
165
175
166
176
def report_pass (diff_report : Diff ) -> bool :
167
177
report_passed = True
168
178
if len (diff_report .new_alerts ) > 0 :
169
179
for alert in diff_report .new_alerts :
170
- alert : Alert
180
+ alert : Issue
171
181
if report_passed and alert .error :
172
182
report_passed = False
173
183
break
@@ -184,11 +194,17 @@ def cli():
184
194
main_code ()
185
195
except KeyboardInterrupt :
186
196
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 )
188
201
except Exception as error :
189
202
log .error ("Unexpected error when running the cli" )
190
203
log .error (error )
191
- sys .exit (3 )
204
+ if not blocking_disabled :
205
+ sys .exit (3 )
206
+ else :
207
+ sys .exit (0 )
192
208
193
209
194
210
def main_code ():
@@ -214,6 +230,10 @@ def main_code():
214
230
disable_overview = arguments .disable_overview
215
231
disable_security_issue = arguments .disable_security_issue
216
232
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
217
237
files = arguments .files
218
238
log .info (f"Starting Socket Security Scan version { __version__ } " )
219
239
api_token = os .getenv ("SOCKET_SECURITY_API_KEY" ) or arguments .api_token
@@ -244,6 +264,7 @@ def main_code():
244
264
is_repo = True
245
265
except InvalidGitRepositoryError :
246
266
is_repo = False
267
+ ignore_commit_files = True
247
268
pass
248
269
except NoSuchPathError :
249
270
raise Exception (f"Unable to find path { target_path } " )
@@ -265,12 +286,15 @@ def main_code():
265
286
if scm is not None :
266
287
default_branch = scm .is_default_branch
267
288
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
272
289
base_api_url = os .getenv ("BASE_API_URL" ) or None
273
290
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
+
274
298
set_as_pending_head = False
275
299
if default_branch :
276
300
set_as_pending_head = True
@@ -295,7 +319,9 @@ def main_code():
295
319
log .info ("Push initiated flow" )
296
320
diff : Diff
297
321
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" :
299
325
log .info ("Starting comment logic for PR/MR event" )
300
326
log .debug (f"Getting comments for Repo { scm .repository } for PR { scm .pr_number } " )
301
327
comments = scm .get_comments_for_pr (repo , str (pr_number ))
@@ -307,14 +333,24 @@ def main_code():
307
333
security_comment = Messages .security_comment_template (diff )
308
334
new_security_comment = True
309
335
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
+ )
310
346
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 :
312
348
new_security_comment = False
313
349
log .debug ("No new alerts or security issue comment disabled" )
314
350
else :
315
351
log .debug ("Updated security comment with no new alerts" )
316
352
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 :
318
354
new_overview_comment = False
319
355
log .debug ("No new/removed packages or Dependency Overview comment disabled" )
320
356
else :
0 commit comments