def save_object(self, **kwargs): """ Add the contents of a policy object to the database. """ print '\n\n Save object \n\n' try: content = escape_string(kwargs['object-content']) object_id = int(kwargs['object-id']) type = kwargs['object-type'] item_id = kwargs['policy-id'] if object_id == 0: policy_item = Session.query(PolicyChain).filter( PolicyChain.id == item_id).one() policy_object = PolicyObject(type, content) Session.add(policy_object) Session.flush() policy_item.policyobject_id = policy_object.id else: policy_object= Session.query(PolicyObject).filter(PolicyObject.id == object_id).one() policy_object.contents = content policy_object.type = type Session.merge(policy_object) Session.flush() except KeyError as e: print e except NoResultFound as e: #policy_object = PolicyObject(type, content) #Session.add(policy_object) #Session.flush() print 'no result found' pass
def add_object(self, **kwargs): """ Creates a new policy object. """ try: if kwargs['object_id'] is None or not kwargs['object_id'].isdigit(): raise TypeError('Policy ID is either None or ID is not int') if kwargs ['object_content'] is None: raise TypeError('Policy content None') except KeyError as e: return 'Key %s does not exist' % e except TypeError as e: return '<p>An error occured</p><p><b>Errorinfo:</b><br/>%s' % e policy_id = kwargs['object_id'] try: policy_object = Session.query(PolicyObject).filter(PolicyObject.id == policy_id).one() policy_object.contents = escape_string(kwargs['object_content']) Session.merge(policy_object) Session.flush() except NoResultFound: policy_object = PolicyObject(contents = escape_string(kwargs['object_content'])) Session.add(policy_object) Session.flush() finally: raise cherrypy.HTTPRedirect("/policy/")
def save_sensor_data(self, **kwargs): """ Get a sensors data provided bu a form, and either change it or add new. """ try: """ Retrieves information from web request. """ addName = escape_string(kwargs['sensor_name']) addIp = escape_string(kwargs['sensor_ip']) addLocation = kwargs['sensor_location'] addDescription = escape_string(kwargs['sensor_description']) sensor_id = kwargs['sensor_id'] except KeyError as e: print 'There was a key error %s' % (e) try: """ In case sensor does not excist. """ if sensor_id == 0: raise NoResultFound sensor = Session.query(Sensor).filter(Sensor.id == sensor_id).one() sensor.name = addName sensor.ip = addIp sensor.description = addDescription Session.merge(sensor) except NoResultFound: # Sensor does not exsist, add new. new_sensor = Sensor(addName, addIp, addLocation, addDescription) Session.add(new_sensor) finally: Session.flush() Session.flush()
def add_rules_to_database(self, file_id, ruleList): """ Add rules to database based on file """ stored_rules = Session.query(Rules).filter(Rules.file == file_id).all() # Returns entire set of rules stored_list = {} # Cache of already accessed rules rules_to_add = [] # List of rule to add to database for new_rule in ruleList: try: try: if int(new_rule.sid) not in stored_list or stored_list[new_rule.sid] is None: stored_list[int(new_rule.sid)] = self._find_local_rule(stored_rules, int(new_rule.sid)) stored_local_rules = stored_list[int(new_rule.sid)] if stored_local_rules is None or len(stored_local_rules) == 0: # print 'print %s is not in DB. Type %s' % (int(new_rule.sid), type(new_rule.sid)) # for rules in stored_rules: # print 'Sid: %s Rev: %s\nRule: %s' % (rules.sid, rules.rev, rules.rule) raise NoResultFound old_rule = stored_local_rules[0] # print '--#--\n SID: %s' % (new_rule.sid) # print '---\n ID: %s \n Rule: %s --#--' % (old_rule.sid, old_rule.rule) # print '-#- \n %s \n -#- \n %s-#-' % (old_rule, new_rule) if int(old_rule.rev) < int(new_rule.rev) or str(old_rule.rule) != str(new_rule.raw): old_rule = self.deactivate_rule(old_rule) print "Different revision or content %s" % old_rule.sid raise NoResultFound if new_rule.active != old_rule.active: print "Rule is deactiviated: %s" % old_rule.sid old_rule = deactiviate_rule(old_rule) stored_local_rules[0] = old_rule stored_list[int(new_rule.sid)] = stored_local_rules except NoResultFound: # No result found, add rule new_rule_add = Rules(new_rule, file_id) rules_to_add.append(new_rule_add) except IndexError as e: print "List index: %s" % (e) print stored_local_rules exit(1) except TypeError as e: print "Type error: %s" % (e) # print type(new_rule.sid) # print stored_local_rules exit(1) except OperationalError as e: print "EXCEPTION in add_rules_to_database\nError information: %s" % (e) # TODO: Needs to f**k off Session.add_all(rules_to_add) Session.flush()
def delete_sensor(self, **kwargs): """ Deactivates a sensor in the system. """ try: id = kwargs['sensor_id'] sensor = Session.query(Sensor).filter(Sensor.id == id).one() sensor.active = 0 Session.merge(sensor) Session.flush() except NoResultFound: print 'No result found' except KeyError as e: print 'Key error occured %s' % e
def add_policy(self, name='', description=''): """ Creates a new policy based on user input. Known bug: Doesn't check exsitence of policy. """ try: self.check_status() if len(name) > 0 and len(description) > 0: new_policy = PolicyChainMeta(escape_string(name.capitalize()), escape_string(description)) Session.add(new_policy) Session.flush() except SystemLockedException: pass finally: raise cherrypy.HTTPRedirect("/policy/") # Return to index
def delete_policy(self, id = 0): """ Sets a policy to deactive in the database. """ try: self.check_status() except SystemLockedException: raise cherrypy.HTTPRedirect('/policy/') if id == 0: raise NoResultFound try: policy = Session.query(PolicyChainMeta).filter(PolicyChainMeta.id == id).one() policy.active = 0 Session.merge(policy) Session.flush() except NoResultFound: pass finally: raise cherrypy.HTTPRedirect('/policy/')
def choose_object(self, **kwargs): """ Choose a excisting policy object. """ try: if 'object-id' not in kwargs and 'policy-id' not in kwargs: raise KeyError object_id = kwargs['object-id'] policy_id = kwargs['policy-id'] policy_object = Session.query(PolicyChain).filter( PolicyChain.id == policy_id).one() policy_object.policyobject_id = object_id Session.merge(policy_object) Session.flush() except KeyError: print 'KeyError'
def remove_object(self, **kwargs): """ Remove the object from the list """ if 'Referer' in cherrypy.request.headers: return_url = cherrypy.request.headers['Referer'] else: return_url = '/policy/' try: if kwargs['object-id'] is None or not kwargs['object-id'].isdigit(): raise TypeError('Policy ID is either None or ID is not int') if kwargs['policy-id'] is None: raise TypeError('Policy content None') object_id = int(kwargs['object-id']) policy_id = int(kwargs['policy-id']) delete_object = Session.query(PolicyChain).filter(and_( PolicyChain.chain_id == policy_id, PolicyChain.id == object_id)).one() parent = delete_object.parent child = delete_object.child Session.delete(delete_object) if parent != 0: parent_object = Session.query(PolicyChain).filter(and_( PolicyChain.id == parent, PolicyChain.chain_id == policy_id)).one() parent_object.child = child Session.merge(parent_object) if child != 0: child_object = Session.query(PolicyChain).filter(and_( PolicyChain.id == child, PolicyChain.chain_id == policy_id)).one() child_object.parent = parent Session.merge(child_object) Session.flush() raise cherrypy.HTTPRedirect(return_url) except NoResultFound: raise cherrypy.HTTPRedirect(return_url) except KeyError as e: raise
def add_location(self, **kwargs): """ Adds a new location to the database """ try: name = escape_string(kwargs['location_name']) if len(name) == 0: raise TypeError('The location name is empty!') new_location = SensorLocation(name) Session.add(new_location) Session.flush() print new_location return simplejson.dumps(new_location.id) except KeyError as e: print 'KEYERROR; this key does not excist: %s' % s except TypeError as e: print e
def new_object(self, id = 0): """ Adds a new object to the end of the policy """ if 'Referer' in cherrypy.request.headers: return_url = cherrypy.request.headers['Referer'] else: return_url = '/policy/' try: last_object = Session.query(PolicyChain).filter(and_( PolicyChain.chain_id == id, PolicyChain.child == 0)).one(); parent = last_object.id new_object = PolicyChain(id, 0, parent, 0) Session.add(new_object) Session.flush() last_object.child = new_object.id Session.merge(last_object) Session.flush() except NoResultFound: new_object = PolicyChain(id, 0, 0, 0) Session.add(new_object) Session.flush() raise cherrypy.HTTPRedirect(return_url)
def register_file_in_database(self, file, urlid): """ Register a Snort rule in the database This method will register a Snort rule file with its full path and file name. :param file: the full path with filename. :param urlid: the update source. Used to connect registered file to correct source in the database. """ try: base_path = str(self.tempDownloadFolder) search_pattern = re.compile(r"(?P<path>[A-z]+)/(?P<name>(\w|\.|-)+)$") # Search for file ending file_attributes = search_pattern.search(file) # file_name = file_attributes.group("name") file_path = file_attributes.group("path") try: file_from_db = ( Session.query(UpdateFile) .filter(and_(UpdateFile.name == file_name, UpdateFile.updatesource == urlid)) .one() ) if file_from_db is None: raise NoResulFound else: return file_from_db.id except NoResultFound: new_file = UpdateFile(file_name, urlid, file_path) Session.add(new_file) Session.flush() return new_file.id except ImportError: # print('') #TODO: TRANSLATe # raise # exit(1) print e exit() return 0