Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def new_ring(self, name, parentring=None, **kw):
'Create a new ring (or return a pre-existing one), and put it in the ring index'
ring = self.node_new(CMAdb.NODE_ring, name, unique=True, **kw)
if parentring is not None:
self.db.relate((ring, CMAdb.REL_parentring, parentring.node),)
return ring
@staticmethod
def initglobal(io, cleanoutdb=False):
CMAdb.io = io
CMAdb.cdb = CMAdb()
if cleanoutdb:
#print >>sys.stderr, 'Re-initializing the database'
CMAdb.cdb.delete_all()
CMAdb.cdb = CMAdb()
CMAdb.TheOneRing = HbRing('The_One_Ring', HbRing.THEONERING)
'''Create a new (ip, port) object related to some IPaddr object'''
port = jsonobj['port']
table = {}
for key in jsonobj.keys():
type = jsonobj.gettype(key)
if not (type == CFG_BOOL or type == CFG_INT64 or type == CFG_STRING
or type == CFG_FLOAT or type == CFG_ARRAY):
continue
table[key] = jsonobj[key]
tcpipport = self.node_new(CMAdb.NODE_tcpipport, name, unique=True, **table)
## FIXME? Should I make this relationship a REL_baseip + ':' + port type?
if isserver:
args = (
(tcpipport, CMAdb.REL_baseip, ipaddrnode, {'port':port}),
(tcpipport, CMAdb.REL_tcpservice, ipproc),
(tcpipport, CMAdb.REL_ipphost, dronenode))
else:
args = (
(tcpipport, CMAdb.REL_baseip, ipaddrnode, {'port':port}),
(ipproc, CMAdb.REL_tcpclient, tcpipport))
CMAdb.cdb.db.relate(*args)
def new_ipproc(self, ##< Self... The usual self object
name, ##< What should we be called? (no index)
jsonobj, ##< The JSON ConfigContext object for us alone...
drone): ##< Drone we are running on
'''Create a new ipproc object from its JSON discovery data'''
table = {}
for key in jsonobj.keys():
type = jsonobj.gettype(key)
if not (type == CFG_BOOL or type == CFG_INT64 or type == CFG_STRING
or type == CFG_FLOAT or type == CFG_ARRAY):
continue
if jsonobj[key] is None: continue
# We assume any arrays are of same-typed simple objects (presumably Strings)
# This is a reasonable assumption for our process discovery data
table[key] = jsonobj[key]
ipproc = self.node_new(CMAdb.NODE_ipproc, name, unique=False, **table)
self.db.relate((ipproc, CMAdb.REL_runningon, drone),)
return ipproc
attrs = {}
thisport = ports[portname]
for key in thisport.keys():
value = thisport[key]
if isinstance(value, pyNetAddr):
value = str(value)
attrs[key] = value
if 'sourceMAC' in thisport:
nicmac = thisport['sourceMAC']
else:
nicmac = ChassisId # Hope that works ;-)
nicnode = CMAdb.cdb.new_nic(portname, nicmac, switch, **attrs)
try:
assert thisport['ConnectsToHost'] == self.node['name']
matchnic = thisport['ConnectsToInterface']
niclist = self.node.get_related_nodes(neo4j.Direction.INCOMING, CMAdb.REL_nicowner)
for dronenic in niclist:
if dronenic['nicname'] == matchnic:
nicnode.create_relationship_from(dronenic, CMAdb.REL_wiredto)
break
except KeyError:
print 'OOPS! got an exception...'
pass
def add_tcpipports(self, isserver, jsonobj, ipproc, allourips):
'''We create tcpipports objects that correspond to the given json object in
the context of the set of IP addresses that we support - including support
for the ANY ipv4 and ipv6 addresses'''
addr = str(jsonobj['addr'])
port = jsonobj['port']
name = addr + ':' + str(port)
# Were we given the ANY address?
if isserver and (addr == '0.0.0.0' or addr == '::'):
for ipaddr in allourips:
name = ipaddr['name'] + ':' + str(port)
tcpipport = CMAdb.cdb.new_tcpipport(name, isserver, jsonobj, self.node, ipproc, ipaddr)
elif isserver:
for ipaddr in allourips:
if ipaddr['name'] == addr:
CMAdb.cdb.new_tcpipport(name, isserver, jsonobj, self.node, ipproc, ipaddr)
return
raise ValueError('IP Address mismatch for Drone %s - could not find address %s'
% (self.node['name'], addr))
else:
name = addr + ':' + str(port)
ipaddr = CMAdb.cdb.new_IPaddr(None, addr)
CMAdb.cdb.new_tcpipport(name, isserver, jsonobj, None, ipproc, ipaddr)
def dispatch(self, origaddr, frameset):
fstype = frameset.get_framesettype()
if CMAdb.debug:
print >>sys.stderr,"DispatchJSDISCOVERY: received [%s] FrameSet from [%s]" \
% (FrameSetTypes.get(fstype)[0], repr(origaddr))
sysname = None
for frame in frameset.iter():
frametype=frame.frametype()
if frametype == FrameTypes.HOSTNAME:
sysname = frame.getstr()
if frametype == FrameTypes.JSDISCOVER:
json = frame.getstr()
#print 'JSON received: ', json
if sysname is None:
jsonconfig = pyConfigContext(init=json)
if not jsonconfig:
print 'BAD JSON [%s]' % json
return
sysname = jsonconfig.getstring('host')
def new_IPaddr(self, nic, ipaddr, **kw):
'''Create a new IP address (or return a pre-existing one), and point it at its parent
NIC and its grandparent drone'''
#print 'Adding IP address %s' % (ipaddr)
ipaddrs = self.ipindex.get(CMAdb.NODE_ipaddr, ipaddr)
if nic is not None:
for ip in ipaddrs:
if ip.is_related_to(nic, neo4j.Direction.OUTGOING, CMAdb.REL_ipowner):
#print 'Found this IP address (%s) ipowner-related to NIC %s' % (ipaddr, nic)
return ip
if len(ipaddrs) == 0:
ip = self.node_new(CMAdb.NODE_ipaddr, ipaddr, unique=False, **kw)
else:
ip = ipaddrs[0] # May have been created by a client - pick the first one...
if nic is not None:
ip.create_relationship_to(nic, CMAdb.REL_ipowner)
drone = nic.get_single_related_node(neo4j.Direction.OUTGOING, CMAdb.REL_nicowner)
ip.create_relationship_to(drone, CMAdb.REL_iphost)
return ip
if CMAdb.debug:
print 'Neo4j version: %s' % str(self.dbversion)
#
# Make sure all our indexes are present and that we
# have a top level node for each node type for creating
# IS_A relationships to. Not sure if the IS_A relationships
# are really needed, but they're kinda cool...
#
nodetypes = {
CMAdb.NODE_ring: True
, CMAdb.NODE_drone: True
, CMAdb.NODE_switch: True
, CMAdb.NODE_NIC: True # NICs are indexed by MAC address
# MAC addresses are not always unique...
, CMAdb.NODE_ipaddr: True # Note that IPaddrs also might not be unique
, CMAdb.NODE_tcpipport: True # We index IP and port - handy to have...
, CMAdb.NODE_ipproc: False
}
indices = [key for key in nodetypes.keys() if nodetypes[key]]
self.indextbl = {}
self.nodetypetbl = {}
for index in indices:
#print >>sys.stderr, ('Ensuring index %s exists' % index)
self.indextbl[index] = self.db.get_or_create_index(neo4j.Node, index)
#print >>sys.stderr, ('Ensuring index %s exists' % 'nodetype')
self.indextbl['nodetype'] = self.db.get_or_create_index(neo4j.Node, 'nodetype')
nodetypeindex = self.indextbl['nodetype']
nodezero = self.db.get_node(0)
for index in nodetypes.keys():
top = nodetypeindex.get_or_create('nodetype', index
, {'name':index, 'nodetype':'nodetype'})
def new_ring(self, name, parentring=None, **kw):
'Create a new ring (or return a pre-existing one), and put it in the ring index'
ring = self.node_new(CMAdb.NODE_ring, name, unique=True, **kw)
if parentring is not None:
self.db.relate((ring, CMAdb.REL_parentring, parentring.node),)
return ring