|
34 | 34 |
|
35 | 35 | import java.io.ByteArrayOutputStream;
|
36 | 36 | import java.io.IOException;
|
| 37 | +import java.nio.charset.StandardCharsets; |
37 | 38 | import java.util.Arrays;
|
38 | 39 | import java.util.List;
|
| 40 | +import java.util.Locale; |
39 | 41 | import java.util.Objects;
|
40 | 42 |
|
| 43 | +import static org.hamcrest.Matchers.containsString; |
41 | 44 | import static org.hamcrest.Matchers.nullValue;
|
42 | 45 | import static org.hamcrest.core.Is.is;
|
43 | 46 | import static org.hamcrest.core.IsEqual.equalTo;
|
@@ -257,6 +260,70 @@ public void testPutRoleRequestContainsNonIndexPrivileges() {
|
257 | 260 | assertThat(permissionCheck(permission, "cluster:admin/xpack/security/role/put", putRoleRequest), is(false));
|
258 | 261 | }
|
259 | 262 |
|
| 263 | + public void testParseInvalidPrivilege() throws Exception { |
| 264 | + final String unknownPrivilege = randomValueOtherThanMany( |
| 265 | + i -> IndexPrivilege.values().containsKey(i), |
| 266 | + () -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT) |
| 267 | + ); |
| 268 | + |
| 269 | + final String invalidJsonString = String.format(Locale.ROOT, """ |
| 270 | + { |
| 271 | + "manage": { |
| 272 | + "indices": [ |
| 273 | + { |
| 274 | + "names": ["test-*"], |
| 275 | + "privileges": ["%s"] |
| 276 | + } |
| 277 | + ] |
| 278 | + } |
| 279 | + }""", unknownPrivilege); |
| 280 | + assertInvalidPrivilegeParsing(invalidJsonString, unknownPrivilege); |
| 281 | + } |
| 282 | + |
| 283 | + public void testParseMixedValidAndInvalidPrivileges() throws Exception { |
| 284 | + final String unknownPrivilege = randomValueOtherThanMany( |
| 285 | + i -> IndexPrivilege.values().containsKey(i), |
| 286 | + () -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT) |
| 287 | + ); |
| 288 | + |
| 289 | + final String validPrivilege = "read"; |
| 290 | + final String mixedPrivilegesJson = String.format(Locale.ROOT, """ |
| 291 | + { |
| 292 | + "manage": { |
| 293 | + "indices": [ |
| 294 | + { |
| 295 | + "names": ["test-*"], |
| 296 | + "privileges": ["%s", "%s"] |
| 297 | + } |
| 298 | + ] |
| 299 | + } |
| 300 | + }""", validPrivilege, unknownPrivilege); |
| 301 | + |
| 302 | + assertInvalidPrivilegeParsing(mixedPrivilegesJson, unknownPrivilege); |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Helper method to assert that parsing the given JSON payload results in an |
| 307 | + * IllegalArgumentException due to an unknown privilege. |
| 308 | + * |
| 309 | + * @param jsonPayload The JSON string containing the privilege data. |
| 310 | + * @param expectedErrorDetail The specific unknown privilege name expected in the error message. |
| 311 | + */ |
| 312 | + private static void assertInvalidPrivilegeParsing(final String jsonPayload, final String expectedErrorDetail) throws Exception { |
| 313 | + final XContent xContent = XContentType.JSON.xContent(); |
| 314 | + |
| 315 | + try ( |
| 316 | + XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY, jsonPayload.getBytes(StandardCharsets.UTF_8)) |
| 317 | + ) { |
| 318 | + assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT)); |
| 319 | + assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME)); |
| 320 | + |
| 321 | + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> ManageRolesPrivilege.parse(parser)); |
| 322 | + |
| 323 | + assertThat(exception.getMessage(), containsString("unknown index privilege [" + expectedErrorDetail + "]")); |
| 324 | + } |
| 325 | + } |
| 326 | + |
260 | 327 | private static boolean permissionCheck(ClusterPermission permission, String action, ActionRequest request) {
|
261 | 328 | final Authentication authentication = AuthenticationTestHelper.builder().build();
|
262 | 329 | assertThat(request.validate(), nullValue());
|
|
0 commit comments