From caa0f425e55b92e6baf3fc5d703f1ffc5b82c7fa Mon Sep 17 00:00:00 2001 From: shayhanan Date: Tue, 3 Nov 2026 08:25:56 +0000 Subject: [PATCH] Publish share configuration and directory search for the access-map crawl. The crawl needs a read-only share configuration and a searchAD lookup that does not import accounts. Co-authored-by: Cursor --- cterasdk/core/directoryservice.py | 30 +++++++++--- cterasdk/core/shares.py | 16 ++++--- .../UserGuides/Miscellaneous/Changelog.rst | 10 ++++ .../UserGuides/Portal/Administration.rst | 18 +++++++ .../ut/core/admin/test_directory_services.py | 30 +++++++++++- tests/ut/core/admin/test_shares.py | 47 +++++++++++++++++++ 6 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 tests/ut/core/admin/test_shares.py diff --git a/cterasdk/core/directoryservice.py b/cterasdk/core/directoryservice.py index 36cd24fb..1d3c6837 100644 --- a/cterasdk/core/directoryservice.py +++ b/cterasdk/core/directoryservice.py @@ -202,6 +202,9 @@ def fetch(self, active_directory_accounts): """ Instruct the Portal to fetch the provided Active Directory Accounts + This imports the accounts into the Portal (``syncAD``). A read-only lookup is + :meth:`search_directory_services`. + :param list[cterasdk.core.types.PortalAccount] active_directory_accounts: List of Active Directory Accounts to fetch :return: Response Code @@ -233,23 +236,36 @@ def fetch(self, active_directory_accounts): return response def _search_users(self, domain, user): - return self._search_directory_services(SearchType.Users, domain, user) + return self.search_directory_services(SearchType.Users, domain, user) def _search_groups(self, domain, group): - return self._search_directory_services(SearchType.Groups, domain, group) + return self.search_directory_services(SearchType.Groups, domain, group) + + def search_directory_services(self, mode, domain, name): + """ + Search directory services for one user or group. - def _search_directory_services(self, search_type, domain, name): + Read-only. Calls ``searchAD`` and returns the matching principal. The principal + carries ``rid``, not ``sid``. This method does not import the account into the Portal. + + :param str mode: Search mode, :attr:`cterasdk.core.enum.SearchType.Users` or + :attr:`cterasdk.core.enum.SearchType.Groups` + :param str domain: Directory domain + :param str name: Account name + :returns: The matching directory principal + :rtype: cterasdk.common.object.Object + """ param = Object() - param.mode = search_type + param.mode = mode param.name = name param.domain = domain - logger.info('Searching %(search_type)s: %(info)s', dict(search_type=search_type, info={'domain': domain, 'name': name})) + logger.info('Searching %(mode)s: %(info)s', dict(mode=mode, info={'domain': domain, 'name': name})) objects = self._core.api.execute('', 'searchAD', param) if not objects: - logger.info('Could not find %s: %s@%s.', search_type, name, domain) - raise CTERAException(f'Could not find {search_type[:-1]}: {name}@{domain}') + logger.info('Could not find %s: %s@%s.', mode, name, domain) + raise CTERAException(f'Could not find {mode[:-1]}: {name}@{domain}') for principal in objects: if principal.name == name: diff --git a/cterasdk/core/shares.py b/cterasdk/core/shares.py index 287da54f..4ea68658 100644 --- a/cterasdk/core/shares.py +++ b/cterasdk/core/shares.py @@ -30,12 +30,16 @@ def _deduce_unique_identifier(share): return share raise ValueError('Could not determine share identifier') - def _get_configuration(self, share): + def get_configuration(self, share): """ - Get Share + Get a share's configuration. - :param cterasdk.core.types.Share or str: Share object or unique idenifier. - :returns: Share metadata + Read-only. Returns the Portal configuration, including ``directory``, ``access_type``, + ``device_shares`` and ``acl_rules``. This is the raw configuration, not + :class:`cterasdk.core.types.ShareInfo`. + + :param cterasdk.core.types.Share or str share: Share object or unique identifier. + :returns: Share configuration :rtype: cterasdk.common.object.Object """ uid = Shares._deduce_unique_identifier(share) @@ -52,7 +56,7 @@ def get(self, share): :returns: Share information :rtype: cterasdk.core.types.ShareInfo """ - response = self._get_configuration(share) + response = self.get_configuration(share) return ShareInfo.from_server_object(response) def all(self, devices=None, protocol=None, search=None): @@ -154,7 +158,7 @@ def modify(self, share, name=None, directory=None, # pylint: disable=too-many-a devices=None, acl=None, description=None, access=None, export_to_nfs=None, nfs_krb=None, trusted_nfs_clients=None, krb_sec=None, block_files=None, export_to_ftp=None, validate_acl=True): - param = self._get_configuration(share) + param = self.get_configuration(share) if name: param.name = name diff --git a/docs/source/UserGuides/Miscellaneous/Changelog.rst b/docs/source/UserGuides/Miscellaneous/Changelog.rst index 631378a4..2e8c29d8 100644 --- a/docs/source/UserGuides/Miscellaneous/Changelog.rst +++ b/docs/source/UserGuides/Miscellaneous/Changelog.rst @@ -1,6 +1,16 @@ Changelog ========= +Unreleased +---------- + +Improvements +^^^^^^^^^^^^ + +* Publish ``Shares.get_configuration`` and ``DirectoryService.search_directory_services``. + Both are read-only. Directory search calls ``searchAD`` and does not import accounts. + ``DirectoryService.fetch`` still calls ``syncAD``. + 2.20.50 ------- diff --git a/docs/source/UserGuides/Portal/Administration.rst b/docs/source/UserGuides/Portal/Administration.rst index cd583ae1..009e98e1 100644 --- a/docs/source/UserGuides/Portal/Administration.rst +++ b/docs/source/UserGuides/Portal/Administration.rst @@ -998,6 +998,15 @@ Fetch Users & Groups Directory Services ================== +.. automethod:: cterasdk.core.directoryservice.DirectoryService.search_directory_services + :noindex: + +.. code-block:: python + + """Look up a domain group without importing it into the Portal""" + group = admin.directoryservice.search_directory_services(core_enum.SearchType.Groups, 'demo.local', 'Domain Users') + print(group.name, group.rid) + .. automethod:: cterasdk.core.directoryservice.DirectoryService.connect :noindex: @@ -1611,6 +1620,15 @@ Shares .. automethod:: cterasdk.core.shares.Shares.get :noindex: +.. automethod:: cterasdk.core.shares.Shares.get_configuration + :noindex: + +.. code-block:: python + + """Read a share's configuration, including its directory, access type and ACL rules""" + configuration = admin.shares.get_configuration(share) + print(configuration.directory, configuration.access_type) + .. automethod:: cterasdk.core.shares.Shares.add :noindex: diff --git a/tests/ut/core/admin/test_directory_services.py b/tests/ut/core/admin/test_directory_services.py index 91dba5d5..e0c7422d 100644 --- a/tests/ut/core/admin/test_directory_services.py +++ b/tests/ut/core/admin/test_directory_services.py @@ -3,7 +3,7 @@ from cterasdk.core import directoryservice from cterasdk.core.types import UserAccount, GroupAccount, DomainControllers, AccessControlEntry -from cterasdk.core.enum import Role, DirectoryServiceType, DirectoryServiceFetchMode, DirectorySearchEntityType +from cterasdk.core.enum import Role, DirectoryServiceType, DirectoryServiceFetchMode, DirectorySearchEntityType, SearchType from cterasdk.common.types import ADDomainIDMapping from cterasdk.common.object import Object from cterasdk import exceptions @@ -234,6 +234,34 @@ def test_user_search_found(self): self._global_admin.api.execute.assert_called_once_with('', 'searchAD', mock.ANY) self.assertEqual(ret.name, self._account_user_name) + def test_search_directory_services_returns_rid(self): + principal = munch.Munch({'name': self._account_group_name, 'rid': 513}) + self._init_global_admin(execute_response=[principal]) + ret = directoryservice.DirectoryService(self._global_admin).search_directory_services( + SearchType.Groups, self._domain, self._account_group_name) + self._global_admin.api.execute.assert_called_once_with('', 'searchAD', mock.ANY) + actual_param = self._global_admin.api.execute.call_args[0][2] + self.assertEqual(actual_param.mode, SearchType.Groups) + self.assertEqual(actual_param.domain, self._domain) + self.assertEqual(actual_param.name, self._account_group_name) + self.assertEqual(ret.rid, 513) + self.assertNotIn('syncAD', [call.args[1] for call in self._global_admin.api.execute.call_args_list]) + + def test_search_directory_services_not_found(self): + self._init_global_admin(execute_response=None) + with self.assertRaises(exceptions.CTERAException) as error: + directoryservice.DirectoryService(self._global_admin).search_directory_services( + SearchType.Users, self._domain, self._account_user_name) + self._global_admin.api.execute.assert_called_once_with('', 'searchAD', mock.ANY) + self.assertEqual(f'Could not find user: {self._account_user_name}@{self._domain}', str(error.exception)) + + def test_search_directory_services_no_match(self): + self._init_global_admin(execute_response=[munch.Munch({'name': 'random'})]) + with self.assertRaises(exceptions.CTERAException) as error: + directoryservice.DirectoryService(self._global_admin).search_directory_services( + SearchType.Users, self._domain, self._account_user_name) + self.assertEqual('Search returned multiple results, but none matched the specified criteria.', str(error.exception)) + def _create_access_control_entry(self, ace_type, ace_name, ace_role): ace = Object() ace.group = Object() diff --git a/tests/ut/core/admin/test_shares.py b/tests/ut/core/admin/test_shares.py new file mode 100644 index 00000000..29c2ce34 --- /dev/null +++ b/tests/ut/core/admin/test_shares.py @@ -0,0 +1,47 @@ +from unittest import mock + +import munch + +from cterasdk import exceptions +from cterasdk.core import shares +from cterasdk.core.types import Share +from tests.ut.core.admin import base_admin + + +class TestCoreShares(base_admin.BaseCoreTest): + + def setUp(self): + super().setUp() + self._share_id = '05db1713-842e-4f7b-ac27-43787f10c695' + self._configuration = munch.Munch({ + 'id': self._share_id, + 'directory': '/cloud/users/Admin user/Projects', + 'access_type': 'winAclMode', + 'device_shares': [munch.Munch({'device_name': 'edge1'})], + 'acl_rules': [], + }) + + def test_get_configuration(self): + self._mock_configuration(self._configuration) + ret = shares.Shares(self._global_admin).get_configuration(self._share_id) + self._global_admin.clients.v2.get.assert_called_once_with(f'shareManagement/configurations/{self._share_id}') + self.assertEqual(ret.directory, '/cloud/users/Admin user/Projects') + self.assertEqual(ret.access_type, 'winAclMode') + self.assertEqual(ret.device_shares[0].device_name, 'edge1') + + def test_get_configuration_from_share(self): + self._mock_configuration(self._configuration) + share = Share(self._share_id, 'Projects', 'Admin user/Projects', ['SMB'], 'winAclMode', ['edge1']) + ret = shares.Shares(self._global_admin).get_configuration(share) + self._global_admin.clients.v2.get.assert_called_once_with(f'shareManagement/configurations/{self._share_id}') + self.assertEqual(ret, self._configuration) + + def test_get_configuration_not_found(self): + self._mock_configuration(None) + with self.assertRaises(exceptions.ObjectNotFoundException) as error: + shares.Shares(self._global_admin).get_configuration(self._share_id) + self.assertEqual(f'Object not found: {self._share_id}', str(error.exception)) + + def _mock_configuration(self, configuration): + response = None if configuration is None else munch.Munch({'data': configuration}) + self._global_admin.clients.v2.get = mock.MagicMock(return_value=response)