-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: avoid redundant DB calls when checking for root admin account #11390
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
base: main
Are you sure you want to change the base?
Changes from all commits
0576578
61dbcca
9ff1440
9ec24e3
3be274c
e14ffd4
b2d4fb6
ef5cf76
ffddb7f
1235a7d
c826221
02b4e38
b98f54b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,17 +23,20 @@ | |
|
|
||
| import org.apache.cloudstack.api.ApiCommandResourceType; | ||
| import org.apache.cloudstack.managed.threadlocal.ManagedThreadLocal; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.ThreadContext; | ||
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
|
|
||
| import com.cloud.exception.CloudAuthenticationException; | ||
| import com.cloud.projects.Project; | ||
| import com.cloud.user.Account; | ||
| import com.cloud.user.AccountService; | ||
| import com.cloud.user.User; | ||
| import com.cloud.utils.UuidUtils; | ||
| import com.cloud.utils.component.ComponentContext; | ||
| import com.cloud.utils.db.EntityManager; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import org.apache.logging.log4j.ThreadContext; | ||
|
|
||
| /** | ||
| * CallContext records information about the environment the call is made. This | ||
|
|
@@ -53,6 +56,7 @@ protected Stack<CallContext> initialValue() { | |
| private String contextId; | ||
| private Account account; | ||
| private long accountId; | ||
| private Boolean isAccountRootAdmin = null; | ||
| private long startEventId = 0; | ||
| private String eventDescription; | ||
| private String eventDetails; | ||
|
|
@@ -134,6 +138,21 @@ public Account getCallingAccount() { | |
| return account; | ||
| } | ||
|
|
||
| public boolean isCallingAccountRootAdmin() { | ||
| if (isAccountRootAdmin == null) { | ||
| AccountService accountService; | ||
| try { | ||
| accountService = ComponentContext.getDelegateComponentOfType(AccountService.class); | ||
| } catch (NoSuchBeanDefinitionException e) { | ||
| LOGGER.warn("Falling back to account type check for isRootAdmin for account ID: {} as no AccountService bean found: {}", accountId, e.getMessage()); | ||
| Account caller = getCallingAccount(); | ||
| return caller != null && caller.getType() == Account.Type.ADMIN; | ||
| } | ||
| isAccountRootAdmin = accountService.isRootAdmin(getCallingAccount()); | ||
| } | ||
| return Boolean.TRUE.equals(isAccountRootAdmin); | ||
| } | ||
|
Comment on lines
+141
to
+154
|
||
|
|
||
| public static CallContext current() { | ||
| CallContext context = s_currentContext.get(); | ||
|
|
||
|
|
||
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.
The fallback check
caller.getType() == Account.Type.ADMINis inconsistent with the actual root admin check performed by AccountService, which uses security checkers. This could lead to different behavior when AccountService is unavailable. Consider removing the fallback entirely or throwing an exception to fail fast when AccountService is not available.