|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
package org.elasticsearch.xpack.security.authz;
|
|
|
|
|
|
+import org.elasticsearch.action.ActionListener;
|
|
|
import org.elasticsearch.action.admin.cluster.health.ClusterHealthAction;
|
|
|
import org.elasticsearch.action.admin.cluster.state.ClusterStateAction;
|
|
|
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsAction;
|
|
@@ -27,6 +28,7 @@ import org.elasticsearch.common.bytes.BytesArray;
|
|
|
import org.elasticsearch.common.collect.MapBuilder;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.util.set.Sets;
|
|
|
+import org.elasticsearch.core.Tuple;
|
|
|
import org.elasticsearch.license.GetLicenseAction;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.transport.TransportRequest;
|
|
@@ -53,6 +55,7 @@ import org.elasticsearch.xpack.core.security.authc.esnative.NativeRealmSettings;
|
|
|
import org.elasticsearch.xpack.core.security.authc.file.FileRealmSettings;
|
|
|
import org.elasticsearch.xpack.core.security.authc.ldap.LdapRealmSettings;
|
|
|
import org.elasticsearch.xpack.core.security.authc.pki.PkiRealmSettings;
|
|
|
+import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine;
|
|
|
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.AuthorizationInfo;
|
|
|
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.PrivilegesCheckResult;
|
|
|
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.PrivilegesToCheck;
|
|
@@ -96,10 +99,12 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
import static java.util.Collections.emptyMap;
|
|
|
import static org.elasticsearch.common.util.set.Sets.newHashSet;
|
|
|
+import static org.elasticsearch.test.ActionListenerUtils.anyActionListener;
|
|
|
import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.RESTRICTED_INDICES;
|
|
|
import static org.elasticsearch.xpack.security.authz.AuthorizedIndicesTests.getRequestInfo;
|
|
|
import static org.hamcrest.Matchers.aMapWithSize;
|
|
|
import static org.hamcrest.Matchers.containsInAnyOrder;
|
|
|
+import static org.hamcrest.Matchers.emptyArray;
|
|
|
import static org.hamcrest.Matchers.emptyIterable;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.hasItem;
|
|
@@ -126,12 +131,54 @@ import static org.mockito.Mockito.when;
|
|
|
public class RBACEngineTests extends ESTestCase {
|
|
|
|
|
|
private RBACEngine engine;
|
|
|
+ private CompositeRolesStore rolesStore;
|
|
|
|
|
|
@Before
|
|
|
public void createEngine() {
|
|
|
final LoadAuthorizedIndicesTimeChecker.Factory timerFactory = mock(LoadAuthorizedIndicesTimeChecker.Factory.class);
|
|
|
when(timerFactory.newTimer(any())).thenReturn(LoadAuthorizedIndicesTimeChecker.NO_OP_CONSUMER);
|
|
|
- engine = new RBACEngine(Settings.EMPTY, mock(CompositeRolesStore.class), timerFactory);
|
|
|
+ rolesStore = mock(CompositeRolesStore.class);
|
|
|
+ engine = new RBACEngine(Settings.EMPTY, rolesStore, timerFactory);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testResolveAuthorizationInfoForEmptyRolesWithAuthentication() {
|
|
|
+ doAnswer(invocation -> {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ final var listener = (ActionListener<Tuple<Role, Role>>) invocation.getArgument(1);
|
|
|
+ listener.onResponse(new Tuple<>(Role.EMPTY, Role.EMPTY));
|
|
|
+ return null;
|
|
|
+ }).when(rolesStore).getRoles(any(), anyActionListener());
|
|
|
+
|
|
|
+ final PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
|
|
|
+ engine.resolveAuthorizationInfo(
|
|
|
+ new AuthorizationEngine.RequestInfo(
|
|
|
+ AuthenticationTestHelper.builder().build(),
|
|
|
+ mock(TransportRequest.class),
|
|
|
+ randomAlphaOfLengthBetween(20, 30),
|
|
|
+ null
|
|
|
+ ),
|
|
|
+ future
|
|
|
+ );
|
|
|
+
|
|
|
+ final AuthorizationInfo authorizationInfo = future.actionGet();
|
|
|
+ assertThat((String[]) authorizationInfo.asMap().get("user.roles"), emptyArray());
|
|
|
+ assertThat((String[]) authorizationInfo.getAuthenticatedUserAuthorizationInfo().asMap().get("user.roles"), emptyArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testResolveAuthorizationInfoForEmptyRoleWithSubject() {
|
|
|
+ doAnswer(invocation -> {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ final var listener = (ActionListener<Role>) invocation.getArgument(1);
|
|
|
+ listener.onResponse(Role.EMPTY);
|
|
|
+ return null;
|
|
|
+ }).when(rolesStore).getRole(any(), anyActionListener());
|
|
|
+
|
|
|
+ final PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
|
|
|
+ engine.resolveAuthorizationInfo(AuthenticationTestHelper.builder().build().getEffectiveSubject(), future);
|
|
|
+
|
|
|
+ final AuthorizationInfo authorizationInfo = future.actionGet();
|
|
|
+ assertThat((String[]) authorizationInfo.asMap().get("user.roles"), emptyArray());
|
|
|
+ assertThat((String[]) authorizationInfo.getAuthenticatedUserAuthorizationInfo().asMap().get("user.roles"), emptyArray());
|
|
|
}
|
|
|
|
|
|
public void testSameUserPermission() {
|