Mercurial > p > roundup > code
comparison roundup/rest.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 | 3f60a71b0812 |
| children | 9811073b289e |
comparison
equal
deleted
inserted
replaced
| 6822:5053ee6c846b | 6823:fe0091279f50 |
|---|---|
| 47 try: | 47 try: |
| 48 basestring | 48 basestring |
| 49 except NameError: | 49 except NameError: |
| 50 basestring = str | 50 basestring = str |
| 51 unicode = str | 51 unicode = str |
| 52 | |
| 53 import roundup.anypy.random_ as random_ | |
| 54 | |
| 55 import logging | |
| 56 logger = logging.getLogger('roundup.rest') | |
| 57 | |
| 58 if not random_.is_weak: | |
| 59 logger.debug("Importing good random generator") | |
| 60 else: | |
| 61 logger.warning("**SystemRandom not available. Using poor random generator") | |
| 62 | |
| 63 chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | |
| 64 | 52 |
| 65 | 53 |
| 66 def _data_decorator(func): | 54 def _data_decorator(func): |
| 67 """Wrap the returned data into an object.""" | 55 """Wrap the returned data into an object.""" |
| 68 def format_object(self, *args, **kwargs): | 56 def format_object(self, *args, **kwargs): |
| 1138 @_data_decorator | 1126 @_data_decorator |
| 1139 def get_post_once_exactly(self, class_name, input): | 1127 def get_post_once_exactly(self, class_name, input): |
| 1140 """Get the Post Once Exactly token to create a new instance of class | 1128 """Get the Post Once Exactly token to create a new instance of class |
| 1141 See https://tools.ietf.org/html/draft-nottingham-http-poe-00""" | 1129 See https://tools.ietf.org/html/draft-nottingham-http-poe-00""" |
| 1142 otks = self.db.Otk | 1130 otks = self.db.Otk |
| 1143 poe_key = ''.join([random_.choice(chars) for x in range(40)]) | 1131 poe_key = otks.getUniqueKey() |
| 1144 while otks.exists(u2s(poe_key)): | |
| 1145 poe_key = ''.join([random_.choice(chars) for x in range(40)]) | |
| 1146 | 1132 |
| 1147 try: | 1133 try: |
| 1148 lifetime = int(input['lifetime'].value) | 1134 lifetime = int(input['lifetime'].value) |
| 1149 except KeyError: | 1135 except KeyError: |
| 1150 lifetime = 30 * 60 # 30 minutes | 1136 lifetime = 30 * 60 # 30 minutes |
