def send_indicators_to_threat_intelligence()

in Solutions/Infoblox/Data Connectors/InfobloxCloudDataConnector/SharedCode/utils.py [0:0]


    def send_indicators_to_threat_intelligence(self, indicator_list):
        """Create indicators in sentinel workspace thereat intelligence section.

        Return response in json formate if status code is in between [200, 299]

        Args:
            url (String): URL of the rest call.
            method (String): HTTP method of rest call. Eg. "GET", etc.
            headers (Dict, optional): headers. Defaults to None.
            params (Dict, optional): parameters. Defaults to None.
            payload (Type : As required by the rest call, optional): body. Defaults to None.

        Returns:
            response : response of the rest call.
        """
        __method_name = inspect.currentframe().f_code.co_name
        try:
            for i in range(consts.MAX_RETRIES):
                upload_indicator_url = consts.UPLOAD_SENTINEL_INDICATORS_URL.format(consts.WORKSPACE_ID)
                applogger.debug(
                    self.log_format.format(
                        consts.LOGS_STARTS_WITH,
                        __method_name,
                        self.azure_function_name,
                        "Calling url: {}".format(upload_indicator_url),
                    )
                )
                body = {
                    "sourcesystem": "Infoblox-TIDE-Threats-Custom",
                    "value": indicator_list,
                }
                try:
                    response = requests.post(
                        url=upload_indicator_url,
                        headers=self.headers,
                        data=json.dumps(body, ensure_ascii=False),
                    )
                except requests.ConnectionError as error:
                    applogger.error(
                        self.log_format.format(
                            consts.LOGS_STARTS_WITH,
                            __method_name,
                            self.azure_function_name,
                            "ConnectionError error : Retry = {} : Error-{}".format(i, error),
                        )
                    )
                    time.sleep(randrange(1, 10))
                    continue

                if response.status_code >= 200 and response.status_code <= 299:
                    applogger.info(
                        self.log_format.format(
                            consts.LOGS_STARTS_WITH,
                            __method_name,
                            self.azure_function_name,
                            "Rest Call Completed, Status code : {}".format(response.status_code),
                        )
                    )
                    response_json = response.json()
                    return response_json
                elif response.status_code == 401:
                    applogger.error(
                        self.log_format.format(
                            consts.LOGS_STARTS_WITH,
                            __method_name,
                            self.azure_function_name,
                            "Unauthorized, Status code : {}, Generating new access token, Retry = {}".format(
                                response.status_code, i
                            ),
                        )
                    )
                    self.auth_sentinel()
                    continue
                elif response.status_code == 429:
                    message = response.json().get("message", "")
                    match = re.search(r"Try again in (\d+) seconds", message)
                    seconds = (int(match.group(1)) + 2) if match else consts.SLEEP_TIME
                    applogger.error(
                        self.log_format.format(
                            consts.LOGS_STARTS_WITH,
                            __method_name,
                            self.azure_function_name,
                            "Too many requests, Status code : {}, Retry {}, After {} seconds".format(
                                response.status_code, i, seconds
                            ),
                        )
                    )
                    time.sleep(seconds)
                    continue
                else:
                    applogger.error(
                        self.log_format.format(
                            consts.LOGS_STARTS_WITH,
                            __method_name,
                            self.azure_function_name,
                            "Error while creating indicators, Status code: {}, Error-{}".format(
                                response.status_code, response.content
                            ),
                        )
                    )
                    raise InfobloxException()
            applogger.error(
                self.log_format.format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    self.azure_function_name,
                    "Max retries exceeded.",
                )
            )
            raise InfobloxException()
        except requests.HTTPError as error:
            applogger.error(
                self.log_format.format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    self.azure_function_name,
                    consts.HTTP_ERROR_MSG.format(error),
                )
            )
            raise InfobloxException()
        except requests.RequestException as error:
            applogger.error(
                self.log_format.format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    self.azure_function_name,
                    "Request error : Error-{}".format(error),
                )
            )
            raise InfobloxException()
        except Exception as error:
            applogger.error(
                self.log_format.format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    self.azure_function_name,
                    consts.UNEXPECTED_ERROR_MSG.format(error),
                )
            )
            raise InfobloxException()