From 5dc53f2282d1d97c3b5baf730bb4b8165f06d8a1 Mon Sep 17 00:00:00 2001 From: JR Conlin Date: Tue, 30 Apr 2024 14:33:08 -0700 Subject: [PATCH] Feat/range2 (#1550) * bug: validate val names * not sure why we look for two args when the secret should always be last * fix names of the range vals to match opts * normalize `range` to `uid_range` everywhere to prevent possible problems. --- tools/tokenserver/database.py | 16 +++++++-------- tools/tokenserver/purge_old_records.py | 27 +++++++++++++++++--------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/tools/tokenserver/database.py b/tools/tokenserver/database.py index 79a2e6b510..1e8d7be249 100644 --- a/tools/tokenserver/database.py +++ b/tools/tokenserver/database.py @@ -440,17 +440,17 @@ def get_user_records(self, email): finally: res.close() - def _build_old_user_query(self, range, params, **kwargs): - if range: + def _build_old_user_query(self, uid_range, params, **kwargs): + if uid_range: # construct the range from the passed arguments rstr = [] try: - if range[0]: + if uid_range[0]: rstr.append("uid > :start") - params["start"] = range[0] - if range[1]: + params["start"] = uid_range[0] + if uid_range[1]: rstr.append("uid < :end") - params["end"] = range[1] + params["end"] = uid_range[1] except IndexError: pass rrep = " and ".join(rstr) @@ -462,7 +462,7 @@ def _build_old_user_query(self, range, params, **kwargs): return sql def get_old_user_records(self, grace_period=-1, limit=100, - offset=0, range=None): + offset=0, uid_range=None): """Get user records that were replaced outside the grace period.""" if grace_period < 0: grace_period = 60 * 60 * 24 * 7 # one week, in seconds @@ -474,7 +474,7 @@ def get_old_user_records(self, grace_period=-1, limit=100, "offset": offset } - sql = self._build_old_user_query(range, params) + sql = self._build_old_user_query(uid_range, params) res = self._execute_sql(sql, **params) try: diff --git a/tools/tokenserver/purge_old_records.py b/tools/tokenserver/purge_old_records.py index 6a53ccdf83..89f90a59f6 100644 --- a/tools/tokenserver/purge_old_records.py +++ b/tools/tokenserver/purge_old_records.py @@ -29,8 +29,12 @@ from database import Database from util import format_key_id -logger = logging.getLogger("tokenserver.scripts.purge_old_records") -logger.setLevel(os.environ.get("PYTHON_LOG", "ERROR").upper()) +LOGGER = "tokenserver.scripts.purge_old_records" + +logger = logging.getLogger(LOGGER) +log_level = os.environ.get("PYTHON_LOG", "INFO").upper() +logger.setLevel(log_level) +logger.debug(f"Setting level to {log_level}") PATTERN = "{node}/1.5/{uid}" @@ -70,7 +74,7 @@ def purge_old_records( "grace_period": grace_period, "limit": max_per_loop, "offset": offset, - "range": uid_range, + "uid_range": uid_range, } rows = list(database.get_old_user_records(**kwds)) if not rows: @@ -250,6 +254,7 @@ def main(args=None): This function parses command-line arguments and passes them on to the purge_old_records() function. """ + logger = logging.getLogger(LOGGER) usage = "usage: %prog [options] secret" parser = optparse.OptionParser(usage=usage) parser.add_option( @@ -336,17 +341,21 @@ def main(args=None): ) opts, args = parser.parse_args(args) - if len(args) != 2: + + if len(args) == 0: parser.print_usage() return 1 - secret = args[1] + # Secret is the last arg? + secret = args[-1] + logger.debug(f"Secret: {secret}") util.configure_script_logging(opts) uid_range = None - if opts.start_range or opts.end_range: - uid_range = (opts.start_range, opts.end_range) + if opts.range_start or opts.range_end: + uid_range = (opts.range_start, opts.range_end) + logger.debug(f"Looking in range {uid_range}") purge_old_records( secret, @@ -358,7 +367,7 @@ def main(args=None): dryrun=opts.dryrun, force=opts.force, override_node=opts.override_node, - range=uid_range, + uid_range=uid_range, ) if not opts.oneshot: while True: @@ -378,7 +387,7 @@ def main(args=None): dryrun=opts.dryrun, force=opts.force, override_node=opts.override_node, - range=uid_range, + uid_range=uid_range, ) return 0