Skip to content

RANGER-5215 : Policy authroisation fails for Ranger Plugins in case of users/groups converted by Ranger userysnc as per given Regex #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions agents-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@
<artifactId>ranger-plugins-cred</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ranger</groupId>
<artifactId>ugsync-util</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.plugin.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.ranger.plugin.util.RangerUserStoreUtil;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import java.util.Map;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UgsyncNameTransformRules extends RangerBaseModelObject implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private final String name;
Map<String, String> nameTransformRules;

public UgsyncNameTransformRules() {
this(null, null, null);
}

public UgsyncNameTransformRules(String guid, String name, Map<String, String> nameTransformRules) {
super();
setGuid(guid);
setId(Long.valueOf(1));
setVersion(Long.valueOf(1));
this.name = name;
this.nameTransformRules = nameTransformRules;
}

public String getName() {
return name;
}

public Map<String, String> getNameTransformRules() {
return nameTransformRules;
}

@Override
public String toString() {
return "{nameTransformRules=" + RangerUserStoreUtil.getPrintableOptions(nameTransformRules)
+ "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.ranger.plugin.resourcematcher.RangerResourceMatcher;
import org.apache.ranger.plugin.service.RangerAuthContext;
import org.apache.ranger.plugin.service.RangerAuthContextListener;
import org.apache.ranger.ugsyncutil.transform.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,6 +41,10 @@ public class RangerPluginContext {
private final RangerPluginConfig config;
private final Map<String, Map<RangerPolicy.RangerPolicyResource, RangerResourceMatcher>> resourceMatchers = new HashMap<>();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); // fair lock
private Mapper userNameTransformInst;
private Mapper groupNameTransformInst;
private String userNameCaseConversion;
private String groupNameCaseConversion;
private RangerAuthContext authContext;
private RangerAuthContextListener authContextListener;
private RangerAdminClient adminClient;
Expand All @@ -48,6 +53,38 @@ public RangerPluginContext(RangerPluginConfig config) {
this.config = config;
}

public Mapper getUserNameTransformInst() {
return userNameTransformInst;
}

public void setUserNameTransformInst(Mapper userNameTransformInst) {
this.userNameTransformInst = userNameTransformInst;
}

public Mapper getGroupNameTransformInst() {
return groupNameTransformInst;
}

public void setGroupNameTransformInst(Mapper groupNameTransformInst) {
this.groupNameTransformInst = groupNameTransformInst;
}

public String getUserNameCaseConversion() {
return userNameCaseConversion;
}

public void setUserNameCaseConversion(String userNameCaseConversion) {
this.userNameCaseConversion = userNameCaseConversion;
}

public String getGroupNameCaseConversion() {
return groupNameCaseConversion;
}

public void setGroupNameCaseConversion(String groupNameCaseConversion) {
this.groupNameCaseConversion = groupNameCaseConversion;
}

public RangerPluginConfig getConfig() {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.apache.ranger.plugin.util.GrantRevokeRoleRequest;
import org.apache.ranger.plugin.util.PerfDataRecorder;
import org.apache.ranger.plugin.util.PolicyRefresher;
import org.apache.ranger.plugin.util.RangerCommonConstants;
import org.apache.ranger.plugin.util.RangerPolicyDeltaUtil;
import org.apache.ranger.plugin.util.RangerRoles;
import org.apache.ranger.plugin.util.RangerRolesUtil;
Expand All @@ -69,6 +70,7 @@
import org.apache.ranger.plugin.util.ServiceGdsInfo;
import org.apache.ranger.plugin.util.ServicePolicies;
import org.apache.ranger.plugin.util.ServiceTags;
import org.apache.ranger.ugsyncutil.transform.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -208,7 +210,6 @@ public RangerBasePlugin(RangerPluginConfig pluginConfig, ServicePolicies policie
this(pluginConfig);

init();

setPolicies(policies);
setRoles(roles);

Expand Down Expand Up @@ -441,7 +442,7 @@ public long getUserStoreVersion() {

public void setPolicies(ServicePolicies policies) {
LOG.debug("==> setPolicies({})", policies);

configurePluginContextFromServicePoliciesForUserGroupName(policies);
this.serviceConfigs = (policies != null && policies.getServiceConfig() != null) ? policies.getServiceConfig() : new HashMap<>();

if (pluginConfig.isEnableImplicitUserStoreEnricher() && policies != null && !ServiceDefUtil.isUserStoreEnricherPresent(policies)) {
Expand Down Expand Up @@ -887,7 +888,7 @@ public Set<RangerRole> getRangerRoleForPrincipal(String principal, String type)
RangerPolicyEngine policyEngine = this.policyEngine;
RangerRoles roles = policyEngine != null ? policyEngine.getRangerRoles() : null;
Set<RangerRole> rangerRoles = roles != null ? roles.getRangerRoles() : null;
Map<String, Set<String>> roleMapping = null;
Map<String, Set<String>> roleMapping = null;

if (rangerRoles != null) {
RangerPluginContext rangerPluginContext = policyEngine.getPluginContext();
Expand Down Expand Up @@ -1274,6 +1275,74 @@ private RangerAdminClient getAdminClient() throws Exception {
return admin;
}

private void configurePluginContextFromServicePoliciesForUserGroupName(ServicePolicies servicePolicies) {
Map<String, String> serviceConfigMap = servicePolicies.getServiceConfig();
if (MapUtils.isNotEmpty(serviceConfigMap)) {
LOG.debug("==> RangerBasePlugin(" + serviceConfigMap.keySet() + ")");
pluginContext.setUserNameCaseConversion(serviceConfigMap.get(RangerCommonConstants.PLUGINS_USERNAME_CASE_CONVERSION_PARAM));
pluginContext.setGroupNameCaseConversion(serviceConfigMap.get(RangerCommonConstants.PLUGINS_GROUPNAME_CASE_CONVERSION_PARAM));
String mappingUserNameHandler = serviceConfigMap.get(RangerCommonConstants.PLUGINS_MAPPING_USERNAME_HANDLER);
try {
if (mappingUserNameHandler != null) {
Class<Mapper> regExClass = (Class<Mapper>) Class.forName(mappingUserNameHandler);
Mapper userNameRegExInst = regExClass.newInstance();
if (userNameRegExInst != null) {
String baseProperty = RangerCommonConstants.PLUGINS_MAPPING_USERNAME;
userNameRegExInst.init(baseProperty, getAllRegexPatterns(baseProperty, serviceConfigMap),
serviceConfigMap.get(RangerCommonConstants.PLUGINS_MAPPING_SEPARATOR));
pluginContext.setUserNameTransformInst(userNameRegExInst);
} else {
LOG.error("RegEx handler instance for username is null!");
}
}
} catch (ClassNotFoundException cne) {
LOG.error("Failed to load " + mappingUserNameHandler + " ", cne);
} catch (Throwable te) {
LOG.error("Failed to instantiate " + mappingUserNameHandler + " ", te);
}
String mappingGroupNameHandler = serviceConfigMap.get(RangerCommonConstants.PLUGINS_MAPPING_GROUPNAME_HANDLER);
try {
if (mappingGroupNameHandler != null) {
Class<Mapper> regExClass = (Class<Mapper>) Class.forName(mappingGroupNameHandler);
Mapper groupNameRegExInst = regExClass.newInstance();
if (groupNameRegExInst != null) {
String baseProperty = RangerCommonConstants.PLUGINS_MAPPING_GROUPNAME;
groupNameRegExInst.init(baseProperty, getAllRegexPatterns(baseProperty, serviceConfigMap),
serviceConfigMap.get(RangerCommonConstants.PLUGINS_MAPPING_SEPARATOR));
pluginContext.setGroupNameTransformInst(groupNameRegExInst);
} else {
LOG.error("RegEx handler instance for groupname is null!");
}
}
} catch (ClassNotFoundException cne) {
LOG.error("Failed to load " + mappingGroupNameHandler + " ", cne);
} catch (Throwable te) {
LOG.error("Failed to instantiate " + mappingGroupNameHandler + " ", te);
}
}
}

private List<String> getAllRegexPatterns(String baseProperty, Map<String, String> serviceConfig) throws Throwable {
List<String> regexPatterns = new ArrayList<String>();
String baseRegex = serviceConfig.get(baseProperty);
LOG.debug("==> getAllRegexPatterns(" + baseProperty + ")");
LOG.debug("baseRegex = " + baseRegex);
LOG.debug("pluginConfig = " + serviceConfig.keySet());
if (baseRegex == null) {
return regexPatterns;
}
regexPatterns.add(baseRegex);
int i = 1;
String nextRegex = serviceConfig.get(baseProperty + "." + i);
while (nextRegex != null) {
regexPatterns.add(nextRegex);
i++;
nextRegex = serviceConfig.get(baseProperty + "." + i);
}
LOG.debug("<== getAllRegexPatterns(" + regexPatterns + ")");
return regexPatterns;
}

private List<RangerChainedPlugin> initChainedPlugins() {
List<RangerChainedPlugin> ret = new ArrayList<>();
String chainedServicePropPrefix = pluginConfig.getPropertyPrefix() + ".chained.services";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,29 @@
import org.apache.ranger.plugin.policyengine.RangerMutableResource;
import org.apache.ranger.plugin.policyengine.RangerPluginContext;
import org.apache.ranger.plugin.util.RangerAccessRequestUtil;
import org.apache.ranger.plugin.util.RangerCommonConstants;
import org.apache.ranger.plugin.util.RangerPerfTracer;
import org.apache.ranger.plugin.util.RangerUserStoreUtil;
import org.apache.ranger.ugsyncutil.transform.Mapper;
import org.apache.ranger.ugsyncutil.util.UgsyncCommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class RangerDefaultRequestProcessor implements RangerAccessRequestProcessor {
private static final Logger LOG = LoggerFactory.getLogger(RangerDefaultRequestProcessor.class);
private static final Logger PERF_CONTEXTENRICHER_REQUEST_LOG = RangerPerfTracer.getPerfLogger("contextenricher.request");

protected final PolicyEngine policyEngine;
private final boolean useRangerGroups;
private final boolean useOnlyRangerGroups;
private final boolean convertEmailToUser;
private final boolean useRangerGroups;
private final boolean useOnlyRangerGroups;
private final boolean convertEmailToUser;

public RangerDefaultRequestProcessor(PolicyEngine policyEngine) {
this.policyEngine = policyEngine;
Expand Down Expand Up @@ -98,6 +103,53 @@ public void preProcess(RangerAccessRequest request) {
reqImpl.setClusterType(pluginContext.getClusterType());
}

RangerPluginConfig config = policyEngine.getPluginContext().getConfig();
LOG.debug("RangerPluginConfig = " + config.getPropertyPrefix());
if (config != null) {
boolean isNameTransformationSupported = config.getBoolean(config.getPropertyPrefix() + RangerCommonConstants.PLUGIN_CONFIG_SUFFIX_NAME_TRANSFORMATION, false);

LOG.debug("isNameTransformationSupported = " + isNameTransformationSupported);

if (isNameTransformationSupported) {
String user = request.getUser();
String userNameCaseConversion = policyEngine.getPluginContext().getUserNameCaseConversion();
if (userNameCaseConversion != null && userNameCaseConversion.equalsIgnoreCase(UgsyncCommonConstants.UGSYNC_LOWER_CASE_CONVERSION_VALUE)) {
user = user.toLowerCase();
} else if (userNameCaseConversion != null && userNameCaseConversion.equalsIgnoreCase(UgsyncCommonConstants.UGSYNC_UPPER_CASE_CONVERSION_VALUE)) {
user = user.toUpperCase();
}
Mapper userNameTransformInst = policyEngine.getPluginContext().getUserNameTransformInst();
if (userNameTransformInst != null) {
String originalUser = request.getUser();
String transformedUser = userNameTransformInst.transform(user);
LOG.debug("Original username = {}, Transformed username = {}", originalUser, transformedUser);
reqImpl.setUser(transformedUser);
}
Mapper groupNameTransformInst = policyEngine.getPluginContext().getGroupNameTransformInst();
if (groupNameTransformInst != null) {
Set<String> transformedGroups = request.getUserGroups().stream()
.filter(Objects::nonNull)
.map(group -> {
String originalGroupName = group;
String conversionType = policyEngine.getPluginContext().getGroupNameCaseConversion();

if (UgsyncCommonConstants.UGSYNC_LOWER_CASE_CONVERSION_VALUE.equalsIgnoreCase(conversionType)) {
group = group.toLowerCase();
} else if (UgsyncCommonConstants.UGSYNC_UPPER_CASE_CONVERSION_VALUE.equalsIgnoreCase(conversionType)) {
group = group.toUpperCase();
}

String transformedGroup = groupNameTransformInst.transform(group);
LOG.debug("Original group name = {}, Transformed group name = {}", originalGroupName, transformedGroup);
return transformedGroup;
})
.collect(Collectors.toSet());

reqImpl.setUserGroups(transformedGroups);
}
}
}

convertEmailToUsername(reqImpl);

updateUserGroups(reqImpl);
Expand Down
Loading