Beispiel #1
0
 def get_user_id_from_token(self, token: str) -> int:
     # Recover user_id from token
     try:
         s = Serializer(self.secret_key)
         payload = s.loads(token)
         return payload
     except SignatureExpired:
         logger.info(GenericErrorMessages.EXPIRED_TOKEN_ERROR)
         raise AppException(msg=GenericErrorMessages.EXPIRED_TOKEN_ERROR)
     except BadSignature:
         logger.warning(GenericErrorMessages.BAD_SIGNATURE_TOKEN_ERROR)
         raise AppException(
             msg=GenericErrorMessages.BAD_SIGNATURE_TOKEN_ERROR)
Beispiel #2
0
    def get_all_users(self):
        """
        This method retrieves all users from DB
        :return: {User}
        """
        try:
            alchemy_users_list = self.session.query(AlchemyUser).all()

            user_list = list()

            for u in alchemy_users_list:
                role_list = list()
                for r in u.roles:
                    permission_list = list()
                    for p in r.permissions:
                        new_permission = Permission(p.id, p.name, p.description)
                        permission_list.append(new_permission)
                    role = Role(r.id, r.name, r.description, permission_list)
                    role_list.append(role)
                new_user = User(u.id, u.username, u.password, u.first_name, u.family_name, u.email, role_list)
                user_list.append(new_user)

            return user_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #3
0
    def get_all_evidences(self):
        """
        This method retrieves all evidences from DB
        :return: {Evidence}
        """
        try:
            alchemy_evidences_list = self.session.query(AlchemyEvidence).all()
            evidence_list = list()

            for e in alchemy_evidences_list:
                roles_list = list()
                for r in e.owner.roles:
                    permission_list = list()
                    for p in r.permissions:
                        new_permission = Permission(p.id, p.name, p.description)
                        permission_list.append(new_permission)
                    new_role = Role(r.id, r.name, r.description, permission_list)
                    roles_list.append(new_role)

                new_user = User(e.owner.id, e.owner.username, e.owner.password, e.owner.first_name, e.owner.family_name,
                                e.owner.email, roles_list)
                new_evidence = Evidence(e.id, e.alias, e.size, new_user)
                evidence_list.append(new_evidence)

            return evidence_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #4
0
    def get_all_uploads(self, evidence_id):
        """
        This method retrieves all uploads from DB that belongs to evidence_id
        :return: {Upload}
        """
        try:
            alchemy_upload_list = self.session.query(AlchemyUpload).filter_by(evidence_id=evidence_id).all()

            upload_list = list()

            for u in alchemy_upload_list:
                roles_list = list()
                for r in u.evidence.owner.roles:
                    permission_list = list()
                    for p in r.permissions:
                        new_permission = Permission(p.id, p.name, p.description)
                        permission_list.append(new_permission)
                    new_role = Role(r.id, r.name, r.description, permission_list)
                    roles_list.append(new_role)
                new_user = User(u.evidence.owner.id, u.evidence.owner.username, u.evidence.owner.password,
                                u.evidence.owner.first_name, u.evidence.owner.family_name, u.evidence.owner.email,
                                roles_list)
                new_evidence = Evidence(u.evidence.id, u.evidence.alias, u.evidence.size, new_user)
                new_upload = Upload(u.id, u.path, u.size, u.type, new_evidence)
                upload_list.append(new_upload)

            return upload_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #5
