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 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_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 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 register_source(self, **kwargs): """ Register source data, and adds to database. If source exsists, it will be updated. If the source does not excist it will be added. """ try: # Check if source is active, deactivate if necessary. if 'active' not in kwargs or kwargs['active'] == 'off': active = 0 else: active = 1 url = kwargs['source-url'] except KeyError: raise if 'source-id' in kwargs: id = kwargs['source-id'] else: id = None if id: sourceUrl = Session.query(UpdateSource).get(id) sourceUrl.url = url sourceUrl.active = active else: sourceUrl = UpdateSource(active, url) Session.merge(sourceUrl) raise cherrypy.HTTPRedirect('/rules/')
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 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 deactivate_rule(self, rule): """ Deactivates and merges rules. Also returns the old rule to """ rule.active = 0 Session.merge(rule) return rule