|
48 | 48 | import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor;
|
49 | 49 | import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilege;
|
50 | 50 | import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges.ManageApplicationPrivileges;
|
| 51 | +import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; |
51 | 52 | import org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege;
|
52 | 53 | import org.elasticsearch.xpack.core.security.authz.privilege.Privilege;
|
53 | 54 | import org.elasticsearch.xpack.core.security.user.User;
|
@@ -493,6 +494,130 @@ public void testCheckingIndexPermissionsDefinedOnDifferentPatterns() throws Exce
|
493 | 494 | ));
|
494 | 495 | }
|
495 | 496 |
|
| 497 | + public void testCheckExplicitRestrictedIndexPermissions() throws Exception { |
| 498 | + User user = new User(randomAlphaOfLengthBetween(4, 12)); |
| 499 | + Authentication authentication = mock(Authentication.class); |
| 500 | + when(authentication.getUser()).thenReturn(user); |
| 501 | + final boolean restrictedIndexPermission = randomBoolean(); |
| 502 | + final boolean restrictedMonitorPermission = randomBoolean(); |
| 503 | + Role role = Role.builder("role") |
| 504 | + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, restrictedIndexPermission, ".sec*") |
| 505 | + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.MONITOR, restrictedMonitorPermission, ".security*") |
| 506 | + .build(); |
| 507 | + RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); |
| 508 | + |
| 509 | + String explicitRestrictedIndex = randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES); |
| 510 | + HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() |
| 511 | + .indices(new String[] {".secret-non-restricted", explicitRestrictedIndex}) |
| 512 | + .privileges("index", "monitor") |
| 513 | + .allowRestrictedIndices(false) // explicit false for test |
| 514 | + .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); |
| 515 | + assertThat(response.isCompleteMatch(), is(false)); |
| 516 | + assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); |
| 517 | + assertThat(response.getIndexPrivileges(), containsInAnyOrder( |
| 518 | + ResourcePrivileges.builder(".secret-non-restricted") // matches ".sec*" but not ".security*" |
| 519 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 520 | + .put("index", true).put("monitor", false).map()).build(), |
| 521 | + ResourcePrivileges.builder(explicitRestrictedIndex) // matches both ".sec*" and ".security*" |
| 522 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 523 | + .put("index", restrictedIndexPermission).put("monitor", restrictedMonitorPermission).map()).build())); |
| 524 | + |
| 525 | + explicitRestrictedIndex = randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES); |
| 526 | + response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() |
| 527 | + .indices(new String[] {".secret-non-restricted", explicitRestrictedIndex}) |
| 528 | + .privileges("index", "monitor") |
| 529 | + .allowRestrictedIndices(true) // explicit true for test |
| 530 | + .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); |
| 531 | + assertThat(response.isCompleteMatch(), is(false)); |
| 532 | + assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); |
| 533 | + assertThat(response.getIndexPrivileges(), containsInAnyOrder( |
| 534 | + ResourcePrivileges.builder(".secret-non-restricted") // matches ".sec*" but not ".security*" |
| 535 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 536 | + .put("index", true).put("monitor", false).map()).build(), |
| 537 | + ResourcePrivileges.builder(explicitRestrictedIndex) // matches both ".sec*" and ".security*" |
| 538 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 539 | + .put("index", restrictedIndexPermission).put("monitor", restrictedMonitorPermission).map()).build())); |
| 540 | + } |
| 541 | + |
| 542 | + public void testCheckRestrictedIndexWildcardPermissions() throws Exception { |
| 543 | + User user = new User(randomAlphaOfLengthBetween(4, 12)); |
| 544 | + Authentication authentication = mock(Authentication.class); |
| 545 | + when(authentication.getUser()).thenReturn(user); |
| 546 | + Role role = Role.builder("role") |
| 547 | + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, false, ".sec*") |
| 548 | + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.MONITOR, true, ".security*") |
| 549 | + .build(); |
| 550 | + RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); |
| 551 | + |
| 552 | + HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() |
| 553 | + .indices(".sec*", ".security*") |
| 554 | + .privileges("index", "monitor") |
| 555 | + .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); |
| 556 | + assertThat(response.isCompleteMatch(), is(false)); |
| 557 | + assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); |
| 558 | + assertThat(response.getIndexPrivileges(), containsInAnyOrder( |
| 559 | + ResourcePrivileges.builder(".sec*") |
| 560 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 561 | + .put("index", true).put("monitor", false).map()).build(), |
| 562 | + ResourcePrivileges.builder(".security*") |
| 563 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 564 | + .put("index", true).put("monitor", true).map()).build() |
| 565 | + )); |
| 566 | + |
| 567 | + response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() |
| 568 | + .indices(".sec*", ".security*") |
| 569 | + .privileges("index", "monitor") |
| 570 | + .allowRestrictedIndices(true) |
| 571 | + .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); |
| 572 | + assertThat(response.isCompleteMatch(), is(false)); |
| 573 | + assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); |
| 574 | + assertThat(response.getIndexPrivileges(), containsInAnyOrder( |
| 575 | + ResourcePrivileges.builder(".sec*") |
| 576 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 577 | + .put("index", false).put("monitor", false).map()).build(), |
| 578 | + ResourcePrivileges.builder(".security*") |
| 579 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 580 | + .put("index", false).put("monitor", true).map()).build() |
| 581 | + )); |
| 582 | + |
| 583 | + role = Role.builder("role") |
| 584 | + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, true, ".sec*") |
| 585 | + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.MONITOR, false, ".security*") |
| 586 | + .build(); |
| 587 | + authzInfo = new RBACAuthorizationInfo(role, null); |
| 588 | + |
| 589 | + response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() |
| 590 | + .indices(".sec*", ".security*") |
| 591 | + .privileges("index", "monitor") |
| 592 | + .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); |
| 593 | + assertThat(response.isCompleteMatch(), is(false)); |
| 594 | + assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); |
| 595 | + assertThat(response.getIndexPrivileges(), containsInAnyOrder( |
| 596 | + ResourcePrivileges.builder(".sec*") |
| 597 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 598 | + .put("index", true).put("monitor", false).map()).build(), |
| 599 | + ResourcePrivileges.builder(".security*") |
| 600 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 601 | + .put("index", true).put("monitor", true).map()).build() |
| 602 | + )); |
| 603 | + |
| 604 | + response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() |
| 605 | + .indices(".sec*", ".security*") |
| 606 | + .privileges("index", "monitor") |
| 607 | + .allowRestrictedIndices(true) |
| 608 | + .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); |
| 609 | + assertThat(response.isCompleteMatch(), is(false)); |
| 610 | + assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); |
| 611 | + assertThat(response.getIndexPrivileges(), containsInAnyOrder( |
| 612 | + ResourcePrivileges.builder(".sec*") |
| 613 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 614 | + .put("index", true).put("monitor", false).map()).build(), |
| 615 | + ResourcePrivileges.builder(".security*") |
| 616 | + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<String, Boolean>()) |
| 617 | + .put("index", true).put("monitor", false).map()).build() |
| 618 | + )); |
| 619 | + } |
| 620 | + |
496 | 621 | public void testCheckingApplicationPrivilegesOnDifferentApplicationsAndResources() throws Exception {
|
497 | 622 | List<ApplicationPrivilegeDescriptor> privs = new ArrayList<>();
|
498 | 623 | final ApplicationPrivilege app1Read = defineApplicationPrivilege(privs, "app1", "read", "data:read/*");
|
|
0 commit comments