2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.authorization.importer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.Hashtable;
32 import java.util.List;
33 import java.util.UUID;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.Marshaller;
36 import org.collectionspace.services.authorization.perms.ActionType;
37 import org.collectionspace.services.authorization.perms.Permission;
38 import org.collectionspace.services.authorization.perms.EffectType;
39 import org.collectionspace.services.authorization.perms.PermissionAction;
40 import org.collectionspace.services.authorization.PermissionActionUtil;
41 import org.collectionspace.services.authorization.PermissionRole;
42 import org.collectionspace.services.authorization.PermissionValue;
43 import org.collectionspace.services.authorization.perms.PermissionsList;
44 import org.collectionspace.services.authorization.PermissionsRolesList;
45 import org.collectionspace.services.client.RoleClient;
46 import org.collectionspace.services.client.TenantClient;
47 import org.collectionspace.services.authorization.Role;
48 import org.collectionspace.services.authorization.RoleValue;
49 import org.collectionspace.services.authorization.RolesList;
50 import org.collectionspace.services.authorization.SubjectType;
51 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
52 import org.collectionspace.services.common.config.ServicesConfigReaderImpl;
53 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
54 import org.collectionspace.services.common.security.SecurityUtils;
55 import org.collectionspace.services.config.service.ServiceBindingType;
56 import org.collectionspace.services.config.tenant.TenantBindingType;
59 * AuthorizationGen generates authorizations (permissions and roles)
63 public class AuthorizationGen {
66 // Should the base resource act as a proxy for its sub-resources for AuthZ purposes
68 final public static boolean AUTHZ_IS_ENTITY_PROXY = false;
70 final public static String TENANT_MGMNT_ID = "0";
72 final Logger logger = LoggerFactory.getLogger(AuthorizationGen.class);
73 private List<Permission> tenantMgmntPermList = new ArrayList<Permission>();
74 private List<PermissionRole> tenantMgmntPermRoleList = new ArrayList<PermissionRole>();
75 private List<Permission> adminPermList = new ArrayList<Permission>();
76 private List<PermissionRole> adminPermRoleList = new ArrayList<PermissionRole>();
77 private List<Permission> readerPermList = new ArrayList<Permission>();
78 private List<PermissionRole> readerPermRoleList = new ArrayList<PermissionRole>();
79 private List<Role> adminRoles = new ArrayList<Role>();
80 private List<Role> readerRoles = new ArrayList<Role>();
81 private Role cspaceTenantMgmntRole;
82 private Hashtable<String, TenantBindingType> tenantBindings =
83 new Hashtable<String, TenantBindingType>();
85 // Store the list of default roles, perms, and roleperms
87 private List<PermissionRole> allPermRoleList = null;
88 private List<Permission> allPermList;
89 private List<Role> allRoleList;
91 public void initialize(String tenantRootDirPath) throws Exception {
92 ServicesConfigReaderImpl servicesConfigReader = new ServicesConfigReaderImpl(tenantRootDirPath);
93 servicesConfigReader.read();
94 Boolean useAppGeneratedBindings = servicesConfigReader.getConfiguration().isUseAppGeneratedTenantBindings();
96 TenantBindingConfigReaderImpl tenantBindingConfigReader =
97 new TenantBindingConfigReaderImpl(tenantRootDirPath);
98 tenantBindingConfigReader.read(useAppGeneratedBindings);
99 tenantBindings = tenantBindingConfigReader.getTenantBindings();
100 cspaceTenantMgmntRole = buildTenantMgmntRole();
102 if (logger.isDebugEnabled()) {
103 logger.debug("initialized with tenant bindings from " + tenantRootDirPath);
108 * createDefaultPermissions creates default admin and reader permissions
109 * for each tenant found in the given tenant binding file
113 public void createDefaultPermissions() {
114 for (String tenantId : tenantBindings.keySet()) {
115 List<Permission> adminPerms = createDefaultAdminPermissions(tenantId, AUTHZ_IS_ENTITY_PROXY);
116 adminPermList.addAll(adminPerms);
118 List<Permission> readerPerms = createDefaultReaderPermissions(tenantId, AUTHZ_IS_ENTITY_PROXY);
119 readerPermList.addAll(readerPerms);
122 List<Permission> tenantMgmntPerms = createDefaultTenantMgmntPermissions();
123 tenantMgmntPermList.addAll(tenantMgmntPerms);
127 * createDefaultAdminPermissions creates default admin permissions for all services
128 * used by the given tenant
132 public List<Permission> createDefaultAdminPermissions(String tenantId, boolean isEntityProxy) {
133 ArrayList<Permission> apcList = new ArrayList<Permission>();
134 TenantBindingType tbinding = tenantBindings.get(tenantId);
135 for (ServiceBindingType sbinding : tbinding.getServiceBindings()) {
137 //add permissions for the main path
138 String resourceName = sbinding.getName().toLowerCase().trim();
139 if (isEntityProxy == true) {
140 resourceName = SecurityUtils.getResourceEntity(resourceName);
142 Permission perm = buildAdminPermission(tbinding.getId(),
146 //add permissions for alternate paths
147 if (isEntityProxy == false) {
148 List<String> uriPaths = sbinding.getUriPath();
149 for (String uriPath : uriPaths) {
150 perm = buildAdminPermission(tbinding.getId(),
151 uriPath.toLowerCase());
161 * createDefaultTenantMgmntPermissions creates default permissions for known
162 * Tenant Mgmnt services.
165 public List<Permission> createDefaultTenantMgmntPermissions() {
166 ArrayList<Permission> apcList = new ArrayList<Permission>();
167 // Later can think about ways to configure this if we want to
168 Permission perm = createTenantMgmntPermission(TenantClient.SERVICE_NAME);
175 * createTenantMgmntPermission creates special admin permissions for tenant management
178 private Permission createTenantMgmntPermission(String resourceName) {
179 Permission perm = buildAdminPermission(TENANT_MGMNT_ID, resourceName);
183 private Permission buildAdminPermission(String tenantId, String resourceName) {
184 String description = "Generated admin permission.";
185 return AuthorizationCommon.createPermission(tenantId, resourceName, description, AuthorizationCommon.ACTIONGROUP_CRUDL_NAME);
189 * createDefaultReaderPermissions creates read only permissions for all services
190 * used by the given tenant
194 public List<Permission> createDefaultReaderPermissions(String tenantId, boolean isEntityProxy) {
195 ArrayList<Permission> apcList = new ArrayList<Permission>();
196 TenantBindingType tbinding = tenantBindings.get(tenantId);
197 for (ServiceBindingType sbinding : tbinding.getServiceBindings()) {
198 //add permissions for the main path
199 String resourceName = sbinding.getName().toLowerCase().trim();
200 if (isEntityProxy == true) {
201 resourceName = SecurityUtils.getResourceEntity(resourceName);
203 Permission perm = buildReaderPermission(tbinding.getId(),
207 //add permissions for alternate paths
208 if (isEntityProxy == false) {
209 List<String> uriPaths = sbinding.getUriPath();
210 for (String uriPath : uriPaths) {
211 perm = buildReaderPermission(tbinding.getId(),
212 uriPath.toLowerCase());
221 private Permission buildReaderPermission(String tenantId, String resourceName) {
222 String description = "Generated read-only permission.";
223 return AuthorizationCommon.createPermission(tenantId, resourceName, description, AuthorizationCommon.ACTIONGROUP_RL_NAME);
226 public List<Permission> getDefaultPermissions() {
227 if (allPermList == null) {
228 allPermList = new ArrayList<Permission>();
229 allPermList.addAll(adminPermList);
230 allPermList.addAll(readerPermList);
231 allPermList.addAll(tenantMgmntPermList);
236 public List<Permission> getDefaultAdminPermissions() {
237 return adminPermList;
240 public List<Permission> getDefaultReaderPermissions() {
241 return readerPermList;
244 public List<Permission> getDefaultTenantMgmntPermissions() {
245 return tenantMgmntPermList;
249 * createDefaultRoles creates default admin and reader roles
250 * for each tenant found in the given tenant binding file
252 public void createDefaultRoles() {
253 for (String tenantId : tenantBindings.keySet()) {
255 Role arole = buildTenantAdminRole(tenantId);
256 adminRoles.add(arole);
258 Role rrole = buildTenantReaderRole(tenantId);
259 readerRoles.add(rrole);
263 private Role buildTenantAdminRole(String tenantId) {
264 String type = "admin";
265 Role result = AuthorizationCommon.getRole(tenantId, AuthorizationCommon.ROLE_TENANT_ADMINISTRATOR);
267 if (result == null) {
268 // the role doesn't exist already, so we need to create it
269 String description = "Generated tenant " + type + " role.";
270 result = AuthorizationCommon.createRole(tenantId, AuthorizationCommon.ROLE_TENANT_ADMINISTRATOR, description, true /*immutable*/);
276 private Role buildTenantReaderRole(String tenantId) {
277 String type = "read only";
278 Role result = AuthorizationCommon.getRole(tenantId, AuthorizationCommon.ROLE_TENANT_READER);
280 if (result == null) {
281 // the role doesn't exist already, so we need to create it
282 String description = "Generated tenant " + type + " role.";
283 result = AuthorizationCommon.createRole(tenantId, AuthorizationCommon.ROLE_TENANT_READER, description, true /*immutable*/);
290 public List<Role> getDefaultRoles() {
291 if (allRoleList == null) {
292 allRoleList = new ArrayList<Role>();
293 allRoleList.addAll(adminRoles);
294 allRoleList.addAll(readerRoles);
295 // Finally, add the tenant manager role to the list
296 allRoleList.add(cspaceTenantMgmntRole);
301 public void associateDefaultPermissionsRoles() {
302 for (Permission p : adminPermList) {
303 PermissionRole permAdmRole = associatePermissionRoles(p, adminRoles, true);
304 adminPermRoleList.add(permAdmRole);
307 for (Permission p : readerPermList) {
308 PermissionRole permRdrRole = associatePermissionRoles(p, readerRoles, true);
309 readerPermRoleList.add(permRdrRole);
312 //CSpace Tenant Manager has all access
313 // PLS - this looks wrong. This should be a tenantMgmnt role, and only have access to
314 // tenantMgmnt perms. Will leave this for now...
315 List<Role> roles = new ArrayList<Role>();
316 roles.add(cspaceTenantMgmntRole);
317 /* for (Permission p : adminPermList) {
318 PermissionRole permCAdmRole = associatePermissionRoles(p, roles, false);
319 adminPermRoleList.add(permCAdmRole);
321 // Now associate the tenant management perms to the role
322 for (Permission p : tenantMgmntPermList) {
323 // Note we enforce tenant, as should all be tenant 0 (the special one)
324 PermissionRole permTMRole = associatePermissionRoles(p, roles, true);
325 tenantMgmntPermRoleList.add(permTMRole);
329 public List<PermissionRole> associatePermissionsRoles(List<Permission> perms, List<Role> roles, boolean enforceTenancy) {
330 List<PermissionRole> result = null;
332 List<PermissionRole> permRoles = new ArrayList<PermissionRole>();
333 for (Permission perm : perms) {
334 PermissionRole permRole = associatePermissionRoles(perm, roles, enforceTenancy);
335 if (permRole != null) {
336 permRoles.add(permRole);
340 if (permRoles.isEmpty() == false) {
347 private PermissionRole associatePermissionRoles(Permission perm,
348 List<Role> roles, boolean enforceTenancy) {
349 PermissionRole result = null;
351 PermissionRole pr = new PermissionRole();
352 pr.setSubject(SubjectType.ROLE);
353 List<PermissionValue> permValues = new ArrayList<PermissionValue>();
354 pr.setPermission(permValues);
355 PermissionValue permValue = new PermissionValue();
356 permValue.setPermissionId(perm.getCsid());
357 permValue.setResourceName(perm.getResourceName().toLowerCase());
358 permValue.setActionGroup(perm.getActionGroup());
359 permValues.add(permValue);
361 List<RoleValue> roleValues = new ArrayList<RoleValue>();
362 for (Role role : roles) {
363 boolean tenantIdsMatched = true;
364 if (enforceTenancy == true) {
365 tenantIdsMatched = role.getTenantId().equals(perm.getTenantId());
367 if (tenantIdsMatched == true) {
368 RoleValue rv = new RoleValue();
369 // This needs to use the qualified name, not the display name
370 rv.setRoleName(role.getRoleName().toUpperCase());
371 rv.setRoleId(role.getCsid());
374 if (logger.isTraceEnabled() == true) {
375 logger.trace("Role and Permission tenant ID did not match."); //FIXME: REM - Remove this debug statement.
380 // If 'roleValues' is not empty, then associate it with the incoming 'perm' values
381 // otherwise, return null;
383 if (roleValues.isEmpty() == false) {
384 pr.setRole(roleValues);
391 public List<PermissionRole> getDefaultPermissionRoles() {
392 if (allPermRoleList == null) {
393 allPermRoleList = new ArrayList<PermissionRole>();
394 allPermRoleList.addAll(adminPermRoleList);
395 allPermRoleList.addAll(readerPermRoleList);
396 allPermRoleList.addAll(tenantMgmntPermRoleList);
398 return allPermRoleList;
401 public List<PermissionRole> getDefaultAdminPermissionRoles() {
402 return adminPermRoleList;
405 public List<PermissionRole> getDefaultReaderPermissionRoles() {
406 return readerPermRoleList;
409 private Role buildTenantMgmntRole() {
410 Role role = new Role();
412 role.setDescription("A generated super role that has permissions to manage tenants.");
413 role.setDisplayName(AuthorizationCommon.ROLE_ALL_TENANTS_MANAGER);
414 role.setRoleName(AuthorizationCommon.getQualifiedRoleName(
415 AuthorizationCommon.ALL_TENANTS_MANAGER_TENANT_ID, role.getDisplayName()));
416 role.setCsid(AuthorizationCommon.ROLE_ALL_TENANTS_MANAGER_ID);
417 role.setTenantId(AuthorizationCommon.ALL_TENANTS_MANAGER_TENANT_ID);
423 public void exportDefaultRoles(String fileName) {
424 RolesList rList = new RolesList();
425 rList.setRole(this.getDefaultRoles());
427 // Since it is missing the @XMLRootElement annotation, create a JAXBElement wrapper for the RoleList instance
428 // so we can have it marshalled it correctly.
430 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
431 toFile(objectFactory.createRolesList(rList), RolesList.class,
433 if (logger.isDebugEnabled()) {
434 logger.debug("exported roles to " + fileName);
438 public void exportDefaultPermissions(String fileName) {
439 PermissionsList pcList = new PermissionsList();
440 pcList.setPermission(this.getDefaultPermissions());
441 org.collectionspace.services.authorization.ObjectFactory objectFactory =
442 new org.collectionspace.services.authorization.ObjectFactory();
443 toFile(pcList, PermissionsList.class,
444 // toFile(objectFactory.createPermissionsList(pcList), PermissionsList.class,
446 if (logger.isDebugEnabled()) {
447 logger.debug("exported permissions to " + fileName);
451 public void exportDefaultPermissionRoles(String fileName) {
452 PermissionsRolesList psrsl = new PermissionsRolesList();
453 psrsl.setPermissionRole(this.getDefaultAdminPermissionRoles());
454 toFile(psrsl, PermissionsRolesList.class,
456 if (logger.isDebugEnabled()) {
457 logger.debug("exported permissions-roles to " + fileName);
461 private void toFile(Object o, Class jaxbClass, String fileName) {
462 File f = new File(fileName);
464 JAXBContext jc = JAXBContext.newInstance(jaxbClass);
465 Marshaller m = jc.createMarshaller();
466 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
469 } catch (Exception e) {