2727from compliance .utils .services .github import Github
2828from compliance .utils .test import parse_test_id
2929
30- from ibm_cloud_sdk_core .api_exception import ApiException
31- from ibm_cloud_sdk_core .authenticators import IAMAuthenticator
32-
33- from ibm_cloud_security_advisor import FindingsApiV1
34-
3530import requests
3631
3732
@@ -861,7 +856,9 @@ def _send_message(self, message, channels):
861856 retries = self ._config .get ("retries" , 3 )
862857 retry = 0
863858 while retry < retries :
864- response = requests .post (url , headers = headers , data = json .dumps (msg ))
859+ response = requests .post (
860+ url , headers = headers , data = json .dumps (msg ), timeout = 180
861+ )
865862 if response .status_code == 429 :
866863 time .sleep (int (response .headers .get ("Retry-After" , retry )) + 1 )
867864 retry += 1
@@ -1050,77 +1047,6 @@ def _resolve_alert(self, test_id, test_desc, msg, accreditation):
10501047 )
10511048
10521049
1053- class FindingsNotifier (_BaseNotifier ):
1054- """
1055- Findings notifier class.
1056-
1057- Notifications are sent using the Findings API. This notifier is
1058- configurable via :class:`compliance.config.ComplianceConfig`.
1059- """
1060-
1061- def __init__ (self , results , controls , push_error = False ):
1062- """
1063- Construct and initialize the Findings notifier object.
1064-
1065- :param results: dictionary generated by
1066- :py:class:`compliance.runners.CheckMode` at the end of the execution.
1067- :param controls: the control descriptor that manages accreditations.
1068- """
1069- super (FindingsNotifier , self ).__init__ (results , controls , push_error )
1070- self ._config = get_config ().get ("notify.findings" )
1071- self ._creds = get_config ().creds
1072- api_key = self ._creds ["findings" ].api_key
1073- authenticator = IAMAuthenticator (apikey = api_key )
1074- self .findings_api = FindingsApiV1 (authenticator = authenticator )
1075-
1076- def notify (self ):
1077- """Send notifications to the Findings API."""
1078- if self ._push_error :
1079- self .logger .error (
1080- "Remote locker push failed. Findings notifier not triggered."
1081- )
1082- return
1083- self .logger .info ("Running the Findings notifier..." )
1084- if not self ._config :
1085- self .logger .warning ("Using findings notification without config" )
1086-
1087- messages = list (self ._messages_by_accreditations ().items ())
1088- messages .sort (key = lambda x : x [0 ])
1089- for accreditation , desc in messages :
1090- if accreditation not in self ._config :
1091- continue
1092- findings_api_endpoint = self ._config [accreditation ]
1093- self .findings_api .set_service_url (findings_api_endpoint )
1094-
1095- passed , failed , warned , errored = self ._split_by_status (desc )
1096- for _ , _ , msg in failed + errored + passed + warned :
1097- self ._create_findings (msg ["body" ])
1098-
1099- def _create_findings (self , data ):
1100- occurrence_list = data ["occurrence_list" ]
1101- account_id = data ["account_id" ]
1102- provider_id = data ["provider_id" ]
1103- status = 0
1104-
1105- for occurrence in occurrence_list :
1106- try :
1107- response = self .findings_api .create_occurrence (
1108- account_id = account_id , provider_id = provider_id , ** occurrence
1109- )
1110- self .logger .info (response .status_code )
1111- except ApiException as e :
1112- status = e .code
1113- self .logger .error (
1114- "Finding creation failed "
1115- f'for occurrence id { occurrence ["id" ]} '
1116- f"with { str (e .code )} : { str (e )} "
1117- )
1118- except Exception as e :
1119- status = - 1
1120- self .logger .error (f"Unexpected error occurred: { str (e )} " )
1121- return status
1122-
1123-
11241050def get_notifiers ():
11251051 """
11261052 Provide a dictionary of all notifier class objects.
@@ -1137,5 +1063,4 @@ def get_notifiers():
11371063 "pagerduty" : PagerDutyNotifier ,
11381064 "gh_issues" : GHIssuesNotifier ,
11391065 "locker" : LockerNotifier ,
1140- "findings" : FindingsNotifier ,
11411066 }
0 commit comments