Skip to content

Commit ffd8775

Browse files
committed
Fix RoleBasedQuerySelector to handle new listAll semantics. If
listAll=true, show all resources that caller (or impersonater) has ListEntry access type; otherwise, show all resources that caller (or impersonater) has UseEntry access type.
1 parent 3ae9b57 commit ffd8775

8 files changed

Lines changed: 94 additions & 66 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
22+
2123
import com.cloud.user.Account;
2224
import com.cloud.utils.component.Adapter;
2325

@@ -33,7 +35,7 @@ public interface QuerySelector extends Adapter {
3335
* @param action action
3436
* @return list of domain Ids granted to the caller account.
3537
*/
36-
List<Long> getAuthorizedDomains(Account caller, String action);
38+
List<Long> getAuthorizedDomains(Account caller, String action, AccessType accessType);
3739

3840
/**
3941
* List granted accounts for the caller, given a specific action.
@@ -42,7 +44,7 @@ public interface QuerySelector extends Adapter {
4244
* @param action action.
4345
* @return list of domain Ids granted to the caller account.
4446
*/
45-
List<Long> getAuthorizedAccounts(Account caller, String action);
47+
List<Long> getAuthorizedAccounts(Account caller, String action, AccessType accessType);
4648

4749

4850
/**
@@ -52,15 +54,15 @@ public interface QuerySelector extends Adapter {
5254
* @param action action.
5355
* @return list of domain Ids granted to the caller account.
5456
*/
55-
List<Long> getAuthorizedResources(Account caller, String action);
57+
List<Long> getAuthorizedResources(Account caller, String action, AccessType accessType);
5658

5759
/**
5860
* Check if this account is associated with a policy with scope of ALL
5961
* @param caller account to check
6062
* @param action action.
6163
* @return true if this account is attached with a policy for the given action of ALL scope.
6264
*/
63-
boolean isGrantedAll(Account caller, String action);
65+
boolean isGrantedAll(Account caller, String action, AccessType accessType);
6466

6567
/**
6668
* List of ACL group the given account belongs to

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,7 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
21652165
List<Long> permittedResources, Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation,
21662166
String action) {
21672167

2168+
Account owner = null; // for impersonation
21682169
Long domainId = domainIdRecursiveListProject.first();
21692170
if (id == null) {
21702171
// if id is specified, it will ignore all other parameters
@@ -2178,7 +2179,7 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
21782179
checkAccess(caller, domain);
21792180
}
21802181

2181-
// specific account is specified
2182+
// specific account is specified, we need to impersonate that account instead of caller
21822183
if (accountName != null) {
21832184
if (projectId != null) {
21842185
throw new InvalidParameterValueException("Account and projectId can't be specified together");
@@ -2197,14 +2198,10 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
21972198
if (userAccount != null) {
21982199
//check permissions
21992200
checkAccess(caller, null, userAccount);
2200-
permittedAccounts.add(userAccount.getId());
2201+
owner = userAccount;
22012202
} else {
22022203
throw new InvalidParameterValueException("could not find account " + accountName + " in domain " + domain.getUuid());
22032204
}
2204-
} else if (!listAll && domainId == null) {
2205-
// listAll=false with no domain specified will only show caller owned resources
2206-
// if domainId is specified, we will filter the list based caller visible resources (owned + granted)
2207-
permittedAccounts.add(caller.getId());
22082205
}
22092206
}
22102207

@@ -2229,6 +2226,15 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
22292226
}
22302227
}
22312228
} else {
2229+
if (owner == null) {
2230+
// no impersonation, so we directly check permission for caller
2231+
owner = caller;
2232+
}
2233+
AccessType accessType = AccessType.UseEntry;
2234+
if (listAll) {
2235+
// listAll = true should show all resources that owner has ListEntry access type
2236+
accessType = AccessType.ListEntry;
2237+
}
22322238
domainIdRecursiveListProject.third(Project.ListProjectResourcesCriteria.SkipProjectResources);
22332239

22342240
// search for policy permissions associated with caller to get all his authorized domains, accounts, and resources
@@ -2239,18 +2245,18 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
22392245
return; // no futher filtering
22402246

22412247
QuerySelector qs = _querySelectors.get(0);
2242-
boolean grantedAll = qs.isGrantedAll(caller, action);
2248+
boolean grantedAll = qs.isGrantedAll(owner, action, accessType);
22432249
if ( grantedAll ){
2244-
if (permittedAccounts.isEmpty() && domainId != null) {
2250+
if (domainId != null) {
22452251
permittedDomains.add(domainId);
22462252
}
22472253
}
22482254
else {
2249-
List<Long> grantedDomains = qs.getAuthorizedDomains(caller, action);
2250-
List<Long> grantedAccounts = qs.getAuthorizedAccounts(caller, action);
2251-
List<Long> grantedResources = qs.getAuthorizedResources(caller, action);
2255+
List<Long> grantedDomains = qs.getAuthorizedDomains(owner, action, accessType);
2256+
List<Long> grantedAccounts = qs.getAuthorizedAccounts(owner, action, accessType);
2257+
List<Long> grantedResources = qs.getAuthorizedResources(owner, action, accessType);
22522258

2253-
if (permittedAccounts.isEmpty() && domainId != null) {
2259+
if (domainId != null) {
22542260
// specific domain and no account is specified
22552261
if (grantedDomains.contains(domainId)) {
22562262
permittedDomains.add(domainId);
@@ -2268,7 +2274,7 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
22682274
// like NetworkACLItem
22692275
permittedResources.addAll(grantedResources);
22702276
}
2271-
} else if (permittedAccounts.isEmpty()) {
2277+
} else {
22722278
// neither domain nor account is not specified
22732279
permittedDomains.addAll(grantedDomains);
22742280
permittedAccounts.addAll(grantedAccounts);

‎services/iam/plugin/src/org/apache/cloudstack/iam/RoleBasedEntityQuerySelector.java‎

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.cloudstack.acl.PermissionScope;
2727
import org.apache.cloudstack.acl.QuerySelector;
28+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
2829
import org.apache.cloudstack.iam.api.IAMGroup;
2930
import org.apache.cloudstack.iam.api.IAMPolicy;
3031
import org.apache.cloudstack.iam.api.IAMPolicyPermission;
@@ -41,14 +42,22 @@ public class RoleBasedEntityQuerySelector extends AdapterBase implements QuerySe
4142
IAMService _iamService;
4243

4344
@Override
44-
public List<Long> getAuthorizedDomains(Account caller, String action) {
45+
public List<Long> getAuthorizedDomains(Account caller, String action, AccessType accessType) {
4546
long accountId = caller.getAccountId();
47+
if (accessType == null) {
48+
accessType = AccessType.UseEntry; // default always show resources authorized to use
49+
}
4650
// Get the static Policies of the Caller
4751
List<IAMPolicy> policies = _iamService.listIAMPolicies(accountId);
4852
// for each policy, find granted permission with Domain scope
4953
List<Long> domainIds = new ArrayList<Long>();
5054
for (IAMPolicy policy : policies) {
51-
List<IAMPolicyPermission> pp = _iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.DOMAIN.toString());
55+
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
56+
for (AccessType type : AccessType.values()) {
57+
if (type.ordinal() >= accessType.ordinal()) {
58+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.DOMAIN.toString(), type));
59+
}
60+
}
5261
if (pp != null) {
5362
for (IAMPolicyPermission p : pp) {
5463
if (p.getScopeId() != null) {
@@ -65,14 +74,22 @@ public List<Long> getAuthorizedDomains(Account caller, String action) {
6574
}
6675

6776
@Override
68-
public List<Long> getAuthorizedAccounts(Account caller, String action) {
77+
public List<Long> getAuthorizedAccounts(Account caller, String action, AccessType accessType) {
6978
long accountId = caller.getAccountId();
79+
if (accessType == null) {
80+
accessType = AccessType.UseEntry; // default always show resources authorized to use
81+
}
7082
// Get the static Policies of the Caller
7183
List<IAMPolicy> policies = _iamService.listIAMPolicies(accountId);
7284
// for each policy, find granted permission with Account scope
7385
List<Long> accountIds = new ArrayList<Long>();
7486
for (IAMPolicy policy : policies) {
75-
List<IAMPolicyPermission> pp = _iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ACCOUNT.toString());
87+
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
88+
for (AccessType type : AccessType.values()) {
89+
if (type.ordinal() >= accessType.ordinal()) {
90+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ACCOUNT.toString(), type));
91+
}
92+
}
7693
if (pp != null) {
7794
for (IAMPolicyPermission p : pp) {
7895
if (p.getScopeId() != null) {
@@ -89,8 +106,11 @@ public List<Long> getAuthorizedAccounts(Account caller, String action) {
89106
}
90107

91108
@Override
92-
public List<Long> getAuthorizedResources(Account caller, String action) {
109+
public List<Long> getAuthorizedResources(Account caller, String action, AccessType accessType) {
93110
long accountId = caller.getAccountId();
111+
if (accessType == null) {
112+
accessType = AccessType.UseEntry; // default always show resources authorized to use
113+
}
94114
// Get the static Policies of the Caller
95115
List<IAMPolicy> policies = _iamService.listIAMPolicies(accountId);
96116

@@ -107,7 +127,12 @@ public List<Long> getAuthorizedResources(Account caller, String action) {
107127
// for each policy, find granted permission with Resource scope
108128
List<Long> entityIds = new ArrayList<Long>();
109129
for (IAMPolicy policy : policies) {
110-
List<IAMPolicyPermission> pp = _iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.RESOURCE.toString());
130+
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
131+
for (AccessType type : AccessType.values()) {
132+
if (type.ordinal() >= accessType.ordinal()) {
133+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.RESOURCE.toString(), type));
134+
}
135+
}
111136
if (pp != null) {
112137
for (IAMPolicyPermission p : pp) {
113138
if (p.getScopeId() != null) {
@@ -120,15 +145,23 @@ public List<Long> getAuthorizedResources(Account caller, String action) {
120145
}
121146

122147
@Override
123-
public boolean isGrantedAll(Account caller, String action) {
148+
public boolean isGrantedAll(Account caller, String action, AccessType accessType) {
124149
long accountId = caller.getAccountId();
150+
if (accessType == null) {
151+
accessType = AccessType.UseEntry; // default always show resources authorized to use
152+
}
125153
// Get the static Policies of the Caller
126154
List<IAMPolicy> policies = _iamService.listIAMPolicies(accountId);
127155
// for each policy, find granted permission with ALL scope
128156
for (IAMPolicy policy : policies) {
129-
List<IAMPolicyPermission> pp = _iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ALL.toString());
130-
if (pp != null && pp.size() > 0) {
131-
return true;
157+
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
158+
for (AccessType type : AccessType.values()) {
159+
if (type.ordinal() >= accessType.ordinal()) {
160+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ALL.toString(), type));
161+
}
162+
if (pp != null && pp.size() > 0) {
163+
return true;
164+
}
132165
}
133166
}
134167
return false;

‎services/iam/server/src/org/apache/cloudstack/iam/api/IAMService.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
2122
import org.apache.cloudstack.iam.api.IAMPolicyPermission.Permission;
2223

2324
import com.cloud.utils.Pair;
@@ -72,7 +73,7 @@ IAMPolicy removeIAMPermissionFromIAMPolicy(long iamPolicyId, String entityType,
7273

7374
List<IAMPolicyPermission> listPolicyPermissions(long policyId);
7475

75-
List<IAMPolicyPermission> listPolicyPermissionsByScope(long policyId, String action, String scope);
76+
List<IAMPolicyPermission> listPolicyPermissionsByScope(long policyId, String action, String scope, AccessType accessType);
7677

7778
List<IAMPolicyPermission> listPolicyPermissionByActionAndEntity(long policyId, String action, String entityType);
7879

‎services/iam/server/src/org/apache/cloudstack/iam/server/IAMServiceImpl.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.log4j.Logger;
2626

2727
import org.apache.cloudstack.acl.PermissionScope;
28+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
2829
import org.apache.cloudstack.iam.api.IAMGroup;
2930
import org.apache.cloudstack.iam.api.IAMPolicy;
3031
import org.apache.cloudstack.iam.api.IAMPolicyPermission;
@@ -742,8 +743,8 @@ public List<Long> getGrantedEntities(long accountId, String action, String scope
742743
// for each policy, find granted permission within the given scope
743744
List<Long> entityIds = new ArrayList<Long>();
744745
for (IAMPolicy policy : policies) {
745-
List<IAMPolicyPermissionVO> pp = _policyPermissionDao.listGrantedByActionAndScope(policy.getId(), action,
746-
scope);
746+
List<IAMPolicyPermissionVO> pp = _policyPermissionDao.listByPolicyActionAndScope(policy.getId(), action,
747+
scope, null);
747748
if (pp != null) {
748749
for (IAMPolicyPermissionVO p : pp) {
749750
if (p.getScopeId() != null) {
@@ -765,9 +766,9 @@ public List<IAMPolicyPermission> listPolicyPermissions(long policyId) {
765766

766767
@SuppressWarnings("unchecked")
767768
@Override
768-
public List<IAMPolicyPermission> listPolicyPermissionsByScope(long policyId, String action, String scope) {
769+
public List<IAMPolicyPermission> listPolicyPermissionsByScope(long policyId, String action, String scope, AccessType accessType) {
769770
@SuppressWarnings("rawtypes")
770-
List pp = _policyPermissionDao.listGrantedByActionAndScope(policyId, action, scope);
771+
List pp = _policyPermissionDao.listByPolicyActionAndScope(policyId, action, scope, accessType.toString());
771772
return pp;
772773
}
773774

‎services/iam/server/src/org/apache/cloudstack/iam/server/dao/IAMPolicyPermissionDao.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface IAMPolicyPermissionDao extends GenericDao<IAMPolicyPermissionVO
2929
IAMPolicyPermissionVO findByPolicyAndEntity(long policyId, String entityType, String scope, Long scopeId,
3030
String action, Permission perm);
3131

32-
List<IAMPolicyPermissionVO> listGrantedByActionAndScope(long policyId, String action, String scope);
32+
List<IAMPolicyPermissionVO> listByPolicyActionAndScope(long policyId, String action, String scope, String accessType);
3333

3434
List<IAMPolicyPermissionVO> listByPolicyActionAndEntity(long policyId, String action, String entityType);
3535

‎services/iam/server/src/org/apache/cloudstack/iam/server/dao/IAMPolicyPermissionDaoImpl.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class IAMPolicyPermissionDaoImpl extends GenericDaoBase<IAMPolicyPermissi
3333

3434
private SearchBuilder<IAMPolicyPermissionVO> policyIdSearch;
3535
private SearchBuilder<IAMPolicyPermissionVO> fullSearch;
36-
private SearchBuilder<IAMPolicyPermissionVO> actionScopeSearch;
3736
private SearchBuilder<IAMPolicyPermissionVO> entitySearch;
3837

3938
@Override
@@ -54,13 +53,6 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
5453
fullSearch.and("accessType", fullSearch.entity().getAccessType(), SearchCriteria.Op.EQ);
5554
fullSearch.done();
5655

57-
actionScopeSearch = createSearchBuilder();
58-
actionScopeSearch.and("policyId", actionScopeSearch.entity().getAclPolicyId(), SearchCriteria.Op.EQ);
59-
actionScopeSearch.and("scope", actionScopeSearch.entity().getScope(), SearchCriteria.Op.EQ);
60-
actionScopeSearch.and("action", actionScopeSearch.entity().getAction(), SearchCriteria.Op.EQ);
61-
actionScopeSearch.and("permission", actionScopeSearch.entity().getPermission(), SearchCriteria.Op.EQ);
62-
actionScopeSearch.done();
63-
6456
entitySearch = createSearchBuilder();
6557
entitySearch.and("entityType", entitySearch.entity().getEntityType(), SearchCriteria.Op.EQ);
6658
entitySearch.and("scopeId", entitySearch.entity().getScopeId(), SearchCriteria.Op.EQ);
@@ -90,12 +82,16 @@ public IAMPolicyPermissionVO findByPolicyAndEntity(long policyId, String entityT
9082
}
9183

9284
@Override
93-
public List<IAMPolicyPermissionVO> listGrantedByActionAndScope(long policyId, String action, String scope) {
94-
SearchCriteria<IAMPolicyPermissionVO> sc = actionScopeSearch.create();
85+
public List<IAMPolicyPermissionVO> listByPolicyActionAndScope(long policyId, String action, String scope, String accessType) {
86+
SearchCriteria<IAMPolicyPermissionVO> sc = fullSearch.create();
9587
sc.setParameters("policyId", policyId);
9688
sc.setParameters("action", action);
9789
sc.setParameters("scope", scope);
9890
sc.setParameters("permission", Permission.Allow);
91+
if ( accessType != null ){
92+
// accessType can be optional, used mainly in list apis with ListEntry and UseEntry distinction
93+
sc.setParameters("accessType", accessType);
94+
}
9995
return listBy(sc);
10096
}
10197

@@ -120,7 +116,7 @@ public List<IAMPolicyPermissionVO> listByPolicyAccessAndEntity(long policyId, St
120116

121117
@Override
122118
public List<IAMPolicyPermissionVO> listByEntity(String entityType, Long entityId) {
123-
SearchCriteria<IAMPolicyPermissionVO> sc = fullSearch.create();
119+
SearchCriteria<IAMPolicyPermissionVO> sc = entitySearch.create();
124120
sc.setParameters("entityType", entityType);
125121
sc.setParameters("scopeId", entityId);
126122
return listBy(sc);

0 commit comments

Comments
 (0)