Skip to content

Commit 068d393

Browse files
authored
allow update of ldap linked account (#13949)
Co-authored-by: Daan Hoogland <dahn@apache.org>
1 parent bd35fb6 commit 068d393

2 files changed

Lines changed: 207 additions & 4 deletions

File tree

‎plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import com.cloud.user.dao.AccountDao;
6464
import com.cloud.utils.Pair;
6565
import com.cloud.utils.component.ComponentLifecycleBase;
66+
import com.cloud.utils.db.Transaction;
67+
import com.cloud.utils.db.TransactionCallback;
6668
import com.cloud.utils.exception.CloudRuntimeException;
6769

6870
@Component
@@ -473,8 +475,11 @@ private LinkAccountToLdapResponse linkAccountToLdapAndGetResponse(LinkAccountToL
473475
}
474476

475477
long accountId = account.getAccountId();
476-
clearOldAccountMapping(cmd);
477-
LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(cmd.getDomainId(), linkType, cmd.getLdapDomain(), cmd.getAccountType(), accountId));
478+
LdapTrustMapVO vo = Transaction.execute((TransactionCallback<LdapTrustMapVO>) status -> {
479+
clearOldAccountMapping(cmd, accountId);
480+
clearAccountsOwnMapping(cmd.getDomainId(), accountId);
481+
return _ldapTrustMapDao.persist(new LdapTrustMapVO(cmd.getDomainId(), linkType, cmd.getLdapDomain(), cmd.getAccountType(), accountId));
482+
});
478483
return new LinkAccountToLdapResponse(domain.getUuid(), vo.getType().toString(), vo.getName(), vo.getAccountType().ordinal(), account.getUuid(), cmd.getAccountName());
479484
}
480485

@@ -515,10 +520,23 @@ public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
515520
return linkAccountToLdapAndGetResponse(cmd);
516521
}
517522