0
    def get_upload(self, upload_id):
        """
        This method retrieves uploads with given id from DB
        :return: Upload
        """
        try:
            alchemy_upload = self.session.query(AlchemyUpload).filter_by(id=upload_id).first()
            if alchemy_upload is None:
                new_upload = None
            else:

                roles_list = list()
                for r in alchemy_upload.evidence.owner.roles:
                    permissions_list = list()
                    for p in r.permissions:
                        new_permission = Permission(p.id, p.name, p.description)
                        permissions_list.append(new_permission)
                    new_role = Role(r.id, r.name, r.description, permissions_list)
                    roles_list.append(new_role)

                new_user = User(alchemy_upload.evidence.owner.id, alchemy_upload.evidence.owner.username,
                                alchemy_upload.evidence.owner.password, alchemy_upload.evidence.owner.first_name,
                                alchemy_upload.evidence.owner.family_name, alchemy_upload.evidence.owner.email,
                                roles_list)
                new_evidence = Evidence(alchemy_upload.evidence.id, alchemy_upload.evidence.alias,
                                        alchemy_upload.evidence.size, new_user)
                new_upload = Upload(alchemy_upload.id, alchemy_upload.path, alchemy_upload.size, alchemy_upload.type,
                                    new_evidence)

            return new_upload

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #6
0
    def get_all_files(self, upload_id):
        """
        This method retrieves all files from DB that belongs to upload_id
        :return: {File}
        """
        try:
            alchemy_file_list = self.session.query(AlchemyFile).filter_by(upload_id=upload_id).all()
            file_list = list()

            for f in alchemy_file_list:
                roles_list = list()
                for r in f.upload.evidence.owner.roles:
                    permission_list = list()
                    for p in r.permissions:
                        new_permission = Permission(p.id, p.name, p.description)
                        permission_list.append(new_permission)
                    new_role = Role(r.id, r.name, r.description, permission_list)
                    roles_list.append(new_role)

                new_owner = User(f.upload.evidence.owner.id, f.upload.evidence.owner.username,
                                 f.upload.evidence.owner.password, f.upload.evidence.owner.first_name,
                                 f.upload.evidence.owner.family_name, f.upload.evidence.owner.email, roles_list)

                new_evidence = Evidence(f.upload.evidence.id, f.upload.evidence.alias, f.upload.evidence.size,
                                        new_owner)

                new_upload = Upload(f.upload.id, f.upload.path, f.upload.size, f.upload.type, new_evidence)

                new_file = File(f.id, f.path, f.size, f.name, f.hash_md5, f.hash_sha256, new_upload)
                file_list.append(new_file)

            return file_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #7
0
    def get_user(self, user_id):
        """
        This method retrieves users with given id from DB
        :return: User
        """
        try:
            alchemy_user = self.session.query(AlchemyUser).filter_by(id=user_id).first()

            if alchemy_user is None:
                new_user = None
            else:
                role_list = list()
                for r in alchemy_user.roles:
                    permissions_list = list()
                    for p in r.permissions:
                        new_permission = Permission(p.id, p.name, p.description)
                        permissions_list.append(new_permission)
                    new_role = Role(r.id, r.name, r.description, permissions_list)
                    role_list.append(new_role)

                new_user = User(alchemy_user.id, alchemy_user.username, alchemy_user.password, alchemy_user.first_name,
                                alchemy_user.family_name, alchemy_user.email, role_list)

            return new_user

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #8
0
    def check_token(self, token: str) -> bool:
        # Check if token is ok and it is in tokens dictionary
        self.get_user_id_from_token(token)

        if token in self.tokens:
            return True
        else:
            raise AppException(
                msg=GenericErrorMessages.TOKEN_DOES_NOT_EXIST_ERROR)
Beispiel #9
0
def get_module(module_name: str) -> Module:
    """
    Get module by name
    :param module_name: str
    :return: module
    """
    if module_name not in modules_manager.keys():
        raise AppException(msg=GenericErrorMessages.MODULE_NOT_FOUND)
    return modules_manager[module_name]
