Skip to content

Commit

Permalink
Feat/range2 (#1550)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jrconlin authored Apr 30, 2024
1 parent 97389f4 commit 5dc53f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
16 changes: 8 additions & 8 deletions tools/tokenserver/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand Down
27 changes: 18 additions & 9 deletions tools/tokenserver/purge_old_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit 5dc53f2

Please sign in to comment.