518-
private void clearOldAccountMapping(LinkAccountToLdapCmd cmd) {
523+
/**
524+
* Replaces the account's existing LDAP mapping, if any, so {@link #linkAccountToLdap}
525+
* can update the ldapDomain/type of an existing link instead of failing on the
526+
* domain_id/account_id unique key.
527+
*/
528+
private void clearAccountsOwnMapping(Long domainId, long accountId) {
529+
LdapTrustMapVO ownVo = _ldapTrustMapDao.findByAccount(domainId, accountId);
530+
if (ownVo != null) {
531+
logger.warn("account {} in domain {} is already linked to ldap {} '{}'; replacing with the new mapping", accountId, domainId, ownVo.getType(), ownVo.getName());
532+
_ldapTrustMapDao.expunge(ownVo.getId());
533+
}
534+
}
535+
536+
private void clearOldAccountMapping(LinkAccountToLdapCmd cmd, long accountId) {
519537
// first find if exists log warning and update
520538
LdapTrustMapVO oldVo = _ldapTrustMapDao.findGroupInDomain(cmd.getDomainId(), cmd.getLdapDomain());
521-
if (oldVo != null) {
539+
if (oldVo != null && oldVo.getAccountId() != accountId) {
522540
// deal with edge cases, i.e. check if the old account is indeed deleted etc.
523541
if (oldVo.getAccountId() != 0L) {
524542
AccountVO oldAcount = accountDao.findByIdIncludingRemoved(oldVo.getAccountId());
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.ldap;
18+
19+
import com.cloud.domain.DomainVO;
20+
import com.cloud.domain.dao.DomainDao;
21+
import com.cloud.user.Account;
22+
import com.cloud.user.AccountVO;
23+
import com.cloud.user.dao.AccountDao;
24+
import com.cloud.utils.exception.CloudRuntimeException;
25+
import org.apache.cloudstack.acl.RoleService;
26+
import org.apache.cloudstack.api.command.LinkAccountToLdapCmd;
27+
import org.apache.cloudstack.api.response.LinkAccountToLdapResponse;
28+
import org.apache.cloudstack.ldap.dao.LdapTrustMapDao;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.mockito.Mock;
34+
import org.mockito.Mockito;
35+
import org.mockito.MockedStatic;
36+
import org.mockito.junit.MockitoJUnitRunner;
37+
import org.springframework.test.util.ReflectionTestUtils;
38+
39+
import java.util.Date;
40+
41+
import static org.junit.Assert.assertEquals;
42+
import static org.junit.Assert.assertThrows;
43+
import static org.mockito.ArgumentMatchers.any;
44+
import static org.mockito.Mockito.doThrow;
45+
import static org.mockito.Mockito.never;
46+
import static org.mockito.Mockito.times;
47+
import static org.mockito.Mockito.verify;
48+
import static org.mockito.Mockito.when;
49+
50+
/**
51+
* Tests {@link LdapManagerImpl#linkAccountToLdap}: re-linking an account replaces its own
52+
* mapping, refuses a group already claimed by another live account, and leaves no mapping
53+
* persisted if clearing the old one fails.
54+
*/
55+
@RunWith(MockitoJUnitRunner.class)
56+
public class LdapManagerImplTest {
57+
58+
private static final Long DOMAIN_ID = 1L;
59+
private static final long ACCOUNT_ID = 24L;
60+
private static final long OLD_MAPPING_ID = 5L;
61+
62+
private LdapManagerImpl ldapManager;
63+
64+
private MockedStatic<LdapConfiguration> ldapConfigurationMockedStatic;
65+
66+
@Mock
67+
private LdapTrustMapDao ldapTrustMapDaoMock;
68+
69+
@Mock
70+
private DomainDao domainDaoMock;
71+
72+
@Mock
73+
private AccountDao accountDaoMock;
74+
75+
@Mock
76+
private RoleService roleServiceMock;
77+
78+
@Before
79+
public void setup() {
80+
ldapConfigurationMockedStatic = Mockito.mockStatic(LdapConfiguration.class, Mockito.CALLS_REAL_METHODS);
81+
when(LdapConfiguration.getBaseDn(DOMAIN_ID)).thenReturn("dc=my,dc=domain,dc=com");
82+
83+
ldapManager = new LdapManagerImpl();
84+
ldapManager._ldapTrustMapDao = ldapTrustMapDaoMock;
85+
ReflectionTestUtils.setField(ldapManager, "domainDao", domainDaoMock);
86+
ReflectionTestUtils.setField(ldapManager, "accountDao", accountDaoMock);
87+
when(domainDaoMock.findById(DOMAIN_ID)).thenReturn(new DomainVO());
88+
89+
AccountVO existingAccount = new AccountVO("jdoe", DOMAIN_ID, null, Account.Type.NORMAL, null, "acct-uuid");
90+
ReflectionTestUtils.setField(existingAccount, "id", ACCOUNT_ID);
91+
when(accountDaoMock.findActiveAccount("jdoe", DOMAIN_ID)).thenReturn(existingAccount);
92+
when(ldapTrustMapDaoMock.persist(any())).thenAnswer(invocation -> invocation.getArgument(0));
93+
}
94+
95+
@After
96+
public void tearDown() {
97+
ldapConfigurationMockedStatic.close();
98+
}
99+
100+
@Test
101+
public void relinkingAccountReplacesItsOwnExistingMapping() {
102+
LdapTrustMapVO ownMapping = new LdapTrustMapVO(DOMAIN_ID, LdapManager.LinkType.GROUP, "cn=old,dc=my,dc=domain,dc=com", Account.Type.NORMAL, ACCOUNT_ID);
103+
ReflectionTestUtils.setField(ownMapping, "id", OLD_MAPPING_ID);
104+
when(ldapTrustMapDaoMock.findByAccount(DOMAIN_ID, ACCOUNT_ID)).thenReturn(ownMapping);
105+
106+
LinkAccountToLdapResponse response = ldapManager.linkAccountToLdap(buildCmd("cn=new,dc=my,dc=domain,dc=com"));
107+
108+
verify(ldapTrustMapDaoMock, times(1)).expunge(Long.valueOf(OLD_MAPPING_ID));
109+
assertEquals("cn=new,dc=my,dc=domain,dc=com", response.getLdapDomain());
110+
}
111+
112+
@Test
113+
public void firstLinkOfAccountDoesNotExpungeAnything() {
114+
when(ldapTrustMapDaoMock.findByAccount(DOMAIN_ID, ACCOUNT_ID)).thenReturn(null);
115+
116+
ldapManager.linkAccountToLdap(buildCmd("cn=first,dc=my,dc=domain,dc=com"));
117+
118+
verify(ldapTrustMapDaoMock, never()).expunge(any(Long.class));
119+
}
120+
121+
@Test
122+
public void relinkingAccountToItsCurrentGroupDoesNotThrow() {
123+
LdapTrustMapVO ownMapping = new LdapTrustMapVO(DOMAIN_ID, LdapManager.LinkType.GROUP, "cn=same,dc=my,dc=domain,dc=com", Account.Type.NORMAL, ACCOUNT_ID);
124+
ReflectionTestUtils.setField(ownMapping, "id", OLD_MAPPING_ID);
125+
when(ldapTrustMapDaoMock.findGroupInDomain(DOMAIN_ID, "cn=same,dc=my,dc=domain,dc=com")).thenReturn(ownMapping);
126+
when(ldapTrustMapDaoMock.findByAccount(DOMAIN_ID, ACCOUNT_ID)).thenReturn(ownMapping);
127+
128+
LinkAccountToLdapResponse response = ldapManager.linkAccountToLdap(buildCmd("cn=same,dc=my,dc=domain,dc=com"));
129+
130+
assertEquals("cn=same,dc=my,dc=domain,dc=com", response.getLdapDomain());
131+
}
132+
133+
@Test
134+
public void relinkingAccountRefusesGroupClaimedByAnotherLiveAccount() {
135+
long otherAccountId = 99L;
136+
LdapTrustMapVO otherMapping = new LdapTrustMapVO(DOMAIN_ID, LdapManager.LinkType.GROUP, "cn=claimed,dc=my,dc=domain,dc=com", Account.Type.NORMAL, otherAccountId);
137+
when(ldapTrustMapDaoMock.findGroupInDomain(DOMAIN_ID, "cn=claimed,dc=my,dc=domain,dc=com")).thenReturn(otherMapping);
138+
AccountVO otherAccount = new AccountVO();
139+
when(accountDaoMock.findByIdIncludingRemoved(otherAccountId)).thenReturn(otherAccount);
140+
141+
LinkAccountToLdapCmd cmd = buildCmd("cn=claimed,dc=my,dc=domain,dc=com");
142+
assertThrows(CloudRuntimeException.class, () -> ldapManager.linkAccountToLdap(cmd));
143+
144+
verify(ldapTrustMapDaoMock, never()).persist(any());
145+
}
146+
147+
@Test
148+
public void relinkingAccountAllowsGroupOnceOtherClaimingAccountIsRemoved() {
149+
long removedAccountId = 99L;
150+
LdapTrustMapVO otherMapping = new LdapTrustMapVO(DOMAIN_ID, LdapManager.LinkType.GROUP, "cn=stale,dc=my,dc=domain,dc=com", Account.Type.NORMAL, removedAccountId);
151+
ReflectionTestUtils.setField(otherMapping, "id", OLD_MAPPING_ID);
152+
when(ldapTrustMapDaoMock.findGroupInDomain(DOMAIN_ID, "cn=stale,dc=my,dc=domain,dc=com")).thenReturn(otherMapping);
153+
AccountVO removedAccount = new AccountVO();
154+
ReflectionTestUtils.setField(removedAccount, "removed", new Date());
155+
when(accountDaoMock.findByIdIncludingRemoved(removedAccountId)).thenReturn(removedAccount);
156+
157+
LinkAccountToLdapResponse response = ldapManager.linkAccountToLdap(buildCmd("cn=stale,dc=my,dc=domain,dc=com"));
158+
159+
assertEquals("cn=stale,dc=my,dc=domain,dc=com", response.getLdapDomain());
160+
}
161+
162+
@Test
163+
public void relinkingAccountDoesNotPersistWhenClearingOldMappingFails() {
164+
LdapTrustMapVO ownMapping = new LdapTrustMapVO(DOMAIN_ID, LdapManager.LinkType.GROUP, "cn=old,dc=my,dc=domain,dc=com", Account.Type.NORMAL, ACCOUNT_ID);
165+
ReflectionTestUtils.setField(ownMapping, "id", OLD_MAPPING_ID);
166+
when(ldapTrustMapDaoMock.findByAccount(DOMAIN_ID, ACCOUNT_ID)).thenReturn(ownMapping);
167+
doThrow(new CloudRuntimeException("db blip")).when(ldapTrustMapDaoMock).expunge(Long.valueOf(OLD_MAPPING_ID));
168+
169+
LinkAccountToLdapCmd cmd = buildCmd("cn=new,dc=my,dc=domain,dc=com");
170+
assertThrows(CloudRuntimeException.class, () -> ldapManager.linkAccountToLdap(cmd));
171+
172+
verify(ldapTrustMapDaoMock, never()).persist(any());
173+
}
174+
175+
private LinkAccountToLdapCmd buildCmd(String ldapDomain) {
176+
LinkAccountToLdapCmd cmd = new LinkAccountToLdapCmd();
177+
cmd.roleService = roleServiceMock;
178+
ReflectionTestUtils.setField(cmd, "domainId", DOMAIN_ID);
179+
ReflectionTestUtils.setField(cmd, "type", "GROUP");
180+
ReflectionTestUtils.setField(cmd, "ldapDomain", ldapDomain);
181+
ReflectionTestUtils.setField(cmd, "accountName", "jdoe");
182+
ReflectionTestUtils.setField(cmd, "accountType", Account.Type.NORMAL.ordinal());
183+
return cmd;
184+
}
185+
}

0 commit comments

Comments
 (0)