More logic to Role based checker

Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/0b1aaf51
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/0b1aaf51
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/0b1aaf51

Branch: refs/heads/rbac
Commit: 0b1aaf514f7957a252ce71bcfbe5c7e3cecd5625
Parents: 7ebb972
Author: Prachi Damle <[email protected]>
Authored: Tue Oct 8 15:36:23 2013 -0700
Committer: Prachi Damle <[email protected]>
Committed: Thu Oct 10 00:43:56 2013 -0700

----------------------------------------------------------------------
 api/src/org/apache/cloudstack/acl/AclRole.java  |  4 ++
 .../org/apache/cloudstack/acl/AclService.java   |  3 ++
 .../org/apache/cloudstack/acl/AclRoleVO.java    | 14 +++++++
 .../entity/RoleBasedEntityAccessChecker.java    | 41 ++++++++++++++++----
 .../apache/cloudstack/acl/AclServiceImpl.java   | 18 +++++++++
 setup/db/db/schema-420to430.sql                 | 15 +++----
 6 files changed, 81 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0b1aaf51/api/src/org/apache/cloudstack/acl/AclRole.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/acl/AclRole.java 
b/api/src/org/apache/cloudstack/acl/AclRole.java
index 3324879..207aece 100644
--- a/api/src/org/apache/cloudstack/acl/AclRole.java
+++ b/api/src/org/apache/cloudstack/acl/AclRole.java
@@ -28,4 +28,8 @@ public interface AclRole extends PartOf, InternalIdentity, 
Identity {
     String getDescription();
 
     // Long getParentRoleId();
+
+    public enum RoleType {
+        Static, Dynamic
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0b1aaf51/api/src/org/apache/cloudstack/acl/AclService.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/acl/AclService.java 
b/api/src/org/apache/cloudstack/acl/AclService.java
index c7a1174..749fc15 100644
--- a/api/src/org/apache/cloudstack/acl/AclService.java
+++ b/api/src/org/apache/cloudstack/acl/AclService.java
@@ -21,6 +21,7 @@ import java.util.List;
 import org.apache.cloudstack.acl.SecurityChecker.AccessType;
 
 import com.cloud.utils.Pair;
+import com.cloud.user.Account;
 
 public interface AclService {
 
@@ -86,4 +87,6 @@ public interface AclService {
 
     boolean isAPIAccessibleForRoles(String apiName, List<AclRole> roles);
 
+    List<AclRole> getEffectiveRoles(Account caller, ControlledEntity entity);
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0b1aaf51/engine/schema/src/org/apache/cloudstack/acl/AclRoleVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/org/apache/cloudstack/acl/AclRoleVO.java 
b/engine/schema/src/org/apache/cloudstack/acl/AclRoleVO.java
index 767fdfe..30ba472 100644
--- a/engine/schema/src/org/apache/cloudstack/acl/AclRoleVO.java
+++ b/engine/schema/src/org/apache/cloudstack/acl/AclRoleVO.java
@@ -21,6 +21,8 @@ import java.util.UUID;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
@@ -55,6 +57,10 @@ public class AclRoleVO implements AclRole {
     @Column(name = GenericDao.CREATED_COLUMN)
     private Date created;
 
+    @Column(name = "role_type")
+    @Enumerated(value = EnumType.STRING)
+    private AclRole.RoleType roleType;
+
     public AclRoleVO() {
        uuid = UUID.randomUUID().toString();
     }
@@ -63,6 +69,7 @@ public class AclRoleVO implements AclRole {
         this.name = name;
         this.description = description;
        uuid = UUID.randomUUID().toString();
+        this.roleType = AclRole.RoleType.Static;
     }
 
     @Override
@@ -107,4 +114,11 @@ public class AclRoleVO implements AclRole {
         this.domainId = domainId;
     }
 
+    public RoleType getRoleType() {
+        return roleType;
+    }
+
+    public void setRoleType(RoleType roleType) {
+        this.roleType = roleType;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0b1aaf51/plugins/acl/role-based-access-checkers/src/org/apache/cloudstack/acl/entity/RoleBasedEntityAccessChecker.java
----------------------------------------------------------------------
diff --git 
a/plugins/acl/role-based-access-checkers/src/org/apache/cloudstack/acl/entity/RoleBasedEntityAccessChecker.java
 
b/plugins/acl/role-based-access-checkers/src/org/apache/cloudstack/acl/entity/RoleBasedEntityAccessChecker.java
index 21ced06..5be8836 100644
--- 
a/plugins/acl/role-based-access-checkers/src/org/apache/cloudstack/acl/entity/RoleBasedEntityAccessChecker.java
+++ 
b/plugins/acl/role-based-access-checkers/src/org/apache/cloudstack/acl/entity/RoleBasedEntityAccessChecker.java
@@ -21,8 +21,10 @@ import java.util.List;
 import javax.inject.Inject;
 
 import org.apache.cloudstack.acl.AclEntityPermissionVO;
+import org.apache.cloudstack.acl.AclEntityType;
 import org.apache.cloudstack.acl.AclGroupAccountMapVO;
 import org.apache.cloudstack.acl.AclRole;
+import org.apache.cloudstack.acl.AclRolePermissionVO;
 import org.apache.cloudstack.acl.AclService;
 import org.apache.cloudstack.acl.ControlledEntity;
 import org.apache.cloudstack.acl.SecurityChecker;
@@ -30,6 +32,7 @@ import org.apache.cloudstack.acl.SecurityChecker.AccessType;
 import org.apache.cloudstack.acl.dao.AclEntityPermissionDao;
 import org.apache.cloudstack.acl.dao.AclGroupAccountMapDao;
 import org.apache.cloudstack.acl.dao.AclGroupDao;
+import org.apache.cloudstack.acl.dao.AclRolePermissionDao;
 import org.apache.cloudstack.api.InternalIdentity;
 import org.apache.log4j.Logger;
 
@@ -55,11 +58,14 @@ public class RoleBasedEntityAccessChecker extends 
DomainChecker implements Secur
     @Inject
     AclEntityPermissionDao _entityPermissionDao;
 
+    @Inject
+    AclRolePermissionDao _rolePermissionDao;
+
     @Override
     public boolean checkAccess(Account caller, ControlledEntity entity, 
AccessType accessType)
             throws PermissionDeniedException {
 
-        String entityType = "";
+        String entityType = AclEntityType.VM.toString();
 
         // check if explicit allow/deny is present for this entity in
         // acl_entity_permission
@@ -96,14 +102,35 @@ public class RoleBasedEntityAccessChecker extends 
DomainChecker implements Secur
         if (caller.getId() == entity.getAccountId()) {
             return true;
         }
-        // Get the Roles of the Caller
-        List<AclRole> roles = _aclService.getAclRoles(caller.getId());
 
-        // Do you have DomainAdmin Role? If yes can access the entity in the
-        // domaintree
-
-        // check the entity grant table
+        // get all Roles of this caller w.r.t the entity
+        List<AclRole> roles = _aclService.getEffectiveRoles(caller, entity);
+
+        for (AclRole role : roles) {
+            AclRolePermissionVO permission = 
_rolePermissionDao.findByRoleAndEntity(role.getId(), entityType,
+                    accessType);
+            boolean operationAllowedForAll = true;
+
+            if (permission.getEntityType().equals(entityType)) {
+                if (permission.isAllowed()) {
+                    return true;
+                } else {
+                    if (s_logger.isDebugEnabled()) {
+                        s_logger.debug("Account " + caller + " does not have 
permission to access resource " + entity
+                                + " for access type: " + accessType);
+                    }
+                    throw new PermissionDeniedException(caller + " does not 
have permission to access resource "
+                            + entity);
+                }
+            } else if (permission.getEntityType().equals("*")) {
+                if (permission.isAllowed()) {
+                    operationAllowedForAll = true;
+                } else {
+                    operationAllowedForAll = false;
+                }
+            }
 
+        }
 
 
         return false;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0b1aaf51/server/src/org/apache/cloudstack/acl/AclServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/acl/AclServiceImpl.java 
b/server/src/org/apache/cloudstack/acl/AclServiceImpl.java
index 85d70ac..f879d2b 100644
--- a/server/src/org/apache/cloudstack/acl/AclServiceImpl.java
+++ b/server/src/org/apache/cloudstack/acl/AclServiceImpl.java
@@ -38,6 +38,7 @@ import org.apache.cloudstack.acl.dao.AclRolePermissionDao;
 import org.apache.cloudstack.api.Identity;
 import org.apache.cloudstack.context.CallContext;
 
+import com.cloud.domain.Domain;
 import com.cloud.event.ActionEvent;
 import com.cloud.event.EventTypes;
 import com.cloud.exception.InvalidParameterValueException;
@@ -534,6 +535,7 @@ public class AclServiceImpl extends ManagerBase implements 
AclService, Manager {
     @Override
     public List<AclRole> getAclRoles(long accountId) {
 
+        // static roles of the account
         SearchBuilder<AclGroupAccountMapVO> groupSB = 
_aclGroupAccountMapDao.createSearchBuilder();
         groupSB.and("account", groupSB.entity().getAccountId(), Op.EQ);
 
@@ -643,4 +645,20 @@ public class AclServiceImpl extends ManagerBase implements 
AclService, Manager {
         return accessible;
     }
 
+    @Override
+    public List<AclRole> getEffectiveRoles(Account caller, ControlledEntity 
entity) {
+
+        // Get the static Roles of the Caller
+        List<AclRole> roles = getAclRoles(caller.getId());
+
+        // add any dynamic roles w.r.t the entity
+        if (caller.getId() == entity.getAccountId()) {
+            // The caller owns the entity
+            AclRole owner = _aclRoleDao.findByName(Domain.ROOT_DOMAIN, 
"RESOURCE_OWNER");
+            roles.add(owner);
+        }
+
+        return roles;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0b1aaf51/setup/db/db/schema-420to430.sql
----------------------------------------------------------------------
diff --git a/setup/db/db/schema-420to430.sql b/setup/db/db/schema-420to430.sql
index 259c936..eb4dfff 100644
--- a/setup/db/db/schema-420to430.sql
+++ b/setup/db/db/schema-420to430.sql
@@ -312,9 +312,10 @@ CREATE TABLE `cloud`.`acl_role` (
   `name` varchar(255) NOT NULL,
   `description` varchar(255) default NULL,  
   `uuid` varchar(40),
-  `domain_id` bigint unsigned NOT NULL,  
+  `domain_id` bigint unsigned NOT NULL,
   `removed` datetime COMMENT 'date the role was removed',
   `created` datetime COMMENT 'date the role was created',
+  `role_type` varchar(64) DEFAULT 'Static' COMMENT 'Static or Dynamic',
   PRIMARY KEY  (`id`),
   INDEX `i_acl_role__removed`(`removed`),
   CONSTRAINT `uc_acl_role__uuid` UNIQUE (`uuid`)  
@@ -332,12 +333,12 @@ CREATE TABLE `cloud`.`acl_group_role_map` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;        
 
 
-INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (1, 'NORMAL', 'Domain user role', UUID(), 1, Now());
-INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (2, 'ADMIN', 'Root admin role', UUID(), 1, Now());
-INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (3, 'DOMAIN_ADMIN', 'Domain admin role', UUID(), 1, Now());
-INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (4, 'RESOURCE_DOMAIN_ADMIN', 'Resource domain admin role', 
UUID(), 1, Now());
-INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (5, 'READ_ONLY_ADMIN', 'Read only admin role', UUID(), 1, 
Now());
-INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (6, 'RESOURCE_OWNER', 'Resource owner role', UUID(), -1, Now());
+INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (1, 'NORMAL', 'Domain user role', UUID(), 1, Now(), 'Static');
+INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (2, 'ADMIN', 'Root admin role', UUID(), 1, Now(), 'Static');
+INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (3, 'DOMAIN_ADMIN', 'Domain admin role', UUID(), 1, Now(), 
'Static');
+INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (4, 'RESOURCE_DOMAIN_ADMIN', 'Resource domain admin role', 
UUID(), 1, Now(), 'Static');
+INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (5, 'READ_ONLY_ADMIN', 'Read only admin role', UUID(), 1, 
Now(), 'Static');
+INSERT IGNORE INTO `cloud`.`acl_role` (id, name, description, uuid, domain_id, 
created) VALUES (6, 'RESOURCE_OWNER', 'Resource owner role', UUID(), 1, Now(), 
'Dynamic');
 
 INSERT IGNORE INTO `cloud`.`acl_group` (id, name, description, uuid, 
domain_id, created) VALUES (1, 'NORMAL', 'Domain user group', UUID(), 1, Now());
 INSERT IGNORE INTO `cloud`.`acl_group` (id, name, description, uuid, 
domain_id, created) VALUES (2, 'ADMIN', 'Root admin group', UUID(), 1, Now());

Reply via email to