|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | +package org.elasticsearch.xpack.security; |
| 8 | + |
| 9 | +import org.elasticsearch.client.Request; |
| 10 | +import org.elasticsearch.client.RequestOptions; |
| 11 | +import org.elasticsearch.common.settings.SecureString; |
| 12 | +import org.elasticsearch.core.CheckedConsumer; |
| 13 | +import org.elasticsearch.test.TestSecurityClient; |
| 14 | +import org.elasticsearch.test.XContentTestUtils; |
| 15 | +import org.elasticsearch.xcontent.ObjectPath; |
| 16 | +import org.elasticsearch.xcontent.XContentType; |
| 17 | +import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; |
| 18 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 19 | +import org.elasticsearch.xpack.core.security.authz.permission.ResourcePrivileges; |
| 20 | +import org.elasticsearch.xpack.core.security.authz.privilege.ConfigurableClusterPrivilege; |
| 21 | +import org.elasticsearch.xpack.core.security.user.User; |
| 22 | +import org.hamcrest.Matchers; |
| 23 | +import org.junit.Before; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.stream.Collectors; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static org.hamcrest.Matchers.aMapWithSize; |
| 35 | +import static org.hamcrest.Matchers.equalTo; |
| 36 | + |
| 37 | +public class HasApplicationPrivilegesIT extends SecurityInBasicRestTestCase { |
| 38 | + |
| 39 | + private TestSecurityClient securityClient; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setupClient() { |
| 43 | + securityClient = new TestSecurityClient(adminClient()); |
| 44 | + } |
| 45 | + |
| 46 | + public void testUserWithWildcardPrivileges() throws Exception { |
| 47 | + var mainApplication = randomApplicationName(); |
| 48 | + |
| 49 | + // Privilege names that are defined application privileges, possible on the "main" app, possibly for another app. |
| 50 | + final Set<String> definedPrivilegeNames = new HashSet<>(); |
| 51 | + |
| 52 | + // All applications for which application privileges have been defined |
| 53 | + final Set<String> allApplications = new HashSet<>(); |
| 54 | + allApplications.add(mainApplication); |
| 55 | + CheckedConsumer<String, IOException> createApplicationPrivilege = (app) -> { |
| 56 | + final String privilegeName; |
| 57 | + // If this is the first privilege for this app, then maybe (randomly) reuse a privilege name that was defined for another app |
| 58 | + if (allApplications.contains(app) == false && definedPrivilegeNames.size() > 0 && randomBoolean()) { |
| 59 | + privilegeName = randomFrom(definedPrivilegeNames); |
| 60 | + } else { |
| 61 | + privilegeName = randomValueOtherThanMany(definedPrivilegeNames::contains, this::randomPrivilegeName); |
| 62 | + } |
| 63 | + |
| 64 | + createApplicationPrivilege(app, privilegeName, randomArray(1, 4, String[]::new, () -> randomActionName())); |
| 65 | + allApplications.add(app); |
| 66 | + definedPrivilegeNames.add(privilegeName); |
| 67 | + }; |
| 68 | + |
| 69 | + // Create 0 or more application privileges for this application |
| 70 | + for (int i = randomIntBetween(0, 2); i > 0; i--) { |
| 71 | + createApplicationPrivilege.accept(mainApplication); |
| 72 | + } |
| 73 | + |
| 74 | + // Create 0 or more application privileges for other applications |
| 75 | + for (int i = randomIntBetween(0, 3); i > 0; i--) { |
| 76 | + createApplicationPrivilege.accept(randomValueOtherThan(mainApplication, this::randomApplicationName)); |
| 77 | + } |
| 78 | + |
| 79 | + // Define a role with all privileges (by wildcard) for this application |
| 80 | + var roleName = randomAlphaOfLengthBetween(6, 10); |
| 81 | + var singleAppOnly = randomBoolean(); |
| 82 | + createRole(roleName, singleAppOnly ? mainApplication : "*", new String[] { "*" }, new String[] { "*" }); |
| 83 | + |
| 84 | + final Set<String> allRoles = new HashSet<>(); |
| 85 | + allRoles.add(roleName); |
| 86 | + |
| 87 | + // Create 0 or more additional roles with privileges for one of the applications |
| 88 | + for (int i = randomIntBetween(0, 3); i > 0; i--) { |
| 89 | + var extraRoleName = randomValueOtherThanMany(allRoles::contains, () -> randomAlphaOfLengthBetween(8, 16)); |
| 90 | + final String privilegeName = definedPrivilegeNames.size() > 0 && randomBoolean() |
| 91 | + ? randomFrom(definedPrivilegeNames) // This may or may not correspond to the application we pick. Both are valid tests |
| 92 | + : randomPrivilegeName(); |
| 93 | + createRole( |
| 94 | + extraRoleName, |
| 95 | + randomFrom(allApplications), |
| 96 | + new String[] { privilegeName }, |
| 97 | + new String[] { "data/" + randomAlphaOfLength(6) } |
| 98 | + ); |
| 99 | + allRoles.add(extraRoleName); |
| 100 | + } |
| 101 | + |
| 102 | + // Create a user with all (might be 1 or more) of the roles |
| 103 | + var username = randomAlphaOfLengthBetween(8, 12); |
| 104 | + var password = new SecureString(randomAlphaOfLength(12).toCharArray()); |
| 105 | + createUser(username, password, allRoles); |
| 106 | + |
| 107 | + // Assert that has_privileges returns true for any arbitrary privilege or action in that application |
| 108 | + var reqOptions = RequestOptions.DEFAULT.toBuilder() |
| 109 | + .addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(username, password)) |
| 110 | + .build(); |
| 111 | + |
| 112 | + final String testPrivilege; |
| 113 | + if (randomBoolean() && definedPrivilegeNames.size() > 0) { |
| 114 | + testPrivilege = randomFrom(definedPrivilegeNames); |
| 115 | + } else if (randomBoolean()) { |
| 116 | + testPrivilege = randomPrivilegeName(); |
| 117 | + } else { |
| 118 | + testPrivilege = randomActionName(); |
| 119 | + } |
| 120 | + var testResource = randomAlphaOfLengthBetween(4, 12); |
| 121 | + |
| 122 | + { |
| 123 | + final List<ResourcePrivileges> shouldHavePrivileges = hasPrivilege( |
| 124 | + reqOptions, |
| 125 | + mainApplication, |
| 126 | + new String[] { testPrivilege }, |
| 127 | + new String[] { testResource } |
| 128 | + ); |
| 129 | + |
| 130 | + assertSinglePrivilege(shouldHavePrivileges, testResource, testPrivilege, true); |
| 131 | + } |
| 132 | + |
| 133 | + if (singleAppOnly) { |
| 134 | + List<ResourcePrivileges> shouldNotHavePrivileges = hasPrivilege( |
| 135 | + reqOptions, |
| 136 | + randomValueOtherThanMany(allApplications::contains, this::randomApplicationName), |
| 137 | + new String[] { testPrivilege }, |
| 138 | + new String[] { testResource } |
| 139 | + ); |
| 140 | + assertSinglePrivilege(shouldNotHavePrivileges, testResource, testPrivilege, false); |
| 141 | + |
| 142 | + if (allApplications.size() > 1) { // there is an app other than the main app |
| 143 | + shouldNotHavePrivileges = hasPrivilege( |
| 144 | + reqOptions, |
| 145 | + randomValueOtherThan(mainApplication, () -> randomFrom(allApplications)), |
| 146 | + new String[] { testPrivilege }, |
| 147 | + new String[] { testResource } |
| 148 | + ); |
| 149 | + assertSinglePrivilege(shouldNotHavePrivileges, testResource, testPrivilege, false); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private void assertSinglePrivilege( |
| 155 | + List<ResourcePrivileges> hasPrivilegesResult, |
| 156 | + String expectedResource, |
| 157 | + String expectedPrivilegeName, |
| 158 | + boolean shoudHavePrivilege |
| 159 | + ) { |
| 160 | + assertThat(hasPrivilegesResult, Matchers.hasSize(1)); |
| 161 | + assertThat(hasPrivilegesResult.get(0).getResource(), equalTo(expectedResource)); |
| 162 | + assertThat(hasPrivilegesResult.get(0).getPrivileges(), Matchers.hasEntry(expectedPrivilegeName, shoudHavePrivilege)); |
| 163 | + assertThat(hasPrivilegesResult.get(0).getPrivileges(), aMapWithSize(1)); |
| 164 | + } |
| 165 | + |
| 166 | + private List<ResourcePrivileges> hasPrivilege(RequestOptions requestOptions, String appName, String[] privileges, String[] resources) |
| 167 | + throws IOException { |
| 168 | + logger.info("Checking privileges: App=[{}] Privileges=[{}] Resources=[{}]", appName, privileges, resources); |
| 169 | + Request req = new Request("POST", "/_security/user/_has_privileges"); |
| 170 | + req.setOptions(requestOptions); |
| 171 | + Map<String, Object> body = Map.ofEntries( |
| 172 | + Map.entry( |
| 173 | + "application", |
| 174 | + List.of( |
| 175 | + Map.ofEntries( |
| 176 | + Map.entry("application", appName), |
| 177 | + Map.entry("privileges", List.of(privileges)), |
| 178 | + Map.entry("resources", List.of(resources)) |
| 179 | + ) |
| 180 | + ) |
| 181 | + ) |
| 182 | + ); |
| 183 | + req.setJsonEntity(XContentTestUtils.convertToXContent(body, XContentType.JSON).utf8ToString()); |
| 184 | + final Map<String, Object> response = responseAsMap(client().performRequest(req)); |
| 185 | + logger.info("Has privileges: [{}]", response); |
| 186 | + final Map<String, Object> privilegesByResource = ObjectPath.eval("application." + appName, response); |
| 187 | + return Stream.of(resources).map(res -> { |
| 188 | + Map<String, Boolean> priv = ObjectPath.eval(res, privilegesByResource); |
| 189 | + return ResourcePrivileges.builder(res).addPrivileges(priv).build(); |
| 190 | + }).collect(Collectors.toList()); |
| 191 | + } |
| 192 | + |
| 193 | + private void createUser(String username, SecureString password, Set<String> roles) throws IOException { |
| 194 | + logger.info("Create User [{}] with roles [{}]", username, roles); |
| 195 | + securityClient.putUser(new User(username, roles.toArray(String[]::new)), password); |
| 196 | + } |
| 197 | + |
| 198 | + private void createRole(String roleName, String applicationName, String[] privileges, String[] resources) throws IOException { |
| 199 | + logger.info( |
| 200 | + "Create role [{}] with privileges App=[{}] Privileges=[{}] Resources=[{}]", |
| 201 | + roleName, |
| 202 | + applicationName, |
| 203 | + privileges, |
| 204 | + resources |
| 205 | + ); |
| 206 | + securityClient.putRole( |
| 207 | + new RoleDescriptor( |
| 208 | + roleName, |
| 209 | + new String[0], // cluster |
| 210 | + new RoleDescriptor.IndicesPrivileges[0], |
| 211 | + new RoleDescriptor.ApplicationResourcePrivileges[] { |
| 212 | + RoleDescriptor.ApplicationResourcePrivileges.builder() |
| 213 | + .application(applicationName) |
| 214 | + .privileges(privileges) |
| 215 | + .resources(resources) |
| 216 | + .build() }, |
| 217 | + new ConfigurableClusterPrivilege[0], |
| 218 | + new String[0],// run-as |
| 219 | + Map.of(), // metadata |
| 220 | + Map.of() // transient metadata |
| 221 | + ) |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + private void createApplicationPrivilege(String applicationName, String privilegeName, String[] actions) { |
| 226 | + logger.info("Create app privilege App=[{}] Privilege=[{}] Actions=[{}]", applicationName, privilegeName, actions); |
| 227 | + try { |
| 228 | + securityClient.putApplicationPrivilege(applicationName, privilegeName, actions); |
| 229 | + } catch (IOException e) { |
| 230 | + throw new AssertionError( |
| 231 | + "Failed to create application privilege app=[" |
| 232 | + + applicationName |
| 233 | + + "], privilege=[" |
| 234 | + + privilegeName |
| 235 | + + "], actions=[" |
| 236 | + + String.join(",", actions) |
| 237 | + + "]", |
| 238 | + e |
| 239 | + ); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + private String randomApplicationName() { |
| 244 | + return randomAlphaOfLength(1).toLowerCase(Locale.ROOT) + randomAlphaOfLengthBetween(3, 7); |
| 245 | + } |
| 246 | + |
| 247 | + private String randomPrivilegeName() { |
| 248 | + if (randomBoolean()) { |
| 249 | + return randomAlphaOfLength(1).toLowerCase(Locale.ROOT) + randomAlphaOfLengthBetween(3, 7); |
| 250 | + } else { |
| 251 | + return randomAlphaOfLengthBetween(2, 4).toLowerCase(Locale.ROOT) + randomFrom(".", "_", "-") + randomAlphaOfLengthBetween(2, 6); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + private String randomActionName() { |
| 256 | + return randomAlphaOfLengthBetween(3, 5) + ":" + randomAlphaOfLengthBetween(3, 5); |
| 257 | + } |
| 258 | + |
| 259 | +} |
0 commit comments