Beispiel #10
0
    def alchemy_get_case_status(self, status_id):
        """
        This method retrieves case status with given id from DB
        :return: AlchemyCaseStatus
        """
        try:
            alchemy_case_status = self.session.query(AlchemyCaseStatus).filter_by(id=status_id).first()

            return alchemy_case_status

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #11
0
    def alchemy_get_permission(self, permission_id):
        """
        This method retrieves an alchemy permission with given id from DB
        Not included in views (not published)
        :return: {AlchemyPermission}
        """
        try:
            alchemy_permission = self.session.query(AlchemyPermission).filter_by(id=permission_id).first()

            return alchemy_permission

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #12
0
    def alchemy_get_role(self, role_id):
        """
        This method retrieves an alchemy role with given id from DB
        Not included in views (not published)
        :return: {AlchemyRole}
        """
        try:
            alchemy_role = self.session.query(AlchemyRole).filter_by(id=role_id).first()

            return alchemy_role

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #13
0
    def alchemy_get_entity(self, entity_id):
        """
        This method retrieves an alchemy entity with given id from DB
        Not included in views (not published)
        :return: {AlchemyEntity}
        """
        try:
            alchemy_entity = self.session.query(AlchemyEntity).filter_by(id=entity_id).first()

            return alchemy_entity

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #14
0
    def alchemy_get_upload(self, upload_id):
        """
        This method retrieves an alchemy upload with given id from DB
        Not included in views (not published)
        :return: {AlchemyUpload}
        """
        try:
            alchemy_upload = self.session.query(AlchemyUpload).filter_by(id=upload_id).first()

            return alchemy_upload

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #15
0
    def get_user_id(self, name, password):
        """
        This method retrieves user with given name and password (None if it doesn't exist)
        :return: Int
        """
        try:
            alchemy_user = self.session.query(AlchemyUser).filter_by(username=name, password=password).first()
            if alchemy_user:
                return alchemy_user.id
            else:
                return None

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #16
0
    def logout(self, token: str):
        """ This method do logout """

        # Recover payload (user_id) from token
        logger.debug('Recover user_id from token')
        user_id = self.get_user_id_from_token(token)

        # Remove token from dictionary. If raise exception then token does not exist
        try:
            self.tokens.pop(token)
        except KeyError:
            # Something went wrong because token does not exist
            logger.error('{} user_id: {}'.format(
                GenericErrorMessages.TOKEN_DOES_NOT_EXIST_ERROR, user_id))
            raise AppException(
                msg=GenericErrorMessages.TOKEN_DOES_NOT_EXIST_ERROR)
Beispiel #17
0
        def create_input(self, cls_input_data,
                         cls_input_data_schema) -> object:
            """
            This method create an object from POST request
            Has no sense using this method in GET/PUT/DELETE requests
            :param self:
            :param cls_input_data: class that represents object
            :param cls_input_data_schema: schema of the object
            :return: object filled with data from request
            """
            validate_input = cls_input_data_schema().load(request.json)

            if len(validate_input.errors) > 0:
                raise AppException(msg=GenericErrorMessages.VALIDATION_ERROR)

            return cls_input_data(**request.json)
Beispiel #18
0
    def get_case_type(self, type_id):
        """
        This method retrieves case type with given id from DB
        :return: CaseType
        """
        try:
            alchemy_case_type = self.session.query(AlchemyCaseType).filter_by(id=type_id).first()

            if alchemy_case_type is None:
                new_case_type = None
            else:
                new_case_type = CaseType(alchemy_case_type.id, alchemy_case_type.name, alchemy_case_type.description)

            return new_case_type

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #19
0
    def get_all_case_types(self):
        """
        This method retrieves all case types from DB
        :return: {CaseType}
        """
        try:
            alchemy_case_types_list = self.session.query(AlchemyCaseType).all()
            case_types_list = list()

            for t in alchemy_case_types_list:
                new_type = CaseType(t.id, t.name, t.description)
                case_types_list.append(new_type)

            return case_types_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #20
0
    def get_entity(self, entity_id):
        """
        This method retrieves entity with given id from DB
        :return: Entity
        """
        try:
            alchemy_entity = self.session.query(AlchemyEntity).filter_by(id=entity_id).first()

            if alchemy_entity is None:
                new_entity = None
            else:
                new_entity = Entity(alchemy_entity.id, alchemy_entity.name, alchemy_entity.description)

            return new_entity

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #21
0
    def get_instance(self, instance_id):
        """
        This method retrieves instances with given id from DB
        :return: Instance
        """
        try:
            alchemy_instance = self.session.query(AlchemyInstance).filter_by(id=instance_id).first()
            if alchemy_instance is None:
                new_instance = None
            else:
                new_entity = Entity(alchemy_instance.entity.id, alchemy_instance.entity.name,
                                    alchemy_instance.entity.description)
                new_instance = Instance(alchemy_instance.id, alchemy_instance.name, new_entity)

            return new_instance

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #22
0
    def get_all_entities(self):
        """
        This method retrieves all entities from DB
        :return: {Entity}
        """
        try:
            alchemy_entities_list = self.session.query(AlchemyEntity).all()

            entities_list = list()

            for ent in alchemy_entities_list:
                new_entity = Entity(ent.id, ent.name, ent.description)
                entities_list.append(new_entity)

            return entities_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #23
