Skip to content

Commit 64460df

Browse files
gmjehovichelasticsearchmachine
andauthored
Fix unsupported privileges error message during role and API key crea… (#128858)
* Fix unsupported privileges error message during role and API key creation * Update docs/changelog/128858.yaml * [CI] Auto commit changes from spotless * Update docs/changelog/128858.yaml --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent b01d552 commit 64460df

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

docs/changelog/128858.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 128858
2+
summary: Fix unsupported privileges error message during role and API key crea…
3+
area: Authorization
4+
type: enhancement
5+
issues:
6+
- 128132

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: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
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

22+
import static org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege.names;
1823
import static org.hamcrest.Matchers.contains;
1924
import static org.hamcrest.Matchers.containsString;
2025
import static org.hamcrest.Matchers.hasKey;
@@ -316,6 +321,19 @@ 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(IndexPrivilege.names().stream().sorted().collect(Collectors.toList()))
334+
+ "] or a pattern over one of the available index"
335+
+ " actions";
336+
319337
final ResponseException exception = expectThrows(ResponseException.class, () -> upsertRoles(String.format("""
320338
{
321339
"roles": {
@@ -326,17 +344,17 @@ public void testPutRoleWithInvalidManageRolesPrivilege() throws Exception {
326344
"indices": [
327345
{
328346
"names": ["allowed-index-prefix-*"],
329-
"privileges": ["foobar"]
347+
"privileges": ["%s"]
330348
}
331349
]
332350
}
333351
}
334352
}
335353
}
336354
}
337-
}""", badRoleName)));
355+
}""", badRoleName, unknownPrivilege)));
338356

339-
assertThat(exception.getMessage(), containsString("unknown index privilege [foobar]"));
357+
assertThat(exception.getMessage(), containsString(expectedExceptionMessage));
340358
assertEquals(400, exception.getResponse().getStatusLine().getStatusCode());
341359
assertRoleDoesNotExist(badRoleName);
342360
}

0 commit comments

Comments
 (0)