Skip to content

Commit 0b09483

Browse files
committed
Tweak interface
1 parent 3b6e030 commit 0b09483

File tree

7 files changed

+76
-69
lines changed

7 files changed

+76
-69
lines changed

plugins/examples/security-authorization-engine/src/main/java/org/elasticsearch/example/CustomAuthorizationEngine.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.example;
1111

1212
import org.elasticsearch.action.ActionListener;
13+
import org.elasticsearch.action.support.IndexComponentSelector;
1314
import org.elasticsearch.action.support.SubscribableListener;
1415
import org.elasticsearch.cluster.metadata.IndexAbstraction;
1516
import org.elasticsearch.cluster.metadata.ProjectMetadata;
@@ -119,19 +120,19 @@ public void loadAuthorizedIndices(
119120
) {
120121
if (isSuperuser(requestInfo.getAuthentication().getEffectiveSubject().getUser())) {
121122
listener.onResponse(new AuthorizedIndices() {
122-
public Set<String> all(@Nullable String selector) {
123+
public Set<String> all(IndexComponentSelector selector) {
123124
return () -> indicesLookup.keySet();
124125
}
125-
public boolean check(String name, @Nullable String selector) {
126+
public boolean check(String name, IndexComponentSelector selector) {
126127
return indicesLookup.containsKey(name);
127128
}
128129
});
129130
} else {
130131
listener.onResponse(new AuthorizedIndices() {
131-
public Set<String> all(@Nullable String selector) {
132+
public Set<String> all(IndexComponentSelector selector) {
132133
return () -> Set.of();
133134
}
134-
public boolean check(String name, @Nullable String selector) {
135+
public boolean check(String name, IndexComponentSelector selector) {
135136
return false;
136137
}
137138
});

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public List<String> resolveIndexAbstractions(
3737
Iterable<String> indices,
3838
IndicesOptions indicesOptions,
3939
ProjectMetadata projectMetadata,
40-
Function<String, Set<String>> allAuthorizedAndAvailableBySelector,
41-
BiPredicate<String, String> isAuthorized,
40+
Function<IndexComponentSelector, Set<String>> allAuthorizedAndAvailableBySelector,
41+
BiPredicate<String, IndexComponentSelector> isAuthorized,
4242
boolean includeDataStreams
4343
) {
4444
List<String> finalIndices = new ArrayList<>();
@@ -63,6 +63,7 @@ public List<String> resolveIndexAbstractions(
6363
+ "]"
6464
);
6565
}
66+
IndexComponentSelector selector = IndexComponentSelector.getByKeyOrThrow(selectorString);
6667
indexAbstraction = expressionAndSelector.v1();
6768

6869
// we always need to check for date math expressions
@@ -71,7 +72,7 @@ public List<String> resolveIndexAbstractions(
7172
if (indicesOptions.expandWildcardExpressions() && Regex.isSimpleMatchPattern(indexAbstraction)) {
7273
wildcardSeen = true;
7374
Set<String> resolvedIndices = new HashSet<>();
74-
for (String authorizedIndex : allAuthorizedAndAvailableBySelector.apply(selectorString)) {
75+
for (String authorizedIndex : allAuthorizedAndAvailableBySelector.apply(selector)) {
7576
if (Regex.simpleMatch(indexAbstraction, authorizedIndex)
7677
&& isIndexVisible(
7778
indexAbstraction,
@@ -102,7 +103,7 @@ && isIndexVisible(
102103
resolveSelectorsAndCollect(indexAbstraction, selectorString, indicesOptions, resolvedIndices, projectMetadata);
103104
if (minus) {
104105
finalIndices.removeAll(resolvedIndices);
105-
} else if (indicesOptions.ignoreUnavailable() == false || isAuthorized.test(indexAbstraction, selectorString)) {
106+
} else if (indicesOptions.ignoreUnavailable() == false || isAuthorized.test(indexAbstraction, selector)) {
106107
// Unauthorized names are considered unavailable, so if `ignoreUnavailable` is `true` they should be silently
107108
// discarded from the `finalIndices` list. Other "ways of unavailable" must be handled by the action
108109
// handler, see: https://github.com/elastic/elasticsearch/issues/90215

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ interface AuthorizedIndices {
293293
* at a fixed point in time (for a single cluster state view).
294294
* The result is cached and subsequent calls to this method are idempotent.
295295
*/
296-
Set<String> all(@Nullable String selector);
296+
Set<String> all(IndexComponentSelector selector);
297297

298298
/**
299299
* Checks if an index-like resource name is authorized, for an action by a user. The resource might or might not exist.
300300
*/
301-
boolean check(String name, @Nullable String selector);
301+
boolean check(String name, IndexComponentSelector selector);
302302
}
303303

304304
/**

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ ResolvedIndices resolveIndicesAndAliases(
322322
);
323323
}
324324
if (indicesOptions.expandWildcardExpressions()) {
325-
for (String authorizedIndex : authorizedIndices.all(allIndicesPatternSelector)) {
325+
for (String authorizedIndex : authorizedIndices.all(
326+
IndexComponentSelector.getByKeyOrThrow(allIndicesPatternSelector)
327+
)) {
326328
if (IndexAbstractionResolver.isIndexVisible(
327329
"*",
328330
allIndicesPatternSelector,
@@ -432,7 +434,7 @@ ResolvedIndices resolveIndicesAndAliases(
432434
*/
433435
static String getPutMappingIndexOrAlias(
434436
PutMappingRequest request,
435-
BiPredicate<String, String> isAuthorized,
437+
BiPredicate<String, IndexComponentSelector> isAuthorized,
436438
ProjectMetadata projectMetadata
437439
) {
438440
final String concreteIndexName = request.getConcreteIndex().getName();
@@ -451,7 +453,7 @@ static String getPutMappingIndexOrAlias(
451453
+ "], but a concrete index is expected"
452454
);
453455
// we know this is implicit data access (as opposed to another selector) so the default selector check is correct
454-
} else if (isAuthorized.test(concreteIndexName, null)) {
456+
} else if (isAuthorized.test(concreteIndexName, IndexComponentSelector.DATA)) {
455457
// user is authorized to put mappings for this index
456458
resolvedAliasOrIndex = concreteIndexName;
457459
} else {
@@ -462,7 +464,7 @@ static String getPutMappingIndexOrAlias(
462464
if (aliasMetadata != null) {
463465
Optional<String> foundAlias = aliasMetadata.stream().map(AliasMetadata::alias).filter(aliasName -> {
464466
// we know this is implicit data access (as opposed to another selector) so the default selector check is correct
465-
if (false == isAuthorized.test(aliasName, null)) {
467+
if (false == isAuthorized.test(aliasName, IndexComponentSelector.DATA)) {
466468
return false;
467469
}
468470
IndexAbstraction alias = projectMetadata.getIndicesLookup().get(aliasName);
@@ -490,7 +492,7 @@ private static List<String> loadAuthorizedAliases(
490492
) {
491493
List<String> authorizedAliases = new ArrayList<>();
492494
SortedMap<String, IndexAbstraction> existingAliases = projectMetadata.getIndicesLookup();
493-
for (String authorizedIndex : authorizedIndices.all(null)) {
495+
for (String authorizedIndex : authorizedIndices.all(IndexComponentSelector.DATA)) {
494496
IndexAbstraction indexAbstraction = existingAliases.get(authorizedIndex);
495497
if (indexAbstraction != null && indexAbstraction.getType() == IndexAbstraction.Type.ALIAS) {
496498
authorizedAliases.add(authorizedIndex);

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.elasticsearch.common.settings.Settings;
4444
import org.elasticsearch.common.util.CachedSupplier;
4545
import org.elasticsearch.common.util.set.Sets;
46-
import org.elasticsearch.core.Nullable;
4746
import org.elasticsearch.index.Index;
4847
import org.elasticsearch.index.shard.ShardId;
4948
import org.elasticsearch.transport.TransportActionProxy;
@@ -929,9 +928,8 @@ static AuthorizedIndices resolveAuthorizedIndicesFromRole(
929928
}
930929
}
931930
return indicesAndAliases;
932-
}, (name, selectorString) -> {
931+
}, (name, selector) -> {
933932
final IndexAbstraction indexAbstraction = lookup.get(name);
934-
final IndexComponentSelector selector = IndexComponentSelector.getByKeyOrThrow(selectorString);
935933
if (indexAbstraction == null) {
936934
// test access (by name) to a resource that does not currently exist
937935
// the action handler must handle the case of accessing resources that do not exist
@@ -1077,27 +1075,27 @@ static final class AuthorizedIndices implements AuthorizationEngine.AuthorizedIn
10771075

10781076
private final CachedSupplier<Set<String>> authorizedAndAvailableSupplier;
10791077
private final CachedSupplier<Set<String>> failureStoreAuthorizedAndAvailableSupplier;
1080-
private final BiPredicate<String, String> isAuthorizedPredicate;
1078+
private final BiPredicate<String, IndexComponentSelector> isAuthorizedPredicate;
10811079

10821080
AuthorizedIndices(
10831081
Supplier<Set<String>> authorizedAndAvailableSupplier,
10841082
Supplier<Set<String>> failureStoreAuthorizedAndAvailableSupplier,
1085-
BiPredicate<String, String> isAuthorizedPredicate
1083+
BiPredicate<String, IndexComponentSelector> isAuthorizedPredicate
10861084
) {
10871085
this.authorizedAndAvailableSupplier = CachedSupplier.wrap(authorizedAndAvailableSupplier);
10881086
this.failureStoreAuthorizedAndAvailableSupplier = CachedSupplier.wrap(failureStoreAuthorizedAndAvailableSupplier);
10891087
this.isAuthorizedPredicate = Objects.requireNonNull(isAuthorizedPredicate);
10901088
}
10911089

10921090
@Override
1093-
public Set<String> all(@Nullable String selector) {
1094-
return IndexComponentSelector.FAILURES.equals(IndexComponentSelector.getByKeyOrThrow(selector))
1091+
public Set<String> all(IndexComponentSelector selector) {
1092+
return IndexComponentSelector.FAILURES.equals(selector)
10951093
? failureStoreAuthorizedAndAvailableSupplier.get()
10961094
: authorizedAndAvailableSupplier.get();
10971095
}
10981096

10991097
@Override
1100-
public boolean check(String name, @Nullable String selector) {
1098+
public boolean check(String name, IndexComponentSelector selector) {
11011099
return isAuthorizedPredicate.test(name, selector);
11021100
}
11031101
}

0 commit comments

Comments
 (0)