Search in sources :

Example 1 with MonitorHandler

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

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 2 with MonitorHandler

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

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 3 with MonitorHandler

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

the class MonitorHandlerAuthorizationTest method testGetServiceSpecAuthorization.

@Test
public void testGetServiceSpecAuthorization() throws Exception {
    InstanceId instanceId = InstanceId.SELF;
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(instanceId, EntityType.SYSTEM_SERVICE), Arrays.asList(StandardPermission.LIST));
    HttpRequest request = mock(HttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.getServiceSpec(request, responder);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.getServiceSpec(request, responder);
}
Also used : 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) InstanceId(io.cdap.cdap.proto.id.InstanceId) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 4 with MonitorHandler

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

the class MonitorHandlerAuthorizationTest method testGetLatestRestartServiceInstanceStatusAuthorization.

@Test
public void testGetLatestRestartServiceInstanceStatusAuthorization() 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.getLatestRestartServiceInstanceStatus(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.getLatestRestartServiceInstanceStatus(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 5 with MonitorHandler

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

the class MonitorHandlerAuthorizationTest method testSetServiceInstanceAuthorization.

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

Aggregations

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