|
@@ -35,11 +35,13 @@ import org.mockito.Mockito;
|
|
|
import static org.elasticsearch.xpack.security.operator.OperatorPrivileges.NOOP_OPERATOR_PRIVILEGES_SERVICE;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.notNullValue;
|
|
|
import static org.mockito.Matchers.any;
|
|
|
import static org.mockito.Matchers.anyString;
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
|
|
import static org.mockito.Mockito.when;
|
|
@@ -59,16 +61,20 @@ public class OperatorPrivilegesTests extends ESTestCase {
|
|
|
operatorPrivilegesService = new DefaultOperatorPrivilegesService(xPackLicenseState, fileOperatorUsersStore, operatorOnlyRegistry);
|
|
|
}
|
|
|
|
|
|
- public void testWillNotProcessWhenFeatureIsDisabledOrLicenseDoesNotSupport() {
|
|
|
- final Settings settings = Settings.builder()
|
|
|
- .put("xpack.security.operator_privileges.enabled", randomBoolean())
|
|
|
- .build();
|
|
|
- when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(false);
|
|
|
- final ThreadContext threadContext = new ThreadContext(settings);
|
|
|
+ public void testWillMarkThreadContextForAllLicenses() {
|
|
|
+ when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(randomBoolean());
|
|
|
|
|
|
- operatorPrivilegesService.maybeMarkOperatorUser(mock(Authentication.class), threadContext);
|
|
|
- verifyZeroInteractions(fileOperatorUsersStore);
|
|
|
+ final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
|
|
|
+ final Authentication authentication = mock(Authentication.class);
|
|
|
+ when(authentication.getUser()).thenReturn(new User(randomAlphaOfLengthBetween(3, 8)));
|
|
|
+ operatorPrivilegesService.maybeMarkOperatorUser(authentication, threadContext);
|
|
|
+ verify(fileOperatorUsersStore, times(1)).isOperatorUser(authentication);
|
|
|
+ assertThat(threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY), notNullValue());
|
|
|
+ }
|
|
|
|
|
|
+ public void testWillNotCheckWhenLicenseDoesNotSupport() {
|
|
|
+ when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(false);
|
|
|
+ final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
|
|
|
final ElasticsearchSecurityException e =
|
|
|
operatorPrivilegesService.check(mock(Authentication.class), "cluster:action", mock(TransportRequest.class), threadContext);
|
|
|
assertNull(e);
|