|
85 | 85 | import org.apache.commons.codec.binary.Base64; |
86 | 86 | import org.apache.commons.collections.CollectionUtils; |
87 | 87 | import org.apache.commons.lang3.BooleanUtils; |
88 | | -import org.apache.commons.lang3.StringUtils; |
89 | 88 | import org.jetbrains.annotations.NotNull; |
90 | 89 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
91 | 90 |
|
|
175 | 174 | import com.cloud.utils.ConstantTimeComparator; |
176 | 175 | import com.cloud.utils.NumbersUtil; |
177 | 176 | import com.cloud.utils.Pair; |
| 177 | +import com.cloud.utils.StringUtils; |
178 | 178 | import com.cloud.utils.Ternary; |
179 | 179 | import com.cloud.utils.component.ComponentContext; |
180 | 180 | import com.cloud.utils.component.Manager; |
@@ -587,10 +587,9 @@ public boolean isAdmin(Long accountId) { |
587 | 587 | } |
588 | 588 | if ((isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId))) { |
589 | 589 | return true; |
590 | | - } else if (acct.getType() == Account.Type.READ_ONLY_ADMIN) { |
591 | | - return true; |
| 590 | + } else { |
| 591 | + return acct.getType() == Account.Type.READ_ONLY_ADMIN; |
592 | 592 | } |
593 | | - |
594 | 593 | } |
595 | 594 | return false; |
596 | 595 | } |
@@ -644,10 +643,7 @@ public boolean isDomainAdmin(Long accountId) { |
644 | 643 | @Override |
645 | 644 | public boolean isNormalUser(long accountId) { |
646 | 645 | AccountVO acct = _accountDao.findById(accountId); |
647 | | - if (acct != null && acct.getType() == Account.Type.NORMAL) { |
648 | | - return true; |
649 | | - } |
650 | | - return false; |
| 646 | + return acct != null && acct.getType() == Account.Type.NORMAL; |
651 | 647 | } |
652 | 648 |
|
653 | 649 | @Override |
@@ -678,10 +674,7 @@ public boolean isInternalAccount(long accountId) { |
678 | 674 | if (account == null) { |
679 | 675 | return false; //account is deleted or does not exist |
680 | 676 | } |
681 | | - if (isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN)) { |
682 | | - return true; |
683 | | - } |
684 | | - return false; |
| 677 | + return isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN); |
685 | 678 | } |
686 | 679 |
|
687 | 680 | @Override |
@@ -731,12 +724,7 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner |
731 | 724 | HashMap<Long, List<ControlledEntity>> domains = new HashMap<>(); |
732 | 725 |
|
733 | 726 | for (ControlledEntity entity : entities) { |
734 | | - long domainId = entity.getDomainId(); |
735 | | - if (entity.getAccountId() != -1 && domainId == -1) { // If account exists domainId should too so calculate |
736 | | - // it. This condition might be hit for templates or entities which miss domainId in their tables |
737 | | - Account account = ApiDBUtils.findAccountById(entity.getAccountId()); |
738 | | - domainId = account != null ? account.getDomainId() : -1; |
739 | | - } |
| 727 | + long domainId = getDomainIdFor(entity); |
740 | 728 | if (entity.getAccountId() != -1 && domainId != -1 && !(entity instanceof VirtualMachineTemplate) |
741 | 729 | && !(entity instanceof Network && accessType != null && (accessType == AccessType.UseEntry || accessType == AccessType.OperateEntry)) |
742 | 730 | && !(entity instanceof AffinityGroup) && !(entity instanceof VirtualRouter)) { |
@@ -788,6 +776,17 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner |
788 | 776 |
|
789 | 777 | } |
790 | 778 |
|
| 779 | + private static long getDomainIdFor(ControlledEntity entity) { |
| 780 | + long domainId = entity.getDomainId(); |
| 781 | + if (entity.getAccountId() != -1 && domainId == -1) { |
| 782 | + // If account exists domainId should too so calculate it. |
| 783 | + // This condition might be hit for templates or entities which miss domainId in their tables |
| 784 | + Account account = ApiDBUtils.findAccountById(entity.getAccountId()); |
| 785 | + domainId = account != null ? account.getDomainId() : -1; |
| 786 | + } |
| 787 | + return domainId; |
| 788 | + } |
| 789 | + |
791 | 790 | @Override |
792 | 791 | public void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource) { |
793 | 792 | Class<?> resourceClass = resource.getClass(); |
@@ -2813,11 +2812,11 @@ public UserAccount authenticateUser(final String username, final String password |
2813 | 2812 | final Boolean ApiSourceCidrChecksEnabled = ApiServiceConfiguration.ApiSourceCidrChecksEnabled.value(); |
2814 | 2813 |
|
2815 | 2814 | if (ApiSourceCidrChecksEnabled) { |
2816 | | - logger.debug("CIDRs from which account '" + account.toString() + "' is allowed to perform API calls: " + accessAllowedCidrs); |
| 2815 | + logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account, accessAllowedCidrs); |
2817 | 2816 |
|
2818 | 2817 | // Block when is not in the list of allowed IPs |
2819 | 2818 | if (!NetUtils.isIpInCidrList(loginIpAddress, accessAllowedCidrs.split(","))) { |
2820 | | - logger.warn("Request by account '" + account.toString() + "' was denied since " + loginIpAddress.toString().replace("/", "") + " does not match " + accessAllowedCidrs); |
| 2819 | + logger.warn("Request by account '{}' was denied since {} does not match {}", account , loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); |
2821 | 2820 | throw new CloudAuthenticationException("Failed to authenticate user '" + username + "' in domain '" + domain.getPath() + "' from ip " |
2822 | 2821 | + loginIpAddress.toString().replace("/", "") + "; please provide valid credentials"); |
2823 | 2822 | } |
@@ -2990,7 +2989,7 @@ private UserAccount getUserAccountForSSO(String username, Long domainId, Map<Str |
2990 | 2989 | if (unsignedRequestBuffer.length() != 0) { |
2991 | 2990 | unsignedRequestBuffer.append("&"); |
2992 | 2991 | } |
2993 | | - unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, "UTF-8")); |
| 2992 | + unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, StringUtils.getPreferredCharset())); |
2994 | 2993 | } |
2995 | 2994 | } |
2996 | 2995 |
|
|
0 commit comments