Пример #1
0
    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/")
Пример #2
0
 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)
Пример #3
0
 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
Пример #4
0
    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()
Пример #5
0
    def register_update_in_database(self, urlid=0):
        """ Register download in database """
        if urlid != 0:
            try:
                UpdateItem = UpdateLog(urlid, datetime.now())
                Session.add(UpdateItem)
                return UpdateItem.id
            except:
                print ("Error while registering update with database")
                raise

            return 0
Пример #6
0
 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
Пример #7
0
 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
Пример #8
0
    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