-
Notifications
You must be signed in to change notification settings - Fork 1.3k
small refactors on account manager #11784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DaanHoogland
wants to merge
1
commit into
apache:main
Choose a base branch
from
DaanHoogland:accountManagerRefactors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+20
−21
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -85,7 +85,6 @@ | |||||
| import org.apache.commons.codec.binary.Base64; | ||||||
| import org.apache.commons.collections.CollectionUtils; | ||||||
| import org.apache.commons.lang3.BooleanUtils; | ||||||
| import org.apache.commons.lang3.StringUtils; | ||||||
| import org.jetbrains.annotations.NotNull; | ||||||
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||||||
|
|
||||||
|
|
@@ -176,6 +175,7 @@ | |||||
| import com.cloud.utils.ConstantTimeComparator; | ||||||
| import com.cloud.utils.NumbersUtil; | ||||||
| import com.cloud.utils.Pair; | ||||||
| import com.cloud.utils.StringUtils; | ||||||
| import com.cloud.utils.Ternary; | ||||||
| import com.cloud.utils.UuidUtils; | ||||||
| import com.cloud.utils.component.ComponentContext; | ||||||
|
|
@@ -591,10 +591,9 @@ public boolean isAdmin(Long accountId) { | |||||
| } | ||||||
| if ((isRootAdmin(accountId)) || (isDomainAdmin(accountId)) || (isResourceDomainAdmin(accountId))) { | ||||||
| return true; | ||||||
| } else if (acct.getType() == Account.Type.READ_ONLY_ADMIN) { | ||||||
| return true; | ||||||
| } else { | ||||||
| return acct.getType() == Account.Type.READ_ONLY_ADMIN; | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
|
|
@@ -648,10 +647,7 @@ public boolean isDomainAdmin(Long accountId) { | |||||
| @Override | ||||||
| public boolean isNormalUser(long accountId) { | ||||||
| AccountVO acct = _accountDao.findById(accountId); | ||||||
| if (acct != null && acct.getType() == Account.Type.NORMAL) { | ||||||
| return true; | ||||||
| } | ||||||
| return false; | ||||||
| return acct != null && acct.getType() == Account.Type.NORMAL; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
|
|
@@ -682,10 +678,7 @@ public boolean isInternalAccount(long accountId) { | |||||
| if (account == null) { | ||||||
| return false; //account is deleted or does not exist | ||||||
| } | ||||||
| if (isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN)) { | ||||||
| return true; | ||||||
| } | ||||||
| return false; | ||||||
| return isRootAdmin(accountId) || (account.getType() == Account.Type.ADMIN); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
|
|
@@ -735,12 +728,7 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner | |||||
| HashMap<Long, List<ControlledEntity>> domains = new HashMap<>(); | ||||||
|
|
||||||
| for (ControlledEntity entity : entities) { | ||||||
| long domainId = entity.getDomainId(); | ||||||
| if (entity.getAccountId() != -1 && domainId == -1) { // If account exists domainId should too so calculate | ||||||
| // it. This condition might be hit for templates or entities which miss domainId in their tables | ||||||
| Account account = ApiDBUtils.findAccountById(entity.getAccountId()); | ||||||
| domainId = account != null ? account.getDomainId() : -1; | ||||||
| } | ||||||
| long domainId = getDomainIdFor(entity); | ||||||
| if (entity.getAccountId() != -1 && domainId != -1 && !(entity instanceof VirtualMachineTemplate) | ||||||
| && !(entity instanceof Network && accessType != null && (accessType == AccessType.UseEntry || accessType == AccessType.OperateEntry)) | ||||||
| && !(entity instanceof AffinityGroup) && !(entity instanceof VirtualRouter)) { | ||||||
|
|
@@ -792,6 +780,17 @@ public void checkAccess(Account caller, AccessType accessType, boolean sameOwner | |||||
|
|
||||||
| } | ||||||
|
|
||||||
| private static long getDomainIdFor(ControlledEntity entity) { | ||||||
| long domainId = entity.getDomainId(); | ||||||
| if (entity.getAccountId() != -1 && domainId == -1) { | ||||||
| // If account exists domainId should too so calculate it. | ||||||
| // This condition might be hit for templates or entities which miss domainId in their tables | ||||||
| Account account = ApiDBUtils.findAccountById(entity.getAccountId()); | ||||||
| domainId = account != null ? account.getDomainId() : -1; | ||||||
| } | ||||||
| return domainId; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource) { | ||||||
| Class<?> resourceClass = resource.getClass(); | ||||||
|
|
@@ -2829,11 +2828,11 @@ public UserAccount authenticateUser(final String username, final String password | |||||
| final Boolean ApiSourceCidrChecksEnabled = ApiServiceConfiguration.ApiSourceCidrChecksEnabled.value(); | ||||||
|
|
||||||
| if (ApiSourceCidrChecksEnabled) { | ||||||
| logger.debug("CIDRs from which account '" + account.toString() + "' is allowed to perform API calls: " + accessAllowedCidrs); | ||||||
| logger.debug("CIDRs from which account '{}' is allowed to perform API calls: {}", account, accessAllowedCidrs); | ||||||
|
|
||||||
| // Block when is not in the list of allowed IPs | ||||||
| if (!NetUtils.isIpInCidrList(loginIpAddress, accessAllowedCidrs.split(","))) { | ||||||
| logger.warn("Request by account '" + account.toString() + "' was denied since " + loginIpAddress.toString().replace("/", "") + " does not match " + accessAllowedCidrs); | ||||||
| logger.warn("Request by account '{}' was denied since {} does not match {}", account , loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); | ||||||
|
||||||
| logger.warn("Request by account '{}' was denied since {} does not match {}", account , loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); | |
| logger.warn("Request by account '{}' was denied since {} does not match {}", account, loginIpAddress.toString().replace("/", ""), accessAllowedCidrs); |
DaanHoogland marked this conversation as resolved.
Show resolved
Hide resolved
DaanHoogland marked this conversation as resolved.
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
staticbe removed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I think it can. Any pressing reason? It is a bit of a utility method and has no bearing on the manager internals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the keyword is useful here, but it's just a nitpicking. ;)