Improved Bluecat plugin search results

This commit is contained in:
Daniel Dayley 2020-06-19 14:52:26 -06:00
parent 6df5d36da0
commit 8fd7c9d6db

View File

@ -19,21 +19,32 @@ class ServiceDelegate(ServiceBase) :
return
self._host = self._config['host']
self._current_auth_header = self.get_bc_auth_header(self._config['host'],self._config['username'],self._config['key'])
self.debug('Searching BlueCat for hosts...',1)
self.debug('Logged into BlueCat',1)
def lookup(self,subject) :
self._return_payload = {}
if self._current_auth_header :
object = self.search_bc_object(subject)
if object :
self._return_payload.update({'object':object})
parent = self.search_bc_parent_object(subject)
if parent :
self._return_payload.update({'parent':parent})
if len(self._return_payload) > 0 :
return self._return_payload
else :
return None
objects = self.search_bc_objects(subject)
if objects :
if type(objects) is list :
object_list = []
for object in objects :
result = {'object':object}
parent = self.search_bc_parent_object(object)
if parent :
result.update({'parent':parent})
object_list.append(result)
if len(object_list) > 0 :
return object_list
else :
self._return_payload.update({'object':objects})
parent = self.search_bc_parent_object(subject)
if parent :
self._return_payload.update({'parent':parent})
if len(self._return_payload) > 0 :
return self._return_payload
else :
return None
else :
return None
@ -51,19 +62,19 @@ class ServiceDelegate(ServiceBase) :
else :
return None
def search_bc_object(self,subject) :
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)
return self.clean_response(response)
def search_bc_parent_object(self,subject) :
def search_bc_parent_object(self,object) :
"""Search for and return information about the objects parent, if any"""
if 'object' in self._return_payload and type(self._return_payload['object']) is dict and 'id' in self._return_payload['object'] :
response = requests.get('https://' + self._host + '/Services/REST/v1/getParent?entityId=' + str(self._return_payload['object']['id']), headers=self._current_auth_header)
if type(object) is dict and 'id' in object :
response = requests.get('https://' + self._host + '/Services/REST/v1/getParent?entityId=' + str(object['id']), headers=self._current_auth_header)
return self.clean_response(response)
# Make a guess that it belongs to a static net that hasn't been put in bluecat
elif subject != '' :
elif object != '' :
for net in ['/27', '/26', '/25', '/24', '/23', '/22'] :
try :
ip = ipaddress.IPv4Network(subject + net,strict=False)
@ -76,20 +87,24 @@ class ServiceDelegate(ServiceBase) :
def clean_response(self,response) :
def clean(item) :
if 'properties' in item :
item['properties'] = item['properties'][:-1]
bad_delim_props = [x for x in item['properties'].split('|') if x.count('=') > 1]
if len(bad_delim_props) > 0 :
item.update({'unknown':bad_delim_props})
attributes = dict(x.split('=') for x in item['properties'].split('|') if x.count('=') == 1)
item.pop('properties', None)
item.update(attributes)
return item
parsed = {}
try :
parsed = json.loads(response.text)
except Exception :
return parsed
if type(parsed) is list and len(parsed) > 0 :
parsed = parsed[0]
if type(parsed) is dict :
if 'properties' in parsed :
parsed['properties'] = parsed['properties'][:-1]
bad_delim_props = [x for x in parsed['properties'].split('|') if x.count('=') > 1]
if len(bad_delim_props) > 0 :
parsed.update({'unknown':bad_delim_props})
attributes = dict(x.split('=') for x in parsed['properties'].split('|') if x.count('=') == 1)
parsed.pop('properties', None)
parsed.update(attributes)
for item in parsed:
item = clean(item)
return parsed
if type(parsed) is dict :
return clean(parsed)