|
48 | 48 | import org.apache.cloudstack.api.response.LinkAccountToLdapResponse; |
49 | 49 | import org.apache.cloudstack.api.response.LinkDomainToLdapResponse; |
50 | 50 | import org.apache.cloudstack.framework.messagebus.MessageBus; |
51 | | -import org.apache.cloudstack.framework.messagebus.MessageSubscriber; |
52 | 51 | import org.apache.cloudstack.ldap.dao.LdapConfigurationDao; |
53 | 52 | import org.apache.cloudstack.ldap.dao.LdapTrustMapDao; |
54 | 53 | import org.apache.commons.lang.Validate; |
55 | 54 | import org.apache.commons.lang3.StringUtils; |
| 55 | +import org.jetbrains.annotations.NotNull; |
56 | 56 | import org.springframework.stereotype.Component; |
57 | 57 |
|
58 | 58 | import com.cloud.domain.DomainVO; |
@@ -114,36 +114,30 @@ public boolean configure(String name, Map<String, Object> params) throws Configu |
114 | 114 | } |
115 | 115 |
|
116 | 116 | private void addAccountRemovalListener() { |
117 | | - messageBus.subscribe(AccountManager.MESSAGE_REMOVE_ACCOUNT_EVENT, new MessageSubscriber() { |
118 | | - @Override |
119 | | - public void onPublishMessage(String senderAddress, String subject, Object args) { |
120 | | - try { |
121 | | - final Account account = accountDao.findByIdIncludingRemoved((Long) args); |
122 | | - long domainId = account.getDomainId(); |
123 | | - LdapTrustMapVO ldapTrustMapVO = _ldapTrustMapDao.findByAccount(domainId, account.getAccountId()); |
124 | | - if (ldapTrustMapVO != null) { |
125 | | - removeTrustmap(ldapTrustMapVO); |
126 | | - } |
127 | | - } catch (final Exception e) { |
128 | | - logger.error("Caught exception while removing account linked to LDAP", e); |
| 117 | + messageBus.subscribe(AccountManager.MESSAGE_REMOVE_ACCOUNT_EVENT, (senderAddress, subject, args) -> { |
| 118 | + try { |
| 119 | + final Account account = accountDao.findByIdIncludingRemoved((Long) args); |
| 120 | + long domainId = account.getDomainId(); |
| 121 | + LdapTrustMapVO ldapTrustMapVO = _ldapTrustMapDao.findByAccount(domainId, account.getAccountId()); |
| 122 | + if (ldapTrustMapVO != null) { |
| 123 | + removeTrustmap(ldapTrustMapVO); |
129 | 124 | } |
| 125 | + } catch (final Exception e) { |
| 126 | + logger.error("Caught exception while removing account linked to LDAP", e); |
130 | 127 | } |
131 | 128 | }); |
132 | 129 | } |
133 | 130 |
|
134 | 131 | private void addDomainRemovalListener() { |
135 | | - messageBus.subscribe(DomainManager.MESSAGE_REMOVE_DOMAIN_EVENT, new MessageSubscriber() { |
136 | | - @Override |
137 | | - public void onPublishMessage(String senderAddress, String subject, Object args) { |
138 | | - try { |
139 | | - long domainId = ((DomainVO) args).getId(); |
140 | | - List<LdapTrustMapVO> ldapTrustMapVOs = _ldapTrustMapDao.searchByDomainId(domainId); |
141 | | - for (LdapTrustMapVO ldapTrustMapVO : ldapTrustMapVOs) { |
142 | | - removeTrustmap(ldapTrustMapVO); |
143 | | - } |
144 | | - } catch (final Exception e) { |
145 | | - logger.error("Caught exception while removing trust-map for domain linked to LDAP", e); |
| 132 | + messageBus.subscribe(DomainManager.MESSAGE_REMOVE_DOMAIN_EVENT, (senderAddress, subject, args) -> { |
| 133 | + try { |
| 134 | + long domainId = ((DomainVO) args).getId(); |
| 135 | + List<LdapTrustMapVO> ldapTrustMapVOs = _ldapTrustMapDao.searchByDomainId(domainId); |
| 136 | + for (LdapTrustMapVO ldapTrustMapVO : ldapTrustMapVOs) { |
| 137 | + removeTrustmap(ldapTrustMapVO); |
146 | 138 | } |
| 139 | + } catch (final Exception e) { |
| 140 | + logger.error("Caught exception while removing trust-map for domain linked to LDAP", e); |
147 | 141 | } |
148 | 142 | }); |
149 | 143 | } |
@@ -200,10 +194,10 @@ private LdapConfigurationResponse addConfigurationInternal(final String hostname |
200 | 194 |
|
201 | 195 | /** |
202 | 196 | * TODO decide if the principal is good enough to get the domain id or we need to add it as parameter |
203 | | - * @param principal |
204 | | - * @param password |
205 | | - * @param domainId |
206 | | - * @return |
| 197 | + * @param principal ldap user |
| 198 | + * @param password the users password to check |
| 199 | + * @param domainId the domain for logging into |
| 200 | + * @return true if the user can authenticate |
207 | 201 | */ |
208 | 202 | @Override |
209 | 203 | public boolean canAuthenticate(final String principal, final String password, final Long domainId) { |
@@ -428,20 +422,42 @@ private LinkDomainToLdapResponse linkDomainToLdap(Long domainId, String type, St |
428 | 422 | //Account type should be 0 or 2. check the constants in com.cloud.user.Account |
429 | 423 | Validate.isTrue(accountType== Account.Type.NORMAL || accountType== Account.Type.DOMAIN_ADMIN, "accountype should be either 0(normal user) or 2(domain admin)"); |
430 | 424 | LinkType linkType = LdapManager.LinkType.valueOf(type.toUpperCase()); |
431 | | - LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(domainId, linkType, name, accountType, 0)); |
432 | | - DomainVO domain = domainDao.findById(vo.getDomainId()); |
433 | | - String domainUuid = getDomainUuid(domain, vo); |
| 425 | + return linkDomainToLdapAndGetResponse(domainId, name, accountType, linkType); |
| 426 | + } |
| 427 | + |
| 428 | + @NotNull |
| 429 | + private LinkDomainToLdapResponse linkDomainToLdapAndGetResponse(Long domainId, String name, Account.Type accountType, LinkType linkType) { |
| 430 | + DomainVO domain = getDomainToLink(domainId); |
| 431 | + LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(domain.getId(), linkType, name, accountType, 0)); |
| 432 | + String domainUuid = domain.getUuid(); |
434 | 433 | return new LinkDomainToLdapResponse(domainUuid, vo.getType().toString(), vo.getName(), vo.getAccountType().ordinal()); |
435 | 434 | } |
436 | 435 |
|
437 | | - private String getDomainUuid(DomainVO domain, LdapTrustMapVO vo) { |
438 | | - String domainUuid = "<unknown>"; |
| 436 | + @NotNull |
| 437 | + private DomainVO getDomainToLink(Long domainId) { |
| 438 | + DomainVO domain = domainDao.findById(domainId); |
439 | 439 | if (domain == null) { |
440 | | - logger.error("no domain in database for id {}", vo.getDomainId()); |
441 | | - } else { |
442 | | - domainUuid = domain.getUuid(); |
| 440 | + String msg = "Cannot link Domain to LDAP. No domain found"; |
| 441 | + logger.error(msg); |
| 442 | + throw new InvalidParameterValueException(msg); |
| 443 | + } |
| 444 | + return domain; |
| 445 | + } |
| 446 | + |
| 447 | + @NotNull |
| 448 | + private LinkAccountToLdapResponse linkAccountToLdapAndGetResponse(LinkAccountToLdapCmd cmd) { |
| 449 | + DomainVO domain = getDomainToLink(cmd.getDomainId()); |
| 450 | + LinkType linkType = LinkType.valueOf(cmd.getType().toUpperCase()); |
| 451 | + Account account = accountDao.findActiveAccount(cmd.getAccountName(), cmd.getDomainId()); |
| 452 | + if (account == null) { |
| 453 | + account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), cmd.getRoleId(), UUID.randomUUID().toString()); |
| 454 | + accountDao.persist((AccountVO)account); |
443 | 455 | } |
444 | | - return domainUuid; |
| 456 | + |
| 457 | + long accountId = account.getAccountId(); |
| 458 | + clearOldAccountMapping(cmd); |
| 459 | + LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(cmd.getDomainId(), linkType, cmd.getLdapDomain(), cmd.getAccountType(), accountId)); |
| 460 | + return new LinkAccountToLdapResponse(domain.getUuid(), vo.getType().toString(), vo.getName(), vo.getAccountType().ordinal(), account.getUuid(), cmd.getAccountName()); |
445 | 461 | } |
446 | 462 |
|
447 | 463 | @Override |
@@ -469,20 +485,7 @@ public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) { |
469 | 485 | Validate.notEmpty(cmd.getLdapDomain(), "GROUP or OU name cannot be empty"); |
470 | 486 | Validate.isTrue(cmd.getAccountType() != null || cmd.getRoleId() != null, "Either account type or role ID must be given"); |
471 | 487 |
|
472 | | - LinkType linkType = LdapManager.LinkType.valueOf(cmd.getType().toUpperCase()); |
473 | | - Account account = accountDao.findActiveAccount(cmd.getAccountName(),cmd.getDomainId()); |
474 | | - if (account == null) { |
475 | | - account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), cmd.getRoleId(), UUID.randomUUID().toString()); |
476 | | - accountDao.persist((AccountVO)account); |
477 | | - } |
478 | | - |
479 | | - long accountId = account.getAccountId(); |
480 | | - clearOldAccountMapping(cmd); |
481 | | - LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(cmd.getDomainId(), linkType, cmd.getLdapDomain(), cmd.getAccountType(), accountId)); |
482 | | - DomainVO domain = domainDao.findById(vo.getDomainId()); |
483 | | - String domainUuid = getDomainUuid(domain, vo); |
484 | | - |
485 | | - return new LinkAccountToLdapResponse(domainUuid, vo.getType().toString(), vo.getName(), vo.getAccountType().ordinal(), account.getUuid(), cmd.getAccountName()); |
| 488 | + return linkAccountToLdapAndGetResponse(cmd); |
486 | 489 | } |
487 | 490 |
|
488 | 491 | private void clearOldAccountMapping(LinkAccountToLdapCmd cmd) { |
|
0 commit comments