|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.security.authz;
|
7 | 7 |
|
| 8 | +import org.elasticsearch.ElasticsearchParseException; |
8 | 9 | import org.elasticsearch.Version;
|
9 | 10 | import org.elasticsearch.common.Strings;
|
10 | 11 | import org.elasticsearch.common.bytes.BytesArray;
|
|
19 | 20 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
20 | 21 | import org.elasticsearch.common.xcontent.XContentType;
|
21 | 22 | import org.elasticsearch.test.ESTestCase;
|
| 23 | +import org.elasticsearch.test.TestMatchers; |
22 | 24 | import org.elasticsearch.test.VersionUtils;
|
23 | 25 | import org.elasticsearch.xpack.core.XPackClientPlugin;
|
24 | 26 | import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
|
|
27 | 29 | import org.elasticsearch.xpack.core.security.support.MetadataUtils;
|
28 | 30 | import org.hamcrest.Matchers;
|
29 | 31 |
|
| 32 | +import java.io.IOException; |
30 | 33 | import java.util.Arrays;
|
31 | 34 | import java.util.Collections;
|
32 | 35 | import java.util.LinkedHashSet;
|
@@ -296,4 +299,30 @@ public void testParseIgnoresTransientMetadata() throws Exception {
|
296 | 299 | assertEquals(true, parsed.getTransientMetadata().get("enabled"));
|
297 | 300 | }
|
298 | 301 |
|
| 302 | + public void testParseIndicesPrivilegesSucceedsWhenExceptFieldsIsSubsetOfGrantedFields() throws IOException { |
| 303 | + final boolean grantAll = randomBoolean(); |
| 304 | + final String grant = grantAll ? "\"*\"" : "\"f1\",\"f2\""; |
| 305 | + final String except = grantAll ? "\"_fx\",\"f8\"" : "\"f1\""; |
| 306 | + |
| 307 | + final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " + |
| 308 | + "\"grant\" : [" + grant + "], \"except\" : [" + except + "] } }] }"; |
| 309 | + final RoleDescriptor rd = RoleDescriptor.parse("test", |
| 310 | + new BytesArray(json), false, XContentType.JSON); |
| 311 | + assertEquals("test", rd.getName()); |
| 312 | + assertEquals(1, rd.getIndicesPrivileges().length); |
| 313 | + assertArrayEquals(new String[]{"idx1", "idx2"}, rd.getIndicesPrivileges()[0].getIndices()); |
| 314 | + assertArrayEquals((grantAll) ? new String[]{"*"} : new String[]{"f1", "f2"}, rd.getIndicesPrivileges()[0].getGrantedFields()); |
| 315 | + assertArrayEquals((grantAll) ? new String[]{"_fx", "f8"} : new String[]{"f1"}, rd.getIndicesPrivileges()[0].getDeniedFields()); |
| 316 | + } |
| 317 | + |
| 318 | + public void testParseIndicesPrivilegesFailsWhenExceptFieldsAreNotSubsetOfGrantedFields() { |
| 319 | + final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " + |
| 320 | + "\"grant\" : [\"f1\",\"f2\"], \"except\" : [\"f3\"] } }] }"; |
| 321 | + final ElasticsearchParseException epe = expectThrows(ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", |
| 322 | + new BytesArray(json), false, XContentType.JSON)); |
| 323 | + assertThat(epe, TestMatchers.throwableWithMessage(containsString("must be a subset of the granted fields "))); |
| 324 | + assertThat(epe, TestMatchers.throwableWithMessage(containsString("f1"))); |
| 325 | + assertThat(epe, TestMatchers.throwableWithMessage(containsString("f2"))); |
| 326 | + assertThat(epe, TestMatchers.throwableWithMessage(containsString("f3"))); |
| 327 | + } |
299 | 328 | }
|
0 commit comments