diff --git a/.gitignore b/.gitignore
index b0b1e02bc2d08cc57eb6a6c33bb447438c2a531d..bec610f9ee26d504c30c10a2a7fe9cbe978a5107 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ htmlcov/
*.swp
.tox
.coverage
+.idea
diff --git a/chkcrontab b/chkcrontab
index e11510e377b9f63fcfff1d4d2eeb75dd63203144..1e5b3832e4b3644b568c57d90aa6112653e1ce37 100755
--- a/chkcrontab
+++ b/chkcrontab
@@ -29,15 +29,36 @@ __author__ = 'lyda@google.com (Kevin Lyda)'
import sys
import chkcrontab_lib as check
+from optparse import OptionParser
def main(argv):
"""main program."""
- if len(argv) != 2:
+ if len(argv) == 1:
print('ERROR: No crontab file was specified.')
sys.exit(1)
+
log = check.LogCounter()
print('Checking correctness of %s' % argv[1])
- return check.check_crontab(argv[1], log)
+ return check.check_crontab(argv[1], log, get_whitelisted_users(argv))
+
+def get_whitelisted_users(argv):
+ """Gets the list of whitelisted users, if any.
+
+ Args:
+ argv: The argument string supplied by the caller.
+
+ Returns:
+ The list of whitelisted users.
+ """
+ parser = OptionParser()
+ parser.add_option('-w', '--whitelist', dest='whitelisted_users', action='append',
+ help='A user to ignore when warning of unrecognized users This argument may be passed multiple times.')
+ (options, args) = parser.parse_args(argv)
+
+ if options.whitelisted_users:
+ return options.whitelisted_users
+ else:
+ return None
if __name__ == '__main__':
- sys.exit(main(sys.argv))
+ sys.exit(main(sys.argv))
\ No newline at end of file
diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py
index 9eab90f2e8bdb050a56a66868a88f39de1402e39..c1c1004ab8ce41de95bd8e7b49e971eae0a0f4ae 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
@@ -1052,6 +1052,7 @@ def check_crontab(crontab_file, log):
Args:
crontab_file: Name of the crontab file to check.
log: A LogCounter object.
+ whitelisted_users: A comma delimited list of users to ignore when warning on unrecognized users.
Returns:
0 if there were no errors.
@@ -1063,6 +1064,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 whitelisted_users:
+ 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