Beispiel #1
0
	def save(obj, parser):
	
		if parser == None:
			parser = obj._parser
		try:
			ruleTable = DB_SESSION.query(PolicyRuleTableModel).filter(PolicyRuleTableModel.name == obj.name).first()
			ruleTable.type = obj._policy
			ruleTable.defaultParser = obj._parser
			ruleTable.defaultPersistence = obj._persistenceBackend
			ruleTable.defaultPersistenceFlag = obj._persist
			DB_SESSION.add(ruleTable)
			DB_SESSION.commit()
			DB_SESSION.expunge(ruleTable)
		except:		
			ruleTable = PolicyRuleTableModel(name = obj.name, uuid = obj.uuid, type = obj._policy, defaultParser = parser, defaultPersistence = obj._persistenceBackend, defaultPersistenceFlag = obj._persist)
			
			DB_SESSION.add(ruleTable)
			DB_SESSION.commit()
			DB_SESSION.expunge(ruleTable)


		#flag to know if save comes from addRule or removeRule()
		addRule = 0
		uuidInRuleSet=list()
		for rule in obj._ruleSet:
			
			#need to removeRule later
			uuidInRuleSet.append(rule.rule.getUUID())
			#lbergesio: Rule is saved parsed, as a string
			#Possibly if Rule exists (try) it is not required to update its fields.==> 
			#==> IT IS NEEDED FOR THE RuleTable.moveRule()
			try:
				ruleModel = DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleUUID == rule.rule.getUUID()).first()
			except:	
				# If #Rules == #Models then it is the PolicyRuleTableModel being
				# deleted, not the PolicyRuleModel (last is the normal case)
				if len(obj._ruleSet) != DB_SESSION.query(PolicyRuleModel).all().count():
				    addRule = 1
				    ruleModel = DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleUUID == rule.rule.getUUID()).first()

			ruleModel.RuleTableName = obj.name
			ruleModel.Rule = ParseEngine.craftRule(rule.rule, obj._parser)
			ruleModel.RuleIsEnabled = rule.enabled
			ruleModel.RulePosition = obj._ruleSet.index(rule)
			DB_SESSION.add(ruleModel)
			DB_SESSION.commit()
			DB_SESSION.expunge_all()
		#if the save() comes from a removeRule, check which one was removed and delete
		if not addRule:
			for ruleModel in DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleTableName == obj.name).all():
				if ruleModel.RuleUUID not in uuidInRuleSet:
					DB_SESSION.delete(ruleModel)
					DB_SESSION.commit()
					DB_SESSION.expunge_all()
					break
			DB_SESSION.expunge_all()
Beispiel #2
0
	def loadRuleSet(table_uuid):
		Django.logger.debug('Loading Rule set...')
		try: 
			ruleTable = PolicyRuleTableModel.objects.get(uuid = table_uuid)
		except: 
			return list()
		rules = PolicyRuleModel.objects.filter(RuleTableName = ruleTable.name).order_by('RulePosition')
		ruleSet = []
		#The rules sort by priority 
		for rule in rules:
			ruleObj= ParseEngine.parseRule(rule.Rule)
			ruleObj._uuid = rule.RuleUUID
			ruleEntry = RuleEntry(ruleObj,rule.RuleIsEnabled)
			ruleSet.append(ruleEntry)
		return ruleSet
Beispiel #3
0
	def loadRuleSet(table_uuid):
		SQLAlchemy.logger.debug('Loading Rule set...')
		try: 
			ruleTable = DB_SESSION.query(PolicyRuleTableModel).filter(PolicyRuleTableModel.uuid == table_uuid).first()
		except: 
			return list()
		rules = DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleTableName == ruleTable.name).order_by(PolicyRuleModel.RulePosition).all()
		ruleSet = []
		#The rules sort by priority 
		for rule in rules:
			ruleObj= ParseEngine.parseRule(rule.Rule)
			ruleObj._uuid = rule.RuleUUID
			ruleEntry = RuleEntry(ruleObj,rule.RuleIsEnabled)
			ruleSet.append(ruleEntry)
		DB_SESSION.expunge_all()
		return ruleSet
Beispiel #4
0
	def addRule(self,string,enabled=True,pos=None,parser=None,pBackend=None,persist=True):
		if not parser:
			parser = self._parser
		
		rule = ParseEngine.parseRule(string, parser)
		rule.setUUID(uuid.uuid4().hex)
		
		with self._mutex:	
			if pos > len(self._ruleSet):
				#raise Exception("Invalid position")
				self._ruleSet.append(RuleEntry(rule,enabled))				
			elif pos !=None:
				self._ruleSet.insert(pos,RuleEntry(rule,enabled))
			else:
				self._ruleSet.append(RuleEntry(rule,enabled))
			if self._persist:
				self.save()