Skip to content

Commit e14c2ec

Browse files
author
Prachi Damle
committed
CLOUDSTACK-6517: IAM - Admin is allowed to create PortFowarding rule for a regular user, when admin does not have " UseEntry" permission for IpAddress.
Changes: - IAM was applying ordering on accessTypes. Thus if an account had Operate, he got USe access as well. So even if IAM schema did not have 'UseEntry" permission for IpAddress, some other 'OperateEntry' permission on IpAddress was letting this operation go through. - Fixed IAM to NOT do ordering of access types anymore. IAm will perform strict accessType check only. - This fix is needed so that admin does not get permission to USE resources from other account just becase he has OPERATE access on those resources due to some other APIs. - However due to this fix, we break backwards compatibilty with CS 4.3. - CS 4.3 allowed root admin to do the createPF operation for a user by passing in networkId of the user. - Same was the case for domain admins within their domains - Why this worked was due to CS 4.3 simply returning true for root admin/domain admin - So to maintain backwards compatibilty, we are adding the logic to return "true" for root admin and domain admin just like CS 4.3. - Exception is: For Network, AffinityGroup and Templates, we still call IAM even for root admin/domain admin, since thats what CS 4.3 did. Just for these 3 resource_types, it used to perform access checks even for root admin/domain admin.
1 parent 42b803d commit e14c2ec

9 files changed

Lines changed: 121 additions & 56 deletions

File tree

‎server/src/com/cloud/configuration/ConfigurationManagerImpl.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.log4j.Logger;
4040

4141
import org.apache.cloudstack.acl.SecurityChecker;
42+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4243
import org.apache.cloudstack.affinity.AffinityGroup;
4344
import org.apache.cloudstack.affinity.AffinityGroupService;
4445
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
@@ -4326,7 +4327,7 @@ public List<? extends NetworkOffering> searchForNetworkOfferings(ListNetworkOffe
43264327
throw new InvalidParameterValueException("Can't update system networks");
43274328
}
43284329

4329-
_accountMgr.checkAccess(caller, null, network);
4330+
_accountMgr.checkAccess(caller, AccessType.ListEntry, network);
43304331

