From a90c319229d2e0679d37ee9c1f6c862ba24ccbeb Mon Sep 17 00:00:00 2001
From: Jeremy Jarrell <jjarrell@listhub.com>
Date: Tue, 11 Jun 2013 15:48:21 -0400
Subject: [PATCH] Adds any users specified on the command line to the list of
 whitelisted users.

---
 chkcrontab                   | 6 +++---
 chkcrontab_lib.py            | 6 +++++-
 tests/test_check.py          | 7 +++++--
 tests/test_crontab.whitelist | 3 +++
 4 files changed, 16 insertions(+), 6 deletions(-)
 create mode 100644 tests/test_crontab.whitelist

diff --git a/chkcrontab b/chkcrontab
index c9573c7..c0a218d 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 9eab90f..b212519 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 87ac551..57246d5 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 0000000..0e58c22
--- /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
-- 
GitLab