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.Hashtable;
31 import java.util.List;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.Marshaller;
34 import org.collectionspace.services.authorization.perms.Permission;
35 import org.collectionspace.authentication.AuthN;
36 import org.collectionspace.services.authorization.PermissionRole;
37 import org.collectionspace.services.authorization.PermissionValue;
38 import org.collectionspace.services.authorization.perms.PermissionsList;
39 import org.collectionspace.services.authorization.PermissionsRolesList;
40 import org.collectionspace.services.client.TenantClient;
41 import org.collectionspace.services.authorization.Role;
42 import org.collectionspace.services.authorization.RoleValue;
43 import org.collectionspace.services.authorization.RolesList;
44 import org.collectionspace.services.authorization.SubjectType;
45 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
46 import org.collectionspace.services.common.config.ServicesConfigReaderImpl;
47 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
48 import org.collectionspace.services.common.security.SecurityUtils;
49 import org.collectionspace.services.config.service.ServiceBindingType;
50 import org.collectionspace.services.config.tenant.TenantBindingType;
53 * AuthorizationGen generates authorizations (permissions and roles)
57 public class AuthorizationGen {
60 // Should the base resource act as a proxy for its sub-resources for AuthZ purposes
62 final public static boolean AUTHZ_IS_ENTITY_PROXY = false;
64 final public static String TENANT_MGMNT_ID = "0";
66 private static final boolean USE_APP_GENERATED_BINDINGS = true;
68 final Logger logger = LoggerFactory.getLogger(AuthorizationGen.class);
69 private List<Permission> tenantMgmntPermList = new ArrayList<Permission>();
70 private List<PermissionRole> tenantMgmntPermRoleList = new ArrayList<PermissionRole>();
71 private List<Permission> adminPermList = new ArrayList<Permission>();
72 private List<PermissionRole> adminPermRoleList = new ArrayList<PermissionRole>();
73 private List<Permission> readerPermList = new ArrayList<Permission>();
74 private List<PermissionRole> readerPermRoleList = new ArrayList<PermissionRole>();
75 private List<Role> adminRoles = new ArrayList<Role>();
76 private List<Role> readerRoles = new ArrayList<Role>();
77 private Role cspaceTenantMgmntRole;
78 private Hashtable<String, TenantBindingType> tenantBindings =
79 new Hashtable<String, TenantBindingType>();
81 // Store the list of default roles, perms, and roleperms
83 private List<PermissionRole> allPermRoleList = null;
84 private List<Permission> allPermList;
85 private List<Role> allRoleList;
87 public void initialize(String tenantRootDirPath) throws Exception {
88 ServicesConfigReaderImpl servicesConfigReader = new ServicesConfigReaderImpl(tenantRootDirPath);
89 servicesConfigReader.read(USE_APP_GENERATED_BINDINGS);
90 Boolean useAppGeneratedBindings = servicesConfigReader.getConfiguration().isUseAppGeneratedTenantBindings();
92 TenantBindingConfigReaderImpl tenantBindingConfigReader =
93 new TenantBindingConfigReaderImpl(tenantRootDirPath);
94 tenantBindingConfigReader.read(useAppGeneratedBindings);
95 // Note that we build permissions for all tenants, whether or not they are marked create disabled.
96 // This is a hack until we can correctly run an incremental import.
97 tenantBindings = tenantBindingConfigReader.getTenantBindings(
98 TenantBindingConfigReaderImpl.INCLUDE_CREATE_DISABLED_TENANTS);
99 cspaceTenantMgmntRole = buildTenantMgmntRole();
101 if (logger.isDebugEnabled()) {
102 logger.debug("initialized with tenant bindings from " + tenantRootDirPath);
107 * createDefaultPermissions creates default admin and reader permissions
108 * for each tenant found in the given tenant binding file
112 public void createDefaultPermissions() {
113 for (String tenantId : tenantBindings.keySet()) {
114 List<Permission> adminPerms = createDefaultAdminPermissions(tenantId, AUTHZ_IS_ENTITY_PROXY);
115 adminPermList.addAll(adminPerms);
117 List<Permission> readerPerms = createDefaultReaderPermissions(tenantId, AUTHZ_IS_ENTITY_PROXY);
118 readerPermList.addAll(readerPerms);
121 List<Permission> tenantMgmntPerms = createDefaultTenantMgmntPermissions();
122 tenantMgmntPermList.addAll(tenantMgmntPerms);
126 * createDefaultAdminPermissions creates default admin permissions for all services
127 * used by the given tenant
131 public List<Permission> createDefaultAdminPermissions(String tenantId, boolean isEntityProxy) {
132 ArrayList<Permission> apcList = new ArrayList<Permission>();
133 TenantBindingType tbinding = tenantBindings.get(tenantId);
134 for (ServiceBindingType sbinding : tbinding.getServiceBindings()) {
136 //add permissions for the main path
137 String resourceName = sbinding.getName().toLowerCase().trim();
138 if (isEntityProxy == true) {
139 resourceName = SecurityUtils.getResourceEntity(resourceName);
141 Permission perm = buildAdminPermission(tbinding.getId(),
145 //add permissions for alternate paths
146 if (isEntityProxy == false) {
147 List<String> uriPaths = sbinding.getUriPath();
148 for (String uriPath : uriPaths) {
149 perm = buildAdminPermission(tbinding.getId(),
150 uriPath.toLowerCase());
160 * createDefaultTenantMgmntPermissions creates default permissions for known
161 * Tenant Mgmnt services.
164 public List<Permission> createDefaultTenantMgmntPermissions() {
165 ArrayList<Permission> apcList = new ArrayList<Permission>();
166 // Later can think about ways to configure this if we want to
167 Permission perm = createTenantMgmntPermission(TenantClient.SERVICE_NAME);
174 * createTenantMgmntPermission creates special admin permissions for tenant management
177 private Permission createTenantMgmntPermission(String resourceName) {
178 Permission perm = buildAdminPermission(TENANT_MGMNT_ID, resourceName);
182 private Permission buildAdminPermission(String tenantId, String resourceName) {
183 String description = "Generated admin permission.";
184 return AuthorizationCommon.createPermission(tenantId, resourceName, description, AuthorizationCommon.ACTIONGROUP_CRUDL_NAME);
188 * createDefaultReaderPermissions creates read only permissions for all services
189 * used by the given tenant
193 public List<Permission> createDefaultReaderPermissions(String tenantId, boolean isEntityProxy) {
194 ArrayList<Permission> apcList = new ArrayList<Permission>();
195 TenantBindingType tbinding = tenantBindings.get(tenantId);
196 for (ServiceBindingType sbinding : tbinding.getServiceBindings()) {
197 //add permissions for the main path
198 String resourceName = sbinding.getName().toLowerCase().trim();
199 if (isEntityProxy == true) {
200 resourceName = SecurityUtils.getResourceEntity(resourceName);
202 Permission perm = buildReaderPermission(tbinding.getId(),
206 //add permissions for alternate paths
207 if (isEntityProxy == false) {
208 List<String> uriPaths = sbinding.getUriPath();
209 for (String uriPath : uriPaths) {
210 perm = buildReaderPermission(tbinding.getId(),
211 uriPath.toLowerCase());
220 private Permission buildReaderPermission(String tenantId, String resourceName) {
221 String description = "Generated read-only permission.";
222 return AuthorizationCommon.createPermission(tenantId, resourceName, description, AuthorizationCommon.ACTIONGROUP_RL_NAME);
225 public List<Permission> getDefaultPermissions() {
226 if (allPermList == null) {
227 allPermList = new ArrayList<Permission>();
228 allPermList.addAll(adminPermList);
229 allPermList.addAll(readerPermList);
230 allPermList.addAll(tenantMgmntPermList);
235 public List<Permission> getDefaultAdminPermissions() {
236 return adminPermList;
239 public List<Permission> getDefaultReaderPermissions() {
240 return readerPermList;
243 public List<Permission> getDefaultTenantMgmntPermissions() {
244 return tenantMgmntPermList;
248 * createDefaultRoles creates default admin and reader roles
249 * for each tenant found in the given tenant binding file
251 public void createDefaultRoles() {
252 for (String tenantId : tenantBindings.keySet()) {
254 Role arole = buildTenantAdminRole(tenantId);
255 adminRoles.add(arole);
257 Role rrole = buildTenantReaderRole(tenantId);
258 readerRoles.add(rrole);
262 private Role buildTenantAdminRole(String tenantId) {
263 String type = "admin";
264 Role result = AuthorizationCommon.getRole(tenantId, AuthorizationCommon.ROLE_TENANT_ADMINISTRATOR);
266 if (result == null) {
267 // the role doesn't exist already, so we need to create it
268 String description = "Generated tenant " + type + " role.";
269 result = AuthorizationCommon.createRole(tenantId, AuthorizationCommon.ROLE_TENANT_ADMINISTRATOR, description, true /*immutable*/);
275 private Role buildTenantReaderRole(String tenantId) {
276 String type = "read only";
277 Role result = AuthorizationCommon.getRole(tenantId, AuthorizationCommon.ROLE_TENANT_READER);
279 if (result == null) {
280 // the role doesn't exist already, so we need to create it
281 String description = "Generated tenant " + type + " role.";
282 result = AuthorizationCommon.createRole(tenantId, AuthorizationCommon.ROLE_TENANT_READER, description, true /*immutable*/);
289 public List<Role> getDefaultRoles() {
290 if (allRoleList == null) {
291 allRoleList = new ArrayList<Role>();
292 allRoleList.addAll(adminRoles);
293 allRoleList.addAll(readerRoles);
294 // Finally, add the tenant manager role to the list
295 allRoleList.add(cspaceTenantMgmntRole);
300 public void associateDefaultPermissionsRoles() {
301 for (Permission p : adminPermList) {
302 PermissionRole permAdmRole = associatePermissionRoles(p, adminRoles, true);
303 adminPermRoleList.add(permAdmRole);
306 for (Permission p : readerPermList) {
307 PermissionRole permRdrRole = associatePermissionRoles(p, readerRoles, true);
308 readerPermRoleList.add(permRdrRole);
311 //CSpace Tenant Manager has all access
312 // PLS - this looks wrong. This should be a tenantMgmnt role, and only have access to
313 // tenantMgmnt perms. Will leave this for now...
314 List<Role> roles = new ArrayList<Role>();
315 roles.add(cspaceTenantMgmntRole);
316 /* for (Permission p : adminPermList) {
317 PermissionRole permCAdmRole = associatePermissionRoles(p, roles, false);
318 adminPermRoleList.add(permCAdmRole);
320 // Now associate the tenant management perms to the role
321 for (Permission p : tenantMgmntPermList) {
322 // Note we enforce tenant, as should all be tenant 0 (the special one)
323 PermissionRole permTMRole = associatePermissionRoles(p, roles, true);
324 tenantMgmntPermRoleList.add(permTMRole);
328 public List<PermissionRole> associatePermissionsRoles(List<Permission> perms, List<Role> roles, boolean enforceTenancy) {
329 List<PermissionRole> result = null;
331 List<PermissionRole> permRoles = new ArrayList<PermissionRole>();
332 for (Permission perm : perms) {
333 PermissionRole permRole = associatePermissionRoles(perm, roles, enforceTenancy);
334 if (permRole != null) {
335 permRoles.add(permRole);
339 if (permRoles.isEmpty() == false) {
346 private PermissionRole associatePermissionRoles(Permission perm,
347 List<Role> roles, boolean enforceTenancy) {
348 PermissionRole result = null;
350 PermissionRole pr = new PermissionRole();
351 pr.setSubject(SubjectType.ROLE);
352 List<PermissionValue> permValues = new ArrayList<PermissionValue>();
353 pr.setPermission(permValues);
354 PermissionValue permValue = new PermissionValue();
355 permValue.setPermissionId(perm.getCsid());
356 permValue.setResourceName(perm.getResourceName().toLowerCase());
357 permValue.setActionGroup(perm.getActionGroup());
358 permValues.add(permValue);
360 List<RoleValue> roleValues = new ArrayList<RoleValue>();
361 for (Role role : roles) {
362 boolean tenantIdsMatched = true;
363 if (enforceTenancy == true) {
364 tenantIdsMatched = role.getTenantId().equals(perm.getTenantId());
366 if (tenantIdsMatched == true) {
367 RoleValue rv = new RoleValue();
368 // This needs to use the qualified name, not the display name
369 rv.setRoleName(role.getRoleName().toUpperCase());
370 rv.setRoleId(role.getCsid());
373 if (logger.isTraceEnabled() == true) {
374 logger.trace("Role and Permission tenant ID did not match."); //FIXME: REM - Remove this debug statement.
379 // If 'roleValues' is not empty, then associate it with the incoming 'perm' values
380 // otherwise, return null;
382 if (roleValues.isEmpty() == false) {
383 pr.setRole(roleValues);
390 public List<PermissionRole> getDefaultPermissionRoles() {
391 if (allPermRoleList == null) {
392 allPermRoleList = new ArrayList<PermissionRole>();
393 allPermRoleList.addAll(adminPermRoleList);
394 allPermRoleList.addAll(readerPermRoleList);
395 allPermRoleList.addAll(tenantMgmntPermRoleList);
397 return allPermRoleList;
400 public List<PermissionRole> getDefaultAdminPermissionRoles() {
401 return adminPermRoleList;
404 public List<PermissionRole> getDefaultReaderPermissionRoles() {
405 return readerPermRoleList;
408 private Role buildTenantMgmntRole() {
409 Role role = new Role();
411 role.setDescription("A generated super role that has permissions to manage tenants.");
412 role.setDisplayName(AuthN.ROLE_ALL_TENANTS_MANAGER);
413 role.setRoleName(AuthorizationCommon.getQualifiedRoleName(
414 AuthN.ALL_TENANTS_MANAGER_TENANT_ID, role.getDisplayName()));
415 role.setCsid(AuthN.ROLE_ALL_TENANTS_MANAGER_ID);
416 role.setTenantId(AuthN.ALL_TENANTS_MANAGER_TENANT_ID);
422 public void exportDefaultRoles(String fileName) {
423 RolesList rList = new RolesList();
424 rList.setRole(this.getDefaultRoles());
426 // Since it is missing the @XMLRootElement annotation, create a JAXBElement wrapper for the RoleList instance
427 // so we can have it marshalled it correctly.
429 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
430 toFile(objectFactory.createRolesList(rList), RolesList.class,
432 if (logger.isDebugEnabled()) {
433 logger.debug("exported roles to " + fileName);
437 public void exportDefaultPermissions(String fileName) {
438 PermissionsList pcList = new PermissionsList();
439 pcList.setPermission(this.getDefaultPermissions());
440 org.collectionspace.services.authorization.ObjectFactory objectFactory =
441 new org.collectionspace.services.authorization.ObjectFactory();
442 toFile(pcList, PermissionsList.class,
443 // toFile(objectFactory.createPermissionsList(pcList), PermissionsList.class,
445 if (logger.isDebugEnabled()) {
446 logger.debug("exported permissions to " + fileName);
450 public void exportDefaultPermissionRoles(String fileName) {
451 PermissionsRolesList psrsl = new PermissionsRolesList();
452 psrsl.setPermissionRole(this.getDefaultAdminPermissionRoles());
453 toFile(psrsl, PermissionsRolesList.class,
455 if (logger.isDebugEnabled()) {
456 logger.debug("exported permissions-roles to " + fileName);
460 private void toFile(Object o, Class jaxbClass, String fileName) {
461 File f = new File(fileName);
463 JAXBContext jc = JAXBContext.newInstance(jaxbClass);
464 Marshaller m = jc.createMarshaller();
465 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
468 } catch (Exception e) {