Skip to content

Commit 3509cbb

Browse files
[9.0] Fix unsupported privileges error message during role and API key creation (#128858) (#129158) (#129274)
* Fix unsupported privileges error message during role and API key creation * [CI] Auto commit changes from spotless * Add changelog file --------- Co-authored-by: elasticsearchmachine <infra-root+elasticsearchmachine@elastic.co>
1 parent 4e5f7ab commit 3509cbb

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

docs/changelog/129158.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 129158
2+
summary: Fix unsupported privileges error message during role and API key creation
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
@@ -291,7 +291,7 @@ private static IndexPrivilege resolve(Set<String> name) {
291291
+ part
292292
+ "]. a privilege must be either "
293293
+ "one of the predefined fixed indices privileges ["
294-
+ Strings.collectionToCommaDelimitedString(VALUES.entrySet())
294+
+ Strings.collectionToCommaDelimitedString(names().stream().sorted().collect(Collectors.toList()))
295295
+ "] or a pattern over one of the available index"
296296
+ " actions";
297297
logger.debug(errorMessage);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
import org.elasticsearch.action.index.TransportIndexAction;
1515
import org.elasticsearch.action.search.TransportSearchAction;
1616
import org.elasticsearch.action.update.TransportUpdateAction;
17+
import org.elasticsearch.common.Strings;
1718
import org.elasticsearch.common.util.iterable.Iterables;
1819
import org.elasticsearch.test.ESTestCase;
1920
import org.elasticsearch.xpack.core.rollup.action.GetRollupIndexCapsAction;
2021
import org.elasticsearch.xpack.core.transform.action.GetCheckpointAction;
2122

2223
import java.util.Collection;
2324
import java.util.List;
25+
import java.util.Locale;
2426
import java.util.Set;
27+
import java.util.stream.Collectors;
2528

2629
import static org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege.findPrivilegesThatGrant;
2730
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -145,4 +148,25 @@ public void testCrossClusterReplicationPrivileges() {
145148
assertThat(Operations.subsetOf(crossClusterReplicationInternal.automaton, IndexPrivilege.get(Set.of("all")).automaton), is(true));
146149
}
147150

151+
public void testInvalidPrivilegeErrorMessage() {
152+
final String unknownPrivilege = randomValueOtherThanMany(
153+
i -> IndexPrivilege.values().containsKey(i),
154+
() -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT)
155+
);
156+
157+
IllegalArgumentException exception = expectThrows(
158+
IllegalArgumentException.class,
159+
() -> IndexPrivilege.get(Set.of(unknownPrivilege))
160+
);
161+
162+
final String expectedFullErrorMessage = "unknown index privilege ["
163+
+ unknownPrivilege
164+
+ "]. a privilege must be either "
165+
+ "one of the predefined fixed indices privileges ["
166+
+ Strings.collectionToCommaDelimitedString(IndexPrivilege.names().stream().sorted().collect(Collectors.toList()))
167+
+ "] or a pattern over one of the available index"
168+
+ " actions";
169+
170+
assertEquals(expectedFullErrorMessage, exception.getMessage());
171+
}
148172
}

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)