|
6 | 6 | */ |
7 | 7 | package org.elasticsearch.xpack.core.security.authz.permission; |
8 | 8 |
|
| 9 | +import org.apache.logging.log4j.LogManager; |
| 10 | +import org.apache.logging.log4j.Logger; |
9 | 11 | import org.apache.lucene.util.automaton.Automaton; |
10 | 12 | import org.apache.lucene.util.automaton.Operations; |
| 13 | +import org.apache.lucene.util.automaton.TooComplexToDeterminizeException; |
11 | 14 | import org.elasticsearch.action.admin.indices.mapping.put.TransportAutoPutMappingAction; |
12 | 15 | import org.elasticsearch.action.admin.indices.mapping.put.TransportPutMappingAction; |
13 | 16 | import org.elasticsearch.action.support.IndexComponentSelector; |
|
43 | 46 | import java.util.Set; |
44 | 47 | import java.util.concurrent.ConcurrentHashMap; |
45 | 48 | import java.util.function.BiPredicate; |
| 49 | +import java.util.function.Function; |
46 | 50 | import java.util.function.Predicate; |
47 | 51 | import java.util.function.Supplier; |
| 52 | +import java.util.stream.Collectors; |
48 | 53 |
|
49 | 54 | import static java.util.Collections.unmodifiableMap; |
50 | 55 |
|
|
54 | 59 | */ |
55 | 60 | public final class IndicesPermission { |
56 | 61 |
|
| 62 | + private final Logger logger = LogManager.getLogger(getClass()); |
| 63 | + |
57 | 64 | private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(IndicesPermission.class); |
58 | 65 |
|
59 | 66 | public static final IndicesPermission NONE = new IndicesPermission(new RestrictedIndices(Automatons.EMPTY), Group.EMPTY_ARRAY); |
@@ -330,11 +337,23 @@ public boolean checkResourcePrivileges( |
330 | 337 | combineIndexGroups && checkForIndexPatterns.stream().anyMatch(Automatons::isLuceneRegex), |
331 | 338 | IndexComponentSelector.FAILURES |
332 | 339 | ); |
333 | | - for (String forIndexPattern : checkForIndexPatterns) { |
334 | | - Automaton checkIndexAutomaton = Automatons.patterns(forIndexPattern); |
335 | | - if (false == allowRestrictedIndices && false == isConcreteRestrictedIndex(forIndexPattern)) { |
336 | | - checkIndexAutomaton = Automatons.minusAndMinimize(checkIndexAutomaton, restrictedIndices.getAutomaton()); |
337 | | - } |
| 340 | + Map<String, Automaton> checkIndexPatterns = checkForIndexPatterns.stream() |
| 341 | + .collect(Collectors.toMap(Function.identity(), pattern -> { |
| 342 | + try { |
| 343 | + Automaton automaton = Automatons.patterns(pattern); |
| 344 | + if (false == allowRestrictedIndices && false == isConcreteRestrictedIndex(pattern)) { |
| 345 | + automaton = Automatons.minusAndMinimize(automaton, restrictedIndices.getAutomaton()); |
| 346 | + } |
| 347 | + return automaton; |
| 348 | + } catch (TooComplexToDeterminizeException e) { |
| 349 | + final String text = pattern.length() > 260 ? Strings.cleanTruncate(pattern, 256) + "..." : pattern; |
| 350 | + logger.info("refusing to check privileges against complex index pattern [{}]", text); |
| 351 | + throw new IllegalArgumentException("the provided index pattern [" + text + "] is too complex to be evaluated", e); |
| 352 | + } |
| 353 | + })); |
| 354 | + for (var entry : checkIndexPatterns.entrySet()) { |
| 355 | + final String forIndexPattern = entry.getKey(); |
| 356 | + final Automaton checkIndexAutomaton = entry.getValue(); |
338 | 357 | if (false == Operations.isEmpty(checkIndexAutomaton)) { |
339 | 358 | Automaton allowedPrivilegesAutomatonForDataSelector = getIndexPrivilegesAutomaton( |
340 | 359 | indexGroupAutomatonsForDataSelector, |
|
0 commit comments