Skip to content

Commit d8ed014

Browse files
committed
Toss supplier
1 parent 084ac8c commit d8ed014

File tree

7 files changed

+69
-68
lines changed

7 files changed

+69
-68
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Set;
2525
import java.util.function.BiPredicate;
2626
import java.util.function.Function;
27-
import java.util.function.Supplier;
2827

2928
public class IndexAbstractionResolver {
3029

@@ -38,7 +37,7 @@ public List<String> resolveIndexAbstractions(
3837
Iterable<String> indices,
3938
IndicesOptions indicesOptions,
4039
ProjectMetadata projectMetadata,
41-
Function<String, Supplier<Set<String>>> allAuthorizedAndAvailableBySelector,
40+
Function<String, Set<String>> allAuthorizedAndAvailableBySelector,
4241
BiPredicate<String, String> isAuthorized,
4342
boolean includeDataStreams
4443
) {
@@ -72,7 +71,7 @@ public List<String> resolveIndexAbstractions(
7271
if (indicesOptions.expandWildcardExpressions() && Regex.isSimpleMatchPattern(indexAbstraction)) {
7372
wildcardSeen = true;
7473
Set<String> resolvedIndices = new HashSet<>();
75-
for (String authorizedIndex : allAuthorizedAndAvailableBySelector.apply(selectorString).get()) {
74+
for (String authorizedIndex : allAuthorizedAndAvailableBySelector.apply(selectorString)) {
7675
if (Regex.simpleMatch(indexAbstraction, authorizedIndex)
7776
&& isIndexVisible(
7877
indexAbstraction,

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
@@ -39,7 +39,6 @@
3939
import java.util.Map;
4040
import java.util.Objects;
4141
import java.util.Set;
42-
import java.util.function.Supplier;
4342
import java.util.stream.Collectors;
4443

4544
import static org.elasticsearch.action.ValidateActions.addValidationError;
@@ -290,8 +289,9 @@ interface AuthorizedIndices {
290289
/**
291290
* Returns all the index-like resource names that are available and accessible for an action type and selector by a user,
292291
* at a fixed point in time (for a single cluster state view).
292+
* The result is cached and subsequent calls to this method are idempotent.
293293
*/
294-
Supplier<Set<String>> all(@Nullable String selector);
294+
Set<String> all(@Nullable String selector);
295295

296296
/**
297297
* Checks if an index-like resource name is authorized, for an action by a user. The resource might or might not exist.

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
@@ -49,7 +49,6 @@
4949
import java.util.SortedMap;
5050
import java.util.concurrent.CopyOnWriteArraySet;
5151
import java.util.function.BiPredicate;
52-
import java.util.function.Supplier;
5352

5453
import static org.elasticsearch.xpack.core.security.authz.IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER;
5554

@@ -323,7 +322,7 @@ ResolvedIndices resolveIndicesAndAliases(
323322
);
324323
}
325324
if (indicesOptions.expandWildcardExpressions()) {
326-
for (String authorizedIndex : authorizedIndices.all(allIndicesPatternSelector).get()) {
325+
for (String authorizedIndex : authorizedIndices.all(allIndicesPatternSelector)) {
327326
if (IndexAbstractionResolver.isIndexVisible(
328327
"*",
329328
allIndicesPatternSelector,
@@ -389,7 +388,7 @@ ResolvedIndices resolveIndicesAndAliases(
389388
if (aliasesRequest.expandAliasesWildcards()) {
390389
List<String> aliases = replaceWildcardsWithAuthorizedAliases(
391390
aliasesRequest.aliases(),
392-
loadAuthorizedAliases(authorizedIndices.all(null), projectMetadata)
391+
loadAuthorizedAliases(authorizedIndices, projectMetadata)
393392
);
394393
aliasesRequest.replaceAliases(aliases.toArray(new String[aliases.size()]));
395394
}
@@ -483,10 +482,13 @@ static String getPutMappingIndexOrAlias(
483482
return resolvedAliasOrIndex;
484483
}
485484

486-
private static List<String> loadAuthorizedAliases(Supplier<Set<String>> authorizedIndices, ProjectMetadata projectMetadata) {
485+
private static List<String> loadAuthorizedAliases(
486+
AuthorizationEngine.AuthorizedIndices authorizedIndices,
487+
ProjectMetadata projectMetadata
488+
) {
487489
List<String> authorizedAliases = new ArrayList<>();
488490
SortedMap<String, IndexAbstraction> existingAliases = projectMetadata.getIndicesLookup();
489-
for (String authorizedIndex : authorizedIndices.get()) {
491+
for (String authorizedIndex : authorizedIndices.all(null)) {
490492
IndexAbstraction indexAbstraction = existingAliases.get(authorizedIndex);
491493
if (indexAbstraction != null && indexAbstraction.getType() == IndexAbstraction.Type.ALIAS) {
492494
authorizedAliases.add(authorizedIndex);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,10 +1091,10 @@ static final class AuthorizedIndices implements AuthorizationEngine.AuthorizedIn
10911091
}
10921092

10931093
@Override
1094-
public Supplier<Set<String>> all(@Nullable String selector) {
1094+
public Set<String> all(@Nullable String selector) {
10951095
return IndexComponentSelector.FAILURES.equals(IndexComponentSelector.getByKeyOrThrow(selector))
1096-
? failureStoreAuthorizedAndAvailableSupplier
1097-
: authorizedAndAvailableSupplier;
1096+
? failureStoreAuthorizedAndAvailableSupplier.get()
1097+
: authorizedAndAvailableSupplier.get();
10981098
}
10991099

11001100
@Override

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testAuthorizedIndicesUserWithoutRoles() {
5555
Metadata.EMPTY_METADATA.getProject().getIndicesLookup(),
5656
() -> ignore -> {}
5757
);
58-
assertTrue(authorizedIndices.all(null).get().isEmpty());
58+
assertTrue(authorizedIndices.all(null).isEmpty());
5959
}
6060

6161
public void testAuthorizedIndicesUserWithSomeRoles() {
@@ -115,14 +115,14 @@ public void testAuthorizedIndicesUserWithSomeRoles() {
115115
metadata.getProject().getIndicesLookup(),
116116
() -> ignore -> {}
117117
);
118-
assertThat(authorizedIndices.all(null).get(), containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab"));
119-
assertThat(authorizedIndices.all(null).get(), not(contains("bbbbb")));
118+
assertThat(authorizedIndices.all(null), containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab"));
119+
assertThat(authorizedIndices.all(null), not(contains("bbbbb")));
120120
assertThat(authorizedIndices.check("bbbbb", null), is(false));
121-
assertThat(authorizedIndices.all(null).get(), not(contains("ba")));
121+
assertThat(authorizedIndices.all(null), not(contains("ba")));
122122
assertThat(authorizedIndices.check("ba", null), is(false));
123-
assertThat(authorizedIndices.all(null).get(), not(contains(internalSecurityIndex)));
123+
assertThat(authorizedIndices.all(null), not(contains(internalSecurityIndex)));
124124
assertThat(authorizedIndices.check(internalSecurityIndex, null), is(false));
125-
assertThat(authorizedIndices.all(null).get(), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
125+
assertThat(authorizedIndices.all(null), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
126126
assertThat(authorizedIndices.check(SecuritySystemIndices.SECURITY_MAIN_ALIAS, null), is(false));
127127
}
128128

@@ -134,7 +134,7 @@ public void testAuthorizedIndicesUserWithSomeRolesEmptyMetadata() {
134134
Metadata.EMPTY_METADATA.getProject().getIndicesLookup(),
135135
() -> ignore -> {}
136136
);
137-
assertTrue(authorizedIndices.all(null).get().isEmpty());
137+
assertTrue(authorizedIndices.all(null).isEmpty());
138138
}
139139

140140
public void testSecurityIndicesAreRemovedFromRegularUser() {
@@ -145,7 +145,7 @@ public void testSecurityIndicesAreRemovedFromRegularUser() {
145145
Metadata.EMPTY_METADATA.getProject().getIndicesLookup(),
146146
() -> ignore -> {}
147147
);
148-
assertTrue(authorizedIndices.all(null).get().isEmpty());
148+
assertTrue(authorizedIndices.all(null).isEmpty());
149149
}
150150

151151
public void testSecurityIndicesAreRestrictedForDefaultRole() {
@@ -177,12 +177,12 @@ public void testSecurityIndicesAreRestrictedForDefaultRole() {
177177
metadata.getProject().getIndicesLookup(),
178178
() -> ignore -> {}
179179
);
180-
assertThat(authorizedIndices.all(null).get(), containsInAnyOrder("an-index", "another-index"));
180+
assertThat(authorizedIndices.all(null), containsInAnyOrder("an-index", "another-index"));
181181
assertThat(authorizedIndices.check("an-index", null), is(true));
182182
assertThat(authorizedIndices.check("another-index", null), is(true));
183-
assertThat(authorizedIndices.all(null).get(), not(contains(internalSecurityIndex)));
183+
assertThat(authorizedIndices.all(null), not(contains(internalSecurityIndex)));
184184
assertThat(authorizedIndices.check(internalSecurityIndex, null), is(false));
185-
assertThat(authorizedIndices.all(null).get(), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
185+
assertThat(authorizedIndices.all(null), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
186186
assertThat(authorizedIndices.check(SecuritySystemIndices.SECURITY_MAIN_ALIAS, null), is(false));
187187
}
188188

@@ -216,7 +216,7 @@ public void testSecurityIndicesAreNotRemovedFromUnrestrictedRole() {
216216
() -> ignore -> {}
217217
);
218218
assertThat(
219-
authorizedIndices.all(null).get(),
219+
authorizedIndices.all(null),
220220
containsInAnyOrder("an-index", "another-index", SecuritySystemIndices.SECURITY_MAIN_ALIAS, internalSecurityIndex)
221221
);
222222

@@ -227,7 +227,7 @@ public void testSecurityIndicesAreNotRemovedFromUnrestrictedRole() {
227227
() -> ignore -> {}
228228
);
229229
assertThat(
230-
authorizedIndicesSuperUser.all(null).get(),
230+
authorizedIndicesSuperUser.all(null),
231231
containsInAnyOrder("an-index", "another-index", SecuritySystemIndices.SECURITY_MAIN_ALIAS, internalSecurityIndex)
232232
);
233233
}
@@ -297,21 +297,21 @@ public void testDataStreamsAreNotIncludedInAuthorizedIndices() {
297297
metadata.getProject().getIndicesLookup(),
298298
() -> ignore -> {}
299299
);
300-
assertThat(authorizedIndices.all(null).get(), containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab"));
300+
assertThat(authorizedIndices.all(null), containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab"));
301301
for (String resource : List.of("a1", "a2", "aaaaaa", "b", "ab")) {
302302
assertThat(authorizedIndices.check(resource, null), is(true));
303303
}
304-
assertThat(authorizedIndices.all(null).get(), not(contains("bbbbb")));
304+
assertThat(authorizedIndices.all(null), not(contains("bbbbb")));
305305
assertThat(authorizedIndices.check("bbbbb", null), is(false));
306-
assertThat(authorizedIndices.all(null).get(), not(contains("ba")));
306+
assertThat(authorizedIndices.all(null), not(contains("ba")));
307307
assertThat(authorizedIndices.check("ba", null), is(false));
308308
// due to context, datastreams are excluded from wildcard expansion
309-
assertThat(authorizedIndices.all(null).get(), not(contains("adatastream1")));
309+
assertThat(authorizedIndices.all(null), not(contains("adatastream1")));
310310
// but they are authorized when explicitly tested (they are not "unavailable" for the Security filter)
311311
assertThat(authorizedIndices.check("adatastream1", null), is(true));
312-
assertThat(authorizedIndices.all(null).get(), not(contains(internalSecurityIndex)));
312+
assertThat(authorizedIndices.all(null), not(contains(internalSecurityIndex)));
313313
assertThat(authorizedIndices.check(internalSecurityIndex, null), is(false));
314-
assertThat(authorizedIndices.all(null).get(), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
314+
assertThat(authorizedIndices.all(null), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
315315
assertThat(authorizedIndices.check(SecuritySystemIndices.SECURITY_MAIN_ALIAS, null), is(false));
316316
}
317317

@@ -382,14 +382,14 @@ public void testDataStreamsAreIncludedInAuthorizedIndices() {
382382
metadata.getProject().getIndicesLookup(),
383383
() -> ignore -> {}
384384
);
385-
assertThat(authorizedIndices.all(null).get(), containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab", "adatastream1", backingIndex));
386-
assertThat(authorizedIndices.all(null).get(), not(contains("bbbbb")));
385+
assertThat(authorizedIndices.all(null), containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab", "adatastream1", backingIndex));
386+
assertThat(authorizedIndices.all(null), not(contains("bbbbb")));
387387
assertThat(authorizedIndices.check("bbbbb", null), is(false));
388-
assertThat(authorizedIndices.all(null).get(), not(contains("ba")));
388+
assertThat(authorizedIndices.all(null), not(contains("ba")));
389389
assertThat(authorizedIndices.check("ba", null), is(false));
390-
assertThat(authorizedIndices.all(null).get(), not(contains(internalSecurityIndex)));
390+
assertThat(authorizedIndices.all(null), not(contains(internalSecurityIndex)));
391391
assertThat(authorizedIndices.check(internalSecurityIndex, null), is(false));
392-
assertThat(authorizedIndices.all(null).get(), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
392+
assertThat(authorizedIndices.all(null), not(contains(SecuritySystemIndices.SECURITY_MAIN_ALIAS)));
393393
assertThat(authorizedIndices.check(SecuritySystemIndices.SECURITY_MAIN_ALIAS, null), is(false));
394394
}
395395

0 commit comments

Comments
 (0)