42
42
import java .util .Map ;
43
43
import java .util .Map .Entry ;
44
44
import java .util .Optional ;
45
- import java .util .Set ;
46
45
import java .util .function .Predicate ;
47
46
import java .util .stream .Collectors ;
48
47
import java .util .stream .Stream ;
@@ -66,10 +65,12 @@ public class SystemIndices {
66
65
TASKS_FEATURE_NAME , new Feature (TASKS_FEATURE_NAME , "Manages task results" , List .of (TASKS_DESCRIPTOR ))
67
66
);
68
67
69
- private final CharacterRunAutomaton systemIndexAutomaton ;
70
- private final CharacterRunAutomaton systemDataStreamIndicesAutomaton ;
68
+ private final Automaton systemNameAutomaton ;
71
69
private final CharacterRunAutomaton netNewSystemIndexAutomaton ;
72
- private final Predicate <String > systemDataStreamAutomaton ;
70
+ private final CharacterRunAutomaton systemNameRunAutomaton ;
71
+ private final CharacterRunAutomaton systemIndexRunAutomaton ;
72
+ private final CharacterRunAutomaton systemDataStreamIndicesRunAutomaton ;
73
+ private final Predicate <String > systemDataStreamPredicate ;
73
74
private final Map <String , Feature > featureDescriptors ;
74
75
private final Map <String , CharacterRunAutomaton > productToSystemIndicesMatcher ;
75
76
private final ExecutorSelector executorSelector ;
@@ -83,12 +84,19 @@ public SystemIndices(Map<String, Feature> pluginAndModulesDescriptors) {
83
84
featureDescriptors = buildSystemIndexDescriptorMap (pluginAndModulesDescriptors );
84
85
checkForOverlappingPatterns (featureDescriptors );
85
86
checkForDuplicateAliases (this .getSystemIndexDescriptors ());
86
- this .systemIndexAutomaton = buildIndexCharacterRunAutomaton (featureDescriptors );
87
+ Automaton systemIndexAutomata = buildIndexAutomaton (featureDescriptors );
88
+ this .systemIndexRunAutomaton = new CharacterRunAutomaton (systemIndexAutomata );
89
+ Automaton systemDataStreamIndicesAutomata = buildDataStreamBackingIndicesAutomaton (featureDescriptors );
90
+ this .systemDataStreamIndicesRunAutomaton = new CharacterRunAutomaton (systemDataStreamIndicesAutomata );
91
+ this .systemDataStreamPredicate = buildDataStreamNamePredicate (featureDescriptors );
87
92
this .netNewSystemIndexAutomaton = buildNetNewIndexCharacterRunAutomaton (featureDescriptors );
88
- this .systemDataStreamIndicesAutomaton = buildDataStreamBackingIndicesAutomaton (featureDescriptors );
89
- this .systemDataStreamAutomaton = buildDataStreamNamePredicate (featureDescriptors );
90
93
this .productToSystemIndicesMatcher = getProductToSystemIndicesMap (featureDescriptors );
91
94
this .executorSelector = new ExecutorSelector (this );
95
+ this .systemNameAutomaton = MinimizationOperations .minimize (
96
+ Operations .union (List .of (systemIndexAutomata , systemDataStreamIndicesAutomata , buildDataStreamAutomaton (featureDescriptors ))),
97
+ Integer .MAX_VALUE
98
+ );
99
+ this .systemNameRunAutomaton = new CharacterRunAutomaton (systemNameAutomaton );
92
100
}
93
101
94
102
private static void checkForDuplicateAliases (Collection <SystemIndexDescriptor > descriptors ) {
@@ -150,7 +158,7 @@ private static Map<String, CharacterRunAutomaton> getProductToSystemIndicesMap(M
150
158
* is checked against index names, aliases, data stream names, and the names of indices that back a system data stream.
151
159
*/
152
160
public boolean isSystemName (String name ) {
153
- return isSystemIndex ( name ) || isSystemDataStream ( name ) || isSystemIndexBackingDataStream (name );
161
+ return systemNameRunAutomaton . run (name );
154
162
}
155
163
156
164
/**
@@ -169,22 +177,30 @@ public boolean isSystemIndex(Index index) {
169
177
* @return true if the index name matches a pattern from a {@link SystemIndexDescriptor}
170
178
*/
171
179
public boolean isSystemIndex (String indexName ) {
172
- return systemIndexAutomaton .run (indexName );
180
+ return systemIndexRunAutomaton .run (indexName );
173
181
}
174
182
175
183
/**
176
184
* Determines whether the provided name matches that of a system data stream that has been defined by a
177
185
* {@link SystemDataStreamDescriptor}
178
186
*/
179
187
public boolean isSystemDataStream (String name ) {
180
- return systemDataStreamAutomaton .test (name );
188
+ return systemDataStreamPredicate .test (name );
181
189
}
182
190
183
191
/**
184
192
* Determines whether the provided name matches that of an index that backs a system data stream.
185
193
*/
186
194
public boolean isSystemIndexBackingDataStream (String name ) {
187
- return systemDataStreamIndicesAutomaton .run (name );
195
+ return systemDataStreamIndicesRunAutomaton .run (name );
196
+ }
197
+
198
+ /**
199
+ * @return An {@link Automaton} that tests whether strings are names of system indices, aliases, or
200
+ * data streams.
201
+ */
202
+ public Automaton getSystemNameAutomaton () {
203
+ return systemNameAutomaton ;
188
204
}
189
205
190
206
public boolean isNetNewSystemIndex (String indexName ) {
@@ -293,11 +309,11 @@ public Map<String, Feature> getFeatures() {
293
309
return featureDescriptors ;
294
310
}
295
311
296
- private static CharacterRunAutomaton buildIndexCharacterRunAutomaton (Map <String , Feature > descriptors ) {
312
+ private static Automaton buildIndexAutomaton (Map <String , Feature > descriptors ) {
297
313
Optional <Automaton > automaton = descriptors .values ().stream ()
298
314
.map (SystemIndices ::featureToIndexAutomaton )
299
315
.reduce (Operations ::union );
300
- return new CharacterRunAutomaton ( MinimizationOperations .minimize (automaton .orElse (EMPTY ), Integer .MAX_VALUE ) );
316
+ return MinimizationOperations .minimize (automaton .orElse (EMPTY ), Integer .MAX_VALUE );
301
317
}
302
318
303
319
private static CharacterRunAutomaton buildNetNewIndexCharacterRunAutomaton (Map <String , Feature > featureDescriptors ) {
@@ -317,19 +333,26 @@ private static Automaton featureToIndexAutomaton(Feature feature) {
317
333
return systemIndexAutomaton .orElse (EMPTY );
318
334
}
319
335
320
- private static Predicate < String > buildDataStreamNamePredicate (Map <String , Feature > descriptors ) {
321
- Set < String > systemDataStreamNames = descriptors .values ().stream ()
336
+ private static Automaton buildDataStreamAutomaton (Map <String , Feature > descriptors ) {
337
+ Optional < Automaton > automaton = descriptors .values ().stream ()
322
338
.flatMap (feature -> feature .getDataStreamDescriptors ().stream ())
323
339
.map (SystemDataStreamDescriptor ::getDataStreamName )
324
- .collect (Collectors .toUnmodifiableSet ());
325
- return systemDataStreamNames ::contains ;
340
+ .map (dsName -> SystemIndexDescriptor .buildAutomaton (dsName , null ))
341
+ .reduce (Operations ::union );
342
+
343
+ return automaton .isPresent () ? MinimizationOperations .minimize (automaton .get (), Integer .MAX_VALUE ) : EMPTY ;
344
+ }
345
+
346
+ private static Predicate <String > buildDataStreamNamePredicate (Map <String , Feature > descriptors ) {
347
+ CharacterRunAutomaton characterRunAutomaton = new CharacterRunAutomaton (buildDataStreamAutomaton (descriptors ));
348
+ return characterRunAutomaton ::run ;
326
349
}
327
350
328
- private static CharacterRunAutomaton buildDataStreamBackingIndicesAutomaton (Map <String , Feature > descriptors ) {
351
+ private static Automaton buildDataStreamBackingIndicesAutomaton (Map <String , Feature > descriptors ) {
329
352
Optional <Automaton > automaton = descriptors .values ().stream ()
330
353
.map (SystemIndices ::featureToDataStreamBackingIndicesAutomaton )
331
354
.reduce (Operations ::union );
332
- return new CharacterRunAutomaton (automaton .orElse (EMPTY ));
355
+ return MinimizationOperations . minimize (automaton .orElse (EMPTY ), Integer . MAX_VALUE );
333
356
}
334
357
335
358
private static Automaton featureToDataStreamBackingIndicesAutomaton (Feature feature ) {
@@ -343,7 +366,7 @@ private static Automaton featureToDataStreamBackingIndicesAutomaton(Feature feat
343
366
}
344
367
345
368
public SystemDataStreamDescriptor validateDataStreamAccess (String dataStreamName , ThreadContext threadContext ) {
346
- if (systemDataStreamAutomaton .test (dataStreamName )) {
369
+ if (systemDataStreamPredicate .test (dataStreamName )) {
347
370
SystemDataStreamDescriptor dataStreamDescriptor = featureDescriptors .values ().stream ()
348
371
.flatMap (feature -> feature .getDataStreamDescriptors ().stream ())
349
372
.filter (descriptor -> descriptor .getDataStreamName ().equals (dataStreamName ))
@@ -405,6 +428,7 @@ IllegalArgumentException dataStreamAccessException(@Nullable String product, Str
405
428
/**
406
429
* Determines what level of system index access should be allowed in the current context.
407
430
*
431
+ * @param threadContext the current thread context that has headers associated with the current request
408
432
* @return {@link SystemIndexAccessLevel#ALL} if unrestricted system index access should be allowed,
409
433
* {@link SystemIndexAccessLevel#RESTRICTED} if a subset of system index access should be allowed, or
410
434
* {@link SystemIndexAccessLevel#NONE} if no system index access should be allowed.
0 commit comments