Search in sources :

Example 26 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.

the class MonitorHandler method resetServiceLogLevels.

/**
 * Reset the log levels of the service.
 * All loggers will be reset to the level when the service started.
 */
@Path("system/services/{service-name}/resetloglevels")
@POST
public void resetServiceLogLevels(FullHttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.UPDATE);
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    if (!masterServiceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Failed to reset log levels for service %s " + "because the service is not enabled", serviceName));
    }
    try {
        Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
        masterServiceManager.resetServiceLogLevels(loggerNames == null ? Collections.emptySet() : loggerNames);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        throw new ServiceUnavailableException(String.format("Failed to reset log levels for service %s " + "because the service may not be ready yet", serviceName));
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid Json in the body");
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) JsonSyntaxException(com.google.gson.JsonSyntaxException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 27 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.

the class MonitorHandler method getServiceLiveInfo.

/**
 * Returns the live info of CDAP Services
 */
@Path("/system/services/{service-name}/live-info")
@GET
public void getServiceLiveInfo(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.GET);
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    if (serviceManager.isServiceEnabled()) {
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(serviceManager.getLiveInfo()));
    } else {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) NotFoundException(io.cdap.cdap.common.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 28 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId 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)

Example 29 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.

the class MonitorHandlerAuthorizationTest method testResetServiceLogLevelsAuthorization.

@Test
public void testResetServiceLogLevelsAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(StandardPermission.UPDATE));
    List<String> bodyArgs = new ArrayList<>();
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "system/services/service/resetloglevels", Unpooled.copiedBuffer(GSON.toJson(bodyArgs), StandardCharsets.UTF_8));
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.resetServiceLogLevels(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.resetServiceLogLevels(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) ArrayList(java.util.ArrayList) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 30 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.

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)

Aggregations

SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)38 NotFoundException (io.cdap.cdap.common.NotFoundException)22 Test (org.junit.Test)16 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)14 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)14 HttpResponder (io.cdap.http.HttpResponder)14 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)14 BadRequestException (io.cdap.cdap.common.BadRequestException)12 ForbiddenException (io.cdap.cdap.common.ForbiddenException)12 MasterServiceManager (io.cdap.cdap.common.twill.MasterServiceManager)12 Path (javax.ws.rs.Path)12 HttpResponse (io.cdap.common.http.HttpResponse)8 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)8 URL (java.net.URL)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)6 GET (javax.ws.rs.GET)6 Instances (io.cdap.cdap.proto.Instances)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 PUT (javax.ws.rs.PUT)4