Example #1
0
    def get_current_user(self):
        username = self.request.headers.get(settings.user_auth_header)
        if not username:
            return

        # Users must be fully qualified
        if not re.match("^{}$".format(USERNAME_VALIDATION), username):
            raise InvalidUser()

        try:
            user, created = User.get_or_create(self.session, username=username)
            if created:
                logging.info("Created new user %s", username)
                self.session.commit()
                # Because the graph doesn't initialize until the updates table
                # is populated, we need to refresh the graph here in case this
                # is the first update.
                self.graph.update_from_db(self.session)
        except sqlalchemy.exc.OperationalError:
            # Failed to connect to database or create user, try to reconfigure the db. This invokes
            # the fetcher to try to see if our URL string has changed.
            Session.configure(bind=get_db_engine(get_database_url(settings)))
            raise DatabaseFailure()

        return user
Example #2
0
 def run(self):
     while True:
         logging.debug("Sending emails...")
         try:
             session = Session()
             self.send_emails(session, datetime.utcnow())
             session.commit()
             session.close()
         except OperationalError:
             Session.configure(
                 bind=get_db_engine(get_database_url(self.settings)))
             logging.critical("Failed to connect to database.")
         sleep(60)
Example #3
0
def session(request, tmpdir):
    db_path = tmpdir.join("grouper.sqlite")
    db_engine = get_db_engine("sqlite:///%s" % db_path)

    models.Model.metadata.create_all(db_engine)
    Session.configure(bind=db_engine)
    session = Session()

    def fin():
        session.close()
        # Useful if testing against MySQL
        # models.Model.metadata.drop_all(db_engine)
    request.addfinalizer(fin)

    return session
Example #4
0
def session(request, tmpdir):
    db_path = tmpdir.join("grouper.sqlite")
    db_engine = get_db_engine("sqlite:///%s" % db_path)

    models.Model.metadata.create_all(db_engine)
    Session.configure(bind=db_engine)
    session = Session()

    def fin():
        session.close()
        # Useful if testing against MySQL
        #models.Model.metadata.drop_all(db_engine)
    request.addfinalizer(fin)

    return session
Example #5
0
    def run(self):
        while True:
            sleep(self.refresh_interval)

            logging.debug("Updating Graph from Database.")
            try:
                session = Session()
                self.graph.update_from_db(session)
                session.close()
                stats.set_gauge("successful-db-update", 1)
            except OperationalError:
                Session.configure(bind=get_db_engine(get_database_url(self.settings)))
                logging.critical("Failed to connect to database.")
                stats.set_gauge("successful-db-update", 0)
                self.capture_exception()
            except:
                stats.set_gauge("successful-db-update", 0)
                self.capture_exception()
                raise
Example #6
0
def sync_db_command(args):
    db_engine = get_db_engine(get_database_url(settings))
    Model.metadata.create_all(db_engine)

    # Add some basic database structures we know we will need if they don't exist.
    session = make_session()

    for name, description in SYSTEM_PERMISSIONS:
        test = Permission.get(session, name)
        if test:
            continue
        permission = Permission(name=name, description=description)
        try:
            permission.add(session)
            session.flush()
        except IntegrityError:
            session.rollback()
            raise Exception('Failed to create permission: %s' % (name, ))
        session.commit()

    # This group is needed to bootstrap a Grouper installation.
    admin_group = Group.get(session, name="grouper-administrators")
    if not admin_group:
        admin_group = Group(
                groupname="grouper-administrators",
                description="Administrators of the Grouper system.",
                canjoin="nobody",
        )

        try:
            admin_group.add(session)
            session.flush()
        except IntegrityError:
            session.rollback()
            raise Exception('Failed to create group: grouper-administrators')

        for permission_name in (GROUP_ADMIN, PERMISSION_ADMIN, USER_ADMIN):
            permission = Permission.get(session, permission_name)
            assert permission, "Permission should have been created earlier!"
            admin_group.grant_permission(permission)

        session.commit()
Example #7
0
 def run(self):
     while True:
         try:
             session = Session()
             logging.debug("Expiring edges....")
             self.expire_edges(session)
             logging.debug("Sending emails...")
             process_async_emails(self.settings, session, datetime.utcnow())
             logging.debug("Pruning old traces....")
             prune_old_traces(session)
             session.commit()
             session.close()
             stats.set_gauge("successful-background-run", 1)
         except OperationalError:
             Session.configure(bind=get_db_engine(get_database_url(self.settings)))
             logging.critical("Failed to connect to database.")
             stats.set_gauge("successful-background-run", 0)
             self.capture_exception()
         except:
             stats.set_gauge("successful-background-run", 0)
             self.capture_exception()
             raise
         sleep(60)
Example #8
0
def make_session():
    db_engine = get_db_engine(get_database_url(settings))
    Session.configure(bind=db_engine)
    return Session()