Skip to content

Commit 4a44748

Browse files
committed
More split tests
1 parent 17855ab commit 4a44748

File tree

2 files changed

+83
-9
lines changed

2 files changed

+83
-9
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,14 @@ private static Set<IndexPrivilege> resolve(Set<String> name) {
369369
}
370370
}
371371

372-
final Set<IndexPrivilege> result = combineIndexPrivileges(
372+
final Set<IndexPrivilege> combined = combineIndexPrivileges(
373373
allSelectorAccessPrivileges,
374374
dataSelectorAccessPrivileges,
375375
failuresSelectorAccessPrivileges,
376376
actions
377377
);
378-
assertNamesMatch(name, result);
379-
return result;
378+
assertNamesMatch(name, combined);
379+
return combined;
380380
}
381381

382382
private static Set<IndexPrivilege> combineIndexPrivileges(
@@ -396,14 +396,14 @@ private static Set<IndexPrivilege> combineIndexPrivileges(
396396
return Set.of(union(allSelectorAccessPrivileges, actions, IndexComponentSelectorPredicate.ALL));
397397
}
398398

399-
final Set<IndexPrivilege> result = new HashSet<>();
399+
final Set<IndexPrivilege> combined = new HashSet<>();
400400
if (false == failuresSelectorAccessPrivileges.isEmpty()) {
401-
result.add(union(failuresSelectorAccessPrivileges, Set.of(), IndexComponentSelectorPredicate.FAILURES));
401+
combined.add(union(failuresSelectorAccessPrivileges, Set.of(), IndexComponentSelectorPredicate.FAILURES));
402402
}
403403
if (false == dataSelectorAccessPrivileges.isEmpty() || false == actions.isEmpty()) {
404-
result.add(union(dataSelectorAccessPrivileges, actions, IndexComponentSelectorPredicate.DATA));
404+
combined.add(union(dataSelectorAccessPrivileges, actions, IndexComponentSelectorPredicate.DATA));
405405
}
406-
return result;
406+
return combined;
407407
}
408408

409409
private static void assertNamesMatch(Set<String> names, Set<IndexPrivilege> privileges) {
@@ -428,7 +428,7 @@ private static IndexPrivilege union(
428428

429429
if (false == actions.isEmpty()) {
430430
names.addAll(actions);
431-
automata.add(patterns(actions.stream().map(Privilege::actionToPattern).toArray(String[]::new)));
431+
automata.add(patterns(actions.stream().map(Privilege::actionToPattern).toList()));
432432
}
433433
return new IndexPrivilege(names, unionAndMinimize(automata), selectorPredicate);
434434
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/IndexPrivilegeTests.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void testGetWithSingleSelectorAccess() {
105105
}
106106
}
107107

108-
public void testGetSingleSelectorWithFailuresSelectorOrThrow() {
108+
public void testGetWithSingleSelectorAccessFailuresSelector() {
109109
assumeTrue("This test requires the failure store to be enabled", DataStream.isFailureStoreFeatureFlagEnabled());
110110
{
111111
IndexPrivilege actual = IndexPrivilege.getWithSingleSelectorAccess(Set.of("read_failure_store"));
@@ -146,6 +146,80 @@ public void testGetSingleSelectorWithFailuresSelectorOrThrow() {
146146
);
147147
}
148148

149+
public void testGetSplitBySelectorAccess() {
150+
assumeTrue("This test requires the failure store to be enabled", DataStream.isFailureStoreFeatureFlagEnabled());
151+
{
152+
Set<IndexPrivilege> actual = IndexPrivilege.getSplitBySelectorAccess(Set.of("read_failure_store"));
153+
assertThat(actual, containsInAnyOrder(IndexPrivilege.READ_FAILURE_STORE));
154+
assertThat(actual.iterator().next().getSelectorPredicate(), equalTo(IndexComponentSelectorPredicate.FAILURES));
155+
}
156+
{
157+
Set<IndexPrivilege> actual = IndexPrivilege.getSplitBySelectorAccess(Set.of("read_failure_store", "READ_FAILURE_STORE"));
158+
assertThat(actual, containsInAnyOrder(IndexPrivilege.READ_FAILURE_STORE));
159+
assertThat(actual.iterator().next().getSelectorPredicate(), equalTo(IndexComponentSelectorPredicate.FAILURES));
160+
}
161+
{
162+
Set<IndexPrivilege> actual = IndexPrivilege.getSplitBySelectorAccess(
163+
Set.of("read_failure_store", "read", "READ_FAILURE_STORE")
164+
);
165+
assertThat(actual, containsInAnyOrder(IndexPrivilege.READ_FAILURE_STORE, IndexPrivilege.READ));
166+
List<IndexComponentSelectorPredicate> actualPredicates = actual.stream().map(IndexPrivilege::getSelectorPredicate).toList();
167+
assertThat(
168+
actualPredicates,
169+
containsInAnyOrder(IndexComponentSelectorPredicate.DATA, IndexComponentSelectorPredicate.FAILURES)
170+
);
171+
}
172+
{
173+
Set<IndexPrivilege> actual = IndexPrivilege.getSplitBySelectorAccess(
174+
Set.of("read_failure_store", "read", "view_index_metadata")
175+
);
176+
assertThat(
177+
actual,
178+
containsInAnyOrder(
179+
IndexPrivilege.READ_FAILURE_STORE,
180+
IndexPrivilege.getWithSingleSelectorAccess(Set.of("read", "view_index_metadata"))
181+
)
182+
);
183+
List<IndexComponentSelectorPredicate> actualPredicates = actual.stream().map(IndexPrivilege::getSelectorPredicate).toList();
184+
assertThat(
185+
actualPredicates,
186+
containsInAnyOrder(IndexComponentSelectorPredicate.DATA, IndexComponentSelectorPredicate.FAILURES)
187+
);
188+
}
189+
{
190+
Set<IndexPrivilege> actual = IndexPrivilege.getSplitBySelectorAccess(
191+
Set.of("read_failure_store", "read", "indices:data/read/search", "view_index_metadata")
192+
);
193+
assertThat(
194+
actual,
195+
containsInAnyOrder(
196+
IndexPrivilege.READ_FAILURE_STORE,
197+
IndexPrivilege.getWithSingleSelectorAccess(Set.of("read", "indices:data/read/search", "view_index_metadata"))
198+
)
199+
);
200+
List<IndexComponentSelectorPredicate> actualPredicates = actual.stream().map(IndexPrivilege::getSelectorPredicate).toList();
201+
assertThat(
202+
actualPredicates,
203+
containsInAnyOrder(IndexComponentSelectorPredicate.DATA, IndexComponentSelectorPredicate.FAILURES)
204+
);
205+
}
206+
{
207+
Set<IndexPrivilege> actual = IndexPrivilege.getSplitBySelectorAccess(
208+
Set.of("read_failure_store", "all", "read", "indices:data/read/search", "view_index_metadata")
209+
);
210+
assertThat(
211+
actual,
212+
containsInAnyOrder(
213+
IndexPrivilege.getWithSingleSelectorAccess(
214+
Set.of("read_failure_store", "all", "read", "indices:data/read/search", "view_index_metadata")
215+
)
216+
)
217+
);
218+
List<IndexComponentSelectorPredicate> actualPredicates = actual.stream().map(IndexPrivilege::getSelectorPredicate).toList();
219+
assertThat(actualPredicates, containsInAnyOrder(IndexComponentSelectorPredicate.ALL));
220+
}
221+
}
222+
149223
public void testPrivilegesForRollupFieldCapsAction() {
150224
final Collection<String> privileges = findPrivilegesThatGrant(GetRollupIndexCapsAction.NAME);
151225
assertThat(Set.copyOf(privileges), equalTo(Set.of("manage", "all", "view_index_metadata", "read")));

0 commit comments

Comments
 (0)