Repository navigation
allow creating ldap on existing linked domain #13945
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
DaanHoogland
wants to merge
4
commits into
4.20
Choose a base branch
from
ghi8732-ldap-createaccount-group-scope
base: 4.20
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
172 changes: 172 additions & 0 deletions
172
...enticators/ldap/src/test/java/org/apache/cloudstack/ldap/OpenLdapUserManagerImplTest.java
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,172 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package org.apache.cloudstack.ldap; | ||
|
|
||
| import com.cloud.user.Account; | ||
| import org.apache.cloudstack.ldap.dao.LdapTrustMapDao; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| import javax.naming.NamingEnumeration; | ||
| import javax.naming.NamingException; | ||
| import javax.naming.directory.SearchControls; | ||
| import javax.naming.directory.SearchResult; | ||
| import javax.naming.ldap.Control; | ||
| import javax.naming.ldap.LdapContext; | ||
| import javax.naming.ldap.PagedResultsResponseControl; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.atLeastOnce; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Regression tests: creating an ldap account must not fail just because another | ||
| * account is already linked to an ldap group in the domain; browsing/importing | ||
| * still honours that group scope. | ||
| */ | ||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class OpenLdapUserManagerImplTest { | ||
|
|
||
| private static final Long DOMAIN_ID = 1L; | ||
| private static final String LINKED_GROUP = "cn=test admins,ou=groups,dc=my,dc=domain,dc=com"; | ||
|
|
||
| private OpenLdapUserManagerImpl openLdapUserManager; | ||
|
|
||
| private MockedStatic<LdapConfiguration> ldapConfigurationMockedStatic; | ||
|
|
||
| @Mock | ||
| private LdapConfiguration ldapConfigurationMock; | ||
|
|
||
| @Mock | ||
| private LdapTrustMapDao ldapTrustMapDaoMock; | ||
|
|
||
| @Mock | ||
| private LdapContext ldapContextMock; | ||
|
|
||
| @Before | ||
| public void setup() throws Exception { | ||
| // getUserMemberOfAttribute is static, unlike its LdapConfiguration siblings; mock statically. | ||
| ldapConfigurationMockedStatic = Mockito.mockStatic(LdapConfiguration.class, Mockito.CALLS_REAL_METHODS); | ||
| when(LdapConfiguration.getUserMemberOfAttribute(any())).thenReturn("memberOf"); | ||
|
DaanHoogland marked this conversation as resolved.
Resolved
|
||
|
|
||
| openLdapUserManager = new OpenLdapUserManagerImpl(ldapConfigurationMock); | ||
| openLdapUserManager._ldapTrustMapDao = ldapTrustMapDaoMock; | ||
|
|
||
| when(ldapConfigurationMock.getScope()).thenReturn(SearchControls.SUBTREE_SCOPE); | ||
| when(ldapConfigurationMock.getReturnAttributes(any())).thenReturn(new String[]{"uid"}); | ||
| when(ldapConfigurationMock.getSearchGroupPrinciple(any())).thenReturn(null); | ||
| when(ldapConfigurationMock.getBaseDn(any())).thenReturn("dc=my,dc=domain,dc=com"); | ||
| when(ldapConfigurationMock.getUsernameAttribute(any())).thenReturn("uid"); | ||
| when(ldapConfigurationMock.getUserObject(any())).thenReturn("inetOrgPerson"); | ||
| when(ldapConfigurationMock.getLdapPageSize(any())).thenReturn(1000); | ||
|
|
||
| LdapTrustMapVO linkedGroup = new LdapTrustMapVO(DOMAIN_ID, LdapManager.LinkType.GROUP, LINKED_GROUP, Account.Type.NORMAL, 5L); | ||
| when(ldapTrustMapDaoMock.searchByDomainId(anyLong())).thenReturn(Collections.singletonList(linkedGroup)); | ||
|
|
||
| NamingEnumeration<SearchResult> noResults = mock(NamingEnumeration.class); | ||
| when(noResults.hasMoreElements()).thenReturn(false); | ||
| when(ldapContextMock.search(any(String.class), any(String.class), any(SearchControls.class))).thenReturn(noResults); | ||
| when(ldapContextMock.getResponseControls()).thenReturn(null); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| if (ldapConfigurationMockedStatic != null) { | ||
| ldapConfigurationMockedStatic.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void getUserDoesNotRestrictToAlreadyLinkedGroups() throws Exception { | ||
| try { | ||
| openLdapUserManager.getUser("test_user", ldapContextMock, DOMAIN_ID); | ||
| } catch (NamingException expected) { | ||
| // no results stubbed; only the filter sent matters here | ||
| } | ||
|
|
||
| String filter = capturedSearchFilter(); | ||
| assertFalse("a lookup for one specific username must not be scoped to already-linked groups: " + filter, | ||
| filter.contains("memberOf=" + LINKED_GROUP)); | ||
| } | ||
|
|
||
| @Test | ||
| public void getUsersStillRestrictsToAlreadyLinkedGroups() throws Exception { | ||
| openLdapUserManager.getUsers("test_user", ldapContextMock, DOMAIN_ID); | ||
|
|
||
| String filter = capturedSearchFilter(); | ||
| assertTrue("browsing/importing users should still be scoped to already-linked groups: " + filter, | ||
| filter.contains("memberOf=" + LINKED_GROUP)); | ||
| } | ||
|
|
||
| @Test | ||
| public void getUsersDoesNotAddGroupScopeWhenDomainHasNoLinkedGroups() throws Exception { | ||
| when(ldapTrustMapDaoMock.searchByDomainId(anyLong())).thenReturn(Collections.emptyList()); | ||
|
|
||
| openLdapUserManager.getUsers("test_user", ldapContextMock, DOMAIN_ID); | ||
|
|
||
| String filter = capturedSearchFilter(); | ||
| assertFalse("no linked groups in the domain means no group scoping should be applied: " + filter, | ||
| filter.contains("memberOf=")); | ||
| } | ||
|
|
||
| @Test | ||
| public void searchStopsAfterOnePageWhenNoPagedResultsControlIsReturned() throws Exception { | ||
| Control unrelatedControl = mock(Control.class); | ||
| when(ldapContextMock.getResponseControls()).thenReturn(new Control[]{unrelatedControl}); | ||
|
|
||
| List<LdapUser> result = openLdapUserManager.searchUsers("test_user", ldapContextMock, DOMAIN_ID); | ||
|
|
||
| assertTrue(result.isEmpty()); | ||
| verify(ldapContextMock, times(1)).search(any(String.class), any(String.class), any(SearchControls.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void searchFollowsCookieFromPagedResultsControlToNextPage() throws Exception { | ||
| PagedResultsResponseControl pagedControl = mock(PagedResultsResponseControl.class); | ||
| when(pagedControl.getCookie()).thenReturn(new byte[]{1, 2, 3}); | ||
| when(ldapContextMock.getResponseControls()) | ||
| .thenReturn(new Control[]{pagedControl}) | ||
| .thenReturn(null); | ||
|
|
||
| List<LdapUser> result = openLdapUserManager.searchUsers("test_user", ldapContextMock, DOMAIN_ID); | ||
|
|
||
| assertTrue(result.isEmpty()); | ||
| verify(ldapContextMock, times(2)).search(any(String.class), any(String.class), any(SearchControls.class)); | ||
| } | ||
|
|
||
| private String capturedSearchFilter() throws Exception { | ||
| ArgumentCaptor<String> filterCaptor = ArgumentCaptor.forClass(String.class); | ||
| verify(ldapContextMock, atLeastOnce()).search(any(String.class), filterCaptor.capture(), any(SearchControls.class)); | ||
| return filterCaptor.getValue(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.