Added expanded search to Bluecat. Search result count no longer limited but controlled by config variables.

This commit is contained in:
Daniel Dayley 2020-09-24 13:07:47 -06:00
parent d302d437fb
commit 47d025a594

View File

@ -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) :