|
@@ -58,10 +58,14 @@ import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.Privilege
|
|
|
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.PrivilegesToCheck;
|
|
|
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges;
|
|
|
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges;
|
|
|
+import org.elasticsearch.xpack.core.security.authz.permission.ApplicationPermission;
|
|
|
+import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission;
|
|
|
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions;
|
|
|
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition;
|
|
|
+import org.elasticsearch.xpack.core.security.authz.permission.IndicesPermission;
|
|
|
import org.elasticsearch.xpack.core.security.authz.permission.ResourcePrivileges;
|
|
|
import org.elasticsearch.xpack.core.security.authz.permission.Role;
|
|
|
+import org.elasticsearch.xpack.core.security.authz.permission.RunAsPermission;
|
|
|
import org.elasticsearch.xpack.core.security.authz.permission.SimpleRole;
|
|
|
import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege;
|
|
|
import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor;
|
|
@@ -105,6 +109,7 @@ import static org.hamcrest.Matchers.is;
|
|
|
import static org.hamcrest.Matchers.iterableWithSize;
|
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
|
import static org.hamcrest.Matchers.nullValue;
|
|
|
+import static org.hamcrest.Matchers.sameInstance;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
@@ -1505,6 +1510,39 @@ public class RBACEngineTests extends ESTestCase {
|
|
|
verify(supplier, never()).get();
|
|
|
}
|
|
|
|
|
|
+ public void testGetUserPrivilegesThrowsIaeForUnsupportedOperation() {
|
|
|
+ final RBACAuthorizationInfo authorizationInfo = mock(RBACAuthorizationInfo.class);
|
|
|
+ final Role role = mock(Role.class);
|
|
|
+ when(authorizationInfo.getRole()).thenReturn(role);
|
|
|
+ when(role.cluster()).thenReturn(ClusterPermission.NONE);
|
|
|
+ when(role.indices()).thenReturn(IndicesPermission.NONE);
|
|
|
+ when(role.application()).thenReturn(ApplicationPermission.NONE);
|
|
|
+ when(role.runAs()).thenReturn(RunAsPermission.NONE);
|
|
|
+
|
|
|
+ final UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException();
|
|
|
+ switch (randomIntBetween(0, 3)) {
|
|
|
+ case 0 -> when(role.cluster()).thenThrow(unsupportedOperationException);
|
|
|
+ case 1 -> when(role.indices()).thenThrow(unsupportedOperationException);
|
|
|
+ case 2 -> when(role.application()).thenThrow(unsupportedOperationException);
|
|
|
+ case 3 -> when(role.runAs()).thenThrow(unsupportedOperationException);
|
|
|
+ default -> throw new IllegalStateException("unknown case number");
|
|
|
+ }
|
|
|
+
|
|
|
+ final PlainActionFuture<GetUserPrivilegesResponse> future = new PlainActionFuture<>();
|
|
|
+ engine.getUserPrivileges(authorizationInfo, future);
|
|
|
+
|
|
|
+ final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, future::actionGet);
|
|
|
+
|
|
|
+ assertThat(
|
|
|
+ e.getMessage(),
|
|
|
+ equalTo(
|
|
|
+ "Cannot retrieve privileges for API keys with assigned role descriptors. "
|
|
|
+ + "Please use the Get API key information API https://ela.st/es-api-get-api-key"
|
|
|
+ )
|
|
|
+ );
|
|
|
+ assertThat(e.getCause(), sameInstance(unsupportedOperationException));
|
|
|
+ }
|
|
|
+
|
|
|
private GetUserPrivilegesResponse.Indices findIndexPrivilege(Set<GetUserPrivilegesResponse.Indices> indices, String name) {
|
|
|
return indices.stream().filter(i -> i.getIndices().contains(name)).findFirst().get();
|
|
|
}
|