예제 #1
0
    def delete(self, dbconnection, setup=None):
        # Get Project from local DB
        # to update the pi_users after Save
        current = db.get(dbconnection, table='projects', id=self.id)

        result = super(Project, self).delete(setup)
        errors = result['errors']

        if errors:
            raising = True
            for err in errors:
                if "Record not found" in err['exception']:
                    raising = False
                    break
            if raising:
                raise ProjectException(errors)

        db.delete(dbconnection, 'projects', self.id)

        for u in current['pi_users']:
            user = q(User).id(u).get().first()
            user = user.merge(dbconnection)
            logger.debug("Update user %s after Project delete()" % u)
            logger.debug(user)
            db.users(dbconnection, user.dict(), user.id)

        # Slices will be removed by Sync

        return True
예제 #2
0
def events_run(lock, qPasswordEvents):
    """
    Process the user after approval 
    """

    logger.info("Worker password events starting")

    dbconnection = connect()

    while True:

        try:
            event = Event(qPasswordEvents.get())
        except Exception as e:
            logger.error("Problem with event: {}".format(e))
            event.logError(str(e))
            event.setError()
            db.dispatch(dbconnection, event)
            continue
        else:
            logger.info("Processing password event from user {}".format(
                event.user))

            event.setRunning()
            event.logInfo("Event is running")
            logger.debug("Event %s is running" % event.id)
            ##
            # Creating a new password for user
            if event.creatingObject():
                logger.info("Creating password {}".format(event.object.id))

                try:
                    cursor = db.users(dbconnection, email=event.data['email'])
                    u = cursor.next()
                    u['password'] = event.data['password']
                    db.users(dbconnection, u, u['id'])
                except Exception as e:
                    logger.error(
                        "Problem updating password of user: {} - {}".format(
                            event.object.id, e))
                    event.logError(str(e))
                    event.setError()
                    continue
                else:
                    event.setSuccess()
                    event.logInfo("Event success")
                    logger.debug("Event %s Success" % event.id)
            ##
            # we then dispatch the event
            db.dispatch(dbconnection, event)
예제 #3
0
파일: slice.py 프로젝트: cemdrk/myslice2
    def delete(self, dbconnection, setup=None):
        # Get Slice from local DB 
        # to update the users after Save
        current = db.get(dbconnection, table='slices', id=self.id)

        result = super(Slice, self).delete(setup)
        errors = result['errors']

        # Signal only Registry Errors
        if errors:
            raising = False
            for err in errors:
                if err['type'] == "Reg":
                    if "Record not found" in err['exception']:
                        raising = False
                    else:
                        raising = True
            if raising:
                raise SliceException(errors)

        db.delete(dbconnection, 'slices', self.id)

        for u in current['users']:
            user = q(User).id(u).get().first()
            if user:
                user = user.merge(dbconnection)
                logger.debug("Update user %s after Slice delete()" % u)
                logger.debug(user)
                user = user.dict()
            else:
                logger.error("Could not update user after Slice.delete(), no answer from Registry")
                logger.warning("Updating the local DB manually")
                user = db.get(dbconnection, table='users', id=u)
                # Remove slice from user
                del u['slices'][self.id]

            db.users(dbconnection, user, u)

        # Update the Project of the slice
        project = db.get(dbconnection, table='projects', id=self.project)
        project['slices'] = list(set(project['slices']) - set([self.id]))
        db.projects(dbconnection, project)

        # Warning if errors on AMs
        #if errors:
        #    raise SliceWarningException(errors)

        return True
