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 7, 2024
1 parent 5995885 commit ce95b19
Show file tree
Hide file tree
Showing 2 changed files with 18 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
16 changes: 16 additions & 0 deletions tests/api2/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ 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):
# Perform two queries that can be represented using SQL queryset
# and then compare what we get with different ordering to validate
# that order_by directive is functional
head_ts = call('audit.query', {'services': ['SMB'], 'query-options': {
'get': True,
'order_by': ['message_timestamp']
}})['message_timestamp']

tail_ts = call('audit.query', {'query-options': {
'get': True,
'order_by': ['-message_timestamp']
}})['message_timestamp']

assert head_ts != tail_ts

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 ce95b19

Please sign in to comment.