Skip to content

Commit 69a9a64

Browse files
committed
Fix unsupported privileges error message during role and API key creation
1 parent 96df3a9 commit 69a9a64

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-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
@@ -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
@@ -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,7 +22,9 @@
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(Automatons.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: 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)