예제 #4
0
    def save(self, dbconnection, setup=None):
        # Get Project from local DB
        # to update the pi_users after Save
        current = db.get(dbconnection, table='projects', id=self.id)

        result = super(Project, self).save(setup)
        errors = result['errors']

        if errors:
            raise ProjectException(errors)

        result = {**(self.dict()), **result['data'][0]}
        # add status if not present and update on db
        if not 'status' in result:
            result['status'] = Status.ENABLED
            result['enabled'] = format_date()

        # New Project created
        if current is None:
            db.projects(dbconnection, result)
            current = db.get(dbconnection, table='projects', id=self.id)
        # Update existing project
        else:
            db.projects(dbconnection, result, self.id)

        # update pi_users after Save
        pi_users = list(
            set(current['pi_users']) | set(self.getAttribute('pi_users')))
        for u in pi_users:
            user = q(User).id(u).get().first()
            user = user.merge(dbconnection)
            logger.debug("Update user %s after Project save()" % u)
            logger.debug(user)
            db.users(dbconnection, user.dict(), user.id)

        # update slices after Save
        slices = list(
            set(current['slices']) | set(self.getAttribute('slices')))
        if setup:
            setup.setEndpoints(myslicelibsetup.endpoints)

        for s in current['slices']:
            sl = q(Slice, setup).id(s).get().first()
            db.slices(dbconnection, sl.dict())

        return True
예제 #5
0
    def save(self, dbconnection, setup=None):
        logger.warning("User.save() called")
        # Get User from local DB
        current = db.get(dbconnection, table='users', id=self.id)

        if self.getAttribute('generate_keys'):
            private_key, public_key = generate_RSA()
            self.setAttribute('private_key', private_key.decode('utf-8'))
            self.setAttribute('public_key', public_key.decode('utf-8'))
            self.appendAttribute('keys', self.getAttribute('public_key'))
            # Once it is generated, turn this option off
            self.setAttribute('generate_keys', False)

        result = super(User, self).save(setup)
        errors = result['errors']
        # Raise exception to send the errors as logs
        if errors:
            raise UserException(errors)

        result = {**(self.dict()), **result['data'][0]}
        # add status if not present and update on db
        if not 'status' in result:
            result['status'] = Status.ENABLED
            result['enabled'] = format_date()
        # Update user in local DB
        if not self.getAttribute('id'):
            raise UserException(
                "This user has no id, could not be saved in local DB: {}".
                format(self))
        logger.debug("DB user save()")
        logger.debug(result)
        if not self.getAttribute('id'):
            logger.critical("-------> USER HAS NO ID <-------")
            raise Exception(
                "Trying to save a user that has no id will remove all data in users table!!!"
            )
        # New User created
        if current is None:
            db.users(dbconnection, result)
        else:
            db.users(dbconnection, result, self.getAttribute('id'))
        return True
예제 #6
0
    def __init__(self, data={}):
        # initialize the object with its id
        if isinstance(data, str):
            data = db.users(id=data)

        data = data if data is not None else {}
        # Generate keys by default
        data['generate_keys'] = data.get('generate_keys', True)
        data['private_key'] = data.get('private_key', None)
        data['public_key'] = data.get('public_key', None)
        data['keys'] = data.get('keys', [])
        data['credentials'] = data.get('credentials', [])
        data['terms'] = data.get('terms', 'on')
        super(User, self).__init__(data)
예제 #7
0
    def delete(self, dbconnection, setup=None):
        logger.debug("Delete Authority %s" % self.id)
        # Get Authority from local DB
        # to update the pi_users after Save
        logger.debug("Get current object")
        current = db.get(dbconnection, table='authorities', id=self.id)
        logger.debug("Delete sent to myslicelib")
        result = super(Authority, self).delete(setup)
        logger.debug("result from myslicelib")
        logger.debug(result)
        errors = result['errors']
        logger.debug("checking errors")
        if errors:
            raising = True
            for err in errors:
                if "Record not found" in err['exception']:
                    raising = False
                    break
            if raising:
                raise AuthorityException(errors)
        logger.debug("Delete Authority from local DB")
        db.delete(dbconnection, 'authorities', self.id)
        logger.debug("Delete users of the Authority from local DB")
        for u in current['users']:
            logger.debug("Delete user %s" % u)
            db.delete(dbconnection, 'users', u)
        logger.debug("Update PI users of the Authority in local DB")
        for u in current['pi_users']:
            logger.debug("Get user %s" % u)
            user = q(User).id(u).get().first()
            if user:
                logger.debug("Update user %s in Authority delete()" % u)
                logger.debug(user)
                user = user.merge(dbconnection)
                db.users(dbconnection, user.dict(), user.id)

        return True
