|
11 | 11 | import org.elasticsearch.test.ESTestCase; |
12 | 12 | import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; |
13 | 13 | import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 14 | +import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; |
| 15 | +import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; |
14 | 16 |
|
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
15 | 19 | import java.util.Map; |
| 20 | +import java.util.Set; |
16 | 21 | import java.util.concurrent.ExecutionException; |
17 | 22 |
|
18 | 23 | import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.RESTRICTED_INDICES; |
19 | 24 | import static org.hamcrest.Matchers.emptyArray; |
20 | 25 | import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.hamcrest.Matchers.is; |
21 | 27 | import static org.hamcrest.Matchers.notNullValue; |
22 | 28 | import static org.hamcrest.Matchers.nullValue; |
23 | 29 |
|
@@ -56,4 +62,84 @@ public void testHasPrivilegesCache() throws ExecutionException { |
56 | 62 | assertThat(role.checkPrivilegesWithCache(privilegesToCheck), equalTo(privilegesCheckResult)); |
57 | 63 | } |
58 | 64 |
|
| 65 | + public void testBuildFromRoleDescriptorWithApplicationPrivileges() { |
| 66 | + final boolean wildcardApplication = randomBoolean(); |
| 67 | + final boolean wildcardPrivileges = randomBoolean(); |
| 68 | + final boolean wildcardResources = randomBoolean(); |
| 69 | + final RoleDescriptor.ApplicationResourcePrivileges applicationPrivilege = RoleDescriptor.ApplicationResourcePrivileges.builder() |
| 70 | + .application(wildcardApplication ? "*" : randomAlphaOfLengthBetween(5, 12)) |
| 71 | + // concrete privileges need to be prefixed with lower case letters to be considered valid, so use "app" |
| 72 | + .privileges(wildcardPrivileges ? "*" : "app" + randomAlphaOfLengthBetween(5, 12)) |
| 73 | + .resources(wildcardResources ? new String[] { "*" } : generateRandomStringArray(6, randomIntBetween(4, 8), false, false)) |
| 74 | + .build(); |
| 75 | + |
| 76 | + final String allowedApplicationActionPattern = randomAlphaOfLengthBetween(5, 12); |
| 77 | + final SimpleRole role = Role.buildFromRoleDescriptor( |
| 78 | + new RoleDescriptor( |
| 79 | + "r1", |
| 80 | + null, |
| 81 | + null, |
| 82 | + new RoleDescriptor.ApplicationResourcePrivileges[] { applicationPrivilege }, |
| 83 | + null, |
| 84 | + null, |
| 85 | + null, |
| 86 | + null |
| 87 | + ), |
| 88 | + new FieldPermissionsCache(Settings.EMPTY), |
| 89 | + RESTRICTED_INDICES, |
| 90 | + wildcardPrivileges |
| 91 | + ? List.of() |
| 92 | + : List.of( |
| 93 | + new ApplicationPrivilegeDescriptor( |
| 94 | + applicationPrivilege.getApplication(), |
| 95 | + Arrays.stream(applicationPrivilege.getPrivileges()).iterator().next(), |
| 96 | + Set.of(allowedApplicationActionPattern), |
| 97 | + Map.of() |
| 98 | + ) |
| 99 | + ) |
| 100 | + ); |
| 101 | + assertThat( |
| 102 | + "expected grant for role with application privilege to be: " + applicationPrivilege, |
| 103 | + role.application() |
| 104 | + .grants( |
| 105 | + new ApplicationPrivilege( |
| 106 | + wildcardApplication ? randomAlphaOfLengthBetween(1, 10) : applicationPrivilege.getApplication(), |
| 107 | + wildcardPrivileges ? Set.of(randomAlphaOfLengthBetween(1, 10)) : Set.of(applicationPrivilege.getPrivileges()), |
| 108 | + wildcardPrivileges ? randomAlphaOfLengthBetween(1, 10) : allowedApplicationActionPattern |
| 109 | + ), |
| 110 | + wildcardResources ? randomAlphaOfLengthBetween(1, 10) : randomFrom(applicationPrivilege.getResources()) |
| 111 | + ), |
| 112 | + is(true) |
| 113 | + ); |
| 114 | + // This gives decent but not complete coverage of denial cases; for any non-wildcard field we pick a mismatched value to force a |
| 115 | + // denial |
| 116 | + assertThat( |
| 117 | + "expected grant for role with application privilege to be: " + applicationPrivilege, |
| 118 | + role.application() |
| 119 | + .grants( |
| 120 | + new ApplicationPrivilege( |
| 121 | + false == wildcardApplication |
| 122 | + ? randomValueOtherThan(applicationPrivilege.getApplication(), () -> randomAlphaOfLengthBetween(1, 10)) |
| 123 | + : randomAlphaOfLengthBetween(1, 10), |
| 124 | + false == wildcardPrivileges |
| 125 | + ? randomValueOtherThan( |
| 126 | + Set.of(applicationPrivilege.getPrivileges()), |
| 127 | + () -> Set.of(randomAlphaOfLengthBetween(1, 10)) |
| 128 | + ) |
| 129 | + : Set.of(randomAlphaOfLengthBetween(1, 10)), |
| 130 | + false == wildcardPrivileges |
| 131 | + ? randomValueOtherThan(allowedApplicationActionPattern, () -> randomAlphaOfLengthBetween(1, 10)) |
| 132 | + : randomAlphaOfLengthBetween(1, 10) |
| 133 | + ), |
| 134 | + false == wildcardResources |
| 135 | + ? randomValueOtherThanMany( |
| 136 | + it -> List.of(applicationPrivilege.getResources()).contains(it), |
| 137 | + () -> randomAlphaOfLengthBetween(1, 10) |
| 138 | + ) |
| 139 | + : randomAlphaOfLengthBetween(1, 10) |
| 140 | + ), |
| 141 | + // If all are wildcards, then we necessarily get a grant, otherwise expect a denial |
| 142 | + is(wildcardApplication && wildcardPrivileges && wildcardResources) |
| 143 | + ); |
| 144 | + } |
59 | 145 | } |
0 commit comments