Initial Support for BlueCat plugin

This commit is contained in:
Daniel Dayley 2019-12-05 09:52:32 -07:00
parent c181e3db41
commit 3530dc965c
2 changed files with 65 additions and 16 deletions

View File

@ -148,9 +148,9 @@ if __name__ == "__main__":
# Gather Argument options
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', action='store', help="Specify a config file (~/.config/ifxtool.yml by default)")
parser.add_argument('-j', '--json', action='store_true', help="Return results as a json object")
parser.add_argument('-l', '--link', action='store_true', help="Return physical link information about the subject")
parser.add_argument('-c', '--config', action='store', help="Specify a config file (~/.config/ifxtool.yml by default)")
for delegate in delegates :
arguments = delegate.get_arguments(delegate)
if arguments and len(arguments) == 4 :

View File

@ -1,24 +1,73 @@
from servicebase import ServiceBase
import requests
import json
class ServiceDelegate(ServiceBase) :
current_auth_header = None
return_payload = {}
host = None
def get_arguments(cls) :
"""Returns an array of information used to construct an argumentparser argument."""
# [ <short flag>,<unix flag>,<arg type>,<description> ]
# Example return: [ '-n', '--net', 'store_true', "Return network information about the subject" ]
return ['-n', '--net', 'store_true', "Return network information about the subject"]
def get_bc_auth_header(host,username,password) :
"""Given authentication information, return the authenticated header for use in REST requests to a bluecat device."""
pass
def startup(self) :
for requirement in ['host','username','key'] :
if requirement not in self.config or (requirement in self.config and (self.config[requirement] is '' or type(self.config[requirement]) is not str)):
self.return_payload.update({'error':'Missing required config option ' + requirement})
return
self.host = self.config['host']
self.current_auth_header = self.get_bc_auth_header(self.config['host'],self.config['username'],self.config['key'])
def get_bc_parent_object_id(bc_id) :
"""Given a Bluecat Object ID, return the Object ID of the parent."""
pass
def perform_lookup(self,host_tuple) :
self.return_payload = {}
if self.current_auth_header :
object = self.search_bc_object(host_tuple)
self.return_payload.update({'object':object})
parent = self.search_bc_parent_object(host_tuple)
self.return_payload.update({'parent':parent})
return self.return_payload
def get_bc_onject_name(bc_id) :
"""Given a Bluecat Object ID, return the name of the object."""
pass
def host_to_ip(hostname) :
"""Given a hostname, return the IP address of the hostname."""
pass
def shutdown(self) :
if self.host and self.current_auth_header :
logout = requests.get("https://" + self.host + "/Services/REST/v1/logout",headers=self.current_auth_header)
if not logout.text.endswith("successfully logged out.\"") :
self.return_payload.update({'error':'Unable to log out of BlueCat API session'})
def get_bc_auth_header(self,host,username,password) :
auth_token = requests.get("https://" + host + "/Services/REST/v1/login?username=" + username + "&password=" + password).text[17:71]
if auth_token.startswith('BAMAuthToken') :
return { "Authorization" : auth_token }
else :
return None
def search_bc_object(self,host_tuple) :
"""Searches BC for the subject."""
response = requests.get("https://" + self.host + "/Services/REST/v1/searchByObjectTypes?keyword=" + host_tuple[2][0] + "&types=IP4Address,IP6Address&start=0&count=10",headers=self.current_auth_header)
return self.clean_response(response)
def search_bc_parent_object(self,host_tuple) :
"""Search for and return information about the objects parent, if any"""
if 'object' in self.return_payload 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)
return self.clean_response(response)
def clean_response(self,response) :
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)
return parsed