Search in sources :

Example 16 with SystemServiceId

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

the class MonitorHandler method restartInstances.

private void restartInstances(HttpResponder responder, String serviceName, int instanceId, boolean restartAll) throws Exception {
    long startTimeMs = System.currentTimeMillis();
    boolean isSuccess = true;
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, ApplicationPermission.EXECUTE);
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    try {
        if (!masterServiceManager.isServiceEnabled()) {
            String message = String.format("Failed to restart instance for %s because the service is not enabled.", serviceName);
            LOG.debug(message);
            isSuccess = false;
            throw new ForbiddenException(message);
        }
        if (restartAll) {
            masterServiceManager.restartAllInstances();
        } else {
            if (instanceId < 0 || instanceId >= masterServiceManager.getInstances()) {
                throw new IllegalArgumentException();
            }
            masterServiceManager.restartInstances(instanceId);
        }
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        String message = String.format("Failed to restart instance for %s because the service may not be ready yet", serviceName);
        LOG.debug(message, ise);
        isSuccess = false;
        throw new ServiceUnavailableException(message);
    } catch (IllegalArgumentException iex) {
        String message = String.format("Failed to restart instance %d for service: %s because invalid instance id", instanceId, serviceName);
        LOG.debug(message, iex);
        isSuccess = false;
        throw new BadRequestException(message);
    } catch (Exception ex) {
        LOG.warn(String.format("Exception when trying to restart instances for service %s", serviceName), ex);
        isSuccess = false;
        throw new Exception(String.format("Error restarting instance %d for service: %s", instanceId, serviceName));
    } finally {
        long endTimeMs = System.currentTimeMillis();
        if (restartAll) {
            serviceStore.setRestartAllInstancesRequest(serviceName, startTimeMs, endTimeMs, isSuccess);
        } else {
            serviceStore.setRestartInstanceRequest(serviceName, startTimeMs, endTimeMs, isSuccess, instanceId);
        }
    }
}
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) BadRequestException(io.cdap.cdap.common.BadRequestException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ForbiddenException(io.cdap.cdap.common.ForbiddenException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 17 with SystemServiceId

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

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 18 with SystemServiceId

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

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

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

the class AuditMessageTest method testAccessMessage.

@Test
public void testAccessMessage() throws Exception {
    String workerAccessJson = "{\"version\":2,\"time\":2000,\"metadataEntity\":{\"details\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\"}," + "\"type\":\"dataset\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"WRITE\"," + "\"accessor\":{\"namespace\":\"ns1\",\"application\":\"app1\",\"version\":\"v1\",\"type\":\"Worker\"," + "\"program\":\"worker1\",\"run\":\"run1\",\"entity\":\"PROGRAM_RUN\"}}}";
    AuditMessage workerAccess = new AuditMessage(2000L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.WRITE, new NamespaceId("ns1").app("app1", "v1").worker("worker1").run("run1")));
    Assert.assertEquals(jsonToMap(workerAccessJson), jsonToMap(GSON.toJson(workerAccess)));
    Assert.assertEquals(workerAccess, GSON.fromJson(workerAccessJson, AuditMessage.class));
    String exploreAccessJson = "{\"version\":2,\"time\":2500,\"metadataEntity\":{\"details\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\"}," + "\"type\":\"dataset\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"UNKNOWN\"," + "\"accessor\":{\"service\":\"explore\",\"entity\":\"SYSTEM_SERVICE\"}}}";
    AuditMessage exploreAccess = new AuditMessage(2500L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.UNKNOWN, new SystemServiceId("explore")));
    Assert.assertEquals(jsonToMap(exploreAccessJson), jsonToMap(GSON.toJson(exploreAccess)));
    Assert.assertEquals(exploreAccess, GSON.fromJson(exploreAccessJson, AuditMessage.class));
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessPayload) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) 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