0
    def get_permission(self, permission_id):
        """
        This method retrieves permission with given id from DB
        :return: {Permission}
        """
        try:
            alchemy_permission = self.session.query(AlchemyPermission).filter_by(id=permission_id).first()

            if alchemy_permission is None:
                new_permission = None
            else:
                new_permission = Permission(alchemy_permission.id, alchemy_permission.name,
                                            alchemy_permission.description)

            return new_permission

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #24
0
    def get_all_case_status(self):
        """
        This method retrieves all case status from DB
        :return: {CaseStatus}
        """
        try:
            alchemy_case_status_list = self.session.query(AlchemyCaseStatus).all()

            case_status_list = list()

            for s in alchemy_case_status_list:
                new_status = CaseStatus(s.id, s.name, s.description)
                case_status_list.append(new_status)

            return case_status_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #25
0
    def get_case_status(self, status_id):
        """
        This method retrieves case status with given id from DB
        :return: CaseStatus
        """
        try:
            alchemy_case_status = self.session.query(AlchemyCaseStatus).filter_by(id=status_id).first()

            if alchemy_case_status is None:
                new_case_status = None
            else:
                new_case_status = CaseStatus(alchemy_case_status.id, alchemy_case_status.name,
                                             alchemy_case_status.description)

            return new_case_status

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #26
0
    def get_all_instances(self, entity_id):
        """
        This method retrieves all instances from DB that belongs to entity_id
        :return: {Instance}
        """
        try:
            alchemy_instance_list = self.session.query(AlchemyInstance).filter_by(entity_id=entity_id).all()
            instance_list = list()

            for ins in alchemy_instance_list:
                new_entity = Entity(ins.entity.id, ins.entity.name, ins.entity.description)
                new_instance = Instance(ins.id, ins.name, new_entity)
                instance_list.append(new_instance)

            return instance_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #27
0
    def get_all_properties(self, entity_id):
        """
        This method retrieves all properties from DB that belongs to entity_id
        :return: {Property}
        """
        try:
            alchemy_property_list = self.session.query(AlchemyProperty).filter_by(entity_id=entity_id).all()

            property_list = list()

            for prop in alchemy_property_list:
                new_entity = Entity(prop.entity.id, prop.entity.name, prop.entity.description)
                new_property = Property(prop.id, prop.name, prop.description, new_entity)
                property_list.append(new_property)

            return property_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #28
0
    def login(self, username: str, password: str) -> str:
        """ This methods do login and return token """

        # Check if username and password are in database
        #user_id = self.ds.get_user_id(username, password)
        user_id = 1

        # If user exists, generate token, store token and return it. Else raise exception
        if user_id:
            token = self.generate_token(user_id)
            self.tokens[token] = user_id
            logger.info('{} username: {} user_id: {}'.format(
                GenericErrorMessages.USER_LOGIN, username, user_id))
            return token
        else:
            raise AppException(logger=logger,
                               msg='{} username: {}'.format(
                                   GenericErrorMessages.USER_NOT_FOUND,
                                   username))
Beispiel #29
0
    def get_all_permissions(self):
        """
        This method retrieves all permissions from DB
        :return: {Permission}
        """

        try:
            alchemy_permission_list = self.session.query(AlchemyPermission).all()

            permission_list = list()

            for perm in alchemy_permission_list:
                new_permission = Permission(perm.id, perm.name, perm.description)
                permission_list.append(new_permission)

            return permission_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
Beispiel #30
0
    def get_all_cases(self):
        """
        This method retrieves all cases from DB
        :return: {Case}
        """
        try:
            alchemy_cases_list = self.session.query(AlchemyCase).all()

            case_list = list()

            for c in alchemy_cases_list:
                case_type = CaseType(c.type.id, c.type.name, c.type.description)
                case_status = CaseStatus(c.status.id, c.status.name, c.status.description)
                new_case = Case(c.id, c.formal_name, c.friend_name, c.creation_datetime, c.num_win, c.num_lin,
                                c.num_ios, c.num_mal, c.num_pac, c.num_log, case_type, case_status)
                case_list.append(new_case)

            return case_list

        except Exception as e:
            raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))