-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-130428 / 24.10 / Add auditing for API key CRUD operations (#14131)
Generate an audit trail for changes to API keys.
- Loading branch information
Showing
2 changed files
with
43 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from middlewared.test.integration.utils import call | ||
from middlewared.test.integration.utils.audit import expect_audit_method_calls | ||
|
||
API_KEY_NAME = 'AUDIT_API_KEY' | ||
|
||
|
||
def test_api_key_audit(): | ||
payload = {'name': API_KEY_NAME, 'allowlist': [{'resource': '*', 'method': '*'}]} | ||
payload2 = {'allowlist': []} | ||
audit_id = None | ||
|
||
try: | ||
with expect_audit_method_calls([{ | ||
'method': 'api_key.create', | ||
'params': [payload], | ||
'description': f'Create API key {API_KEY_NAME}', | ||
}]): | ||
api_key_id = call('api_key.create', payload)['id'] | ||
|
||
with expect_audit_method_calls([{ | ||
'method': 'api_key.update', | ||
'params': [api_key_id, payload2], | ||
'description': f'Update API key {API_KEY_NAME}', | ||
}]): | ||
call('api_key.update', api_key_id, payload2) | ||
|
||
finally: | ||
if audit_id: | ||
with expect_audit_method_calls([{ | ||
'method': 'api_key.delete', | ||
'params': [api_key_id], | ||
'description': f'Delete API key {API_KEY_NAME}', | ||
}]): | ||
call('api_key.delete', api_key_id) |