示例#1
0
 def next_run_id(cls):
     session = Database().Session
     max_run_id = session.query(sql.func.max(cls.run_id)).scalar()
     if max_run_id:
         return max_run_id + 1
     else:
         return 1
示例#2
0
    def get(self, id=None):
        if id:
            t = db_Result.get(id)
        else:

            session = Database().Session
            q = session.query(db_Result)

            if request.args.get('state') == 'open':
                q = q.filter(db_Result.finished_at == None)

            # q = q.join(db_Result)
            if request.args.get('task_id'):
             q = q.filter_by(task_id=request.args.get('task_id'))

            q = q.join(Organization).join(Node).join(Task, db_Result.task).join(Collaboration)
            if request.args.get('node_id'):
                q = q.filter(db.Node.id==request.args.get('node_id'))\
                    .filter(db.Collaboration.id==db.Node.collaboration_id)

            t = q.all()

        if request.args.get('include') == 'task':
            s = result_inc_schema
        else:
            s = result_schema

        return s.dump(t, many=not bool(id)).data, HTTPStatus.OK
示例#3
0
 def setUpClass(cls):
     Database().connect("sqlite://", allow_drop_all=True)
     # FIXME: move path generation to a function in vantage6.server
     file_ = str(PACAKAGE_FOLDER / APPNAME / "server" / "_data" /
                 "unittest_fixtures.yaml")
     with open(file_) as f:
         cls.entities = yaml.safe_load(f.read())
     load(cls.entities, drop_all=True)
示例#4
0
    def get(self, id=None):

        # obtain requisters organization
        if g.user:
            auth_org = g.user.organization
        elif g.node:
            auth_org = g.node.organization
        else:  # g.container
            auth_org = Organization.get(g.container['organization_id'])

        if id:
            result = db_Result.get(id)
            if not result:
                return {'msg': f'Result id={id} not found!'}, \
                    HTTPStatus.NOT_FOUND
            if not self.r.v_glo.can():
                c_orgs = result.task.collaboration.organizations
                if not (self.r.v_org.can() and auth_org in c_orgs):
                    return {'msg': 'You lack the permission to do that!'}, \
                        HTTPStatus.UNAUTHORIZED
        else:

            session = Database().Session
            q = session.query(db_Result)

            if request.args.get('state') == 'open':
                q = q.filter(db_Result.finished_at == None)

            # q = q.join(db_Result)
            if request.args.get('task_id'):
                q = q.filter_by(task_id=request.args.get('task_id'))

            q = q.join(Organization).join(Node).join(Task, db_Result.task)\
                .join(Collaboration)
            if request.args.get('node_id'):
                q = q.filter(db.Node.id == request.args.get('node_id'))\
                    .filter(db.Collaboration.id == db.Node.collaboration_id)

            result = q.all()

            # filter results based on permissions
            if not self.r.v_glo.can():
                if self.r.v_org.can():
                    filtered_result = []
                    for res in result:
                        if res.task.collaboration in auth_org.collaborations:
                            filtered_result.append(res)
                    result = filtered_result
                else:
                    return {'msg': 'You lack the permission to do that!'}, \
                        HTTPStatus.UNAUTHORIZED

        if request.args.get('include') == 'task':
            s = result_inc_schema
        else:
            s = result_schema

        return s.dump(result, many=not id).data, HTTPStatus.OK
示例#5
0
 def get_by_(cls, name, scope, operation):
     session = Database().Session
     try:
         return session.query(cls).filter_by(
             name=name,
             operation=operation,
             scope=scope
         ).first()
     except NoResultFound:
         return None
示例#6
0
    def get_by_api_key(cls, api_key):
        """returns Node based on the provided API key.

        Returns None if no Node is associated with api_key.
        """
        session = Database().Session

        try:
            return session.query(cls).filter_by(api_key=api_key).one()
        except NoResultFound:
            return None
示例#7
0
    def get(self):
        """displays the health of services"""

        # test DB
        session = Database().Session
        db_ok = False
        try:
            session.execute('SELECT 1')
            db_ok = True
        except Exception as e:
            log.error("DB not responding")
            log.debug(e)

        return {'database': db_ok}, HTTPStatus.OK
