Skip to content

Commit 00da48a

Browse files
DmitryOlshanskyDmitry Olshansky
andauthored
[Optimization] Implement faster version of implies type perm (#198)
Co-authored-by: Dmitry Olshansky <[email protected]>
1 parent 0081f4d commit 00da48a

File tree

1 file changed

+26
-30
lines changed
  • src/main/java/com/amazon/opendistroforelasticsearch/security/securityconf

1 file changed

+26
-30
lines changed

src/main/java/com/amazon/opendistroforelasticsearch/security/securityconf/ConfigModelV7.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.elasticsearch.common.transport.TransportAddress;
4848

4949
import com.amazon.opendistroforelasticsearch.security.resolver.IndexResolverReplacer.Resolved;
50-
import com.amazon.opendistroforelasticsearch.security.securityconf.ConfigModelV6.SecurityRole;
5150
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.SecurityDynamicConfiguration;
5251
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.v7.ActionGroupsV7;
5352
import com.amazon.opendistroforelasticsearch.security.securityconf.impl.v7.RoleMappingsV7;
@@ -936,38 +935,35 @@ private static String toQuotedCommaSeparatedString(final Set<String> roles) {
936935
}));
937936
}
938937

939-
private static boolean impliesTypePerm(Set<IndexPattern> ipatterns, Resolved resolved, User user, String[] actions,
940-
IndexNameExpressionResolver resolver, ClusterService cs) {
941-
Set<String> matchingIndex = new HashSet<>(resolved.getAllIndices());
942-
943-
for (String in : resolved.getAllIndices()) {
944-
//find index patterns who are matching
945-
Set<String> matchingActions = new HashSet<>(Arrays.asList(actions));
946-
//Set<String> matchingTypes = new HashSet<>(resolved.getTypes(-));
947-
for (IndexPattern p : ipatterns) {
948-
if (WildcardMatcher.matchAny(p.getResolvedIndexPattern(user, resolver, cs), in)) {
949-
//per resolved index per pattern
950-
//for (String t : resolved.getTypes(-)) {
951-
//for (TypePerm tp : p.typePerms) {
952-
//if (WildcardMatcher.match(tp.typePattern, t)) {
953-
//matchingTypes.remove(t);
954-
for (String a : Arrays.asList(actions)) {
955-
if (WildcardMatcher.matchAny(p.perms, a)) {
956-
matchingActions.remove(a);
957-
}
958-
}
959-
//}
960-
//}
961-
//}
962-
}
963-
}
938+
private static final class IndexPatternsAndPermissions {
939+
private String[] pattern;
940+
private Set<String> perms;
941+
public IndexPatternsAndPermissions(String[] pattern, Set<String> perms) {
942+
this.pattern = pattern;
943+
this.perms = perms;
944+
}
964945

965-
if (matchingActions.isEmpty() /*&& matchingTypes.isEmpty()*/) {
966-
matchingIndex.remove(in);
967-
}
946+
public boolean matches(String index, String action) {
947+
return WildcardMatcher.matchAny(pattern, index) && WildcardMatcher.matchAny(perms, action);
968948
}
949+
}
969950

970-
return matchingIndex.isEmpty();
951+
private static boolean impliesTypePerm(Set<IndexPattern> ipatterns, Resolved resolved, User user, String[] requestedActions,
952+
IndexNameExpressionResolver resolver, ClusterService cs) {
953+
Set<String> resolvedRequestedIndices = resolved.getAllIndices();
954+
IndexPatternsAndPermissions[] indexPatternsAndPermissions = ipatterns
955+
.stream()
956+
.map(p -> new IndexPatternsAndPermissions(p.getResolvedIndexPattern(user, resolver, cs), p.perms))
957+
.toArray(IndexPatternsAndPermissions[]::new);
958+
return resolvedRequestedIndices
959+
.stream()
960+
.allMatch(index ->
961+
Arrays.stream(requestedActions).allMatch(action ->
962+
Arrays.stream(indexPatternsAndPermissions).anyMatch(ipap ->
963+
ipap.matches(index, action)
964+
)
965+
)
966+
);
971967
}
972968

973969

0 commit comments

Comments
 (0)