Skip to content

Commit 11c0c26

Browse files
committed
QueryChecker interface and ACL search criteria to be used for query api
for entities with db views created.
1 parent 9d0d962 commit 11c0c26

8 files changed

Lines changed: 201 additions & 42 deletions

File tree

‎api/src/org/apache/cloudstack/acl/AclService.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
2222

23-
import com.cloud.utils.Pair;
2423
import com.cloud.user.Account;
24+
import com.cloud.utils.Pair;
2525

2626
public interface AclService {
2727

@@ -89,4 +89,10 @@ public interface AclService {
8989

9090
List<AclRole> getEffectiveRoles(Account caller, ControlledEntity entity);
9191

92+
List<Long> getGrantedDomains(long accountId, AclEntityType entityType, String action);
93+
94+
List<Long> getGrantedAccounts(long accountId, AclEntityType entityType, String action);
95+
96+
List<Long> getGrantedResources(long accountId, AclEntityType entityType, String action);
97+
9298
}

‎api/src/org/apache/cloudstack/acl/QueryChecker.java‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ public interface QueryChecker extends Adapter {
3535
*/
3636
List<Long> getAuthorizedDomains(Account caller, AclEntityType entityType);
3737

38-
/**
39-
* List denied domains for the caller, given a specific entity type.
40-
*
41-
* @param caller account to check against.
42-
* @param entityType entity type
43-
* @return list of domain Ids granted to the caller account.
44-
*/
45-
List<Long> getDeniedDomains(Account caller, AclEntityType entityType);
46-
4738
/**
4839
* List granted accounts for the caller, given a specific entity type.
4940
*
@@ -53,14 +44,6 @@ public interface QueryChecker extends Adapter {
5344
*/
5445
List<Long> getAuthorizedAccounts(Account caller, AclEntityType entityType);
5546

56-
/**
57-
* List denied accounts for the caller, given a specific entity type.
58-
*
59-
* @param caller account to check against.
60-
* @param entityType entity type
61-
* @return list of domain Ids granted to the caller account.
62-
*/
63-
List<Long> getDeniedAccounts(Account caller, AclEntityType entityType);
6447

6548
/**
6649
* List granted resources for the caller, given a specific entity type.
@@ -71,13 +54,5 @@ public interface QueryChecker extends Adapter {
7154
*/
7255
List<Long> getAuthorizedResources(Account caller, AclEntityType entityType);
7356

74-
/**
75-
* List denied resources for the caller, given a specific entity type.
76-
*
77-
* @param caller account to check against.
78-
* @param entityType entity type
79-
* @return list of domain Ids granted to the caller account.
80-
*/
81-
List<Long> getDeniedResources(Account caller, AclEntityType entityType);
8257

8358
}

‎plugins/network-elements/juniper-contrail/test/org/apache/cloudstack/network/contrail/management/MockAccountManager.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,22 @@ public boolean isNormalUser(long accountId) {
397397
// TODO Auto-generated method stub
398398
return false;
399399
}
400+
401+
@Override
402+
public void buildACLSearchParameters(Account caller, Long id, String accountName, Long projectId, List<Long> permittedDomains, List<Long> permittedAccounts,
403+
List<Long> permittedResources, Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation,
404+
String action) {
405+
// TODO Auto-generated method stub
406+
407+
}
408+
409+
@Override
410+
public void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledEntity> sc, SearchCriteria<? extends ControlledEntity> aclSc, boolean isRecursive,
411+
List<Long> permittedDomains, List<Long> permittedAccounts, List<Long> permittedResources, ListProjectResourcesCriteria listProjectResourcesCriteria) {
412+
// TODO Auto-generated method stub
413+
414+
}
415+
400416

401417

402418
}

‎server/src/com/cloud/api/query/QueryManagerImpl.java‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
import org.apache.log4j.Logger;
3030
import org.springframework.stereotype.Component;
3131

