use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by caskdata.
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);
}
use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.
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));
}
}
use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.
the class MonitorHandler method updateServiceLogLevels.
/**
* Update log levels for this service.
*/
@Path("system/services/{service-name}/loglevels")
@PUT
public void updateServiceLogLevels(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 update log levels for service %s " + "because the service is not enabled", serviceName));
}
try {
// we are decoding the body to Map<String, String> instead of Map<String, LogEntry.Level> here since Gson will
// serialize invalid enum values to null, which is allowed for log level, instead of throw an Exception.
masterServiceManager.updateServiceLogLevels(transformLogLevelsMap(decodeArguments(request)));
responder.sendStatus(HttpResponseStatus.OK);
} catch (IllegalStateException ise) {
throw new ServiceUnavailableException(String.format("Failed to update log levels for service %s " + "because the service may not be ready yet", serviceName));
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
} catch (JsonSyntaxException e) {
throw new BadRequestException("Invalid Json in the body");
}
}
use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.
the class MonitorHandler method getServiceInstance.
/**
* Returns the number of instances of CDAP Services
*/
@Path("/system/services/{service-name}/instances")
@GET
public void getServiceInstance(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
JsonObject reply = new JsonObject();
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()) {
int actualInstance = serviceManagementMap.get(serviceName).getInstances();
reply.addProperty("provisioned", actualInstance);
reply.addProperty("requested", getSystemServiceInstanceCount(serviceName));
responder.sendJson(HttpResponseStatus.OK, reply.toString());
} else {
throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
}
}
use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.
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);
}
Aggregations