Skip to main content
Sign in
Snippets Groups Projects
Commit a90c3192 authored by Jeremy Jarrell's avatar Jeremy Jarrell
Browse files

Adds any users specified on the command line to the list of whitelisted users.

parent 9e15c34c
Branches
Tags
No related merge requests found
...@@ -36,15 +36,15 @@ def main(argv): ...@@ -36,15 +36,15 @@ def main(argv):
print('ERROR: No crontab file was specified.') print('ERROR: No crontab file was specified.')
sys.exit(1) sys.exit(1)
whitelisted_users = None
if len(argv) > 2: if len(argv) > 2:
for arg in argv: for arg in argv:
if '--whitelist' in arg: if '--whitelist' in arg:
whitelist_users = arg['--whitelist='.__len__():].split(',') whitelisted_users = arg['--whitelist='.__len__():].split(',')
log = check.LogCounter() log = check.LogCounter()
print('Checking correctness of %s' % argv[1]) print('Checking correctness of %s' % argv[1])
return check.check_crontab(argv[1], log) return check.check_crontab(argv[1], log, whitelisted_users)
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main(sys.argv)) sys.exit(main(sys.argv))
\ No newline at end of file
...@@ -1043,7 +1043,7 @@ class LogCounter(object): ...@@ -1043,7 +1043,7 @@ class LogCounter(object):
return self._error_count return self._error_count
def check_crontab(crontab_file, log): def check_crontab(crontab_file, log, whitelisted_users=None):
"""Check a crontab file. """Check a crontab file.
Checks crontab_file for a variety of errors or potential errors. This only Checks crontab_file for a variety of errors or potential errors. This only
...@@ -1063,6 +1063,10 @@ def check_crontab(crontab_file, log): ...@@ -1063,6 +1063,10 @@ def check_crontab(crontab_file, log):
if not os.path.exists(crontab_file): if not os.path.exists(crontab_file):
return log.Summary() return log.Summary()
# Add the any specified users to the whitelist
if not whitelisted_users is None:
USER_WHITELIST.update(whitelisted_users)
# Check the file name. # Check the file name.
if re.search('[^A-Za-z0-9_-]', os.path.basename(crontab_file)): if re.search('[^A-Za-z0-9_-]', os.path.basename(crontab_file)):
in_whitelist = False in_whitelist = False
... ...
......
...@@ -307,11 +307,11 @@ class CheckCrontabUnitTest(unittest.TestCase): ...@@ -307,11 +307,11 @@ class CheckCrontabUnitTest(unittest.TestCase):
exp_rc = 0 exp_rc = 0
return (exp_warn, exp_fail, exp_rc) return (exp_warn, exp_fail, exp_rc)
def CheckACrontab(self, crontab): def CheckACrontab(self, crontab, whitelisted_users=None):
log = check.LogCounter() log = check.LogCounter()
crontab_file = os.path.join(BASE_PATH, crontab) crontab_file = os.path.join(BASE_PATH, crontab)
(exp_warn, exp_fail, exp_rc) = self.GetExpWFRs(crontab_file) (exp_warn, exp_fail, exp_rc) = self.GetExpWFRs(crontab_file)
self.assertEquals(check.check_crontab(crontab_file, log), exp_rc, self.assertEquals(check.check_crontab(crontab_file, log, whitelisted_users), exp_rc,
'Failed to return %d for crontab errors.' % exp_rc) 'Failed to return %d for crontab errors.' % exp_rc)
self.assertEquals(log.warn_count, exp_warn, self.assertEquals(log.warn_count, exp_warn,
'Found %d warns not %d.' % (log.warn_count, exp_warn)) 'Found %d warns not %d.' % (log.warn_count, exp_warn))
...@@ -330,6 +330,9 @@ class CheckCrontabUnitTest(unittest.TestCase): ...@@ -330,6 +330,9 @@ class CheckCrontabUnitTest(unittest.TestCase):
def testCheckBadWithDisablesCrontab(self): def testCheckBadWithDisablesCrontab(self):
self.CheckACrontab('test_crontab.disable') self.CheckACrontab('test_crontab.disable')
def testCheckWarnWithWhitelistedUser(self):
self.CheckACrontab('test_crontab.whitelist', ['not_a_user'])
if __name__ == '__main__': if __name__ == '__main__':
result = unittest.main() result = unittest.main()
... ...
......
# WARN 1 for questionable file name.
# WARN 0 for missing user
1 * * * * not_a_user Command
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment