Skip to content

Commit e7f84d9

Browse files
committed
Maybe HasPrivileges
1 parent ed85b19 commit e7f84d9

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,15 @@ public boolean checkResourcePrivileges(
296296
Set<String> checkForIndexPatterns,
297297
boolean allowRestrictedIndices,
298298
Set<String> checkForPrivileges,
299+
@Nullable IndexComponentSelector selector,
299300
@Nullable ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder
300301
) {
301302
return checkResourcePrivileges(
302303
checkForIndexPatterns,
303304
allowRestrictedIndices,
304305
checkForPrivileges,
305306
false,
307+
selector,
306308
resourcePrivilegesMapBuilder
307309
);
308310
}
@@ -326,11 +328,13 @@ public boolean checkResourcePrivileges(
326328
boolean allowRestrictedIndices,
327329
Set<String> checkForPrivileges,
328330
boolean combineIndexGroups,
331+
@Nullable IndexComponentSelector selector,
329332
@Nullable ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder
330333
) {
331334
boolean allMatch = true;
332335
Map<Automaton, Automaton> indexGroupAutomatons = indexGroupAutomatons(
333-
combineIndexGroups && checkForIndexPatterns.stream().anyMatch(Automatons::isLuceneRegex)
336+
combineIndexGroups && checkForIndexPatterns.stream().anyMatch(Automatons::isLuceneRegex),
337+
selector
334338
);
335339
for (String forIndexPattern : checkForIndexPatterns) {
336340
Automaton checkIndexAutomaton = Automatons.patterns(forIndexPattern);
@@ -390,7 +394,8 @@ public boolean checkResourcePrivileges(
390394
public Automaton allowedActionsMatcher(String index) {
391395
List<Automaton> automatonList = new ArrayList<>();
392396
for (Group group : groups) {
393-
if (group.indexNameMatcher.test(index)) {
397+
// TODO failure store?
398+
if (group.checkIndex(index) && group.checkSelector(null)) {
394399
automatonList.add(group.privilege.getAutomaton());
395400
}
396401
}
@@ -809,10 +814,13 @@ private static boolean containsPrivilegeThatGrantsMappingUpdatesForBwc(Group gro
809814
*
810815
* @return a map of all index and privilege pattern automatons
811816
*/
812-
private Map<Automaton, Automaton> indexGroupAutomatons(boolean combine) {
817+
private Map<Automaton, Automaton> indexGroupAutomatons(boolean combine, @Nullable IndexComponentSelector selector) {
813818
// Map of privilege automaton object references (cached by IndexPrivilege::CACHE)
814819
Map<Automaton, Automaton> allAutomatons = new HashMap<>();
815820
for (Group group : groups) {
821+
if (false == group.checkSelector(selector)) {
822+
continue;
823+
}
816824
Automaton indexAutomaton = group.getIndexMatcherAutomaton();
817825
allAutomatons.compute(
818826
group.privilege().getAutomaton(),

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRole.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.logging.log4j.Logger;
1212
import org.apache.lucene.util.automaton.Automaton;
1313
import org.elasticsearch.TransportVersion;
14+
import org.elasticsearch.action.support.IndexComponentSelector;
1415
import org.elasticsearch.cluster.metadata.Metadata;
1516
import org.elasticsearch.common.Strings;
1617
import org.elasticsearch.core.Nullable;
@@ -239,12 +240,14 @@ public boolean checkIndicesPrivileges(
239240
Set<String> checkForIndexPatterns,
240241
boolean allowRestrictedIndices,
241242
Set<String> checkForPrivileges,
243+
@Nullable IndexComponentSelector selector,
242244
@Nullable ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder
243245
) {
244246
boolean baseRoleCheck = baseRole.checkIndicesPrivileges(
245247
checkForIndexPatterns,
246248
allowRestrictedIndices,
247249
checkForPrivileges,
250+
selector,
248251
resourcePrivilegesMapBuilder
249252
);
250253
if (false == baseRoleCheck && null == resourcePrivilegesMapBuilder) {
@@ -255,6 +258,7 @@ public boolean checkIndicesPrivileges(
255258
checkForIndexPatterns,
256259
allowRestrictedIndices,
257260
checkForPrivileges,
261+
selector,
258262
resourcePrivilegesMapBuilder
259263
);
260264
return baseRoleCheck && limitedByRoleCheck;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/Role.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,25 @@ boolean checkIndicesPrivileges(
128128
Set<String> checkForIndexPatterns,
129129
boolean allowRestrictedIndices,
130130
Set<String> checkForPrivileges,
131+
@Nullable IndexComponentSelector selector,
131132
@Nullable ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder
132133
);
133134

135+
default boolean checkIndicesPrivileges(
136+
Set<String> checkForIndexPatterns,
137+
boolean allowRestrictedIndices,
138+
Set<String> checkForPrivileges,
139+
@Nullable ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder
140+
) {
141+
return checkIndicesPrivileges(
142+
checkForIndexPatterns,
143+
allowRestrictedIndices,
144+
checkForPrivileges,
145+
null,
146+
resourcePrivilegesMapBuilder
147+
);
148+
}
149+
134150
/**
135151
* Check if cluster permissions allow for the given action in the context of given
136152
* authentication.

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/SimpleRole.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.apache.lucene.util.automaton.Automaton;
1010
import org.elasticsearch.TransportVersion;
11+
import org.elasticsearch.action.support.IndexComponentSelector;
1112
import org.elasticsearch.cluster.metadata.Metadata;
1213
import org.elasticsearch.common.bytes.BytesReference;
1314
import org.elasticsearch.common.cache.Cache;
@@ -154,12 +155,14 @@ public boolean checkIndicesPrivileges(
154155
Set<String> checkForIndexPatterns,
155156
boolean allowRestrictedIndices,
156157
Set<String> checkForPrivileges,
158+
@Nullable IndexComponentSelector selector,
157159
@Nullable ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder
158160
) {
159161
return indices.checkResourcePrivileges(
160162
checkForIndexPatterns,
161163
allowRestrictedIndices,
162164
checkForPrivileges,
165+
selector,
163166
resourcePrivilegesMapBuilder
164167
);
165168
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivileges.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ private static boolean requestIndexPatternsAllowed(
653653
String[] requestIndexPatterns,
654654
String[] privileges
655655
) {
656-
return indicesPermission.checkResourcePrivileges(Set.of(requestIndexPatterns), false, Set.of(privileges), true, null);
656+
return indicesPermission.checkResourcePrivileges(Set.of(requestIndexPatterns), false, Set.of(privileges), true, null, null);
657657
}
658658

659659
private static boolean hasNonIndexPrivileges(RoleDescriptor roleDescriptor) {

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/RBACEngine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ public void checkPrivileges(
660660
Sets.newHashSet(check.getIndices()),
661661
check.allowRestrictedIndices(),
662662
Sets.newHashSet(check.getPrivileges()),
663+
null,
663664
combineIndicesResourcePrivileges
664665
);
665666
allMatch = allMatch && privilegesGranted;

0 commit comments

Comments
 (0)