|
125 | 125 | import org.apache.commons.collections.CollectionUtils; |
126 | 126 | import org.apache.commons.lang3.BooleanUtils; |
127 | 127 | import org.apache.commons.lang3.ObjectUtils; |
128 | | -import org.apache.commons.lang3.StringUtils; |
129 | 128 | import org.jetbrains.annotations.NotNull; |
130 | 129 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
131 | 130 |
|
|
212 | 211 | import com.cloud.utils.ConstantTimeComparator; |
213 | 212 | import com.cloud.utils.NumbersUtil; |
214 | 213 | import com.cloud.utils.Pair; |
| 214 | +import com.cloud.utils.StringUtils; |
215 | 215 | import com.cloud.utils.Ternary; |
216 | 216 | import com.cloud.utils.UuidUtils; |
217 | 217 | import com.cloud.utils.component.ComponentContext; |
@@ -647,7 +647,6 @@ public boolean isAdmin(Long accountId) { |
647 | 647 | return false; //account is deleted or does not exist |
648 | 648 | } |
649 | 649 | return (isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId)) || (acct.getType() == Account.Type.READ_ONLY_ADMIN); |
650 | | - |
651 | 650 | } |
652 | 651 | return false; |
653 | 652 | } |
@@ -782,23 +781,13 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner |
782 | 781 | HashMap<Long, List<ControlledEntity>> domains = new HashMap<>(); |
783 | 782 |
|
784 | 783 | for (ControlledEntity entity : entities) { |
785 | | - long domainId = entity.getDomainId(); |
786 | | - if (entity.getAccountId() != -1 && domainId == -1) { // If account exists domainId should too so calculate |
787 | | - // it. 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 | | - } |
| 784 | + long domainId = getDomainIdFor(entity); |
791 | 785 | if (entity.getAccountId() != -1 && domainId != -1 && !(entity instanceof VirtualMachineTemplate) |
792 | 786 | && !(entity instanceof Network && (accessType == AccessType.UseEntry || accessType == AccessType.OperateEntry)) |
793 | 787 | && !(entity instanceof AffinityGroup) && !(entity instanceof VirtualRouter) |
794 | 788 | && !(entity instanceof DnsServer) && !(entity instanceof DnsZone)) { |
795 | | - List<ControlledEntity> toBeChecked = domains.get(entity.getDomainId()); |
796 | 789 | // for templates, we don't have to do cross domains check |
797 | | - if (toBeChecked == null) { |
798 | | - toBeChecked = new ArrayList<>(); |
799 | | - domains.put(domainId, toBeChecked); |
800 | | - } |
801 | | - toBeChecked.add(entity); |
| 790 | + domains.computeIfAbsent(domainId, k -> new ArrayList<>()).add(entity); |
802 | 791 | } |
803 | 792 | boolean granted = false; |
804 | 793 | for (SecurityChecker checker : _securityCheckers) { |
@@ -840,6 +829,17 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner |
840 | 829 |
|
841 | 830 | } |
842 | 831 |
|
| 832 | + private long getDomainIdFor(ControlledEntity entity) { |
| 833 | + long domainId = entity.getDomainId(); |
| 834 | + if (entity.getAccountId() != -1 && domainId == -1) { |
| 835 | + // If account exists domainId should too so calculate it. |
| 836 | + // This condition might be hit for templates or entities which miss domainId in their tables |
| 837 | + Account account = ApiDBUtils.findAccountById(entity.getAccountId()); |
| 838 | + domainId = account != null ? account.getDomainId() : -1; |
| 839 | + } |
| 840 | + return domainId; |
| 841 | + } |
| 842 | + |
843 | 843 | @Override |
844 | 844 | public void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource) { |
845 | 845 | Class<?> resourceClass = resource.getClass(); |
@@ -3067,11 +3067,11 @@ public UserAccount authenticateUser(final String username, final String password |
3067 | 3067 | final Boolean ApiSourceCidrChecksEnabled = ApiServiceConfiguration.ApiSourceCidrChecksEnabled.value(); |
3068 | 3068 |
|
3069 | 3069 | if (ApiSourceCidrChecksEnabled) { |
3070 | | - logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account.toString(), accessAllowedCidrs); |
| 3070 | + logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account, accessAllowedCidrs); |
3071 | 3071 |
|
3072 | 3072 | // Block when is not in the list of allowed IPs |
3073 | 3073 | if (!NetUtils.isIpInCidrList(loginIpAddress, accessAllowedCidrs.split(","))) { |
3074 | | - logger.warn("Request by account '{}' was denied since {} does not match {}", account.toString(), loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); |
| 3074 | + logger.warn("Request by account '{}' was denied since {} does not match {}", account, loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); |
3075 | 3075 | throw new CloudAuthenticationException("Failed to authenticate user '" + username + "' in domain '" + domain.getPath() + "' from ip " |
3076 | 3076 | + loginIpAddress.toString().replace("/", "") + "; please provide valid credentials"); |
3077 | 3077 | } |
@@ -3241,7 +3241,7 @@ private UserAccount getUserAccountForSSO(String username, Long domainId, Map<Str |
3241 | 3241 | if (unsignedRequestBuffer.length() != 0) { |
3242 | 3242 | unsignedRequestBuffer.append("&"); |
3243 | 3243 | } |
3244 | | - unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, com.cloud.utils.StringUtils.getPreferredCharset())); |
| 3244 | + unsignedRequestBuffer.append(paramName).append("=").append(URLEncoder.encode(paramValue, StringUtils.getPreferredCharset())); |
3245 | 3245 | } |
3246 | 3246 | } |
3247 | 3247 |
|
|
0 commit comments