Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.security.acl;
import static com.sun.enterprise.security.common.AppservAccessController.privileged;
import static com.sun.logging.LogDomains.SECURITY_LOGGER;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.Subject;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
import org.glassfish.deployment.common.SecurityRoleMapper;
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.security.common.Group;
import org.glassfish.security.common.PrincipalImpl;
import org.glassfish.security.common.Role;
import com.sun.enterprise.config.serverbeans.SecurityService;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.security.common.AppservAccessController;
import com.sun.logging.LogDomains;
/**
* This class maintains a mapping of users and groups to application specific roles.
*
*
* Using this class the mapping information can be maintained and queried at a later time.
*
* @author Harpreet Singh
*/
public class RoleMapper implements Serializable, SecurityRoleMapper {
private static final long serialVersionUID = -4455830942007736853L;
private static final Logger _logger = LogDomains.getLogger(RoleMapper.class, SECURITY_LOGGER);
private String appName;
private final Map roleToSubject = new HashMap();
// Default mapper to emulate Servlet default p2r mapping semantics
private String defaultP2RMappingClassName = null;
private DefaultRoleToSubjectMapping defaultRTSM = new DefaultRoleToSubjectMapping();
/*
* the following 2 Maps are a copy of roleToSubject. This is added as a support for deployment. Should think of
* optimizing this.
*/
private final Map> roleToPrincipal = new HashMap>();
private final Map> roleToGroup = new HashMap>();
/* The following objects are used to detect conflicts during deployment */
/*
* .....Mapping of module (or application) that is presently calling assignRole(). It is set by the startMappingFor()
* method. After all the subjects have been assigned, stopMappingFor() is called and then the mappings can be checked
* against those previously assigned.
*/
private Mapping currentMapping;
// These override roles mapped in submodules.
private Set topLevelRoles;
// used to identify the application level mapping file
private static final String TOP_LEVEL = "sun-application.xml mapping file";
// used to log a warning only one time
private boolean conflictLogged;
// store roles that have a conflict so they are not re-mapped
private Set conflictedRoles;
/* End conflict detection objects */
private Boolean appDefaultMapping;
private transient SecurityService secService = null;
RoleMapper(String appName) {
this.appName = appName;
secService = Globals.getDefaultHabitat().getService(SecurityService.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
defaultP2RMappingClassName = getDefaultP2RMappingClassName();
}
/**
* Copy constructor. This is called from the JSR88 implementation. This is not stored into the internal rolemapper maps.
*/
public RoleMapper(RoleMapper r) {
this.appName = r.getName();
for (Iterator it = r.getRoles(); it.hasNext();) {
String role = it.next();
// recover groups
Enumeration groups = r.getGroupsAssignedTo(new Role(role));
Set groupsToRole = new HashSet();
for (; groups.hasMoreElements();) {
Group gp = groups.nextElement();
groupsToRole.add(new Group(gp.getName()));
addRoleToPrincipal(gp, role);
}
this.roleToGroup.put(role, groupsToRole);
// Recover principles
Enumeration users = r.getUsersAssignedTo(new Role(role));
Set usersToRole = new HashSet();
for (; users.hasMoreElements();) {
PrincipalImpl gp = (PrincipalImpl) users.nextElement();
usersToRole.add(new PrincipalImpl(gp.getName()));
addRoleToPrincipal(gp, role);
}
this.roleToPrincipal.put(role, usersToRole);
}
}
private boolean getAppDefaultRoleMapping() {
if (appDefaultMapping != null) {
return appDefaultMapping;
}
appDefaultMapping = false;
if (secService != null) {
appDefaultMapping = Boolean.parseBoolean(secService.getActivateDefaultPrincipalToRoleMapping());
if (appDefaultMapping) {
// if set explicitly in the security service allow default mapping
return appDefaultMapping;
}
}
ApplicationRegistry appRegistry = Globals.getDefaultHabitat().getService(ApplicationRegistry.class);
ApplicationInfo appInfo = appRegistry.get(appName);
if (appInfo == null) {
return appDefaultMapping;
}
Application app = appInfo.getMetaData(Application.class);
BundleDescriptor bd = app.getModuleByUri(appName);
appDefaultMapping = bd == null ? app.isDefaultGroupPrincipalMapping()
: app.getModuleByUri(appName).isDefaultGroupPrincipalMapping();
return appDefaultMapping;
}
/**
* @return The application/module name for this RoleMapper
*/
@Override
public String getName() {
return appName;
}
/**
* @param name The application/module name
*/
@Override
public void setName(String name) {
this.appName = name;
}
/**
* @param principal A principal that corresponds to the role
* @param role A role corresponding to this principal
*/
private void addRoleToPrincipal(Principal principal, String role) {
Subject subject = roleToSubject.get(role);
Subject sub = subject == null ? new Subject() : subject;
privileged(() -> sub.getPrincipals().add(principal));
roleToSubject.put(role, sub);
}
/**
* Remove the given role-principal mapping
*
* @param role, Role object
* @param principal, the principal
*/
@Override
public void unassignPrincipalFromRole(Role role, Principal principal) {
String mrole = role.getName();
final Subject sub = roleToSubject.get(mrole);
final Principal p = principal;
if (sub != null) {
AppservAccessController.doPrivileged(new PrivilegedAction