示例#8
0
    def func_with_context(name, config, environment, system_folders,
                          *args, **kwargs):

        # select configuration if none supplied
        if config:
            ctx = ServerContext.from_external_config_file(
                config,
                environment,
                system_folders
            )
        else:
            if name:
                name, environment = (name, environment)
            else:
                try:
                    name, environment = select_configuration_questionaire(
                        system_folders
                    )
                except Exception:
                    error("No configurations could be found!")
                    exit()

            # raise error if config could not be found
            if not ServerContext.config_exists(
                name,
                environment,
                system_folders
            ):
                scope = "system" if system_folders else "user"
                error(
                    f"Configuration {Fore.RED}{name}{Style.RESET_ALL} with "
                    f"{Fore.RED}{environment}{Style.RESET_ALL} does not exist "
                    f"in the {Fore.RED}{scope}{Style.RESET_ALL} folders!"
                )
                exit(1)

            # create server context, and initialize db
            ctx = ServerContext(
                name,
                environment=environment,
                system_folders=system_folders
            )

        # initialize database (singleton)
        allow_drop_all = ctx.config["allow_drop_all"]
        Database().connect(URI=ctx.get_database_uri(), allow_drop_all=allow_drop_all)

        return func(ctx, *args, **kwargs)
示例#9
0
    def get(self):
        """Experimental switch to fix db errors"""

        session = Database().Session

        try:
            session.execute('SELECT 1')

        except InvalidRequestError as e:
            log.error("DB nudge... Does this work?")
            log.debug(e)
            session.invalidate()
            session.rollback()

        finally:
            session.close()
示例#10
0
def tasks(self, collaboration: Collaboration, organization: Organization):
    """A node belongs to a single collaboration. Therefore the node
    only executes task for the organization and collaboration  is
    belongs to.

    Tasks can be assigned to many nodes but are only assigned to
    as single collaboration.
    """

    # find node of the organization that is within the collaboration
    node = collaboration.get_node_from_organization(organization)

    session = Database().Session
    tasks = session.query(Task)\
        .join(Collaboration)\
        .join(Node)\
        .filter_by(collabotation_id=collaboration.id)\
        .filter(Node == node.id)\
        .all()

    return tasks
示例#11
0
    def rule_exists_in_db(name, scope, operation):
        """Check if the rule exists in the DB.

        Parameters
        ----------
        name : String
            (Unique) name of the rule
        scope : Scope (Enum)
            Scope the rule
        operation : Operation (Enum)
            Operation on the entity

        Returns
        -------
        Boolean
            Whenever this rule exists in the database or not
        """
        return Database().Session.query(Rule).filter_by(
            name=name,
            operation=operation,
            scope=scope
        ).scalar()
示例#12
0
    def setUpClass(cls):
        """Called immediately before running a test method."""
        Database().connect("sqlite://", allow_drop_all=True)
        file_ = str(PACAKAGE_FOLDER / APPNAME / "server" / "_data" /
                    "unittest_fixtures.yaml")
        with open(file_) as f:
            cls.entities = yaml.safe_load(f.read())
        load(cls.entities, drop_all=True)

        server.app.testing = True
        server.app.response_class = Response
        server.app.test_client_class = TestNode
        server.app.secret_key = "test-secret"

        ctx = context.TestContext.from_external_config_file(
            "unittest_config.yaml")

        server.init_resources(ctx)

        cls.app = server.app.test_client()

        cls.credentials = {
            'root': {
                'username': '******',
                'password': '******'
            },
            'admin': {
                'username': '******',
                'password': '******'
            },
            'user': {
                'username': '******',
                'password': '******'
            },
            'user-to-delete': {
                'username': '******',
                'password': '******'
            }
        }
