예제 #1
0
    def test_del_before(self):
        """
        test delete rows from reporting table which are older than date
        """
        Session = None
        try:
            engine = engine_from_config(config, 'sqlalchemy.')
            Session = scoped_session(sessionmaker(autocommit=False,
                                                  autoflush=True))
            Session.configure(bind=engine)

            # create table entries
            today = datetime.now()
            yesterday = today - timedelta(days=1)
            two_days_ago = today - timedelta(days=2)

            # create old reports:
            report_2 = Reporting(timestamp=two_days_ago,
                                 event='token_init',
                                 realm='mydefrealm',
                                 parameter='active',
                                 count=1)
            report_1 = Reporting(timestamp=yesterday,
                                 event='token_init',
                                 realm='mydefrealm',
                                 parameter='active',
                                 count=2)
            report_0 = Reporting(event='token_init',
                                 realm='mydefrealm',
                                 parameter='active',
                                 count=3)
            Session.add(report_0)
            Session.add(report_1)
            Session.add(report_2)
            Session.commit()

            # check if reports are in database
            table_content = Session.query(Reporting).count()
            self.assertEqual(table_content, 3, table_content)

            # delete reports
            yest = yesterday.strftime("%Y-%m-%d")
            parameter = {'date': yest, 'realms': '*', 'status': 'active'}
            response = self.make_authenticated_request(controller='reporting',
                                                       action='delete_before',
                                                       params=parameter)
            resp = json.loads(response.body)
            values = resp.get('result')
            self.assertEqual(values.get('status'), True, response)
            self.assertEqual(values.get('value'), 1, response)
        finally:
            if Session:
                Session.close()
예제 #2
0
    def test_delete_all_reports(self):
        Session = None
        try:
            engine = engine_from_config(config, 'sqlalchemy.')
            Session = scoped_session(
                sessionmaker(autocommit=False, autoflush=True))
            Session.configure(bind=engine)

            # check if table is empty
            table_content = Session.query(Reporting).count()
            self.assertEqual(table_content, 0, table_content)

            # create table entries
            today = datetime.now()
            yesterday = today - timedelta(days=1)

            report_1 = Reporting(timestamp=yesterday,
                                 event='token_init',
                                 realm='mydefrealm',
                                 parameter='active',
                                 count=1)
            report_2 = Reporting(event='token_init',
                                 realm='mydefrealm',
                                 parameter='active',
                                 count=2)
            Session.add(report_1)
            Session.add(report_2)
            Session.commit()

            # check if reports are in database
            table_content = Session.query(Reporting).count()
            self.assertEqual(table_content, 2, table_content)

            # delete reports
            response = self.make_authenticated_request(controller='reporting',
                                                       action='delete_all',
                                                       params={
                                                           'realm': '*',
                                                           'status': 'active'
                                                       })
            resp = json.loads(response.body)
            values = resp.get('result')
            self.assertEqual(values.get('status'), True, response)
            self.assertEqual(values.get('value'), 2, response)

            # refresh Session
            Session.commit()
            # check if database table is empty
            table_content = Session.query(Reporting).count()
            self.assertEqual(table_content, 0, table_content)
        finally:
            if Session:
                Session.close()
예제 #3
0
    def test_del_before(self):
        """
        test delete rows from reporting table which are older than date
        """
        self.delete_all_reports()

        today = datetime.now()
        yesterday = today - timedelta(days=1)
        two_days_ago = today - timedelta(days=2)

        # create old reports:
        report_2 = Reporting(timestamp=two_days_ago,
                             event='token_init',
                             realm='mydefrealm',
                             parameter='active',
                             count=1)
        report_1 = Reporting(timestamp=yesterday,
                             event='token_init',
                             realm='mydefrealm',
                             parameter='active',
                             count=2)
        report_0 = Reporting(event='token_init',
                             realm='mydefrealm',
                             parameter='active',
                             count=3)

        with DBSession() as session:
            # check if reports are in database
            session.query(Reporting).delete()
            session.commit()

        with DBSession() as session:

            session.add(report_0)
            session.add(report_1)
            session.add(report_2)
            session.commit()

        with DBSession() as session:
            # check if reports are in database
            table_content = session.query(Reporting).count()
            self.assertEqual(table_content, 3, table_content)

        # delete reports
        yest = yesterday.strftime("%Y-%m-%d")
        parameter = {'date': yest, 'realms': '*', 'status': 'active'}
        response = self.make_authenticated_request(controller='reporting',
                                                   action='delete_before',
                                                   params=parameter)
        resp = json.loads(response.body)
        values = resp.get('result')
        self.assertEqual(values.get('status'), True, response)
        self.assertEqual(values.get('value'), 1, response)
예제 #4
0
    def test_delete_all_reports(self):

        with DBSession() as session:
            # check if table is empty
            table_content = session.query(Reporting).count()
            self.assertEqual(table_content, 0, table_content)

        # create table entries
        today = datetime.now()
        yesterday = today - timedelta(days=1)

        report_1 = Reporting(timestamp=yesterday,
                             event='token_init',
                             realm='mydefrealm',
                             parameter='active',
                             count=1)
        report_2 = Reporting(event='token_init',
                             realm='mydefrealm',
                             parameter='active',
                             count=2)

        with DBSession() as session:
            session.add(report_1)
            session.add(report_2)
            session.commit()

        with DBSession() as session:
            # check if reports are in database
            table_content = session.query(Reporting).count()
            self.assertEqual(table_content, 2, table_content)

        # delete reports
        response = self.make_authenticated_request(controller='reporting',
                                                   action='delete_all',
                                                   params={
                                                       'realm': '*',
                                                       'status': 'active'
                                                   })

        resp = json.loads(response.body)
        values = resp.get('result')
        self.assertEqual(values.get('status'), True, response)
        self.assertEqual(values.get('value'), 2, response)

        with DBSession() as session:
            # check if database table is empty
            table_content = session.query(Reporting).count()
            self.assertEqual(table_content, 0, table_content)
예제 #5
0
def token_reporting(event, tokenrealms):
    """
    log token events into reporting table

    :param event: the event that happened, e.g. init token, delete token
    :param tokenrealms: the realm on which the event happened
    :return: nothing
    """
    realms = tokenrealms
    if not tokenrealms or len(tokenrealms) == 0:
        realms = ['/:no realm:/']
    elif not isinstance(tokenrealms, (list, tuple)):
        realms = [tokenrealms]

    for realm in realms:
        action = check_token_reporting(realm)
        mh = MonitorHandler()
        counters = mh.token_count(realm, action[:])
        for key, val in counters.items():
            report = Reporting(event=event,
                               realm=realm,
                               parameter=key,
                               count=val)
            try:
                Session.add(report)
            except Exception as exce:
                log.exception('Error during saving report. Exception was: '
                              '%r' % exce)