Skip to content

Commit

Permalink
Fix order_by when using raw sql queries on audit backend
Browse files Browse the repository at this point in the history
Order_by directives were not being applied to the generated
SQL queryset that was being passed to the individual audit backend
services. This wasn't caught because often filtering for generic
audit queries happens via filter_list because the filters cannot
be fully duplicated using native SQL features.
  • Loading branch information
anodos325 committed Aug 8, 2024
1 parent 5995885 commit c67e3b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/middlewared/middlewared/plugins/audit/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def query(self, db_name, filters, options):
if wrapper is not None:
order_by[i] = wrapper(order_by[i])

qs = qs.order_by(*order_by)

if options['offset']:
qs = qs.offset(options['offset'])

Expand Down
18 changes: 18 additions & 0 deletions tests/api2/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ def test_audit_query(self, initialize_for_smb_tests):
retries -= 1
assert ops_count > initial_ops_count, f"retries remaining = {retries}"

def test_audit_order_by(self):
entries_forward = call('audit.query', {'services': ['SMB'], 'query-options': {
'order_by': ['audit_id']
}})

entries_reverse = call('audit.query', {'services': ['SMB'], 'query-options': {
'order_by': ['-audit_id']
}})

head_forward_id = entries_forward[0]['audit_id']
tail_forward_id = entries_forward[-1]['audit_id']

head_reverse_id = entries_reverse[0]['audit_id']
tail_reverse_id = entries_reverse[-1]['audit_id']

assert head_forward_id == tail_reverse_id
assert tail_forward_id == head_reverse_id

def test_audit_export(self):
for backend in ['CSV', 'JSON', 'YAML']:
report_path = call('audit.export', {'export_format': backend}, job=True)
Expand Down

0 comments on commit c67e3b7

Please sign in to comment.