-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: gracefully handle missing ldap.group_search to prevent HTTP 500 #12364
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
Open
andrianbalanesq
wants to merge
1
commit into
inventree:master
Choose a base branch
from
andrianbalanesq:fix/ldap-group-search-missing-12225
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -68,6 +68,33 @@ def get_ldap_config(debug: bool = False) -> dict: | |
| str, | ||
| ) | ||
|
|
||
| group_search_dn = get_setting( | ||
| 'INVENTREE_LDAP_GROUP_SEARCH', 'ldap.group_search' | ||
| ) | ||
|
|
||
| find_group_perms = get_boolean_setting( | ||
| 'INVENTREE_LDAP_FIND_GROUP_PERMS', 'ldap.find_group_perms', True | ||
| ) | ||
|
|
||
| # If group search DN is not configured, group-based features cannot | ||
| # work. Disable them gracefully with a warning instead of letting | ||
| # django-auth-ldap crash at runtime with a TypeError (see #12225). | ||
| if group_search_dn is None: | ||
| if find_group_perms or get_setting( | ||
| 'INVENTREE_LDAP_MIRROR_GROUPS', 'ldap.mirror_groups' | ||
| ) or get_setting( | ||
| 'INVENTREE_LDAP_REQUIRE_GROUP', 'ldap.require_group' | ||
| ) or get_setting( | ||
| 'INVENTREE_LDAP_DENY_GROUP', 'ldap.deny_group' | ||
| ): | ||
| print( | ||
| '[LDAP] ldap.group_search is not configured; ' | ||
| 'disabling group-based features (find_group_perms, ' | ||
| 'mirror_groups, require_group, deny_group). ' | ||
| 'Set ldap.group_search to enable them.' | ||
| ) | ||
| find_group_perms = False | ||
|
|
||
| ldap_config = { | ||
| 'AUTH_LDAP_GLOBAL_OPTIONS': global_options, | ||
| 'AUTH_LDAP_SERVER_URI': get_setting( | ||
|
|
@@ -108,13 +135,13 @@ def get_ldap_config(debug: bool = False) -> dict: | |
| ), | ||
| 'AUTH_LDAP_MIRROR_GROUPS': get_boolean_setting( | ||
| 'INVENTREE_LDAP_MIRROR_GROUPS', 'ldap.mirror_groups', False | ||
| ), | ||
| ) if group_search_dn is not None else False, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could simplify to: ) if group_search_dn else False |
||
| 'AUTH_LDAP_GROUP_OBJECT_CLASS': group_object_class, | ||
| 'AUTH_LDAP_GROUP_SEARCH': django_auth_ldap.config.LDAPSearch( | ||
| get_setting('INVENTREE_LDAP_GROUP_SEARCH', 'ldap.group_search'), | ||
| group_search_dn, | ||
| ldap.SCOPE_SUBTREE, | ||
| f'(objectClass={group_object_class})', | ||
| ), | ||
| ) if group_search_dn is not None else None, | ||
| 'AUTH_LDAP_GROUP_TYPE_CLASS': group_type_class, | ||
| 'AUTH_LDAP_GROUP_TYPE_CLASS_ARGS': [*group_type_class_args], | ||
| 'AUTH_LDAP_GROUP_TYPE_CLASS_KWARGS': {**group_type_class_kwargs}, | ||
|
|
@@ -123,17 +150,17 @@ def get_ldap_config(debug: bool = False) -> dict: | |
| ), | ||
| 'AUTH_LDAP_REQUIRE_GROUP': get_setting( | ||
| 'INVENTREE_LDAP_REQUIRE_GROUP', 'ldap.require_group' | ||
| ), | ||
| ) if group_search_dn is not None else None, | ||
| 'AUTH_LDAP_DENY_GROUP': get_setting( | ||
| 'INVENTREE_LDAP_DENY_GROUP', 'ldap.deny_group' | ||
| ), | ||
| ) if group_search_dn is not None else None, | ||
| 'AUTH_LDAP_USER_FLAGS_BY_GROUP': get_setting( | ||
| 'INVENTREE_LDAP_USER_FLAGS_BY_GROUP', | ||
| 'ldap.user_flags_by_group', | ||
| default_value=None, | ||
| typecast=dict, | ||
| ), | ||
| 'AUTH_LDAP_FIND_GROUP_PERMS': True, | ||
| ) if group_search_dn is not None else None, | ||
| 'AUTH_LDAP_FIND_GROUP_PERMS': find_group_perms, | ||
| } | ||
|
|
||
| return ldap_config | ||
Empty file.
145 changes: 145 additions & 0 deletions
145
src/backend/InvenTree/setting/tests/test_ldap_config.py
This file contains hidden or 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,145 @@ | ||
| """Tests for LDAP configuration handling. | ||
|
|
||
| Verifies that missing ``ldap.group_search`` does not cause runtime crashes | ||
| and that ``find_group_perms`` is properly configurable. | ||
| """ | ||
|
|
||
| import importlib | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def ldap_module(): | ||
| """Import the ldap setting module fresh for each test.""" | ||
| # The module imports django_auth_ldap and ldap at function level, | ||
| # so we can import it directly without those packages installed | ||
| # as long as we mock them for the get_ldap_config call. | ||
| import InvenTree.InvenTree.setting.ldap as ldap_mod | ||
| importlib.reload(ldap_mod) | ||
| return ldap_mod | ||
|
|
||
|
|
||
| class TestLdapGroupSearchMissing: | ||
| """Tests for graceful handling of missing ldap.group_search (see #12225).""" | ||
|
|
||
| def test_find_group_perms_disabled_when_group_search_missing(self, ldap_module): | ||
| """When group_search is None, find_group_perms must be False.""" | ||
| with mock.patch.object(ldap_module, 'get_setting') as gs, \ | ||
| mock.patch.object(ldap_module, 'get_boolean_setting') as gbs: | ||
|
|
||
| # get_setting: return None for group_search, defaults for others | ||
| def gs_side_effect(env_key, yaml_key, *args, **kwargs): | ||
| if 'GROUP_SEARCH' in env_key: | ||
| return None | ||
| if 'GLOBAL_OPTIONS' in env_key: | ||
| return kwargs.get('default_value', {}) or {} | ||
| if 'GROUP_TYPE_CLASS_ARGS' in env_key: | ||
| return kwargs.get('default_value', []) if 'default_value' in kwargs else [] | ||
| if 'GROUP_TYPE_CLASS_KWARGS' in env_key: | ||
| return kwargs.get('default_value', {}) or {} | ||
| return kwargs.get('default_value', None) | ||
|
|
||
| gs.side_effect = gs_side_effect | ||
|
|
||
| # get_boolean_setting: return True for find_group_perms, False for others | ||
| def gbs_side_effect(env_key, yaml_key, default): | ||
| if 'FIND_GROUP_PERMS' in env_key: | ||
| return True | ||
| return default | ||
|
|
||
| gbs.side_effect = gbs_side_effect | ||
|
|
||
| # Mock the imports inside get_ldap_config | ||
| with mock.patch('django_auth_ldap.config') as mock_config: | ||
| mock_config.LDAPSearch.return_value = mock.MagicMock() | ||
| mock_config.GroupOfUniqueNamesType.return_value = mock.MagicMock() | ||
|
|
||
| with mock.patch('ldap') as mock_ldap: | ||
| mock_ldap.SCOPE_SUBTREE = 2 | ||
| mock_ldap.OPT_REFERRALS = 0 | ||
|
|
||
| config = ldap_module.get_ldap_config(debug=False) | ||
|
|
||
| assert config['AUTH_LDAP_FIND_GROUP_PERMS'] is False | ||
| assert config['AUTH_LDAP_GROUP_SEARCH'] is None | ||
| assert config['AUTH_LDAP_MIRROR_GROUPS'] is False | ||
| assert config['AUTH_LDAP_REQUIRE_GROUP'] is None | ||
| assert config['AUTH_LDAP_DENY_GROUP'] is None | ||
| assert config['AUTH_LDAP_USER_FLAGS_BY_GROUP'] is None | ||
|
|
||
| def test_find_group_perms_enabled_when_group_search_set(self, ldap_module): | ||
| """When group_search is configured, find_group_perms stays True.""" | ||
| with mock.patch.object(ldap_module, 'get_setting') as gs, \ | ||
| mock.patch.object(ldap_module, 'get_boolean_setting') as gbs: | ||
|
|
||
| def gs_side_effect(env_key, yaml_key, *args, **kwargs): | ||
| if 'GROUP_SEARCH' in env_key: | ||
| return 'ou=groups,dc=example,dc=org' | ||
| if 'GLOBAL_OPTIONS' in env_key: | ||
| return kwargs.get('default_value', {}) or {} | ||
| if 'GROUP_TYPE_CLASS_ARGS' in env_key: | ||
| return kwargs.get('default_value', []) if 'default_value' in kwargs else [] | ||
| if 'GROUP_TYPE_CLASS_KWARGS' in env_key: | ||
| return kwargs.get('default_value', {}) or {} | ||
| return kwargs.get('default_value', None) | ||
|
|
||
| gs.side_effect = gs_side_effect | ||
|
|
||
| def gbs_side_effect(env_key, yaml_key, default): | ||
| if 'FIND_GROUP_PERMS' in env_key: | ||
| return True | ||
| return default | ||
|
|
||
| gbs.side_effect = gbs_side_effect | ||
|
|
||
| with mock.patch('django_auth_ldap.config') as mock_config: | ||
| mock_config.LDAPSearch.return_value = mock.MagicMock() | ||
| mock_config.GroupOfUniqueNamesType.return_value = mock.MagicMock() | ||
|
|
||
| with mock.patch('ldap') as mock_ldap: | ||
| mock_ldap.SCOPE_SUBTREE = 2 | ||
|
|
||
| config = ldap_module.get_ldap_config(debug=False) | ||
|
|
||
| assert config['AUTH_LDAP_FIND_GROUP_PERMS'] is True | ||
| assert config['AUTH_LDAP_GROUP_SEARCH'] is not None | ||
|
|
||
| def test_find_group_perms_can_be_disabled_explicitly(self, ldap_module): | ||
| """User can set find_group_perms=False even when group_search is set.""" | ||
| with mock.patch.object(ldap_module, 'get_setting') as gs, \ | ||
| mock.patch.object(ldap_module, 'get_boolean_setting') as gbs: | ||
|
|
||
| def gs_side_effect(env_key, yaml_key, *args, **kwargs): | ||
| if 'GROUP_SEARCH' in env_key: | ||
| return 'ou=groups,dc=example,dc=org' | ||
| if 'GLOBAL_OPTIONS' in env_key: | ||
| return kwargs.get('default_value', {}) or {} | ||
| if 'GROUP_TYPE_CLASS_ARGS' in env_key: | ||
| return kwargs.get('default_value', []) if 'default_value' in kwargs else [] | ||
| if 'GROUP_TYPE_CLASS_KWARGS' in env_key: | ||
| return kwargs.get('default_value', {}) or {} | ||
| return kwargs.get('default_value', None) | ||
|
|
||
| gs.side_effect = gs_side_effect | ||
|
|
||
| def gbs_side_effect(env_key, yaml_key, default): | ||
| if 'FIND_GROUP_PERMS' in env_key: | ||
| return False | ||
| return default | ||
|
|
||
| gbs.side_effect = gbs_side_effect | ||
|
|
||
| with mock.patch('django_auth_ldap.config') as mock_config: | ||
| mock_config.LDAPSearch.return_value = mock.MagicMock() | ||
| mock_config.GroupOfUniqueNamesType.return_value = mock.MagicMock() | ||
|
|
||
| with mock.patch('ldap') as mock_ldap: | ||
| mock_ldap.SCOPE_SUBTREE = 2 | ||
|
|
||
| config = ldap_module.get_ldap_config(debug=False) | ||
|
|
||
| assert config['AUTH_LDAP_FIND_GROUP_PERMS'] is False | ||
| # Group search is still configured (user might use it for other features) | ||
| assert config['AUTH_LDAP_GROUP_SEARCH'] is not None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you also check for empty string here? e.g.