Skip to content

Commit 9732f8d

Browse files
committed
Assert on names
1 parent 8582f80 commit 9732f8d

File tree

1 file changed

+14
-15
lines changed
  • x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege

1 file changed

+14
-15
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ private static Set<IndexPrivilege> resolve(Set<String> name) {
326326
for (String part : name) {
327327
part = part.toLowerCase(Locale.ROOT);
328328
if (ACTION_MATCHER.test(part)) {
329-
actions.add(actionToPattern(part));
329+
actions.add(part);
330330
} else {
331331
IndexPrivilege indexPrivilege = part == null ? null : VALUES.get(part);
332332
if (indexPrivilege != null && size == 1) {
@@ -356,18 +356,9 @@ private static Set<IndexPrivilege> resolve(Set<String> name) {
356356
}
357357

358358
if (false == allAccessPrivileges.isEmpty()) {
359-
assert name.size() == actions.size() + allAccessPrivileges.size()
360-
: "expected ["
361-
+ name.size()
362-
+ "] but was ["
363-
+ (actions.size() + allAccessPrivileges.size())
364-
+ "] for "
365-
+ name
366-
+ " "
367-
+ allAccessPrivileges
368-
+ " "
369-
+ actions;
370-
return Set.of(union(allAccessPrivileges, actions, IndexComponentSelectorPrivilege.ALL));
359+
Set<IndexPrivilege> result = Set.of(union(allAccessPrivileges, actions, IndexComponentSelectorPrivilege.ALL));
360+
assertNamesMatch(result, name);
361+
return result;
371362
}
372363

373364
final Set<IndexPrivilege> result = new HashSet<>();
@@ -377,9 +368,15 @@ private static Set<IndexPrivilege> resolve(Set<String> name) {
377368
if (false == dataAccessPrivileges.isEmpty() || false == actions.isEmpty()) {
378369
result.add(union(dataAccessPrivileges, actions, IndexComponentSelectorPrivilege.DATA));
379370
}
371+
assertNamesMatch(result, name);
380372
return result;
381373
}
382374

375+
private static void assertNamesMatch(Set<IndexPrivilege> privileges, Set<String> names) {
376+
assert names.equals(privileges.stream().map(Privilege::name).flatMap(Set::stream).collect(Collectors.toSet()))
377+
: "mismatch between names [" + names + "] and names on split privileges [" + privileges + "]";
378+
}
379+
383380
private static IndexPrivilege union(
384381
Collection<IndexPrivilege> privileges,
385382
Collection<String> actions,
@@ -388,12 +385,14 @@ private static IndexPrivilege union(
388385
Set<Automaton> automata = HashSet.newHashSet(privileges.size() + actions.size());
389386
Set<String> names = new HashSet<>();
390387
for (var privilege : privileges) {
391-
automata.add(privilege.automaton);
392388
names.add(privilege.getSingleName());
389+
automata.add(privilege.automaton);
393390
}
391+
394392
if (false == actions.isEmpty()) {
395-
automata.add(patterns(actions));
396393
names.addAll(actions);
394+
// TODO for-loop or optimize?
395+
automata.add(patterns(actions.stream().map(Privilege::actionToPattern).toArray(String[]::new)));
397396
}
398397
return new IndexPrivilege(names, unionAndMinimize(automata), selectorPrivilege);
399398
}

0 commit comments

Comments
 (0)