-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve auditing for middleware methods
Populate `username` and `address` fields in middleware audit entries based on information from the middleware session. The UI prominently displays the `username` field which is currently hard-coded to root. This commit adds a helper function to generate a username string based on the current authenticated credentials for the session generating the audit entry.
- Loading branch information
Showing
5 changed files
with
113 additions
and
48 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
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
31 changes: 31 additions & 0 deletions
31
src/middlewared/middlewared/pytest/unit/utils/test_audit.py
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,31 @@ | ||
import pytest | ||
|
||
from middlewared.auth import ( | ||
ApiKeySessionManagerCredentials, | ||
UserSessionManagerCredentials, | ||
TrueNasNodeSessionManagerCredentials | ||
) | ||
|
||
from middlewared.utils.audit import audit_username_from_session | ||
from types import SimpleNamespace | ||
|
||
|
||
API_KEY = SimpleNamespace(api_key={'id': 1, 'name': 'MY_KEY'}) | ||
|
||
USER_SESSION = UserSessionManagerCredentials({'username': 'bob', 'privilege': {'allowlist': []}}) | ||
API_KEY_SESSION = ApiKeySessionManagerCredentials(API_KEY) | ||
TOKEN_USER_SESSION = SimpleNamespace(root_credentials=USER_SESSION, is_user_session=True, user=USER_SESSION.user) | ||
TOKEN_API_KEY_SESSION = SimpleNamespace(root_credentials=API_KEY_SESSION, is_user_session=False) | ||
NODE_SESSION = TrueNasNodeSessionManagerCredentials() | ||
|
||
|
||
@pytest.mark.parametrize('cred,expected', [ | ||
(None, '.TRUENAS_INTERNAL'), | ||
(USER_SESSION, 'bob'), | ||
(API_KEY_SESSION, '.TRUENAS_API_KEY:MY_KEY'), | ||
(TOKEN_USER_SESSION, 'bob'), | ||
(TOKEN_API_KEY_SESSION, '.TRUENAS_API_KEY:MY_KEY'), | ||
(NODE_SESSION, '.TRUENAS_NODE') | ||
]) | ||
def test_privilege_has_webui_access(cred, expected): | ||
assert audit_username_from_session(cred) == expected |
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,33 @@ | ||
from middlewared.auth import ( | ||
ApiKeySessionManagerCredentials, | ||
TokenSessionManagerCredentials, | ||
TrueNasNodeSessionManagerCredentials | ||
) | ||
|
||
# Special values start with dot to ensure they cannot collide with local usernames | ||
# created via APIs | ||
INTERNAL_SESSION = '.TRUENAS_INTERNAL' | ||
API_KEY_PREFIX = '.TRUENAS_API_KEY:' | ||
NODE_SESSION = '.TRUENAS_NODE' | ||
UNKNOWN_SESSION = '.UNKNOWN' | ||
|
||
|
||
def audit_username_from_session(cred) -> str: | ||
if cred is None: | ||
return INTERNAL_SESSION | ||
|
||
# This works for regular user session and tokens formed on them | ||
if cred.is_user_session: | ||
return cred.user['username'] | ||
|
||
# Track back to root credential if necessary (token session) | ||
if isinstance(cred, TokenSessionManagerCredentials): | ||
cred = cred.root_credentials | ||
|
||
if isinstance(cred, ApiKeySessionManagerCredentials): | ||
return f'{API_KEY_PREFIX}{cred.api_key.api_key["name"]}' | ||
|
||
elif isinstance(cred, TrueNasNodeSessionManagerCredentials): | ||
return NODE_SESSION | ||
|
||
return UNKNOWN_SESSION |