Skip to content

Commit 0a048b3

Browse files
authored
Added support for Warn & Monitor policy modes (#13)
1 parent c5444cb commit 0a048b3

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

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__ = '0.0.99'
2+
__version__ = '1.0.0'

socketsecurity/core/__init__.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,30 +637,16 @@ def compare_issue_alerts(new_scan_alerts: dict, head_scan_alerts: dict, alerts:
637637
if alert_key not in head_scan_alerts:
638638
new_alerts = new_scan_alerts[alert_key]
639639
for alert in new_alerts:
640-
if Core.is_error(alert):
640+
if alert.error or alert.warn:
641641
alerts.append(alert)
642642
else:
643643
new_alerts = new_scan_alerts[alert_key]
644644
head_alerts = head_scan_alerts[alert_key]
645645
for alert in new_alerts:
646-
if alert not in head_alerts and Core.is_error(alert):
646+
if alert not in head_alerts and (alert.error or alert.warn):
647647
alerts.append(alert)
648648
return alerts
649649

650-
@staticmethod
651-
def is_error(alert: Alert):
652-
"""
653-
Compare the current alert against the Security Policy to determine if it should be included. Can be overridden
654-
with all_new_alerts Global setting if desired to return all alerts and not just the error category from the
655-
security policy.
656-
:param alert:
657-
:return:
658-
"""
659-
if all_new_alerts or (alert.type in security_policy and security_policy[alert.type]['action'] == "error"):
660-
return True
661-
else:
662-
return False
663-
664650
@staticmethod
665651
def create_issue_alerts(package: Package, alerts: dict, packages: dict) -> dict:
666652
"""
@@ -704,6 +690,9 @@ def create_issue_alerts(package: Package, alerts: dict, packages: dict) -> dict:
704690
purl=package.purl,
705691
url=package.url
706692
)
693+
if alert.type in security_policy:
694+
action = security_policy[alert.type]['action']
695+
setattr(issue_alert, action, True)
707696
if issue_alert.key not in alerts:
708697
alerts[issue_alert.key] = [issue_alert]
709698
else:

socketsecurity/core/classes.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ class Issue:
140140
pkg_id: str
141141
props: dict
142142
key: str
143-
is_error: bool
143+
error: bool
144+
warn: bool
145+
ignore: bool
146+
monitor: bool
144147
description: str
145148
title: str
146149
emoji: str
@@ -162,6 +165,14 @@ def __init__(self, **kwargs):
162165
self.introduced_by = []
163166
if not hasattr(self, "manifests"):
164167
self.manifests = ""
168+
if not hasattr(self, "error"):
169+
self.error = False
170+
if not hasattr(self, "warn"):
171+
self.warn = False
172+
if not hasattr(self, "monitor"):
173+
self.monitor = False
174+
if not hasattr(self, "ignore"):
175+
self.ignore = False
165176

166177
def __str__(self):
167178
return json.dumps(self.__dict__)

socketsecurity/core/messages.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ class Messages:
99

1010
@staticmethod
1111
def create_security_comment_json(diff: Diff) -> dict:
12+
scan_failed = False
1213
if len(diff.new_alerts) == 0:
13-
scan_failed = False
14-
else:
15-
scan_failed = True
14+
for alert in diff.new_alerts:
15+
alert: Issue
16+
if alert.error:
17+
scan_failed = True
18+
break
1619
output = {
1720
"scan_failed": scan_failed,
1821
"new_alerts": []
@@ -22,7 +25,6 @@ def create_security_comment_json(diff: Diff) -> dict:
2225
output["new_alerts"].append(json.loads(str(alert)))
2326
return output
2427

25-
2628
@staticmethod
2729
def security_comment_template(diff: Diff) -> str:
2830
"""
@@ -130,7 +132,8 @@ def create_security_alert_table(diff: Diff, md: MdUtils) -> (MdUtils, list, dict
130132
"Alert",
131133
"Package",
132134
"Introduced by",
133-
"Manifest File"
135+
"Manifest File",
136+
"CI"
134137
]
135138
num_of_alert_columns = len(alert_table)
136139
next_steps = {}
@@ -147,11 +150,16 @@ def create_security_alert_table(diff: Diff, md: MdUtils) -> (MdUtils, list, dict
147150
ignore_commands.append(ignore)
148151
manifest_str, sources = Messages.create_sources(alert, "console")
149152
purl_url = f"[{alert.purl}]({alert.url})"
153+
if alert.error:
154+
emoji = ':no_entry_sign:'
155+
else:
156+
emoji = ':warning:'
150157
row = [
151158
alert.title,
152159
purl_url,
153160
", ".join(sources),
154-
manifest_str
161+
manifest_str,
162+
emoji
155163
]
156164
if row not in alert_table:
157165
alert_table.extend(row)
@@ -262,17 +270,27 @@ def create_console_security_alert_table(diff: Diff) -> PrettyTable:
262270
"Alert",
263271
"Package",
264272
"Introduced by",
265-
"Manifest File"
273+
"Manifest File",
274+
"CI Status"
266275
]
267276
)
268277
for alert in diff.new_alerts:
269278
alert: Issue
270279
manifest_str, sources = Messages.create_sources(alert, "console")
280+
if alert.error:
281+
state = "block"
282+
elif alert.warn:
283+
state = "warn"
284+
elif alert.monitor:
285+
state = "monitor"
286+
else:
287+
state = "ignore"
271288
row = [
272289
alert.title,
273290
alert.url,
274291
", ".join(sources),
275-
manifest_str
292+
manifest_str,
293+
state
276294
]
277295
alert_table.add_row(row)
278296
return alert_table

socketsecurity/socketcli.py

Lines changed: 17 additions & 4 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
4+
from socketsecurity.core.classes import FullScanParams, Diff, Package, Alert
55
from socketsecurity.core.messages import Messages
66
from socketsecurity.core.scm_comments import Comments
77
from socketsecurity.core.git_interface import Git
@@ -146,9 +146,10 @@
146146
def output_console_comments(diff_report: Diff, sbom_file_name: str = None) -> None:
147147
console_security_comment = Messages.create_console_security_alert_table(diff_report)
148148
save_sbom_file(diff_report, sbom_file_name)
149-
if len(diff_report.new_alerts) > 0:
149+
if not report_pass(diff_report):
150150
log.info("Security issues detected by Socket Security")
151-
log.info(console_security_comment)
151+
msg = f"\n{console_security_comment}"
152+
log.info(msg)
152153
sys.exit(1)
153154
else:
154155
log.info("No New Security issues detected by Socket Security")
@@ -158,14 +159,26 @@ def output_console_json(diff_report: Diff, sbom_file_name: str = None) -> None:
158159
console_security_comment = Messages.create_security_comment_json(diff_report)
159160
save_sbom_file(diff_report, sbom_file_name)
160161
print(json.dumps(console_security_comment))
161-
if len(diff_report.new_alerts) > 0:
162+
if not report_pass(diff_report):
162163
sys.exit(1)
163164

164165

166+
def report_pass(diff_report: Diff) -> bool:
167+
report_passed = True
168+
if len(diff_report.new_alerts) > 0:
169+
for alert in diff_report.new_alerts:
170+
alert: Alert
171+
if report_passed and alert.error:
172+
report_passed = False
173+
break
174+
return report_passed
175+
176+
165177
def save_sbom_file(diff_report: Diff, sbom_file_name: str = None):
166178
if diff_report is not None and sbom_file_name is not None:
167179
Core.save_file(sbom_file_name, json.dumps(Core.create_sbom_output(diff_report)))
168180

181+
169182
def cli():
170183
try:
171184
main_code()

0 commit comments

Comments
 (0)