]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-3158: All new accounts created by the services need to have the "ROLE_SPRING_A...
authorRichard Millet <richard.millet@berkeley.edu>
Tue, 9 Nov 2010 03:09:39 +0000 (03:09 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Tue, 9 Nov 2010 03:09:39 +0000 (03:09 +0000)
14 files changed:
services/account/service/src/main/java/org/collectionspace/services/account/AccountRoleSubResource.java
services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/client/test/RoleServiceTest.java
services/authorization-mgt/import/src/main/java/org/collectionspace/services/authorization/driver/AuthorizationSeedDriver.java
services/authorization-mgt/import/src/main/java/org/collectionspace/services/authorization/importer/AuthorizationGen.java
services/authorization-mgt/import/src/main/resources/applicationContext-authorization-test.xml
services/authorization-mgt/import/src/main/resources/hibernate.cfg.xml
services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionDocumentHandler.java
services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/RoleDocumentHandler.java
services/authorization/jaxb/src/main/resources/permissions.xsd
services/authorization/pstore/src/main/resources/db/mysql/authorization.sql
services/authorization/pstore/src/main/resources/db/mysql/test_authorization.sql
services/authorization/service/src/main/java/org/collectionspace/services/authorization/PermissionActionUtil.java
services/authorization/service/src/main/java/org/collectionspace/services/authorization/spring/SpringPermissionEvaluator.java
services/authorization/service/src/main/resources/applicationContext-authorization.xml

index a824db8cd46b39b86056d044b65e20c5c006a510..bc776a1bdfa0b9f9aadf0a2a38c2ea592196510a 100644 (file)
@@ -33,6 +33,7 @@ import org.collectionspace.services.authorization.AccountValue;
 import org.collectionspace.services.authorization.AccountRoleRel;
 import org.collectionspace.services.authorization.Permission;
 import org.collectionspace.services.authorization.Role;
+import org.collectionspace.services.authorization.RoleValue;
 import org.collectionspace.services.authorization.SubjectType;
 
 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
@@ -56,6 +57,10 @@ public class AccountRoleSubResource
 //        extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
        extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
 
+       //FIXME: These belong in an Authorization class, not here
+    private static String ROLE_SPRING_ADMIN_ID = "-1";
+    private static String ROLE_SPRING_ADMIN_NAME = "ROLE_SPRING_ADMIN";    
+       
     final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
     final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
     //this service is never exposed as standalone RESTful service...just use unique
@@ -160,10 +165,20 @@ public class AccountRoleSubResource
      */
     public String createAccountRole(AccountRole input, SubjectType subject)
             throws Exception {
+       
+       //
+       // We need to associate every new account with the Spring Security Admin role so we can make
+       // changes to the Spring Security ACL tables.  The Spring Security Admin role has NO CollectionSpace
+       // specific permissions.  It is an internal/private role that service consumers and end-users NEVER see.
+       //
+       RoleValue springAdminRole = new RoleValue();
+       springAdminRole.setRoleId(ROLE_SPRING_ADMIN_ID);
+       springAdminRole.setRoleName(ROLE_SPRING_ADMIN_NAME);
+       List<RoleValue> roleValues = input.getRoles();
+       roleValues.add(springAdminRole);
 
         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
-        DocumentHandler handler = createDocumentHandler(ctx);
-        
+        DocumentHandler handler = createDocumentHandler(ctx);        
         String bogusCsid = getStorageClient(ctx).create(ctx, handler);
         
         return bogusCsid;
index 41dfe180097832bf3389cd86b0a84aa6c0d4d79a..2c620442e0f3c2407fd8f08f7f0acf90ee26fc78 100644 (file)
@@ -351,7 +351,8 @@ public class RoleServiceTest extends AbstractServiceTestImpl {
         Role output = (Role) res.getEntity();
         Assert.assertNotNull(output);
 
-        String roleNameToVerify = "ROLE_" + verifyRoleName.toUpperCase();
+        //FIXME: Tenant ID of "1" should not be hard coded
+        String roleNameToVerify = "ROLE_" + "1_" + verifyRoleName.toUpperCase();
         Assert.assertEquals(output.getRoleName(), roleNameToVerify,
                 "RoleName fix did not work!");
     }
index 87369550cf03c4e5deb3197de03c0ad3653514dc..9c6faab5a9155f438bf5589afb7d68f5c9e78796 100644 (file)
@@ -165,9 +165,11 @@ public class AuthorizationSeedDriver {
     }
 
     private void login() {
-        GrantedAuthority gauth = new GrantedAuthorityImpl("ROLE_ADMINISTRATOR");
+        GrantedAuthority cspace_admin = new GrantedAuthorityImpl("ROLE_ADMINISTRATOR");
+        GrantedAuthority spring_security_admin = new GrantedAuthorityImpl("ROLE_SPRING_ADMIN");
         HashSet<GrantedAuthority> gauths = new HashSet<GrantedAuthority>();
-        gauths.add(gauth);
+        gauths.add(cspace_admin);
+        gauths.add(spring_security_admin);
         Authentication authRequest = new UsernamePasswordAuthenticationToken(user, password, gauths);
         SecurityContextHolder.getContext().setAuthentication(authRequest);
         if (logger.isDebugEnabled()) {
index c10f290f3f52321d7caf4dc6f494ed506f02d15a..b7105dfaa9ca29ca30ced189d5cb94195efd80e2 100644 (file)
@@ -58,9 +58,10 @@ import org.collectionspace.services.common.security.SecurityUtils;
  */
 public class AuthorizationGen {
 
-    final public static String ROLE_ADMINISTRATOR = "ROLE_ADMINISTRATOR";
-    final public static String ROLE_TENANT_ADMINISTRATOR = "ROLE_TENANT_ADMINISTRATOR";
-    final public static String ROLE_TENANT_READER = "ROLE_TENANT_READER";
+       final public static String ROLE_PREFIX = "ROLE_";
+    final public static String ROLE_ADMINISTRATOR = "ADMINISTRATOR";
+    final public static String ROLE_TENANT_ADMINISTRATOR = "TENANT_ADMINISTRATOR";
+    final public static String ROLE_TENANT_READER = "TENANT_READER";
     final public static String ROLE_ADMINISTRATOR_ID = "0";
     //
     // ActionGroup labels/constants
@@ -262,7 +263,9 @@ public class AuthorizationGen {
     private Role buildTenantAdminRole(String tenantId) {
         Role role = new Role();
         role.setCreatedAtItem(new Date());
-        role.setRoleName(ROLE_TENANT_ADMINISTRATOR);
+        role.setRoleName(ROLE_PREFIX +
+                       tenantId + "_" +
+                       ROLE_TENANT_ADMINISTRATOR);
         String id = UUID.randomUUID().toString();
         role.setCsid(id);
         role.setDescription("generated tenant admin role");
@@ -273,7 +276,9 @@ public class AuthorizationGen {
     private Role buildTenantReaderRole(String tenantId) {
         Role role = new Role();
         role.setCreatedAtItem(new Date());
-        role.setRoleName(ROLE_TENANT_READER);
+        role.setRoleName(ROLE_PREFIX +
+                       tenantId + "_" +
+                       ROLE_TENANT_READER);
         String id = UUID.randomUUID().toString();
         role.setCsid(id);
         role.setDescription("generated tenant read only role");
@@ -289,21 +294,23 @@ public class AuthorizationGen {
     }
 
     public void associateDefaultPermissionsRoles() {
-        List<Role> roles = new ArrayList<Role>();
-        roles.add(cspaceAdminRole);
         for (Permission p : adminPermList) {
             PermissionRole permAdmRole = associatePermissionRoles(p, adminRoles);
             adminPermRoleList.add(permAdmRole);
-
-            //CSpace Administrator has all access
-            PermissionRole permCAdmRole = associatePermissionRoles(p, roles);
-            adminPermRoleList.add(permCAdmRole);
         }
 
         for (Permission p : readerPermList) {
             PermissionRole permRdrRole = associatePermissionRoles(p, readerRoles);
             readerPermRoleList.add(permRdrRole);
         }
+        
+        //CSpace Administrator has all access
+        List<Role> roles = new ArrayList<Role>();
+        roles.add(cspaceAdminRole);
+        for (Permission p : adminPermList) {
+            PermissionRole permCAdmRole = associatePermissionRoles(p, roles);
+            adminPermRoleList.add(permCAdmRole);
+        }        
     }
 
     public List<PermissionRole> associatePermissionsRoles(List<Permission> perms, List<Role> roles) {
@@ -358,7 +365,7 @@ public class AuthorizationGen {
 
     private Role buildCSpaceAdminRole() {
         Role role = new Role();
-        role.setRoleName(ROLE_ADMINISTRATOR);
+        role.setRoleName(ROLE_PREFIX + ROLE_ADMINISTRATOR);
         role.setCsid(ROLE_ADMINISTRATOR_ID);
         return role;
     }
index d313fbc051fdb092015e8b8460e166e1c2c60f57..42e5bf5020329abd5200c5459cdd5cb0a0d4dc4d 100644 (file)
         <constructor-arg>
             <list>
                 <bean class="org.springframework.security.core.authority.GrantedAuthorityImpl">
-                    <constructor-arg value="ROLE_ADMINISTRATOR"/>
+                    <constructor-arg value="ROLE_SPRING_ADMIN"/>
                 </bean>
                 <bean class="org.springframework.security.core.authority.GrantedAuthorityImpl">
-                    <constructor-arg value="ROLE_ADMINISTRATOR"/>
+                    <constructor-arg value="ROLE_SPRING_ADMIN"/>
                 </bean>
                 <bean class="org.springframework.security.core.authority.GrantedAuthorityImpl">
-                    <constructor-arg value="ROLE_ADMINISTRATOR"/>
+                    <constructor-arg value="ROLE_SPRING_ADMIN"/>
                 </bean>
             </list>
         </constructor-arg>
index 8296399cf33de770654f4940a04619ad72791eaf..cb1f64509f1e398b8354f472408686908ccd8365 100644 (file)
@@ -19,6 +19,6 @@
         <property name="dialect">@DB_DIALECT@</property>
         <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
         <property name="current_session_context_class">thread</property>
-        <property name="hibernate.show_sql">true</property>
+        <property name="hibernate.show_sql">false</property>
     </session-factory>
 </hibernate-configuration>
index 9af981151e9925bff1d3876255663e09fb57cf48..bf6977c149cb4706f63d44e790c19971d9935d7e 100644 (file)
@@ -98,6 +98,7 @@ public class PermissionDocumentHandler
                    URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),
                            perm.getResourceName(), action);
                    permAction.setObjectIdentity(uriRes.getHashedId().toString());
+                   permAction.setObjectIdentityResource(uriRes.getId());
                    //PermissionActionUtil.update(perm, permAction);
                }
        } catch (Exception x) {
index 5fd3bf257774484b3ceca8763fb173f4706a06a8..837fbf51373aaa612c31cb2a4c990ff40a96415d 100644 (file)
@@ -54,16 +54,18 @@ public class RoleDocumentHandler
     public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
         String id = UUID.randomUUID().toString();
         Role role = wrapDoc.getWrappedObject();
-        role.setRoleName(fixRoleName(role.getRoleName()));
-        role.setCsid(id);
         setTenant(role);
+        role.setRoleName(fixRoleName(role.getRoleName(),
+                       role.getTenantId()));
+        role.setCsid(id);
     }
 
     @Override
     public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
         Role roleFound = wrapDoc.getWrappedObject();
         Role roleReceived = getCommonPart();
-        roleReceived.setRoleName(fixRoleName(roleReceived.getRoleName()));
+        roleReceived.setRoleName(fixRoleName(roleReceived.getRoleName(),
+                       roleFound.getTenantId()));
         merge(roleReceived, roleFound);
     }
 
@@ -185,9 +187,9 @@ public class RoleDocumentHandler
         }
     }
 
-    private String fixRoleName(String role) {
+    private String fixRoleName(String role, String tenantId) {
         String roleName = role.toUpperCase();
-        String rolePrefix = "ROLE_";
+        String rolePrefix = "ROLE_" + tenantId + "_";
         if (!roleName.startsWith(rolePrefix)) {
             roleName = rolePrefix + roleName;
         }
index 097cea7c5c648a58360c9ba0c9a10ae5ec566966..0b88a8c0f6bbe3270f35545d31d525e0ad6755a9 100644 (file)
                     </xs:appinfo>
                 </xs:annotation>
             </xs:element>
+            <xs:element name="objectIdentityResource" type="xs:string" minOccurs="1">
+                <xs:annotation>
+                    <xs:appinfo>
+                        <hj:basic>
+                            <orm:column name="objectIdentityResource" length="128" nullable="false"/>
+                        </hj:basic>
+                    </xs:appinfo>
+                </xs:annotation>
+            </xs:element>            
         </xs:sequence>
     </xs:complexType>
 
index e878814daa0276bf7dc2a3e62d008a2c013ae9a0..cf11aae9bd5bff8fc1f95b8e9432edcfbf360c8c 100644 (file)
@@ -6,7 +6,7 @@ drop table if exists permissions_roles;
 drop table if exists roles;
 create table accounts_roles (HJID bigint not null auto_increment, account_id varchar(128) not null, created_at datetime not null, role_id varchar(128) not null, role_name varchar(255), screen_name varchar(255), user_id varchar(128) not null, primary key (HJID), unique (account_id, role_id));
 create table permissions (csid varchar(128) not null, action_group varchar(128), attribute_name varchar(128), created_at datetime not null, description varchar(255), effect varchar(32) not null, resource_name varchar(128) not null, tenant_id varchar(128) not null, updated_at datetime, primary key (csid));
-create table permissions_actions (HJID bigint not null auto_increment, name varchar(128) not null, objectIdentity varchar(128) not null, ACTIONS_PERMISSION_CSID varchar(128), primary key (HJID));
+create table permissions_actions (HJID bigint not null auto_increment, name varchar(128) not null, objectIdentity varchar(128) not null, objectIdentityResource varchar(128) not null, ACTIONS_PERMISSION_CSID varchar(128), primary key (HJID));
 create table permissions_roles (HJID bigint not null auto_increment, actionGroup varchar(255), created_at datetime not null, permission_id varchar(128) not null, permission_resource varchar(255), role_id varchar(128) not null, role_name varchar(255), primary key (HJID), unique (permission_id, role_id));
 create table roles (csid varchar(128) not null, created_at datetime not null, description varchar(255), rolegroup varchar(255), rolename varchar(200) not null, tenant_id varchar(128) not null, updated_at datetime, primary key (csid), unique (rolename, tenant_id));
 alter table permissions_actions add index FK85F82042E2DC84FD (ACTIONS_PERMISSION_CSID), add constraint FK85F82042E2DC84FD foreign key (ACTIONS_PERMISSION_CSID) references permissions (csid);
index 2c961dfc3181d3399052275cfc80c6736d439078..f896a5207d2eabaac26ce6f3ee4c6a8071da3c20 100644 (file)
@@ -5,15 +5,19 @@
 --\r
 use cspace;\r
 \r
+insert into `roles` (`csid`, `rolename`, `rolegroup`, `created_at`, `tenant_id`) values ('-1', 'ROLE_SPRING_ADMIN', 'Spring Security Administrator', now(), '0');\r
 insert into `roles` (`csid`, `rolename`, `rolegroup`, `created_at`, `tenant_id`) values ('0', 'ROLE_ADMINISTRATOR', 'CollectionSpace Administrator', now(), '0');\r
 \r
 -- for default test account --\r
+insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('eeca40d7-dc77-4cc5-b489-16a53c75525a', 'test', '-1', 'ROLE_SPRING_ADMIN', now());\r
 insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('eeca40d7-dc77-4cc5-b489-16a53c75525a', 'test', '0', 'ROLE_ADMINISTRATOR', now());\r
 \r
 -- Additional account introduced during integration on release 0.6, and currently relied upon by the Application Layer.\r
+insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test@collectionspace.org', '-1', 'ROLE_SPRING_ADMIN', now());\r
 insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test@collectionspace.org', '0', 'ROLE_ADMINISTRATOR', now());\r
 \r
 -- test account for pahma --\r
+insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('ff2b4440-ed0d-4892-adb4-b6999eba3ae7', 'test-pahma', '-1', 'ROLE_SPRING_ADMIN', now());\r
 insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('ff2b4440-ed0d-4892-adb4-b6999eba3ae7', 'test-pahma', '0', 'ROLE_ADMINISTRATOR', now());\r
 \r
 -- todo: barney is created in security test but accountrole is not yet created there, so add fake account id\r
index 565a3754a65d8cb5f693af50cc63dfcfb7717dc2..d6b80042166646b271a5d38daaa72fe4fe0ed84c 100644 (file)
@@ -15,6 +15,7 @@ public class PermissionActionUtil {
                    perm.getResourceName(), action);\r
            pa.setName(actionType);\r
            pa.setObjectIdentity(uriRes.getHashedId().toString());\r
+           pa.setObjectIdentityResource(uriRes.getId());\r
            \r
            return pa;\r
        }\r
@@ -26,6 +27,7 @@ public class PermissionActionUtil {
            URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),\r
                    perm.getResourceName(), action);\r
            pa.setObjectIdentity(uriRes.getHashedId().toString());\r
+           pa.setObjectIdentityResource(uriRes.getId());\r
            \r
            return pa;\r
        }\r
index c13baf681735471c2192b83395b3f56ca21b0ba6..5fdd1f3c37d05fd8d0bec62e63625a5b438fd6a6 100644 (file)
@@ -23,6 +23,7 @@
  */
 package org.collectionspace.services.authorization.spring;
 
+import java.util.List;
 import java.io.Serializable;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -33,6 +34,7 @@ import org.collectionspace.services.authorization.CSpaceResource;
 import org.springframework.security.access.PermissionEvaluator;
 import org.springframework.security.acls.model.Permission;
 import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
 
 /**
@@ -55,7 +57,28 @@ public class SpringPermissionEvaluator implements CSpacePermissionEvaluator {
         Serializable objectIdId = SpringAuthorizationProvider.getObjectIdentityIdentifier(res);
         String objectIdType = SpringAuthorizationProvider.getObjectIdentityType(res);
         PermissionEvaluator eval = provider.getProviderPermissionEvaluator();
+        
+        debug(res, authToken, objectIdId, objectIdType, perm);
         return eval.hasPermission(authToken,
                 objectIdId, objectIdType, perm);
     }
+    
+    private void debug(CSpaceResource res,
+               Authentication authToken,
+               Serializable objectIdId,
+               String objectIdType,
+               Permission perm) {
+       if (log.isDebugEnabled() == true) {
+               log.debug(this.getClass().getCanonicalName() + ":" + this);
+               String resourceTarget = "[" + res.getId() + "]" + " | " +
+                               "[" + "objectIdId: " + objectIdType + "(" + objectIdId + ")]";
+               System.out.println("PERMISSION CHECK FOR: " + resourceTarget);
+               System.out.println("\tPrincipal: " + authToken.getName() +
+                               "\tTenant ID: " + res.getTenantId());
+               System.out.println("\tRoles: " + authToken.getAuthorities());
+               System.out.println("\tPermission Mask: " + perm.getMask() +
+                               " - Permission Pattern: " + perm.getPattern());
+               System.out.println("");
+       }
+    }
 }
index 75972364fa2165123f98724c44638c17d235f68d..ad3c750c24948f32c71b3f6c301bc0939556a526 100644 (file)
         <constructor-arg>
             <list>
                 <bean class="org.springframework.security.core.authority.GrantedAuthorityImpl">
-                    <constructor-arg value="ROLE_ADMINISTRATOR"/>
+                    <constructor-arg value="ROLE_SPRING_ADMIN"/>
                 </bean>
                 <bean class="org.springframework.security.core.authority.GrantedAuthorityImpl">
-                    <constructor-arg value="ROLE_ADMINISTRATOR"/>
+                    <constructor-arg value="ROLE_SPRING_ADMIN"/>
                 </bean>
                 <bean class="org.springframework.security.core.authority.GrantedAuthorityImpl">
-                    <constructor-arg value="ROLE_ADMINISTRATOR"/>
+                    <constructor-arg value="ROLE_SPRING_ADMIN"/>
                 </bean>
             </list>
         </constructor-arg>