view roundup/backends/sessions_sqlite.py @ 6823:fe0091279f50

Refactor session db logging and key generation for sessions/otks While I was working on the redis sessiondb stuff, I noticed that log_wanrning, get_logger ... was duplicated. Also there was code to generate a unique key for otks that was duplicated. Changes: creating new sessions_common.py and SessionsCommon class to provide methods: log_warning, log_info, log_debug, get_logger, getUniqueKey getUniqueKey method is closer to the method used to make session keys in client.py. sessions_common.py now report when random_.py chooses a weak random number generator. Removed same from rest.py. get_logger reconciles all logging under roundup.hyperdb.backends.<name of BasicDatabase class> some backends used to log to root logger. have BasicDatabase in other sessions_*.py modules inherit from SessionCommon. change logging to use log_* methods. In addition: remove unused imports reported by flake8 and other formatting changes modify actions.py, rest.py, templating.py to use getUniqueKey method. add tests for new methods test_redis_session.py swap out ModuleNotFoundError for ImportError to prevent crash in python2 when redis is not present. allow injection of username:password or just password into redis connection URL. set pytest_redis_pw envirnment variable to password or user:password when running test.
author John Rouillard <rouilj@ieee.org>
date Sun, 07 Aug 2022 01:51:11 -0400
parents 375d40a9e730
children a96a239db0d9
line wrap: on
line source

"""This module defines a very basic store that's used by the CGI interface
to store session and one-time-key information.

Yes, it's called "sessions" - because originally it only defined a session
class. It's now also used for One Time Key handling too.

We needed to split commits to session/OTK database from commits on the
main db structures (user data). This required two connections to the
sqlite db, which wasn't supported. This module was created so sqlite
didn't have to use dbm for the session/otk data. It hopefully will
provide a performance speedup.
"""
__docformat__ = 'restructuredtext'

from roundup.backends import sessions_rdbms


class BasicDatabase(sessions_rdbms.BasicDatabase):
    ''' Provide a nice encapsulation of an RDBMS table.

        Keys are id strings, values are automatically marshalled data.
    '''
    name = None

    def __init__(self, db):
        self.db = db
        self.conn, self.cursor = self.db.sql_open_connection(dbname=self.name)

        self.sql('''SELECT name FROM sqlite_master WHERE type='table' AND '''
                 '''name='%ss';''' % self.name)
        table_exists = self.cursor.fetchone()

        if not table_exists:
            # create table/rows etc.
            self.sql('''CREATE TABLE %(name)ss (%(name)s_key VARCHAR(255),
            %(name)s_value TEXT, %(name)s_time REAL)''' % {"name": self.name})
            self.sql('CREATE INDEX %(name)s_key_idx ON '
                     '%(name)ss(%(name)s_key)' % {"name": self.name})
            self.commit()

    def sql(self, sql, args=None, cursor=None):
        """ Execute the sql with the optional args.
        """
        self.log_debug('SQL %r %r' % (sql, args))
        if not cursor:
            cursor = self.cursor
        if args:
            cursor.execute(sql, args)
        else:
            cursor.execute(sql)


class Sessions(BasicDatabase):
    name = 'session'


class OneTimeKeys(BasicDatabase):
    name = 'otk'

# vim: set et sts=4 sw=4 :

Roundup Issue Tracker: http://roundup-tracker.org/