32-
import org.apache.cloudstack.acl.AclEntityType;
3332
import org.apache.cloudstack.acl.AclGroup;
3433
import org.apache.cloudstack.acl.AclRole;
3534
import org.apache.cloudstack.acl.AclService;
3635
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
37-
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
3836
import org.apache.cloudstack.acl.dao.AclGroupDao;
3937
import org.apache.cloudstack.acl.dao.AclRoleDao;
4038
import org.apache.cloudstack.affinity.AffinityGroupDomainMapVO;
@@ -749,20 +747,17 @@ public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) {
749747

750748
private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cmd) {
751749
Account caller = CallContext.current().getCallingAccount();
750+
List<Long> permittedDomains = new ArrayList<Long>();
752751
List<Long> permittedAccounts = new ArrayList<Long>();
753-
754-
// get granted or denied entity instance permissions
755-
Pair<List<Long>, List<Long>> idPair = _aclService.getAclEntityPermission(caller.getId(), AclEntityType.VM.toString(), AccessType.ListEntry);
756-
List<Long> grantedIds = idPair.first();
757-
List<Long> revokedIds = idPair.second();
752+
List<Long> permittedResources = new ArrayList<Long>();
758753

759754
boolean listAll = cmd.listAll();
760755
Long id = cmd.getId();
761756
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
762757
cmd.getDomainId(), cmd.isRecursive(), null);
763-
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts,
764-
domainIdRecursiveListProject, listAll, false);
765-
Long domainId = domainIdRecursiveListProject.first();
758+
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedDomains, permittedAccounts, permittedResources,
759+
domainIdRecursiveListProject, listAll, false, "listVirtualMachines");
760+
//Long domainId = domainIdRecursiveListProject.first();
766761
Boolean isRecursive = domainIdRecursiveListProject.second();
767762
ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
768763

@@ -773,10 +768,6 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm
773768
SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
774769
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct ids
775770

776-
// build acl search builder condition
777-
_accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts,
778-
listProjectResourcesCriteria, grantedIds, revokedIds);
779-
780771
Map<String, String> tags = cmd.getTags();
781772
String hypervisor = cmd.getHypervisor();
782773
Object name = cmd.getName();
@@ -854,10 +845,10 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm
854845

855846
// populate the search criteria with the values passed in
856847
SearchCriteria<UserVmJoinVO> sc = sb.create();
848+
SearchCriteria<UserVmJoinVO> aclSc = _userVmJoinDao.createSearchCriteria();
857849

858850
// building ACL search criteria
859-
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts,
860-
listProjectResourcesCriteria, grantedIds, revokedIds);
851+
_accountMgr.buildACLViewSearchCriteria(sc, aclSc, isRecursive, permittedDomains, permittedAccounts, permittedResources, listProjectResourcesCriteria);
861852

