Skip to content

Commit

Permalink
refactor: Keep the raw line for rule use (#3533)
Browse files Browse the repository at this point in the history
Signed-off-by: Huanhuan Li <[email protected]>
(cherry picked from commit 07968f2)
  • Loading branch information
huali027 authored and xiangce committed Sep 29, 2022
1 parent 17c985f commit 93d9dc5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
9 changes: 6 additions & 3 deletions insights/parsers/cron_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CronFile(Parser):
It parses the file at ``/etc/cron.d/*```.
Each row of the file is converted into a dictionary with keys for each field.
And also the whole line is saved for use to the "raw" key.
For example one row would look like::
{
Expand All @@ -28,7 +29,8 @@ class CronFile(Parser):
'month': '*',
'day_of_week': '*',
'username': 'test',
'command': '/usr/bin/keystone-manage token_flush > /dev/null 2>&1'
'command': '/usr/bin/keystone-manage token_flush > /dev/null 2>&1',
'raw': '* * * * * * test /usr/bin/keystone-manage token_flush > /dev/null 2>&1'
}
It parses the line in the same way that cron(1) does. Lines that
Expand Down Expand Up @@ -111,7 +113,7 @@ def parse_content(self, content):
# Reboot is 'special':
if line.startswith('@reboot'):
parts = line.split(None, 2)
self.jobs.append({'time': '@reboot', 'username': parts[1], 'command': parts[2]})
self.jobs.append({'time': '@reboot', 'username': parts[1], 'command': parts[2], 'raw': line})
continue
else:
parts = line.split(None, 2)
Expand All @@ -131,7 +133,8 @@ def parse_content(self, content):
'month': items[3],
'day_of_week': items[4],
'username': items[5],
'command': items[6]
'command': items[6],
'raw': line
})
else:
parts = line.split('=')
Expand Down
1 change: 1 addition & 0 deletions insights/tests/parsers/test_cron_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def test_cron_file():
assert len(user1_jobs) == 5
assert user1_jobs[-1].get('time') == '@reboot'
assert user1_jobs[-1].get('command') == '/usr/sbin/somefive'
assert user1_jobs[-1].get('raw') == '@reboot user1 /usr/sbin/somefive'
test_jobs = cron_obj.search(command__contains='test')
assert len(test_jobs) == 3
assert test_jobs[1].get('minute') == '0'
Expand Down

0 comments on commit 93d9dc5

Please sign in to comment.