Skip to content

Commit 3202566

Browse files
committed
Try handling wildcards
1 parent 61ae8ac commit 3202566

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323
import java.util.Set;
2424
import java.util.function.BiPredicate;
25-
import java.util.function.Supplier;
25+
import java.util.function.Function;
2626

2727
public class IndexAbstractionResolver {
2828

@@ -32,13 +32,11 @@ public IndexAbstractionResolver(IndexNameExpressionResolver indexNameExpressionR
3232
this.indexNameExpressionResolver = indexNameExpressionResolver;
3333
}
3434

35-
public record IndexNameWithSelector(String index, @Nullable String selector) {}
36-
3735
public List<String> resolveIndexAbstractions(
3836
Iterable<String> indices,
3937
IndicesOptions indicesOptions,
4038
Metadata metadata,
41-
Supplier<Set<String>> allAuthorizedAndAvailable,
39+
Function<String, Set<String>> allAuthorizedAndAvailable,
4240
BiPredicate<String, String> isAuthorized,
4341
boolean includeDataStreams
4442
) {
@@ -72,7 +70,7 @@ public List<String> resolveIndexAbstractions(
7270
if (indicesOptions.expandWildcardExpressions() && Regex.isSimpleMatchPattern(indexAbstraction)) {
7371
wildcardSeen = true;
7472
Set<String> resolvedIndices = new HashSet<>();
75-
for (String authorizedIndex : allAuthorizedAndAvailable.get()) {
73+
for (String authorizedIndex : allAuthorizedAndAvailable.apply(selectorString)) {
7674
if (Regex.simpleMatch(indexAbstraction, authorizedIndex)
7775
&& isIndexVisible(
7876
indexAbstraction,

server/src/test/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolverTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private List<String> resolveAbstractions(List<String> expressions, IndicesOption
240240
expressions,
241241
indicesOptions,
242242
metadata,
243-
mask,
243+
(selector) -> mask.get(),
244244
(idx, selector) -> true,
245245
true
246246
);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ interface AuthorizedIndices {
292292
*/
293293
Supplier<Set<String>> all();
294294

295+
Set<String> all(String selector);
296+
295297
/**
296298
* Checks if an index-like resource name is authorized, for an action by a user. The resource might or might not exist.
297299
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ ResolvedIndices resolveIndicesAndAliases(
351351
split.getLocal(),
352352
indicesOptions,
353353
metadata,
354-
authorizedIndices.all(),
354+
authorizedIndices::all,
355355
authorizedIndices::check,
356356
indicesRequest.includeDataStreams()
357357
);

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import org.elasticsearch.common.bytes.BytesReference;
4040
import org.elasticsearch.common.regex.Regex;
4141
import org.elasticsearch.common.settings.Settings;
42-
import org.elasticsearch.common.util.CachedSupplier;
4342
import org.elasticsearch.common.util.set.Sets;
43+
import org.elasticsearch.core.Nullable;
4444
import org.elasticsearch.index.Index;
4545
import org.elasticsearch.index.shard.ShardId;
4646
import org.elasticsearch.transport.TransportActionProxy;
@@ -108,6 +108,7 @@
108108
import java.util.TreeSet;
109109
import java.util.function.BiPredicate;
110110
import java.util.function.Consumer;
111+
import java.util.function.Function;
111112
import java.util.function.Predicate;
112113
import java.util.function.Supplier;
113114
import java.util.stream.Collectors;
@@ -870,13 +871,13 @@ static AuthorizedIndices resolveAuthorizedIndicesFromRole(
870871
TransportRequest request = requestInfo.getRequest();
871872
final boolean includeDataStreams = (request instanceof IndicesRequest) && ((IndicesRequest) request).includeDataStreams();
872873
// TODO need a function not a supplier
873-
return new AuthorizedIndices(() -> {
874+
return new AuthorizedIndices((selector) -> {
874875
Consumer<Collection<String>> timeChecker = timerSupplier.get();
875876
Set<String> indicesAndAliases = new HashSet<>();
876877
// TODO: can this be done smarter? I think there are usually more indices/aliases in the cluster then indices defined a roles?
877878
if (includeDataStreams) {
878879
for (IndexAbstraction indexAbstraction : lookup.values()) {
879-
if (predicate.test(indexAbstraction)) {
880+
if (predicate.test(indexAbstraction, selector)) {
880881
indicesAndAliases.add(indexAbstraction.getName());
881882
if (indexAbstraction.getType() == IndexAbstraction.Type.DATA_STREAM) {
882883
// add data stream and its backing indices for any authorized data streams
@@ -1037,17 +1038,26 @@ private static boolean isAsyncRelatedAction(String action) {
10371038

10381039
static final class AuthorizedIndices implements AuthorizationEngine.AuthorizedIndices {
10391040

1040-
private final CachedSupplier<Set<String>> allAuthorizedAndAvailableSupplier;
1041+
// TODO results need to be cached
1042+
private final Function<String, Set<String>> allAuthorizedAndAvailableBySelector;
10411043
private final BiPredicate<String, String> isAuthorizedPredicate;
10421044

1043-
AuthorizedIndices(Supplier<Set<String>> allAuthorizedAndAvailableSupplier, BiPredicate<String, String> isAuthorizedPredicate) {
1044-
this.allAuthorizedAndAvailableSupplier = CachedSupplier.wrap(allAuthorizedAndAvailableSupplier);
1045+
AuthorizedIndices(
1046+
Function<String, Set<String>> allAuthorizedAndAvailableBySelector,
1047+
BiPredicate<String, String> isAuthorizedPredicate
1048+
) {
1049+
this.allAuthorizedAndAvailableBySelector = allAuthorizedAndAvailableBySelector;
10451050
this.isAuthorizedPredicate = Objects.requireNonNull(isAuthorizedPredicate);
10461051
}
10471052

10481053
@Override
10491054
public Supplier<Set<String>> all() {
1050-
return allAuthorizedAndAvailableSupplier;
1055+
return () -> all(null);
1056+
}
1057+
1058+
@Override
1059+
public Set<String> all(@Nullable String selector) {
1060+
return allAuthorizedAndAvailableBySelector.apply(selector);
10511061
}
10521062

10531063
@Override

0 commit comments

Comments
 (0)