862853
if (tags != null && !tags.isEmpty()) {
863854
SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();

‎server/src/com/cloud/user/AccountManager.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledEntity> sc,
107107
void buildACLSearchParameters(Account caller, Long id,
108108
String accountName, Long projectId, List<Long> permittedAccounts, Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation);
109109

110+
// new ACL model routine for query api based on db views
111+
void buildACLSearchParameters(Account caller, Long id,
112+
String accountName, Long projectId, List<Long> permittedDomains, List<Long> permittedAccounts, List<Long> permittedResources,
113+
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation, String action);
114+
115+
void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledEntity> sc, SearchCriteria<? extends ControlledEntity> aclSc, boolean isRecursive,
116+
List<Long> permittedDomains, List<Long> permittedAccounts,
117+
List<Long> permittedResources, ListProjectResourcesCriteria listProjectResourcesCriteria);
118+
110119
/**
111120
* Deletes a user by userId
112121
*

‎server/src/com/cloud/user/AccountManagerImpl.java‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,4 +2537,132 @@ public UserAccount getUserByApiKey(String apiKey) {
25372537
return _userAccountDao.getUserByApiKey(apiKey);
25382538
}
25392539

2540+
@Override
2541+
public void buildACLSearchParameters(Account caller, Long id, String accountName, Long projectId, List<Long> permittedDomains, List<Long> permittedAccounts,
2542+
List<Long> permittedResources, Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation,
2543+
String action) {
2544+
Long domainId = domainIdRecursiveListProject.first();
2545+
if (domainId != null) {
2546+
// look for entity in the given domain
2547+
Domain domain = _domainDao.findById(domainId);
2548+
if (domain == null) {
2549+
throw new InvalidParameterValueException("Unable to find domain by id " + domainId);
2550+
}
2551+
// check permissions
2552+
checkAccess(caller, domain);
2553+
}
2554+
2555+
if (id != null) {
2556+
// look for an individual entity, no other permission criteria are needed
2557+
permittedResources.add(id);
2558+
return;
2559+
}
2560+
2561+
if (accountName != null) {
2562+
if (projectId != null) {
2563+
throw new InvalidParameterValueException("Account and projectId can't be specified together");
2564+
}
2565+
2566+
Account userAccount = null;
2567+
Domain domain = null;
2568+
if (domainId != null) {
2569+
userAccount = _accountDao.findActiveAccount(accountName, domainId);
2570+
domain = _domainDao.findById(domainId);
2571+
} else {
2572+
userAccount = _accountDao.findActiveAccount(accountName, caller.getDomainId());
2573+
domain = _domainDao.findById(caller.getDomainId());
2574+
}
2575+
2576+
if (userAccount != null) {
2577+
//check permissions
2578+
checkAccess(caller, null, false, userAccount);
2579+
permittedAccounts.add(userAccount.getId());
2580+
} else {
2581+
throw new InvalidParameterValueException("could not find account " + accountName + " in domain " + domain.getUuid());
2582+
}
2583+
}
2584+
2585+
// set project information
2586+
if (projectId != null) {
2587+
if (!forProjectInvitation) {
2588+
if (projectId.longValue() == -1) {
2589+
if (isNormalUser(caller.getId())) {
2590+
permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
2591+
} else {
2592+
domainIdRecursiveListProject.third(Project.ListProjectResourcesCriteria.ListProjectResourcesOnly);
2593+
}
2594+
} else {
2595+
Project project = _projectMgr.getProject(projectId);
2596+
if (project == null) {
2597+
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
2598+
}
2599+
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
2600+
throw new PermissionDeniedException("Account " + caller + " can't access project id=" + projectId);
2601+
}
2602+
permittedAccounts.add(project.getProjectAccountId());
2603+
}
2604+
}
2605+
} else {
2606+
domainIdRecursiveListProject.third(Project.ListProjectResourcesCriteria.SkipProjectResources);
2607+
2608+
// search for policy permissions associated with caller to get all his authorized domains, accounts, and resources
2609+
// Assumption: if a domain is in grantedDomains, then all the accounts under this domain will not be returned in "grantedAccounts". Similarly, if an account
2610+
// is in grantedAccounts, then all the resources owned by this account will not be returned in "grantedResources".
2611+
List<Long> grantedDomains = _aclService.getGrantedDomains(caller.getId(), AclEntityType.VM, action);
2612+
List<Long> grantedAccounts = _aclService.getGrantedAccounts(caller.getId(), AclEntityType.VM, action);
2613+
List<Long> grantedResources = _aclService.getGrantedResources(caller.getId(), AclEntityType.VM, action);
2614+
2615+
if (domainId != null) {
2616+
// specific domain is specified
2617+
if (grantedDomains.contains(domainId)) {
2618+
permittedDomains.add(domainId);
2619+
} else {
2620+
for (Long acctId : grantedAccounts) {
2621+
Account acct = _accountDao.findById(acctId);
2622+
if (acct != null && acct.getDomainId() == domainId) {
2623+
permittedAccounts.add(acctId);
2624+
}
2625+
}
2626+
permittedResources.addAll(grantedResources);
2627+
}
2628+
} else if (permittedAccounts.isEmpty()) {
2629+
// neither domain nor account is not specified
2630+
permittedDomains.addAll(grantedDomains);
2631+
permittedAccounts.addAll(grantedAccounts);
2632+
permittedResources.addAll(grantedResources);
2633+
}
2634+
}
2635+
2636+
}
2637+
2638+
@Override
2639+
public void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledEntity> sc, SearchCriteria<? extends ControlledEntity> aclSc, boolean isRecursive,
2640+
List<Long> permittedDomains,
2641+
List<Long> permittedAccounts, List<Long> permittedResources, ListProjectResourcesCriteria listProjectResourcesCriteria) {
2642+
2643+
if (listProjectResourcesCriteria != null) {
2644+
sc.addAnd("accountType", SearchCriteria.Op.EQ, Account.ACCOUNT_TYPE_PROJECT);
2645+
}
2646+
2647+
// Note that this may have limitations on number of permitted domains, accounts, or resource ids are allowed due to sql package size limitation
2648+
if (!permittedDomains.isEmpty()) {
2649+
if (isRecursive) {
2650+
for (int i = 0; i < permittedDomains.size(); i++) {
2651+
Domain domain = _domainDao.findById(permittedDomains.get(i));
2652+
aclSc.addOr("domainPath" + i, SearchCriteria.Op.LIKE, domain.getPath() + "%");
2653+
}
2654+
} else {
2655+
aclSc.addOr("domainIdIN", SearchCriteria.Op.IN, permittedDomains.toArray());
2656+
}
2657+
}
2658+
if (!permittedAccounts.isEmpty()) {
2659+
aclSc.addOr("accountIdIN", SearchCriteria.Op.IN, permittedAccounts.toArray());
2660+
}
2661+
if (!permittedResources.isEmpty()) {
2662+
aclSc.addOr("idIn", SearchCriteria.Op.IN, permittedResources.toArray());
2663+
}
2664+
2665+
sc.addAnd("accountIdIn", SearchCriteria.Op.SC, aclSc);
2666+
}
2667+
25402668
}

‎server/src/org/apache/cloudstack/acl/AclServiceImpl.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,22 @@ public List<AclRole> getEffectiveRoles(Account caller, ControlledEntity entity)
689689
return roles;
690690
}
691691

692+
@Override
693+
public List<Long> getGrantedDomains(long accountId, AclEntityType entityType, String action) {
694+
// TODO Auto-generated method stub
695+
return null;
696+
}
697+
698+
@Override
699+
public List<Long> getGrantedAccounts(long accountId, AclEntityType entityType, String action) {
700+
// TODO Auto-generated method stub
701+
return null;
702+
}
703+
704+
@Override
705+
public List<Long> getGrantedResources(long accountId, AclEntityType entityType, String action) {
706+
// TODO Auto-generated method stub
707+
return null;
708+
}
709+
692710
}

‎server/test/com/cloud/user/MockAccountManagerImpl.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,20 @@ public boolean isNormalUser(long accountId) {
362362
return false;
363363
}
364364

365+
@Override
366+
public void buildACLSearchParameters(Account caller, Long id, String accountName, Long projectId, List<Long> permittedDomains, List<Long> permittedAccounts,
367+
List<Long> permittedResources, Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation,
368+
String action) {
369+
// TODO Auto-generated method stub
370+
371+
}
372+
373+
@Override
374+
public void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledEntity> sc, SearchCriteria<? extends ControlledEntity> aclSc, boolean isRecursive,
375+
List<Long> permittedDomains, List<Long> permittedAccounts, List<Long> permittedResources, ListProjectResourcesCriteria listProjectResourcesCriteria) {
376+
// TODO Auto-generated method stub
377+
378+
}
379+
380+
365381
}

0 commit comments

Comments
 (0)