예제 #8
0
파일: slice.py 프로젝트: cemdrk/myslice2
    def save(self, dbconnection, setup=None):
        # Get Slice from local DB 
        # to update the users after Save
        current = db.get(dbconnection, table='slices', id=self.id)

        result = super(Slice, self).save(setup)
        errors = result['errors']
        result = {**(self.dict()), **result['data'][0]}
        if not errors:
            for r in result['resources']:
                if (not 'services' in r) or (not r['services']):
                    logger.warning("result from slice.save didn't had login info")
                    logger.warning("sleeping 10s before asking again to AMs")
                    import time
                    time.sleep(10)
                    slice = q(Slice, setup).id(self.id).get().first()
                    result = slice.dict()
                    break
        # add status if not present and update on db
        if not 'status' in result:
            result['status'] = Status.ENABLED
            result['enabled'] = format_date()

        # New Slice created
        if current is None:
            db.slices(dbconnection, result)
            current = db.get(dbconnection, table='slices', id=self.id)
        # Update existing slice
        else:
            db.slices(dbconnection, result, self.id)

        # Update users both previously and currently in Slice
        users = list(set(current['users']) | set(self.getAttribute('users')))
        for u in users:
            user = q(User).id(u).get().first()
            if user:
                user = user.merge(dbconnection)
                logger.debug("Update user %s after Slice save()" % u)
                logger.debug(user)
                user = user.dict()
            else:
                logger.error("Could not update user after Slice.save(), no answer from Registry")
                logger.warning("Updating the local DB manually")
                user = db.get(dbconnection, table='users', id=u)
                if u in current['users'] and u not in self.getAttribute('users'):
                    # Remove slice from user
                    del u['slices'][self.id]
                elif u not in current['users'] and u in self.getAttribute('users'):
                    # Add slice to user
                    u['slice'].append(self.id)

            db.users(dbconnection, user, u)

        # Update the Project of the slice
        logger.debug("cooko slice: {}".format(self))
        project = db.get(dbconnection, table='projects', id=self.project)
        project['slices'] = project['slices'] + [self.id]
        db.projects(dbconnection, project)

        # Insert / Delete Leases if necessary
        if self.hasLeases:
            flag = -1
            for lease in self.leases:
                # No resources reserved
                if len(result['leases'])==0:
                    flag = -1
                # All resources of a Lease have been succesfully reserved
                elif lease['resources'] == result['leases'][0]['resources']:
                    flag = 0
                # Some Resources of a Lease have been reserved
                elif len(set(lease['resources']).intersection(set(result['leases'][0]['resources']))) > 0:
                    db.leases(dbconnection, lease)
                    flag = 1
            for lease in self.removedLeases:
                if lease not in result['leases']:
                    db.delete(dbconnection, 'leases', lease.id)
                    flag = False
            if flag == -1:
                errors.append("No reservation has been accepted by the testbeds")
            elif flag == 1:
                errors.append("Some resources have been reserved others were unavailable")
                raise SliceWarningException(errors)

        if errors:
            raise SliceException(errors)
        else:
            return True
예제 #9
0
def syncUsers(lock, email=None):
    # DB connection
    dbconnection = db.connect()

    # acquires lock
    with lock:
        logger.info("Worker users starting synchronization")
        try:
            if email:
                users = q(User).filter('email', email).get()
            else:
                users = q(User).get()
            """
            update local user table
            """
            if len(users) > 0:
                # Add users from Registry unkown from local DB
                # this is used to bootstrap with init_user script
                for u in users:
                    logger.debug("looking for {} in local db".format(u.id))
                    if not db.get(dbconnection, table='users', id=u.id):
                        local_users = db.get(dbconnection, table='users')
                        logger.warning("Number of users in local db: %s" %
                                       len(local_users))
                        #print("this user is not in local db, add it")
                        logger.info("Found new user from Registry: %s" % u.id)
                        #logger.info("We don't add the missing users yet, as portal is the single point of entry")
                        db.users(dbconnection, u.dict())

                local_users = db.get(dbconnection, table='users')
                # Update users known in local DB
                for lu in local_users:
                    logger.info("Synchronize user %s" % lu['id'])
                    try:
                        # add status if not present and update on db
                        if not 'status' in lu:
                            lu['status'] = Status.ENABLED
                            lu['enabled'] = format_date()
                        if not users.has(lu['id']) and lu[
                                'status'] is not Status.PENDING:
                            # delete user that has been deleted in Reg
                            db.delete(dbconnection, 'users', lu['id'])
                            logger.info("User {} deleted".format(lu['id']))
                        else:
                            remote_user = next(
                                (item
                                 for item in users if item.id == lu['id']),
                                False)
                            if remote_user:
                                # merge fields of local user with remote
                                # keep local values for
                                # password, private_key, public_key and generate_keys
                                updated_user = remote_user.merge(dbconnection)
                                updated_user = updated_user.dict()
                                # if user has private key
                                # update its Credentials
                                if 'private_key' in updated_user and updated_user[
                                        'private_key'] is not None:
                                    updated_user = update_credentials(
                                        dbconnection, updated_user)
                                # Update user
                                #logger.debug("Update user %s" % updated_user['id'])
                                db.users(dbconnection, updated_user,
                                         updated_user['id'])
                    except Exception as e:
                        logger.warning("Could not synchronize user %s" %
                                       lu['id'])
                        logger.exception(e)
                        raise

            else:
                logger.warning(
                    "Query users is empty, check myslicelib and the connection with SFA Registry"
                )
        except Exception as e:
            import traceback
            traceback.print_exc()
            logger.exception(e)
            raise

        logger.info("Worker users finished period synchronization")