示例#13
0
def get_node(self, result: Result):
    """A node is assigned by an organization to an collaboration.
    Tasks, that is responsible for a set of Results, are assigned
    to these collaborations.

    A Result is therefore directly related to a single node by it's
    organization and collaboration. Organization can be directly
    derived from the Result while the collaboration can be found from
    the originating task.
    """
    session = Database().Session
    node = session.query(Node)\
        .join(Collaboration)\
        .join(Task)\
        .join(Organization)\
        .join(Result)\
        .filter_by(result_id=result.id)\
        .filter(Task.organization_id == Node.organization_id)\
        .filter(Task.collaboration_id == Node.collaboration_id)\
        .one()

    return node
示例#14
0
 def username_exists(cls, username):
     session = Database().Session
     return session.query(exists().where(cls.username == username)).scalar()
示例#15
0
 def get_user_list(cls, filters=None):
     session = Database().Session
     return session.query(cls).all()
示例#16
0
 def get_by_email(cls, email):
     session = Database().Session
     return session.query(cls).filter_by(email=email).one()
示例#17
0
 def get_by_username(cls, username):
     session = Database().Session
     return session.query(cls).filter_by(username=username).one()
示例#18
0
 def exists(cls, organization_id, collaboration_id):
     session = Database().Session
     return session.query(cls).filter_by(
         organization_id=organization_id,
         collaboration_id=collaboration_id
     ).scalar()
示例#19
0
 def tearDownClass(cls):
     Database().drop_all()
示例#20
0
 def exists(cls, field, value):
     session = Database().Session
     return session.query(exists().where(getattr(cls, field) == value))\
         .scalar()
示例#21
0
 def tearDownClass(cls):
     Database().close()
示例#22
0
def load(fixtures, drop_all=False):
    # TODO we are not sure the DB is connected here....

    if drop_all:
        Database().drop_all()

    log.info("Create Organizations and Users")
    for org in fixtures.get("organizations", {}):

        # create organization
        organization = db.Organization(
            **{
                k: org[k]
                for k in [
                    "name", "domain", "address1", "address2", "zipcode",
                    "country", "public_key"
                ]
            })
        organization.save()
        log.debug(f"processed organization={organization.name}")
        superuserrole = db.Role(name="super",
                                description="Super user",
                                rules=db.Rule.get(),
                                organization=organization)
        # create users
        for usr in org.get("users", {}):
            user = db.User(**usr)
            user.roles = [superuserrole]
            user.organization = organization
            user.save()
            log.debug(f"processed user={user.username}")

    log.info("Create collaborations")
    for col in fixtures.get("collaborations", {}):

        # create collaboration
        collaboration = db.Collaboration(name=col.get("name"),
                                         encrypted=col.get("encrypted", True))
        log.debug(f"processed collaboration={collaboration.name}")

        # append organizations to the collaboration

        for participant in col.get("participants", {}):

            if isinstance(participant, str):
                org_name = participant
                node_api_key = str(uuid.uuid1())
            else:  # == isinstance(participant, dict):
                org_name = participant.get("name")
                node_api_key = participant.get("api-key", str(uuid.uuid1()))

            organization = db.Organization.get_by_name(org_name)
            collaboration.organizations.append(organization)
            log.debug(f"added {org_name} to the collaboration")

            node = db.Node(
                organization=organization,
                collaboration=collaboration,
                name=f"{organization.name} - {collaboration.name} Node",
                api_key=node_api_key)
            node.save()
            log.debug(f"added node {node.name} to {collaboration.name}")

        collaboration.save()

        # append dummy tasks to the collaboration
        log.debug("Processing Task Assignments")
        for image in col.get("tasks", {}):
            initiator = collaboration.organizations[0]
            task = db.Task(name=f"Example task",
                           image=image,
                           collaboration=collaboration,
                           run_id=db.Task.next_run_id(),
                           initiator=initiator)

            for organization in collaboration.organizations:
                result = db.Result(task=task,
                                   input="something",
                                   organization=organization)
                result.save()

            task.save()
            log.debug(f"Processed task {task.name}")
示例#23
0
 def get_by_name(cls, name):
     session = Database().Session
     try:
         return session.query(cls).filter_by(name=name).first()
     except NoResultFound:
         return None