def validate(self): """ Checks if the attributes of the class are valid :returns: Boolean """ # TODO: Verify validation of Group Object ObjectValidator.validateAlNum(self, 'name', withSymbols=True, minLength=3) # TODO: validate return ObjectValidator.isObjectValid(self)
def validateAlpha(string, minLength=0, maxLength=0, withSpaces=False, withNonPrintableCharacters=False): """ Validates if the string is of an alphabetical kind. :param string: The string to be validated :type string: String :param minLength: The minimal length of the string :type minLength: Integer :param maxLength: The maximal length of the string :type maxLength: Integer :param withSpaces: If set the string can contain spaces :type withSpaces: Boolean :param withNonPrintableCharacters: If set the string can contain non printable characters as tab newlines etc. :type withNonPrintableCharacters: Boolean :return Boolean """ obj = Container(string) return ObjectValidator.validateAlpha(obj, 'value', minLength, maxLength, withSpaces, withNonPrintableCharacters, changeAttribute=False)
def update(self, instance, commit=True, validate=True): """ updates an <<get_broker_class()>> :param instance: The get_broker_class() to be updated :type instance: extension of Base """ if validate: errors = not instance.validate() if errors: raise ValidationException( 'Instance to be inserted is invalid.{0}'.format( ObjectValidator.getFirstValidationError(instance))) # an elo den update try: self.session.merge(instance) self.do_commit(commit) except sqlalchemy.exc.IntegrityError as error: raise IntegrityException(error) except sqlalchemy.exc.DatabaseError as error: self.session.rollback() raise BrokerException(error) except sqlalchemy.exc.SQLAlchemyError as error: self.session.rollback() raise BrokerException(error) self.do_commit(commit)
def update_event(self, user, event, mkrelations=True, commit=True): self.logger.debug('User {0} updates a event {1}'.format( user.username, event.identifier)) try: # the the creator user = self.user_broker.get_by_id(user.identifier) self.set_extended_logging(event, user, user.group, False) # TODO relations on update self.event_broker.update(event, False) # generate relations if needed! """ attributes = get_all_attributes_from_event(event) if (mkrelations == 'True' or mkrelations is True) and attributes: self.relation_broker.generate_bulk_attributes_relations(event, attributes, False) """ self.event_broker.do_commit(commit) return event except ValidationException: message = ObjectValidator.getFirstValidationError(event) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def validate(self): """ Checks if the attributes of the class are valid :returns: Boolean """ return ObjectValidator.validateDigits(self, 'value')
def insert(self, instance, commit=True, validate=True): """ Insert a <<get_broker_class()>> :param instance: The get_broker_class() to be inserted :type instance: extension of Base Note: handles the commit and the identifier of the user is taken into account if set """ if validate: errors = not instance.validate() if errors: raise ValidationException( 'Instance to be inserted is invalid.{0}'.format( ObjectValidator.getFirstValidationError(instance))) try: self.session.add(instance) self.do_commit(commit) except sqlalchemy.exc.IntegrityError as error: raise IntegrityException(error) except sqlalchemy.exc.DatabaseError as error: self.session.rollback() raise BrokerException(error) except sqlalchemy.exc.SQLAlchemyError as error: self.session.rollback() raise BrokerException(error)
def validate(self): """ Checks if the attributes of the class are valid :returns: Boolean """ ObjectValidator.validateAlNum(self, 'name', withSymbols=True, minLength=3) ObjectValidator.validateAlNum(self, 'description', minLength=5, withSpaces=True, withNonPrintableCharacters=True, withSymbols=True) return ObjectValidator.isObjectValid(self)
def validate(self): """ Checks if the attributes of the class are valid :returns: Boolean """ # TODO: Verify validation of AttributeDefinition Object ObjectValidator.validateAlNum(self, 'name', withSpaces=True, withSymbols=True, minLength=3) ObjectValidator.validateAlNum(self, 'description', withNonPrintableCharacters=True, withSpaces=True, minLength=3, withSymbols=True) ObjectValidator.validateRegularExpression(self, 'regex') # check if handler is compatible with the class_index allowed_tables = self.handler.get_allowed_types() if not (self.table_id in allowed_tables): self.class_index = FailedValidation( self.table_id, ('Class is not compatible "{0}".\n' 'Supported classes are {1}').format( self.attribute_handler.classname, self.__class_numbers_to_text(allowed_tables))) return ObjectValidator.isObjectValid(self)
def update_condition(self, condition): try: self.condition_broker.update(condition, True) except ValidationException as error: message = ObjectValidator.getFirstValidationError(condition) raise ControllerException( u'Could not add condition due to: {0}'.format(message)) except (BrokerException) as error: raise ControllerException(error)
def validate(self): """ Checks if the attributes of the class are valid :returns: Boolean """ # validate attribute value value_instance = self.__get_value_instance() # TODO: encoding error ObjectValidator.validateRegex( value_instance, 'value', getattr(self.definition, 'regex'), u'The value "{0}" does not match {1} for definition {2}'.format( value_instance.value, getattr(self.definition, 'regex'), getattr(self.definition, 'name')).encode('utf-8'), True) if not isinstance(value_instance, FailedValidation): errors = not getattr(value_instance, 'validate')() if errors: return False return ObjectValidator.isObjectValid(self)
def update_group(self, group): try: self.group_broker.update(group) return group except ValidationException as error: message = ObjectValidator.getFirstValidationError(group) raise ControllerException( u'Could not update group due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def insert_condition(self, condition, commit=True): try: self.condition_broker.insert(condition, False) self.condition_broker.do_commit(commit) except ValidationException as error: message = ObjectValidator.getFirstValidationError(condition) raise ControllerException( u'Could not add condition due to: {0}'.format(message)) except (BrokerException) as error: raise ControllerException(error)
def validate(self): """ Checks if the attributes of the class are valid :returns: Boolean """ return ObjectValidator.validateAlNum(self, 'value', minLength=1, withSpaces=True, withSymbols=True)
def update_mail(self, mail_template, user): try: user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(mail_template, user, insert=False) mail_template = self.mail_broker.update(mail_template) return mail_template except ValidationException as error: message = ObjectValidator.getFirstValidationError(mail_template) raise ControllerException( u'Could not update mail due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def add_dbject(self, obj): """ Add an object to this event :param obj: Obejct to be added :type obj: Obejct """ errors = not obj.validate() if errors: raise ValidationException(u'Invalid Object:' + ValidationException(ObjectValidator.getFirstValidationError(obj))) function = getattr(self.objects, 'append') function(obj)
def add_group(self, group): """ Add a group to this event :param group: Group to be added :type group: Group """ errors = not group.validate() if errors: raise ValidationException(u'Invalid Group:' + ValidationException(ObjectValidator.getFirstValidationError(group))) function = getattr(self.maingroups, 'append') function(group)
def insert_group(self, group, validate=True, commit=True): try: self.group_broker.insert(group, commit, validate) except ValidationException as error: message = ObjectValidator.getFirstValidationError(group) raise ControllerException( u'Could not add group due to: {0}'.format(message)) except IntegrityException as error: raise ControllerIntegrityException(error) except (BrokerException) as error: raise ControllerException(error)
def add_attribute(self, attribute): """ Add an attribute to this event :param attribute: Attribute to be added :type attribute: Attribute """ errors = not attribute.validate() if errors: raise ValidationException(ValidationException(ObjectValidator.getFirstValidationError(attribute))) function = getattr(self.attributes, 'append') function(attribute)
def validateIP(string): """ Validates if the attribute is an IP address. Note: The actual object is changed internally :param string: Text to be analyzed :type string: String :return Boolean """ obj = Container(string) return ObjectValidator.validateIP(obj, 'value', changeAttribute=False)
def update_reference_definition(self, reference_definition, user): try: user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(reference_definition, user, insert=False) reference_definition = self.reference_definition_broker.update( reference_definition) return reference_definition except ValidationException as error: message = ObjectValidator.getFirstValidationError( reference_definition) raise ControllerException( u'Could not update reference due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def validateRegularExpression(regex): """ Validates if the attribute is a valid regular expression. Note: The actual object is changed internally :param regex: regular expression to be validated :type regex: String :return Boolean """ obj = Container(regex) return ObjectValidator.validateRegularExpression(obj, 'value', changeAttribute=False)
def insert_object_definition(self, obj, user, commit=True): try: obj.chksum = gen_obj_chksum(obj) user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(obj, user, insert=True) self.obj_def_broker.insert(obj, False) self.obj_def_broker.do_commit(commit) return obj except ValidationException as error: message = ObjectValidator.getFirstValidationError(obj) raise ControllerException( u'Could not insert object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def update_user(self, user): try: if user.gpg_key: self.mail_controller.import_gpg_key(user.gpg_key) self.user_broker.update(user) # add it again in case of changes # TODO import gpg key # if user.gpg_key: # self.mail_handler.import_gpg_key(user.gpg_key) return user except ValidationException as error: message = ObjectValidator.getFirstValidationError(user) raise ControllerException(u'Could not update user due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def validateDateTime(string): """ Validates if the attribute is a date or date time under the specified formats address. Note: The actual object is changed internally :param string: Text to be analyzed :type string: String :return Boolean """ obj = Container(string) return ObjectValidator.validateDateTime(obj, 'value', changeAttribute=False)
def update_object_definition(self, obj, user, commit=True): if obj.cybox_std: raise ControllerException( u'Could not update object definition as the object is part of the cybox standard' ) try: obj.chksum = gen_obj_chksum(obj) user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(obj, user, insert=False) self.obj_def_broker.update(obj, commit) return obj except ValidationException as error: message = ObjectValidator.getFirstValidationError(obj) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def validateEmailAddress(string): """ Validates if the attribute is an email. Note: The actual object is changed internally :param string: Text to be analyzed :type string: String :param changeAttribute: If set the given attribute will be changed to a type of FailedValidation :type changeAttribute: Boolean :return Boolean """ obj = Container(string) return ObjectValidator.validateEmailAddress(obj, 'value', changeAttribute=False)
def update_attribute_definition(self, attribute, user, commit=True): if attribute.cybox_std: raise ControllerException( u'Could not update attribute definition as the attribute is part of the cybox standard' ) try: attribute.chksum = gen_attr_chksum(attribute) user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(attribute, user, insert=False) attribute = self.attr_def_broker.update(attribute, commit) return attribute except ValidationException as error: message = ObjectValidator.getFirstValidationError(attribute) raise ControllerException( u'Could not update attribute definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def insert_attribute_definition(self, attribute, user, commit=True): try: attribute.chksum = gen_attr_chksum(attribute) user = self.user_broker.get_by_id(user.identifier) # check if handler is associated if not attribute.attribute_handler: handler = self.handler_broker.get_by_id( attribute.attributehandler_id) attribute.attribute_handler = handler self.set_simple_logging(attribute, user, insert=True) attribute = self.attr_def_broker.insert(attribute, False) self.attr_def_broker.do_commit(commit) return attribute except ValidationException as error: message = ObjectValidator.getFirstValidationError(attribute) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def validate(self): """Validates the mail template object""" ObjectValidator.validateAlNum(self, 'name', withSpaces=True, minLength=3, withSymbols=True) ObjectValidator.validateAlNum(self, 'body', withNonPrintableCharacters=True, withSpaces=True, minLength=3, withSymbols=True) ObjectValidator.validateAlNum(self, 'subject', withSpaces=True, minLength=3, withSymbols=True) return ObjectValidator.isObjectValid(self)
def validateHash(string, hashType): """ Validates if the attribute is a valid hash. The supported types are: MD5,SHA1,SHA256,SHA386,SHA512 Note: The actual object is changed internally :param string: Text to be analyzed :type string: String :param attributeName: attribute name of the object :type attributeName: String :param hashType: Hash type to be validated i.e 'MD5' :param hashType: String :return Boolean """ obj = Container(string) return ObjectValidator.validateHash(obj, 'value', hashType, changeAttribute=False)
def validateDigits(string, minimal=None, maximal=None): """ Validates if the attribute is of an numerical kind. Note: The actual object is changed internally :param string: The string to be validated :type string: String :param minimal: the minimal value the number :type minimal: Number :param maximal: the maximal value the number :type maximal: Number :return Boolean """ obj = Container(string) return ObjectValidator.validateDigits(obj, 'value', minimal, maximal, changeAttribute=False)