|
| 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