Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Keep the raw line for rule use #3533

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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