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