diff --git a/chkcrontab b/chkcrontab index c9573c7a89e5e015f1af01e59573f38ab1490e84..c0a218dea65cb76d22217bc29e1ef3edb174cbdb 100755 --- a/chkcrontab +++ b/chkcrontab @@ -36,15 +36,15 @@ def main(argv): print('ERROR: No crontab file was specified.') sys.exit(1) + whitelisted_users = None if len(argv) > 2: for arg in argv: if '--whitelist' in arg: - whitelist_users = arg['--whitelist='.__len__():].split(',') - + whitelisted_users = arg['--whitelist='.__len__():].split(',') log = check.LogCounter() 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__': sys.exit(main(sys.argv)) \ No newline at end of file diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py index 9eab90f2e8bdb050a56a66868a88f39de1402e39..b212519e2397a8bc1111048abc53eeac31dcbb49 100755 --- a/chkcrontab_lib.py +++ b/chkcrontab_lib.py @@ -1043,7 +1043,7 @@ class LogCounter(object): return self._error_count -def check_crontab(crontab_file, log): +def check_crontab(crontab_file, log, whitelisted_users=None): """Check a crontab file. Checks crontab_file for a variety of errors or potential errors. This only @@ -1063,6 +1063,10 @@ def check_crontab(crontab_file, log): if not os.path.exists(crontab_file): 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. if re.search('[^A-Za-z0-9_-]', os.path.basename(crontab_file)): in_whitelist = False diff --git a/tests/test_check.py b/tests/test_check.py index 87ac551646332474e63ce43d5abd63135abd3a8f..57246d5ed039bdf9971c04d94d43fee330cefb6b 100755 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -307,11 +307,11 @@ class CheckCrontabUnitTest(unittest.TestCase): exp_rc = 0 return (exp_warn, exp_fail, exp_rc) - def CheckACrontab(self, crontab): + def CheckACrontab(self, crontab, whitelisted_users=None): log = check.LogCounter() crontab_file = os.path.join(BASE_PATH, crontab) (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) self.assertEquals(log.warn_count, exp_warn, 'Found %d warns not %d.' % (log.warn_count, exp_warn)) @@ -330,6 +330,9 @@ class CheckCrontabUnitTest(unittest.TestCase): def testCheckBadWithDisablesCrontab(self): self.CheckACrontab('test_crontab.disable') + def testCheckWarnWithWhitelistedUser(self): + self.CheckACrontab('test_crontab.whitelist', ['not_a_user']) + if __name__ == '__main__': result = unittest.main() diff --git a/tests/test_crontab.whitelist b/tests/test_crontab.whitelist new file mode 100644 index 0000000000000000000000000000000000000000..0e58c225d781e79aabd8b23d8e56a998f9e38ebe --- /dev/null +++ b/tests/test_crontab.whitelist @@ -0,0 +1,3 @@ +# WARN 1 for questionable file name. +# WARN 0 for missing user +1 * * * * not_a_user Command \ No newline at end of file