43314332
List<Long> offeringIds = _networkModel.listNetworkOfferingsForUpgrade(networkId);
43324333

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

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.apache.cloudstack.acl.RoleType;
4747
import org.apache.cloudstack.acl.SecurityChecker;
4848
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
49+
import org.apache.cloudstack.affinity.AffinityGroup;
4950
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
5051
import org.apache.cloudstack.api.InternalIdentity;
5152
import org.apache.cloudstack.api.command.admin.account.UpdateAccountCmd;
@@ -89,6 +90,7 @@
8990
import com.cloud.exception.ResourceUnavailableException;
9091
import com.cloud.network.IpAddress;
9192
import com.cloud.network.IpAddressManager;
93+
import com.cloud.network.Network;
9294
import com.cloud.network.VpnUserVO;
9395
import com.cloud.network.as.AutoScaleManager;
9496
import com.cloud.network.dao.AccountGuestVlanMapDao;
@@ -123,6 +125,7 @@
123125
import com.cloud.storage.dao.VolumeDao;
124126
import com.cloud.storage.snapshot.SnapshotManager;
125127
import com.cloud.template.TemplateManager;
128+
import com.cloud.template.VirtualMachineTemplate;
126129
import com.cloud.user.Account.State;
127130
import com.cloud.user.dao.AccountDao;
128131
import com.cloud.user.dao.UserAccountDao;
@@ -484,13 +487,67 @@ public void checkAccess(Account caller, AccessType accessType, String apiName, C
484487
}
485488
entityBuf.append("}");
486489
String entityStr = entityBuf.toString();
487-
for (SecurityChecker checker : _securityCheckers) {
488-
if (checker.checkAccess(caller, accessType, apiName, entities)) {
489-
if (s_logger.isDebugEnabled()) {
490-
s_logger.debug("Access to " + entityStr + " granted to " + caller + " by " + checker.getName());
490+
491+
boolean isRootAdmin = isRootAdmin(caller.getAccountId());
492+
boolean isDomainAdmin = isDomainAdmin(caller.getAccountId());
493+
boolean isResourceDomainAdmin = isResourceDomainAdmin(caller.getAccountId());
494+
495+
if ((isRootAdmin || isDomainAdmin || isResourceDomainAdmin || caller.getId() == Account.ACCOUNT_ID_SYSTEM)
496+
&& (accessType == null || accessType == AccessType.UseEntry)) {
497+
498+
for (ControlledEntity entity : entities) {
499+
if (entity instanceof VirtualMachineTemplate || entity instanceof Network
500+
|| entity instanceof AffinityGroup) {
501+
for (SecurityChecker checker : _securityCheckers) {
502+
if (checker.checkAccess(caller, accessType, apiName, entity)) {
503+
if (s_logger.isDebugEnabled()) {
504+
s_logger.debug("Access to " + entityStr + " granted to " + caller + " by "
505+
+ checker.getName());
506+
}
507+
granted = true;
508+
break;
509+
}
510+
}
511+
} else {
512+
if (isRootAdmin || caller.getId() == Account.ACCOUNT_ID_SYSTEM) {
513+
// no need to make permission checks if the system/root
514+
// admin makes the call
515+
if (s_logger.isTraceEnabled()) {
516+
s_logger.trace("No need to make permission check for System/RootAdmin account, returning true");
517+
}
518+
granted = true;
519+
} else if (isDomainAdmin || isResourceDomainAdmin) {
520+
Domain entityDomain = getEntityDomain(entity);
521+
if (entityDomain != null) {
522+
try {
523+
checkAccess(caller, entityDomain);
524+
granted = true;
525+
} catch (PermissionDeniedException e) {
526+
List<ControlledEntity> entityList = new ArrayList<ControlledEntity>();
527+
entityList.add(entity);
528+
e.addDetails(caller, entityList);
529+
throw e;
530+
}
531+
}
532+
}
533+
}
534+
535+
if (!granted) {
536+
assert false : "How can all of the security checkers pass on checking this check: " + entityStr;
537+
throw new PermissionDeniedException("There's no way to confirm " + caller + " has access to "
538+
+ entityStr);
539+
}
540+
541+
}
542+
} else {
543+
for (SecurityChecker checker : _securityCheckers) {
544+
if (checker.checkAccess(caller, accessType, apiName, entities)) {
545+
if (s_logger.isDebugEnabled()) {
546+
s_logger.debug("Access to " + entityStr + " granted to " + caller + " by " + checker.getName());
547+
}
548+
granted = true;
549+
break;
491550
}
492-
granted = true;
493-
break;
494551
}
495552
}
496553

@@ -500,6 +557,27 @@ public void checkAccess(Account caller, AccessType accessType, String apiName, C
500557
}
501558
}
502559

560+
private Domain getEntityDomain(ControlledEntity entity) {
561+
Domain entityDomain = null;
562+
long domainId = entity.getDomainId();
563+
564+
if (domainId != -1) {
565+
entityDomain = _domainMgr.getDomain(domainId);
566+
} else {
567+
if (entity.getAccountId() != -1) {
568+
// If account exists domainId should too so
569+
// calculate
570+
// it. This condition might be hit for templates or
571+
// entities which miss domainId in their tables
572+
Account account = getAccount(entity.getAccountId());
573+
domainId = account != null ? account.getDomainId() : -1;
574+
entityDomain = _domainMgr.getDomain(domainId);
575+
}
576+
}
577+
578+
return entityDomain;
579+
}
580+
503581
@Override
504582
public Long checkAccessAndSpecifyAuthority(Account caller, Long zoneId) {
505583
// We just care for resource domain admin for now. He should be permitted to see only his zone.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,8 @@ private void addDefaultAclPolicyPermission(String apiName, Class<?> cmdClass, Ro
247247
try {
248248
cmdObj = (BaseCmd) cmdClass.newInstance();
249249
if (cmdObj instanceof BaseListCmd) {
250-
if (permissionScope == PermissionScope.ACCOUNT) {
251-
accessType = AccessType.UseEntry;
252-
} else {
253-
accessType = AccessType.ListEntry;
254-
addAccountScopedUseEntry = true;
255-
}
250+
accessType = AccessType.ListEntry;
251+
addAccountScopedUseEntry = true;
256252
} else {
257253
accessType = AccessType.OperateEntry;
258254
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,14 @@ public boolean checkAccess(Account caller, ControlledEntity entity, AccessType a
107107
permissions = _iamSrv.listPolicyPermissionByActionAndEntity(policy.getId(), action, entityType);
108108
if (permissions.isEmpty()) {
109109
if (accessType != null) {
110-
for (AccessType type : AccessType.values()) {
111-
if (type.ordinal() >= accessType.ordinal()) {
112-
permissions.addAll(_iamSrv.listPolicyPermissionByAccessAndEntity(policy.getId(),
113-
type.toString(), entityType));
114-
}
115-
}
110+
permissions.addAll(_iamSrv.listPolicyPermissionByAccessAndEntity(policy.getId(),
111+
accessType.toString(), entityType));
116112
}
117113
}
118114
} else {
119115
if (accessType != null) {
120-
for (AccessType type : AccessType.values()) {
121-
if (type.ordinal() >= accessType.ordinal()) {
122-
permissions.addAll(_iamSrv.listPolicyPermissionByAccessAndEntity(policy.getId(),
123-
type.toString(), entityType));
124-
}
125-
}
116+
permissions.addAll(_iamSrv.listPolicyPermissionByAccessAndEntity(policy.getId(),
117+
accessType.toString(), entityType));
126118
}
127119
}
128120
for (IAMPolicyPermission permission : permissions) {

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ public List<Long> getAuthorizedDomains(Account caller, String action, AccessType
5757
List<Long> domainIds = new ArrayList<Long>();
5858
for (IAMPolicy policy : policies) {
5959
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
60-
for (AccessType type : AccessType.values()) {
61-
if (type.ordinal() >= accessType.ordinal()) {
62-
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.DOMAIN.toString(), type));
63-
}
64-
}
60+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action,
61+
PermissionScope.DOMAIN.toString(), accessType.toString()));
62+
6563
if (pp != null) {
6664
for (IAMPolicyPermission p : pp) {
6765
if (p.getScopeId() != null) {
@@ -101,11 +99,9 @@ public List<Long> getAuthorizedAccounts(Account caller, String action, AccessTyp
10199
List<Long> accountIds = new ArrayList<Long>();
102100
for (IAMPolicy policy : policies) {
103101
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
104-
for (AccessType type : AccessType.values()) {
105-
if (type.ordinal() >= accessType.ordinal()) {
106-
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ACCOUNT.toString(), type));
107-
}
108-
}
102+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action,
103+
PermissionScope.ACCOUNT.toString(), accessType.toString()));
104+
109105
if (pp != null) {
110106
for (IAMPolicyPermission p : pp) {
111107
if (p.getScopeId() != null) {
@@ -144,11 +140,9 @@ public List<Long> getAuthorizedResources(Account caller, String action, AccessTy
144140
List<Long> entityIds = new ArrayList<Long>();
145141
for (IAMPolicy policy : policies) {
146142
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
147-
for (AccessType type : AccessType.values()) {
148-
if (type.ordinal() >= accessType.ordinal()) {
149-
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.RESOURCE.toString(), type));
150-
}
151-
}
143+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action,
144+
PermissionScope.RESOURCE.toString(), accessType.toString()));
145+
152146
if (pp != null) {
153147
for (IAMPolicyPermission p : pp) {
154148
if (p.getScopeId() != null) {
@@ -171,13 +165,10 @@ public boolean isGrantedAll(Account caller, String action, AccessType accessType
171165
// for each policy, find granted permission with ALL scope
172166
for (IAMPolicy policy : policies) {
173167
List<IAMPolicyPermission> pp = new ArrayList<IAMPolicyPermission>();
174-
for (AccessType type : AccessType.values()) {
175-
if (type.ordinal() >= accessType.ordinal()) {
176-
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ALL.toString(), type));
177-
}
178-
if (pp != null && pp.size() > 0) {
179-
return true;
180-
}
168+
pp.addAll(_iamService.listPolicyPermissionsByScope(policy.getId(), action, PermissionScope.ALL.toString(),
169+
accessType.toString()));
170+
if (pp != null && pp.size() > 0) {
171+
return true;
181172
}
182173
}
183174
return false;

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

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

1919
import java.util.List;
2020

21-
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
21+
2222
import org.apache.cloudstack.iam.api.IAMPolicyPermission.Permission;
2323

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

7474
List<IAMPolicyPermission> listPolicyPermissions(long policyId);
7575

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

7878
List<IAMPolicyPermission> listPolicyPermissionByActionAndEntity(long policyId, String action, String entityType);
7979

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

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

2727
import org.apache.cloudstack.acl.PermissionScope;
28-
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
2928
import org.apache.cloudstack.iam.api.IAMGroup;
3029
import org.apache.cloudstack.iam.api.IAMPolicy;
3130
import org.apache.cloudstack.iam.api.IAMPolicyPermission;
@@ -632,7 +631,8 @@ public IAMPolicy addIAMPermissionToIAMPolicy(long iamPolicyId, String entityType
632631
}
633632

634633
// add entry in acl_policy_permission table
635-
IAMPolicyPermissionVO permit = _policyPermissionDao.findByPolicyAndEntity(iamPolicyId, entityType, scope, scopeId, action, perm);
634+
IAMPolicyPermissionVO permit = _policyPermissionDao.findByPolicyAndEntity(iamPolicyId, entityType, scope,
635+
scopeId, action, perm, accessType);
636636
if (permit == null) {
637637
// not there already
638638
permit = new IAMPolicyPermissionVO(iamPolicyId, action, entityType, accessType, scope, scopeId, perm,
@@ -654,7 +654,8 @@ public IAMPolicy removeIAMPermissionFromIAMPolicy(long iamPolicyId, String entit
654654
+ "; failed to revoke permission from policy.");
655655
}
656656
// remove entry from acl_entity_permission table
657-
IAMPolicyPermissionVO permit = _policyPermissionDao.findByPolicyAndEntity(iamPolicyId, entityType, scope, scopeId, action, Permission.Allow);
657+
IAMPolicyPermissionVO permit = _policyPermissionDao.findByPolicyAndEntity(iamPolicyId, entityType, scope,
658+
scopeId, action, Permission.Allow, null);
658659
if (permit != null) {
659660
// not removed yet
660661
_policyPermissionDao.remove(permit.getId());
@@ -766,9 +767,10 @@ public List<IAMPolicyPermission> listPolicyPermissions(long policyId) {
766767

767768
@SuppressWarnings("unchecked")
768769
@Override
769-
public List<IAMPolicyPermission> listPolicyPermissionsByScope(long policyId, String action, String scope, AccessType accessType) {
770+
public List<IAMPolicyPermission> listPolicyPermissionsByScope(long policyId, String action, String scope,
771+
String accessType) {
770772
@SuppressWarnings("rawtypes")
771-
List pp = _policyPermissionDao.listByPolicyActionAndScope(policyId, action, scope, accessType.toString());
773+
List pp = _policyPermissionDao.listByPolicyActionAndScope(policyId, action, scope, accessType);
772774
return pp;
773775
}
774776

‎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
@@ -27,7 +27,7 @@ public interface IAMPolicyPermissionDao extends GenericDao<IAMPolicyPermissionVO
2727
List<IAMPolicyPermissionVO> listByPolicy(long policyId);
2828

2929
IAMPolicyPermissionVO findByPolicyAndEntity(long policyId, String entityType, String scope, Long scopeId,
30-
String action, Permission perm);
30+
String action, Permission perm, String accessType);
3131

3232
List<IAMPolicyPermissionVO> listByPolicyActionAndScope(long policyId, String action, String scope, String accessType);
3333

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,19 @@ public List<IAMPolicyPermissionVO> listByPolicy(long policyId) {
7070

7171
@Override
7272
public IAMPolicyPermissionVO findByPolicyAndEntity(long policyId, String entityType, String scope, Long scopeId,
73-
String action, Permission perm) {
73+
String action, Permission perm, String accessType) {
7474
SearchCriteria<IAMPolicyPermissionVO> sc = fullSearch.create();
7575
sc.setParameters("policyId", policyId);
7676
sc.setParameters("entityType", entityType);
7777
sc.setParameters("scope", scope);
7878
sc.setParameters("scopeId", scopeId);
7979
sc.setParameters("action", action);
8080
sc.setParameters("permission", perm);
81+
if (accessType != null) {
82+
// accessType can be optional, used mainly in list apis with
83+
// ListEntry and UseEntry distinction
84+
sc.setParameters("accessType", accessType);
85+
}
8186
return findOneBy(sc);
8287
}
8388

0 commit comments

Comments
 (0)