Skip to content

Commit 333a5d1

Browse files
committed
Fix unsupported privileges error message during role and API key creation
1 parent 4ebc62c commit 333a5d1

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private static Set<IndexPrivilege> resolve(Set<String> name) {
396396
+ part
397397
+ "]. a privilege must be either "
398398
+ "one of the predefined fixed indices privileges ["
399-
+ Strings.collectionToCommaDelimitedString(VALUES.entrySet())
399+
+ Strings.collectionToCommaDelimitedString(names().stream().sorted().collect(Collectors.toList()))
400400
+ "] or a pattern over one of the available index"
401401
+ " actions";
402402
logger.debug(errorMessage);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.action.index.TransportIndexAction;
1414
import org.elasticsearch.action.search.TransportSearchAction;
1515
import org.elasticsearch.action.update.TransportUpdateAction;
16+
import org.elasticsearch.common.Strings;
1617
import org.elasticsearch.common.util.iterable.Iterables;
1718
import org.elasticsearch.test.ESTestCase;
1819
import org.elasticsearch.xpack.core.rollup.action.GetRollupIndexCapsAction;
@@ -21,8 +22,10 @@
2122

2223
import java.util.Collection;
2324
import java.util.List;
25+
import java.util.Locale;
2426
import java.util.Set;
2527
import java.util.function.Predicate;
28+
import java.util.stream.Collectors;
2629

2730
import static org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege.findPrivilegesThatGrant;
2831
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -392,6 +395,28 @@ public void testCrossClusterReplicationPrivileges() {
392395
);
393396
}
394397

398+
public void testInvalidPrivilegeErrorMessage() {
399+
final String unknownPrivilege = randomValueOtherThanMany(
400+
i -> IndexPrivilege.values().containsKey(i),
401+
() -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT)
402+
);
403+
404+
IllegalArgumentException exception = expectThrows(
405+
IllegalArgumentException.class,
406+
() -> IndexPrivilege.resolveBySelectorAccess(Set.of(unknownPrivilege))
407+
);
408+
409+
final String expectedFullErrorMessage = "unknown index privilege ["
410+
+ unknownPrivilege
411+
+ "]. a privilege must be either "
412+
+ "one of the predefined fixed indices privileges ["
413+
+ Strings.collectionToCommaDelimitedString(IndexPrivilege.names().stream().sorted().collect(Collectors.toList()))
414+
+ "] or a pattern over one of the available index"
415+
+ " actions";
416+
417+
assertEquals(expectedFullErrorMessage, exception.getMessage());
418+
}
419+
395420
public static IndexPrivilege resolvePrivilegeAndAssertSingleton(Set<String> names) {
396421
final Set<IndexPrivilege> splitBySelector = IndexPrivilege.resolveBySelectorAccess(names);
397422
assertThat("expected singleton privilege set but got " + splitBySelector, splitBySelector.size(), equalTo(1));

x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/role/PutRoleRestIT.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99

1010
import org.elasticsearch.client.Request;
1111
import org.elasticsearch.client.ResponseException;
12+
import org.elasticsearch.common.Strings;
1213
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
14+
import org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege;
1315
import org.elasticsearch.xpack.security.SecurityOnTrialLicenseRestTestCase;
1416

1517
import java.util.List;
18+
import java.util.Locale;
1619
import java.util.Map;
20+
import java.util.stream.Collectors;
1721

1822
import static org.hamcrest.Matchers.contains;
1923
import static org.hamcrest.Matchers.containsString;
2024
import static org.hamcrest.Matchers.hasKey;
2125
import static org.hamcrest.Matchers.hasSize;
2226
import static org.hamcrest.Matchers.not;
27+
import static org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege.names;
2328

2429
public class PutRoleRestIT extends SecurityOnTrialLicenseRestTestCase {
2530
public void testPutManyValidRoles() throws Exception {
@@ -316,6 +321,24 @@ public void testBulkUpdates() throws Exception {
316321
public void testPutRoleWithInvalidManageRolesPrivilege() throws Exception {
317322
final String badRoleName = "bad-role";
318323

324+
final String unknownPrivilege = randomValueOtherThanMany(
325+
i -> names().contains(i),
326+
() -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT)
327+
);
328+
329+
final String expectedExceptionMessage = "unknown index privilege ["
330+
+ unknownPrivilege
331+
+ "]. a privilege must be either "
332+
+ "one of the predefined fixed indices privileges ["
333+
+ Strings.collectionToCommaDelimitedString(
334+
IndexPrivilege.names().stream()
335+
.sorted()
336+
.collect(Collectors.toList())
337+
)
338+
+ "] or a pattern over one of the available index"
339+
+ " actions";
340+
341+
319342
final ResponseException exception = expectThrows(ResponseException.class, () -> upsertRoles(String.format("""
320343
{
321344
"roles": {
@@ -326,17 +349,17 @@ public void testPutRoleWithInvalidManageRolesPrivilege() throws Exception {
326349
"indices": [
327350
{
328351
"names": ["allowed-index-prefix-*"],
329-
"privileges": ["foobar"]
352+
"privileges": ["%s"]
330353
}
331354
]
332355
}
333356
}
334357
}
335358
}
336359
}
337-
}""", badRoleName)));
360+
}""", badRoleName, unknownPrivilege)));
338361

339-
assertThat(exception.getMessage(), containsString("unknown index privilege [foobar]"));
362+
assertThat(exception.getMessage(), containsString(expectedExceptionMessage));
340363
assertEquals(400, exception.getResponse().getStatusLine().getStatusCode());
341364
assertRoleDoesNotExist(badRoleName);
342365
}

0 commit comments

Comments
 (0)