Search in sources :

Example 6 with MonitorHandler

use of io.cdap.cdap.gateway.handlers.MonitorHandler in project cdap by cdapio.

the class MonitorHandlerAuthorizationTest method testRestartServiceInstanceAuthorization.

@Test
public void testRestartServiceInstanceAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(ApplicationPermission.EXECUTE));
    FullHttpRequest request = mock(FullHttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.restartServiceInstance(request, responder, SERVICE_NAME, 0);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.restartServiceInstance(request, responder, SERVICE_NAME, 0);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponder(io.cdap.http.HttpResponder) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 7 with MonitorHandler

use of io.cdap.cdap.gateway.handlers.MonitorHandler in project cdap by cdapio.

the class MonitorHandlerAuthorizationTest method createMonitorHandler.

private static MonitorHandler createMonitorHandler(Authorizable authorizable, List<Permission> requiredPermissions) {
    Map<String, MasterServiceManager> masterServiceManagerMap = new HashMap<>();
    MasterServiceManager mockMasterServiceManager = mock(MasterServiceManager.class);
    when(mockMasterServiceManager.getInstances()).thenReturn(1);
    when(mockMasterServiceManager.isServiceEnabled()).thenReturn(true);
    when(mockMasterServiceManager.getMinInstances()).thenReturn(1);
    when(mockMasterServiceManager.getMaxInstances()).thenReturn(1);
    when(mockMasterServiceManager.setInstances(any(Integer.class))).thenReturn(true);
    masterServiceManagerMap.put(SERVICE_NAME, mockMasterServiceManager);
    ServiceStore mockServiceStore = mock(ServiceStore.class);
    InMemoryAccessController inMemoryAccessController = new InMemoryAccessController();
    inMemoryAccessController.grant(authorizable, MASTER_PRINCIPAL, Collections.unmodifiableSet(new HashSet<>(requiredPermissions)));
    AuthenticationContext authenticationContext = new AuthenticationTestContext();
    DefaultContextAccessEnforcer contextAccessEnforcer = new DefaultContextAccessEnforcer(authenticationContext, inMemoryAccessController);
    return new MonitorHandler(masterServiceManagerMap, mockServiceStore, contextAccessEnforcer);
}
Also used : AuthenticationContext(io.cdap.cdap.security.spi.authentication.AuthenticationContext) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) ServiceStore(io.cdap.cdap.app.store.ServiceStore) HashMap(java.util.HashMap) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) InMemoryAccessController(io.cdap.cdap.security.authorization.InMemoryAccessController) AuthenticationTestContext(io.cdap.cdap.security.auth.context.AuthenticationTestContext) DefaultContextAccessEnforcer(io.cdap.cdap.security.authorization.DefaultContextAccessEnforcer) HashSet(java.util.HashSet)

Example 8 with MonitorHandler

use of io.cdap.cdap.gateway.handlers.MonitorHandler in project cdap by cdapio.

the class MonitorHandlerAuthorizationTest method testRestartAllServiceInstancesAuthorization.

@Test
public void testRestartAllServiceInstancesAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(ApplicationPermission.EXECUTE));
    FullHttpRequest request = mock(FullHttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.restartAllServiceInstances(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.restartAllServiceInstances(request, responder, SERVICE_NAME);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponder(io.cdap.http.HttpResponder) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 9 with MonitorHandler

use of io.cdap.cdap.gateway.handlers.MonitorHandler in project cdap by cdapio.

the class MonitorHandlerAuthorizationTest method testGetSystemServiceLiveInfoAuthorization.

@Test
public void testGetSystemServiceLiveInfoAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(StandardPermission.GET));
    HttpRequest request = mock(HttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.getServiceLiveInfo(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.getServiceLiveInfo(request, responder, SERVICE_NAME);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpResponder(io.cdap.http.HttpResponder) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 10 with MonitorHandler

use of io.cdap.cdap.gateway.handlers.MonitorHandler in project cdap by caskdata.

the class MonitorHandlerAuthorizationTest method testUpdateServiceLogLevelsAuthorization.

@Test
public void testUpdateServiceLogLevelsAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(StandardPermission.UPDATE));
    Map<String, String> bodyArgs = new HashMap<>();
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/system/services/service/loglevels", Unpooled.copiedBuffer(GSON.toJson(bodyArgs), StandardCharsets.UTF_8));
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.updateServiceLogLevels(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.updateServiceLogLevels(request, responder, SERVICE_NAME);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponder(io.cdap.http.HttpResponder) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) HashMap(java.util.HashMap) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Aggregations

MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)22 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)18 HttpResponder (io.cdap.http.HttpResponder)18 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)18 Test (org.junit.Test)18 SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)8 ServiceStore (io.cdap.cdap.app.store.ServiceStore)4 InstanceId (io.cdap.cdap.proto.id.InstanceId)4 HashMap (java.util.HashMap)4 PrivateModule (com.google.inject.PrivateModule)2 MasterServiceManager (io.cdap.cdap.common.twill.MasterServiceManager)2 DatasetFramework (io.cdap.cdap.data2.dataset2.DatasetFramework)2 InMemoryDatasetFramework (io.cdap.cdap.data2.dataset2.InMemoryDatasetFramework)2 DatasetServiceStore (io.cdap.cdap.gateway.handlers.DatasetServiceStore)2 Instances (io.cdap.cdap.proto.Instances)2 AuthenticationTestContext (io.cdap.cdap.security.auth.context.AuthenticationTestContext)2 DefaultContextAccessEnforcer (io.cdap.cdap.security.authorization.DefaultContextAccessEnforcer)2 InMemoryAccessController (io.cdap.cdap.security.authorization.InMemoryAccessController)2