13
13
import org .elasticsearch .action .admin .indices .close .CloseIndexAction ;
14
14
import org .elasticsearch .action .admin .indices .create .AutoCreateAction ;
15
15
import org .elasticsearch .action .admin .indices .create .CreateIndexAction ;
16
- import org .elasticsearch .action .admin .indices .resolve .ResolveIndexAction ;
17
- import org .elasticsearch .xpack .core .action .CreateDataStreamAction ;
18
- import org .elasticsearch .xpack .core .action .DeleteDataStreamAction ;
19
- import org .elasticsearch .xpack .core .action .GetDataStreamAction ;
20
16
import org .elasticsearch .action .admin .indices .delete .DeleteIndexAction ;
21
17
import org .elasticsearch .action .admin .indices .get .GetIndexAction ;
22
18
import org .elasticsearch .action .admin .indices .mapping .get .GetFieldMappingsAction ;
23
19
import org .elasticsearch .action .admin .indices .mapping .get .GetMappingsAction ;
24
20
import org .elasticsearch .action .admin .indices .mapping .put .AutoPutMappingAction ;
21
+ import org .elasticsearch .action .admin .indices .resolve .ResolveIndexAction ;
25
22
import org .elasticsearch .action .admin .indices .settings .get .GetSettingsAction ;
26
23
import org .elasticsearch .action .admin .indices .validate .query .ValidateQueryAction ;
27
24
import org .elasticsearch .common .Strings ;
25
+ import org .elasticsearch .xpack .core .action .CreateDataStreamAction ;
26
+ import org .elasticsearch .xpack .core .action .DeleteDataStreamAction ;
27
+ import org .elasticsearch .xpack .core .action .GetDataStreamAction ;
28
28
import org .elasticsearch .xpack .core .ccr .action .ForgetFollowerAction ;
29
29
import org .elasticsearch .xpack .core .ccr .action .PutFollowAction ;
30
30
import org .elasticsearch .xpack .core .ccr .action .UnfollowAction ;
31
31
import org .elasticsearch .xpack .core .ilm .action .ExplainLifecycleAction ;
32
32
import org .elasticsearch .xpack .core .security .support .Automatons ;
33
33
34
34
import java .util .Arrays ;
35
+ import java .util .Collection ;
35
36
import java .util .Collections ;
36
37
import java .util .HashSet ;
37
38
import java .util .Locale ;
38
39
import java .util .Map ;
39
40
import java .util .Set ;
40
41
import java .util .concurrent .ConcurrentHashMap ;
41
42
import java .util .function .Predicate ;
43
+ import java .util .stream .Collectors ;
42
44
43
45
import static java .util .Map .entry ;
44
46
import static org .elasticsearch .xpack .core .security .support .Automatons .patterns ;
@@ -95,7 +97,7 @@ public final class IndexPrivilege extends Privilege {
95
97
public static final IndexPrivilege MAINTENANCE = new IndexPrivilege ("maintenance" , MAINTENANCE_AUTOMATON );
96
98
public static final IndexPrivilege AUTO_CONFIGURE = new IndexPrivilege ("auto_configure" , AUTO_CONFIGURE_AUTOMATON );
97
99
98
- private static final Map <String , IndexPrivilege > VALUES = Map .ofEntries (
100
+ private static final Map <String , IndexPrivilege > VALUES = sortByAccessLevel ( Map .ofEntries (
99
101
entry ("none" , NONE ),
100
102
entry ("all" , ALL ),
101
103
entry ("manage" , MANAGE ),
@@ -114,7 +116,7 @@ public final class IndexPrivilege extends Privilege {
114
116
entry ("manage_leader_index" , MANAGE_LEADER_INDEX ),
115
117
entry ("manage_ilm" , MANAGE_ILM ),
116
118
entry ("maintenance" , MAINTENANCE ),
117
- entry ("auto_configure" , AUTO_CONFIGURE ));
119
+ entry ("auto_configure" , AUTO_CONFIGURE ))) ;
118
120
119
121
public static final Predicate <String > ACTION_MATCHER = ALL .predicate ();
120
122
public static final Predicate <String > CREATE_INDEX_MATCHER = CREATE_INDEX .predicate ();
@@ -152,7 +154,7 @@ private static IndexPrivilege resolve(Set<String> name) {
152
154
if (ACTION_MATCHER .test (part )) {
153
155
actions .add (actionToPattern (part ));
154
156
} else {
155
- IndexPrivilege indexPrivilege = VALUES .get (part );
157
+ IndexPrivilege indexPrivilege = part == null ? null : VALUES .get (part );
156
158
if (indexPrivilege != null && size == 1 ) {
157
159
return indexPrivilege ;
158
160
} else if (indexPrivilege != null ) {
@@ -182,4 +184,16 @@ public static Set<String> names() {
182
184
return Collections .unmodifiableSet (VALUES .keySet ());
183
185
}
184
186
187
+ /**
188
+ * Returns the names of privileges that grant the specified action.
189
+ * @return A collection of names, ordered (to the extent possible) from least privileged (e.g. {@link #CREATE_DOC})
190
+ * to most privileged (e.g. {@link #ALL})
191
+ * @see Privilege#sortByAccessLevel
192
+ */
193
+ public static Collection <String > findPrivilegesThatGrant (String action ) {
194
+ return VALUES .entrySet ().stream ()
195
+ .filter (e -> e .getValue ().predicate .test (action ))
196
+ .map (e -> e .getKey ())
197
+ .collect (Collectors .toUnmodifiableList ());
198
+ }
185
199
}
0 commit comments