Commit 49b866ae authored by Hanspeter Spalinger's avatar Hanspeter Spalinger
Browse files

allow options to pass to CronParser

parent 5b479547
...@@ -36,7 +36,7 @@ def main(argv): ...@@ -36,7 +36,7 @@ def main(argv):
"""main program.""" """main program."""
parser = ArgumentParser(description="Parse crontab files and check each type of line for potential syntax errors.") parser = ArgumentParser(description="Parse crontab files and check each type of line for potential syntax errors.")
parser.add_argument('crontab', help='the crontab file to parse') parser.add_argument('crontab', help='the crontab file to parse')
parser.add_argument('--whitelist','-w', action='append', dest='whitelisted_users', help='A user to ignore when warning of unrecognized users This argument may be passed multiple times.', default=None) parser.add_argument('--whitelist','-w', action='append', dest='whitelisted_users', help='A user to ignore when warning of unrecognized users This argument may be passed multiple times.', default=['postgres', 'buildbot'])
parser.add_argument('-n', '--no-check-passwd', action='store_false', dest='check_passwd', help='Disable user lookup') parser.add_argument('-n', '--no-check-passwd', action='store_false', dest='check_passwd', help='Disable user lookup')
args = parser.parse_args() args = parser.parse_args()
......
...@@ -78,9 +78,6 @@ import re ...@@ -78,9 +78,6 @@ import re
import string import string
# The following usernames are created locally by packages.
USER_WHITELIST = set(('postgres', 'buildbot',
))
# The following extensions imply further postprocessing or that the slack # The following extensions imply further postprocessing or that the slack
# role was for a cron that allowed dots in cron scripts. # role was for a cron that allowed dots in cron scripts.
FILE_RE_WHITELIST = [re.compile(x) for x in FILE_RE_WHITELIST = [re.compile(x) for x in
...@@ -693,11 +690,16 @@ class CronLineTimeAction(object): ...@@ -693,11 +690,16 @@ class CronLineTimeAction(object):
Must be used as a subclass - subclass must implement _CheckTimeField. Must be used as a subclass - subclass must implement _CheckTimeField.
""" """
def __init__(self, time_field, user, command, check_passwd=True): def __init__(self, time_field, user, command, options):
self.time_field = time_field self.time_field = time_field
self.user = user self.user = user
self.command = command self.command = command
self.check_passwd = check_passwd self.whitelisted_users = []
if 'whitelisted_users' in options:
self.whitelisted_users = options.whitelisted_users
self_check_passwd = True
if 'check_passwd' in options:
self.check_passwd = options.check_passwd
def _CheckTimeField(self, log): def _CheckTimeField(self, log):
"""Virtual method to be implemented by subclasses to check time field.""" """Virtual method to be implemented by subclasses to check time field."""
...@@ -712,7 +714,7 @@ class CronLineTimeAction(object): ...@@ -712,7 +714,7 @@ class CronLineTimeAction(object):
self._CheckTimeField(log) self._CheckTimeField(log)
# User checks. # User checks.
if self.user in USER_WHITELIST: if self.user in self.whitelisted_users:
return return
elif len(self.user) > 31: elif len(self.user) > 31:
log.LineError(log.MSG_INVALID_USER, log.LineError(log.MSG_INVALID_USER,
...@@ -807,11 +809,12 @@ class CronLineFactory(object): ...@@ -807,11 +809,12 @@ class CronLineFactory(object):
def __init__(self): def __init__(self):
pass pass
def ParseLine(self, line): def ParseLine(self, line, options ):
"""Classify a line. """Classify a line.
Args: Args:
line: The line to classify. line: The line to classify.
options: a dictionary with options to pass to the CronLineAction Object
Returns: Returns:
A CronLine* class (must have a ValidateAndLog method). A CronLine* class (must have a ValidateAndLog method).
...@@ -842,7 +845,7 @@ class CronLineFactory(object): ...@@ -842,7 +845,7 @@ class CronLineFactory(object):
match = at_line_re.match(line) match = at_line_re.match(line)
if match: if match:
return CronLineAt(match.groups()[0], match.groups()[1], return CronLineAt(match.groups()[0], match.groups()[1],
match.groups()[2]) match.groups()[2], options)
# Is this line a cron job specifier? # Is this line a cron job specifier?
match = time_field_job_line_re.match(line) match = time_field_job_line_re.match(line)
...@@ -854,7 +857,7 @@ class CronLineFactory(object): ...@@ -854,7 +857,7 @@ class CronLineFactory(object):
'month': match.groups()[3], 'month': match.groups()[3],
'day of week': match.groups()[4], 'day of week': match.groups()[4],
} }
return CronLineTime(field, match.groups()[5], match.groups()[6]) return CronLineTime(field, match.groups()[5], match.groups()[6], options)
return CronLineUnknown() return CronLineUnknown()
...@@ -1075,8 +1078,8 @@ def check_crontab(arguments, log): ...@@ -1075,8 +1078,8 @@ def check_crontab(arguments, log):
return log.Summary() return log.Summary()
# Add the any specified users to the whitelist # Add the any specified users to the whitelist
if arguments.whitelisted_users: #if arguments.whitelisted_users:
USER_WHITELIST.update(arguments.whitelisted_users) # USER_WHITELIST.update(arguments.whitelisted_users)
# Check the file name. # Check the file name.
if re.search('[^A-Za-z0-9_-]', os.path.basename(arguments.crontab)): if re.search('[^A-Za-z0-9_-]', os.path.basename(arguments.crontab)):
...@@ -1098,7 +1101,7 @@ def check_crontab(arguments, log): ...@@ -1098,7 +1101,7 @@ def check_crontab(arguments, log):
line = line.strip() line = line.strip()
line_no += 1 line_no += 1
cron_line = cron_line_factory.ParseLine(line) cron_line = cron_line_factory.ParseLine(line,arguments)
cron_line.ValidateAndLog(log) cron_line.ValidateAndLog(log)
log.Emit(line_no, line) log.Emit(line_no, line)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment