diff --git a/CHANGELOG b/CHANGELOG index 879ab077..d6218e53 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ jc changelog 20230402 v1.23.2 - Fix `iwconfig` command parser for SSIDs with dashes in the name +- Fix `crontab` command parsers for incorrect variable parsing in some cases 20230323 v1.23.1 - Fix `zpool-status` command parser for lines that start with tab diff --git a/docs/parsers/crontab.md b/docs/parsers/crontab.md index 7b26cf96..5fc7a16e 100644 --- a/docs/parsers/crontab.md +++ b/docs/parsers/crontab.md @@ -196,4 +196,4 @@ Returns: ### Parser Information Compatibility: linux, darwin, aix, freebsd -Version 1.7 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/docs/parsers/crontab_u.md b/docs/parsers/crontab_u.md index 74845c70..a34a5098 100644 --- a/docs/parsers/crontab_u.md +++ b/docs/parsers/crontab_u.md @@ -193,4 +193,4 @@ Returns: ### Parser Information Compatibility: linux, darwin, aix, freebsd -Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/parsers/crontab.py b/jc/parsers/crontab.py index 031bd8d5..dc0a0168 100644 --- a/jc/parsers/crontab.py +++ b/jc/parsers/crontab.py @@ -174,7 +174,7 @@ import jc.parsers.universal class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.7' + version = '1.8' description = '`crontab` command and file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -245,7 +245,11 @@ def parse(data, raw=False, quiet=False): # Pop any variable assignment lines cron_var = [] for i, line in reversed(list(enumerate(cleandata))): - if '=' in line and not line.strip()[0].isdigit() and not line.strip()[0] == '@': + if '=' in line \ + and not line.strip()[0].isdigit() \ + and not line.strip()[0] == '@' \ + and not line.strip()[0] == '*': + var_line = cleandata.pop(i) var_name = var_line.split('=', maxsplit=1)[0].strip() var_value = var_line.split('=', maxsplit=1)[1].strip() diff --git a/jc/parsers/crontab_u.py b/jc/parsers/crontab_u.py index b847c6ad..76fd1ac6 100644 --- a/jc/parsers/crontab_u.py +++ b/jc/parsers/crontab_u.py @@ -171,7 +171,7 @@ import jc.parsers.universal class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.8' + version = '1.9' description = '`crontab` file parser with user support' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -241,7 +241,11 @@ def parse(data, raw=False, quiet=False): # Pop any variable assignment lines cron_var = [] for i, line in reversed(list(enumerate(cleandata))): - if '=' in line and not line.strip()[0].isdigit() and not line.strip()[0] == '@': + if '=' in line \ + and not line.strip()[0].isdigit() \ + and not line.strip()[0] == '@' \ + and not line.strip()[0] == '*': + var_line = cleandata.pop(i) var_name = var_line.split('=', maxsplit=1)[0].strip() var_value = var_line.split('=', maxsplit=1)[1].strip() diff --git a/tests/fixtures/generic/crontab-u-var-fix.json b/tests/fixtures/generic/crontab-u-var-fix.json new file mode 100644 index 00000000..c838a960 --- /dev/null +++ b/tests/fixtures/generic/crontab-u-var-fix.json @@ -0,0 +1 @@ +{"variables":[],"schedule":[{"minute":["*"],"hour":["*"],"day_of_month":["*"],"month":["*"],"day_of_week":["*"],"user":"root","command":"/bin/bash -l -c \"python3 my_script.py --output-file=/opt/output.json\""}]} diff --git a/tests/fixtures/generic/crontab-u-var-fix.out b/tests/fixtures/generic/crontab-u-var-fix.out new file mode 100644 index 00000000..69b122fe --- /dev/null +++ b/tests/fixtures/generic/crontab-u-var-fix.out @@ -0,0 +1 @@ +* * * * * root /bin/bash -l -c "python3 my_script.py --output-file=/opt/output.json" diff --git a/tests/fixtures/generic/crontab-var-fix.json b/tests/fixtures/generic/crontab-var-fix.json new file mode 100644 index 00000000..b469bb74 --- /dev/null +++ b/tests/fixtures/generic/crontab-var-fix.json @@ -0,0 +1 @@ +{"variables":[],"schedule":[{"minute":["*"],"hour":["*"],"day_of_month":["*"],"month":["*"],"day_of_week":["*"],"command":"/bin/bash -l -c \"python3 my_script.py --output-file=/opt/output.json\""}]} diff --git a/tests/fixtures/generic/crontab-var-fix.out b/tests/fixtures/generic/crontab-var-fix.out new file mode 100644 index 00000000..7086fd3a --- /dev/null +++ b/tests/fixtures/generic/crontab-var-fix.out @@ -0,0 +1 @@ +* * * * * /bin/bash -l -c "python3 my_script.py --output-file=/opt/output.json" diff --git a/tests/test_crontab.py b/tests/test_crontab.py index 81ae4b49..1a8eacc6 100644 --- a/tests/test_crontab.py +++ b/tests/test_crontab.py @@ -15,6 +15,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-no-normal-entries.out'), 'r', encoding='utf-8') as f: generic_crontab_no_normal_entries = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-var-fix.out'), 'r', encoding='utf-8') as f: + generic_crontab_var_fix = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/crontab.json'), 'r', encoding='utf-8') as f: centos_7_7_crontab_json = json.loads(f.read()) @@ -22,6 +25,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-no-normal-entries.json'), 'r', encoding='utf-8') as f: generic_crontab_no_normal_entries_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-var-fix.json'), 'r', encoding='utf-8') as f: + generic_crontab_var_fix_json = json.loads(f.read()) + def test_crontab_nodata(self): """ @@ -41,6 +47,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.crontab.parse(self.generic_crontab_no_normal_entries, quiet=True), self.generic_crontab_no_normal_entries_json) + def test_crontab_var_fix(self): + """ + Test 'crontab' with wildcard schedule should not generate variables from command line section + """ + self.assertEqual(jc.parsers.crontab.parse(self.generic_crontab_var_fix, quiet=True), self.generic_crontab_var_fix_json) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_crontab_u.py b/tests/test_crontab_u.py index ad5429cd..af010a54 100644 --- a/tests/test_crontab_u.py +++ b/tests/test_crontab_u.py @@ -21,6 +21,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-no-normal-entries.out'), 'r', encoding='utf-8') as f: generic_crontab_u_no_normal_entries = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-var-fix.out'), 'r', encoding='utf-8') as f: + generic_crontab_u_var_fix = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/crontab-u.json'), 'r', encoding='utf-8') as f: ubuntu_18_4_crontab_u_json = json.loads(f.read()) @@ -34,6 +37,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-no-normal-entries.json'), 'r', encoding='utf-8') as f: generic_crontab_u_no_normal_entries_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-var-fix.json'), 'r', encoding='utf-8') as f: + generic_crontab_u_var_fix_json = json.loads(f.read()) + def test_crontab_u_nodata(self): """ @@ -65,5 +71,11 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.crontab_u.parse(self.generic_crontab_u_no_normal_entries, quiet=True), self.generic_crontab_u_no_normal_entries_json) + def test_crontab_u_var_fix(self): + """ + Test 'crontab' with wildcard schedule should not generate variables from command line section + """ + self.assertEqual(jc.parsers.crontab_u.parse(self.generic_crontab_u_var_fix, quiet=True), self.generic_crontab_u_var_fix_json) + if __name__ == '__main__': unittest.main()