def get_all_integrations_commands(): """ Send API request with demisto rest api, to get all integration instances configured in the demisto. """ integration_commands_res = demisto.internalHttpRequest('GET', '/settings/integration-commands') if integration_commands_res and integration_commands_res['statusCode'] == 200: return json.loads(integration_commands_res.get('body', '{}')) demisto.debug('Did not receive expected response from Demisto API: /settings/integration-commands') return []
def get_feed_integration_errors() -> TableOrListWidget: integration_search_res = demisto.internalHttpRequest( 'POST', '/settings/integration/search', '{}', ) table = TableOrListWidget() if integration_search_res.get('statusCode') == 200: integrations = json.loads(integration_search_res.get('body', '{}')) instances = integrations.get('instances', []) enabled_instances = { instance.get('name') for instance in instances if instance.get('enabled') == 'true' } instances_health = integrations.get('health', {}) for instance in instances_health.values(): if 'feed' in (brand := instance.get('brand', '')).lower() and \ (error := instance.get('lastError', '')) and \ (instance_name := instance.get('instance')) in enabled_instances: table.add_row({ 'Brand': brand, 'Instance': instance_name, 'Instance Last Modified Time': instance.get('modified'), 'Error Information': error, })
def get_feed_integration_errors() -> TableOrListWidget: integration_search_res = demisto.internalHttpRequest( 'POST', '/settings/integration/search', '{}', ) table = TableOrListWidget() if integration_search_res.get('statusCode') == 200: integrations = json.loads(integration_search_res.get('body', '{}')) instances = integrations.get('instances', []) enabled_instances = { instance.get('name') for instance in instances if instance.get('enabled') == 'true' } instances_health = integrations.get('health', {}) for instance in instances_health.values(): if 'feed' in (brand := instance.get('brand', '')).lower() and \ (error := instance.get('lastError', '')) and \ (instance_name := instance.get('instance')) in enabled_instances: if modified := instance.get('modified', ''): modified_dt = parse(modified) assert modified_dt is not None, f'could not parse {modified}' modified = modified_dt.strftime('%Y-%m-%d %H:%M:%S%z') table.add_row({ 'Brand': brand, 'Instance': instance_name, 'Instance Last Modified Time': modified, 'Error Information': error, })
def get_open_to_do_tasks_of_current_user() -> List[Dict]: body = { "dataType": "todos", "query": "assignee:\"{me}\"", "widgetType": "table" } todo_tasks_query_res = demisto.internalHttpRequest( 'POST', '/v2/statistics/widgets/query', json.dumps(body)) table = [] if todo_tasks_query_res.get('statusCode') == 200: todo_tasks = json.loads(todo_tasks_query_res.get('body', '{}')) todo_tasks_data = todo_tasks.get('data', []) for task in todo_tasks_data: if task.get('status', '') == 'open': # table includes only open tasks title = task.get('title', '') description = task.get('description', '') task_id = task.get('id', '') incident_id = task.get('incidentId', '') clickable_incident_id = f'[{incident_id}]({os.path.join("#/Custom/caseinfoid", incident_id)})' if sla := task.get('dueDate', ''): sla_dt = parse(sla) sla = sla_dt.strftime('%Y-%m-%d %H:%M:%S%z') opened_by = task.get('dbotCreatedBy', '') table.append({ 'Task Name': title, 'Task Description': description, 'Task ID': task_id, 'SLA': sla, 'Opened By': opened_by, 'Incident ID': clickable_incident_id })
def list_used_docker_images( export_to_context: bool = True, ignore_deprecated_automations: bool = True) -> CommandResults: md = None active_docker_list_integration = {} active_docker_list_automation = {} ''' Examples for output: { 'demisto/python3:3.9.7.24076' : ['ListUsedDockerImage', 'VirusTotal',...]}''' result_dict: Dict[str, List[str]] = {} active_integration_instances = demisto.internalHttpRequest( POST_COMMAND, '/settings/integration/search', '{\"size\":500}') demisto.debug(f'response code = {0}', active_integration_instances['statusCode']) if active_integration_instances and active_integration_instances[ 'statusCode'] == 200: active_docker_list_integration = extract_dockers_from_integration_search_result( active_integration_instances['body'], False, True) active_automation = demisto.internalHttpRequest(POST_COMMAND, '/automation/search', '{\"size\":500}') demisto.debug(f'response code = {0}', active_automation['statusCode']) if active_automation and active_automation['statusCode'] == 200: active_docker_list_automation = extract_dockers_from_automation_search_result( active_automation['body'], ignore_deprecated_automations) result_dict = merge_result(active_docker_list_integration, result_dict, MAX_PER_DOCKER) result_dict = merge_result(active_docker_list_automation, result_dict, MAX_PER_DOCKER) ''' format the result for Markdown view''' result_output = [] result_output = format_result_for_markdown(result_dict) md = tableToMarkdown('Docker Images In use:', result_output, headers=['DockerImage', 'ContentItem'], headerTransform=pascalToSpace) if export_to_context: return CommandResults(outputs_prefix='UsedDockerImages', outputs_key_field='DockerImage', outputs=result_output, raw_response=result_dict, readable_output=md) else: return CommandResults(readable_output=md)
def get_all_instances(): """ Send API request with demisto rest api, to get all integration instances configured in the demisto.""" integration_search_res = demisto.internalHttpRequest('POST', '/settings/integration/search', '{\"size\":1000}') if integration_search_res and integration_search_res['statusCode'] == 200: integration_search = json.loads(integration_search_res.get('body', '{}')) return integration_search['instances'] demisto.debug('Did not receive expected response from Demisto API: /settings/integration/search') return []
def main() -> None: """main function, parses params and runs command functions :return: :rtype: """ # get pack version if is_demisto_version_ge("6.1.0"): response = demisto.internalHttpRequest( "GET", "/contentpacks/metadata/installed") packs = json.loads(response["body"]) else: packs = [] pack_version = "1.1.1" for pack in packs: if pack["name"] == "GreyNoise": pack_version = pack["currentVersion"] api_key = demisto.params().get("api_key") proxy = demisto.params().get("proxy", False) demisto.debug(f"Command being called is {demisto.command()}") try: client = Client( api_key=api_key, proxy=handle_proxy("proxy", proxy).get("https", ""), use_cache=False, integration_name=f"xsoar-community-integration-v{pack_version}", offering="community", ) if demisto.command() == "test-module": # This is the call made when pressing the integration Test button. result: Any = test_module(client) return_results(result) elif demisto.command() == "ip": result = ip_reputation_command(client, demisto.args()) return_results(result) # Log exceptions and return errors except RequestFailure: raise DemistoException(EXCEPTION_MESSAGES["UNAUTHENTICATED"]) except Exception as err: demisto.error(traceback.format_exc()) # print the traceback return_error(EXCEPTION_MESSAGES["COMMAND_FAIL"].format( demisto.command(), str(err)))