Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.conditionevaluator;

import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
import org.apache.ranger.plugin.util.RangerActionListMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RangerActionMatcher extends RangerAbstractConditionEvaluator {
private static final Logger LOG = LoggerFactory.getLogger(RangerActionMatcher.class);

private RangerActionListMatcher actionMatcher = new RangerActionListMatcher(null);

@Override
public void init() {
LOG.debug("==> RangerActionMatcher.init({})", condition);

super.init();

this.actionMatcher = new RangerActionListMatcher(condition == null ? null : condition.getValues());

LOG.debug("<== RangerActionMatcher.init({})", condition);
}

@Override
public boolean isMatched(final RangerAccessRequest request) {
LOG.debug("==> RangerActionMatcher.isMatched({})", request);

final boolean ret = actionMatcher.isMatch(request != null ? request.getAction() : null);

LOG.debug("<== RangerActionMatcher.isMatched({}): {}", request, ret);

return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,20 @@ public static class Grant {
private Set<String> principals; // example: [ "u:user1, "g:group1", "r:role1" ]; if empty, means public grant
private Set<String> resources; // example: [ "key:vol1/bucket1/db1/tbl1/*", "key:vol1/bucket1/db1/tbl2/*" ]; if empty, means all resources
private Set<String> permissions; // example: [ "read", "write" ]; if empty, means no permission
private Set<String> actions; // optional: [ "GetObject", "Put*", "*" ]; if empty or null, means all actions

public Grant() {
}

public Grant(Set<String> principals, Set<String> resources, Set<String> permissions) {
this(principals, resources, permissions, null);
}

public Grant(Set<String> principals, Set<String> resources, Set<String> permissions, Set<String> actions) {
this.principals = principals;
this.resources = resources;
this.permissions = permissions;
this.actions = actions;
}

public Set<String> getPrincipals() {
Expand All @@ -146,6 +152,14 @@ public void setPermissions(Set<String> permissions) {
this.permissions = permissions;
}

public Set<String> getActions() {
return actions;
}

public void setActions(Set<String> actions) {
this.actions = actions;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -158,12 +172,13 @@ public boolean equals(Object o) {

return Objects.equals(principals, that.principals) &&
Objects.equals(resources, that.resources) &&
Objects.equals(permissions, that.permissions);
Objects.equals(permissions, that.permissions) &&
Objects.equals(actions, that.actions);
}

@Override
public int hashCode() {
return Objects.hash(principals, resources, permissions);
return Objects.hash(principals, resources, permissions, actions);
}

@Override
Expand All @@ -172,6 +187,7 @@ public String toString() {
"principals=" + principals +
", resources=" + resources +
", permissions=" + permissions +
", actions=" + actions +
'}';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.ranger.plugin.policyresourcematcher.RangerDefaultPolicyResourceMatcher;
import org.apache.ranger.plugin.policyresourcematcher.RangerPolicyResourceMatcher;
import org.apache.ranger.plugin.util.RangerAccessRequestUtil;
import org.apache.ranger.plugin.util.RangerActionListMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -156,11 +157,13 @@ private List<GrantEvaluator> toGrantEvaluators(RangerInlinePolicy policy) {
private class GrantEvaluator {
private final RangerInlinePolicy.Grant grant;
private final Set<String> permissions;
private final RangerActionListMatcher actionMatcher;
private final Set<RangerPolicyResourceMatcher> resourceMatchers = new HashSet<>();

public GrantEvaluator(RangerInlinePolicy.Grant grant) {
this.grant = grant;
this.permissions = policyEngine.getServiceDefHelper().expandImpliedAccessGrants(grant.getPermissions());
this.grant = grant;
this.permissions = policyEngine.getServiceDefHelper().expandImpliedAccessGrants(grant.getPermissions());
this.actionMatcher = new RangerActionListMatcher(grant.getActions());

if (grant.getResources() != null) {
for (String resource : grant.getResources()) {
Expand All @@ -187,7 +190,7 @@ public GrantEvaluator(RangerInlinePolicy.Grant grant) {
}

public boolean isAllowed(RangerAccessRequest request) {
boolean ret = isPrincipalMatch(request) && isPermissionMatch(request) && isResourceMatch(request);
boolean ret = isPrincipalMatch(request) && isPermissionMatch(request) && isActionMatch(request) && isResourceMatch(request);

LOG.debug("isAllowed(grant={}, request={}): ret={}", grant, request, ret);

Expand Down Expand Up @@ -238,6 +241,14 @@ private boolean isPermissionMatch(RangerAccessRequest request) {
return ret;
}

private boolean isActionMatch(RangerAccessRequest request) {
boolean ret = actionMatcher.isMatch(request != null ? request.getAction() : null);

LOG.debug("isActionMatch(grant={}, request={}): ret={}", grant, request, ret);

return ret;
}

private boolean isResourceMatch(RangerAccessRequest request) {
boolean ret = CollectionUtils.isEmpty(grant.getResources()); // match all resources

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.util;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class RangerActionListMatcher {
private final boolean allowAnyAction;
private final Set<String> exactActions = new HashSet<>();
private final String[] prefixActions;

public RangerActionListMatcher(Collection<String> actions) {
boolean allowAny = CollectionUtils.isEmpty(actions);
final List<String> tempPrefixList = new ArrayList<>();

if (!allowAny) {
for (String a : actions) {
final String action = StringUtils.trimToEmpty(a);

if (action.isEmpty()) {
continue;
}

if (action.equals("*")) {
allowAny = true;
exactActions.clear();
tempPrefixList.clear();
break;
}

if (action.endsWith("*")) {
final String prefix = StringUtils.trimToEmpty(action.substring(0, action.length() - 1)).toLowerCase(Locale.ROOT);

if (prefix.isEmpty()) {
allowAny = true;
exactActions.clear();
tempPrefixList.clear();
break;
}

tempPrefixList.add(prefix);
} else {
exactActions.add(action.toLowerCase(Locale.ROOT));
}
}
}

this.allowAnyAction = allowAny;

if (!tempPrefixList.isEmpty()) {
tempPrefixList.sort((s1, s2) -> Integer.compare(s1.length(), s2.length()));

List<String> optimizedPrefixes = new ArrayList<>();
for (String p : tempPrefixList) {
boolean isCovered = false;
for (String optPrefix : optimizedPrefixes) {
if (p.startsWith(optPrefix)) {
isCovered = true;
break;
}
}
if (!isCovered) {
optimizedPrefixes.add(p);
}
}
this.prefixActions = optimizedPrefixes.toArray(new String[0]);
} else {
this.prefixActions = new String[0];
}
}

public boolean isMatch(String requestAction) {
boolean ret = allowAnyAction;

if (!ret) {
// if action is not available, don't enforce action restrictions
if (StringUtils.isBlank(requestAction)) {
ret = true;
} else {
final String actionLower = requestAction.toLowerCase(Locale.ROOT);

if (exactActions.contains(actionLower)) {
ret = true;
} else {
for (String prefix : prefixActions) {
if (actionLower.startsWith(prefix)) {
ret = true;
break;
}
}
}
}
}

return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@
"label": "IP Address Range",
"description": "IP Address Range",
"uiHint" : "{ \"isMultiValue\":true }"
},
{
"itemId": 2,
"name": "action-matches",
"evaluator": "org.apache.ranger.plugin.conditionevaluator.RangerActionMatcher",
"label": "Action",
"description": "Action",
"uiHint": "{ \"isMultiValue\":true, \"options\": [\"*\", \"Get*\", \"List*\", \"Put*\", \"Delete*\", \"Create*\", \"GetObject\", \"GetObjectTagging\", \"PutObject\", \"PutObjectTagging\", \"ListBucket\", \"CreateBucket\", \"DeleteBucket\", \"GetBucketAcl\", \"PutBucketAcl\", \"ListBucketMultipartUploads\", \"DeleteObject\", \"ListAllMyBuckets\", \"ListMultipartUploadParts\", \"AbortMultipartUpload\", \"DeleteObjectTagging\"], \"actionRequirementsFile\": \"ozone\" }"
}
]
}
Loading