From 47d025a594ff6b67f7d551d49e973feec1c636c2 Mon Sep 17 00:00:00 2001 From: Daniel Dayley Date: Thu, 24 Sep 2020 13:07:47 -0600 Subject: [PATCH] Added expanded search to Bluecat. Search result count no longer limited but controlled by config variables. --- ifxlookup/plugins/bluecat.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/ifxlookup/plugins/bluecat.py b/ifxlookup/plugins/bluecat.py index b035790..0437316 100644 --- a/ifxlookup/plugins/bluecat.py +++ b/ifxlookup/plugins/bluecat.py @@ -3,9 +3,12 @@ import ipaddress import requests import json +PAGE_SIZE = 500 +MAX_RESULTS = 1000 + class ServiceDelegate(ServiceBase) : - current_auth_header = None + _current_auth_header = None _return_payload = {} _host = None @@ -13,21 +16,25 @@ class ServiceDelegate(ServiceBase) : return ['-b', '--bluecat', 'store_true', 'Return network information about the subject (bluecat)'] def startup(self) : - for requirement in ['host','username','key'] : + for requirement in ['host','username','key','page_size','max_results'] : if requirement not in self._config or (requirement in self._config and (self._config[requirement] == '' or type(self._config[requirement]) != str)): self._error.append('Missing required config option ' + requirement) return self._host = self._config['host'] + PAGE_SIZE = int(self._config['page_size']) + MAX_RESULTS = int(self._config['max_results']) self._current_auth_header = self.get_bc_auth_header(self._config['host'],self._config['username'],self._config['key']) self.debug('Logged into Bluecat',1) def lookup(self,subject) : self._return_payload = {} if self._current_auth_header : + self.debug('Getting object results from Bluecat...',1) objects = self.search_bc_objects(subject) if objects : if type(objects) == list : object_list = [] + self.debug('Getting parent objects from Bluecat...',1) for object in objects : result = {'object':object} parent = self.search_bc_parent_object(object) @@ -67,7 +74,21 @@ class ServiceDelegate(ServiceBase) : def search_bc_objects(self,subject) : """Searches BC for the subject.""" # Search by IP - response = requests.get('https://' + self._host + '/Services/REST/v1/searchByObjectTypes?keyword=' + subject + '&types=IP4Address,IP6Address&start=0&count=10',headers=self._current_auth_header) + start = 0 + stop = False + result = [] + while not stop and (start + PAGE_SIZE < MAX_RESULTS) : + page = self.get_bc_result_page(subject,start,start+PAGE_SIZE) + self.debug('Got results ' + str(start + 1) + ' through ' + str(start + PAGE_SIZE) + ' from Bluecat',3) + if not page or page == [] : + stop = True + else : + start += PAGE_SIZE + result += page + return result + + def get_bc_result_page(self,subject,start,count) : + response = requests.get('https://' + self._host + '/Services/REST/v1/searchByObjectTypes?keyword=' + subject + '&types=IP4Address,IP6Address&start=' + str(start) + '&count=' + str(count),headers=self._current_auth_header) return self.clean_response(response) def search_bc_parent_object(self,object) :