예제 #10
0
    def save(self, dbconnection, setup=None):
        # Get Authority from local DB
        # to update the pi_users after Save
        current = db.get(dbconnection, table='authorities', id=self.id)

        new_users = self.handleDict('users')
        new_pi_users = self.handleDict('pi_users')

        result = super(Authority, self).save(setup)
        errors = result['errors']
        if errors:
            raise AuthorityException(errors)

        logger.debug(result)

        result = {**(self.dict()), **result['data'][0]}
        # add status if not present and update on db
        if not 'status' in result:
            result['status'] = Status.ENABLED
            result['enabled'] = format_date()

        # New Authority created
        if current is None:
            db.authorities(dbconnection, result)
            current = db.get(dbconnection, table='authorities', id=self.id)
        # Update existing authority
        else:
            db.authorities(dbconnection, result, self.id)

        # Create new users under a New Authority
        # Otherwise users are created with User.save()
        for u in new_users:
            if isinstance(u, dict):
                if not "email" in u:
                    raise Warning("user has no email can not be created")
                user = db.get(dbconnection,
                              table='users',
                              filter={'email': u['email']},
                              limit=1)
                # If the user doesn't exit, create it
                if not user:
                    user = User(u)
                    user.setAttribute('authority', self.id)
                    userCreated = user.save(dbconnection)
                    if not userCreated:
                        raise Warning("user has not been created")
                    self.users.append(user.id)
                    modified = True

        # Grant or Revoke PI Rights
        current['pi_users'] = current.get('pi_users', [])
        pi_users = current['pi_users'] + self.getAttribute(
            'pi_users') + new_pi_users
        modified = False
        for u in pi_users:
            if isinstance(u, dict):
                if not "email" in u:
                    raise Warning(
                        "pi_user has no email can not be added as PI")
                user = db.get(dbconnection,
                              table='users',
                              filter={'email': u['email']},
                              limit=1)
                if not user:
                    raise Warning("user does not exist, can't be PI")
                else:
                    user = User(user[0])
            else:
                user = q(User).id(u).get().first()
                user = user.merge(dbconnection)

            # REMOVE PI Rights
            if user.id not in self.getAttribute('pi_users') and not any(
                    d['email'] == user.email for d in new_pi_users):
                self.removePi(user)
                modified = True
            # GRANT PI Rights
            elif user.id not in current['pi_users']:
                self.addPi(user)
                modified = True
            logger.debug("Update user %s in Authority save()" % u)
            logger.debug(user)
            db.users(dbconnection, user.dict(), user.id)

        if modified:
            result = super(Authority, self).save(setup)
            errors = result['errors']
            if errors:
                raise AuthorityException(errors)
            result = {**(self.dict()), **result['data'][0]}
            # add status if not present and update on db
            if not 'status' in result:
                result['status'] = Status.ENABLED
                result['enabled'] = format_date()
            db.authorities(dbconnection, result, self.id)

        return True