|
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 |
|
|
176 | 175 | import com.cloud.utils.ConstantTimeComparator; |
177 | 176 | import com.cloud.utils.NumbersUtil; |
178 | 177 | import com.cloud.utils.Pair; |
| 178 | +import com.cloud.utils.StringUtils; |
179 | 179 | import com.cloud.utils.Ternary; |
180 | 180 | import com.cloud.utils.UuidUtils; |
181 | 181 | import com.cloud.utils.component.ComponentContext; |
@@ -591,10 +591,9 @@ public boolean isAdmin(Long accountId) { |
591 | 591 | } |
592 | 592 | if ((isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId))) { |
593 | 593 | return true; |
594 | | - } else if (acct.getType() == Account.Type.READ_ONLY_ADMIN) { |
595 | | - return true; |
| 594 | + } else { |
| 595 | + return acct.getType() == Account.Type.READ_ONLY_ADMIN; |
596 | 596 | } |
597 | | - |
598 | 597 | } |
599 | 598 | return false; |
600 | 599 | } |
@@ -648,10 +647,7 @@ public boolean isDomainAdmin(Long accountId) { |
648 | 647 | @Override |
649 | 648 | public boolean isNormalUser(long accountId) { |
650 | 649 | AccountVO acct = _accountDao.findById(accountId); |
651 | | - if (acct != null && acct.getType() == Account.Type.NORMAL) { |
652 | | - return true; |
653 | | - } |
654 | | - return false; |
| 650 | + return acct != null && acct.getType() == Account.Type.NORMAL; |
655 | 651 | } |
656 | 652 |
|
657 | 653 | @Override |
@@ -682,10 +678,7 @@ public boolean isInternalAccount(long accountId) { |
682 | 678 | if (account == null) { |
683 | 679 | return false; //account is deleted or does not exist |
684 | 680 | } |
685 | | - if (isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN)) { |
686 | | - return true; |
687 | | - } |
688 | | - return false; |
| 681 | + return isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN); |
689 | 682 | } |
690 | 683 |
|
691 | 684 | @Override |
@@ -735,12 +728,7 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner |
735 | 728 | HashMap<Long, List<ControlledEntity>> domains = new HashMap<>(); |
736 | 729 |
|
737 | 730 | for (ControlledEntity entity : entities) { |
738 | | - long domainId = entity.getDomainId(); |
739 | | - if (entity.getAccountId() != -1 && domainId == -1) { // If account exists domainId should too so calculate |
740 | | - // it. This condition might be hit for templates or entities which miss domainId in their tables |
741 | | - Account account = ApiDBUtils.findAccountById(entity.getAccountId()); |
742 | | - domainId = account != null ? account.getDomainId() : -1; |
743 | | - } |
| 731 | + long domainId = getDomainIdFor(entity); |
744 | 732 | if (entity.getAccountId() != -1 && domainId != -1 && !(entity instanceof VirtualMachineTemplate) |
745 | 733 | && !(entity instanceof Network && accessType != null && (accessType == AccessType.UseEntry || accessType == AccessType.OperateEntry)) |
746 | 734 | && !(entity instanceof AffinityGroup) && !(entity instanceof VirtualRouter)) { |
@@ -792,6 +780,17 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner |
792 | 780 |
|
793 | 781 | } |
794 | 782 |
|
| 783 | + private static long getDomainIdFor(ControlledEntity entity) { |
| 784 | + long domainId = entity.getDomainId(); |
| 785 | + if (entity.getAccountId() != -1 && domainId == -1) { |
| 786 | + // If account exists domainId should too so calculate it. |
| 787 | + // This condition might be hit for templates or entities which miss domainId in their tables |
| 788 | + Account account = ApiDBUtils.findAccountById(entity.getAccountId()); |
| 789 | + domainId = account != null ? account.getDomainId() : -1; |
| 790 | + } |
| 791 | + return domainId; |
| 792 | + } |
| 793 | + |
795 | 794 | @Override |
796 | 795 | public void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource) { |
797 | 796 | Class<?> resourceClass = resource.getClass(); |
@@ -2823,11 +2822,11 @@ public UserAccount authenticateUser(final String username, final String password |
2823 | 2822 | final Boolean ApiSourceCidrChecksEnabled = ApiServiceConfiguration.ApiSourceCidrChecksEnabled.value(); |
2824 | 2823 |
|
2825 | 2824 | if (ApiSourceCidrChecksEnabled) { |
2826 | | - logger.debug("CIDRs from which account '" + account.toString() + "' is allowed to perform API calls: " + accessAllowedCidrs); |
| 2825 | + logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account, accessAllowedCidrs); |
2827 | 2826 |
|
2828 | 2827 | // Block when is not in the list of allowed IPs |
2829 | 2828 | if (!NetUtils.isIpInCidrList(loginIpAddress, accessAllowedCidrs.split(","))) { |
2830 | | - logger.warn("Request by account '" + account.toString() + "' was denied since " + loginIpAddress.toString().replace("/", "") + " does not match " + accessAllowedCidrs); |
| 2829 | + logger.warn("Request by account '{}' was denied since {} does not match {}", account , loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); |
2831 | 2830 | throw new CloudAuthenticationException("Failed to authenticate user '" + username + "' in domain '" + domain.getPath() + "' from ip " |
2832 | 2831 | + loginIpAddress.toString().replace("/", "") + "; please provide valid credentials"); |
2833 | 2832 | } |
@@ -3000,7 +2999,7 @@ private UserAccount getUserAccountForSSO(String username, Long domainId, Map<Str |
3000 | 2999 | if (unsignedRequestBuffer.length() != 0) { |
3001 | 3000 | unsignedRequestBuffer.append("&"); |
3002 | 3001 | } |
3003 | | - unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, "UTF-8")); |
| 3002 | + unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, StringUtils.getPreferredCharset())); |
3004 | 3003 | } |
3005 | 3004 | } |
3006 | 3005 |
|
|
0 commit comments