Search in sources :

Example 41 with AuditPolicy

use of co.cask.cdap.common.security.AuditPolicy in project cdap by caskdata.

the class MonitorHandler method setServiceInstance.

/**
   * Sets the number of instances of CDAP Services
   */
@Path("/system/services/{service-name}/instances")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setServiceInstance(HttpRequest request, HttpResponder responder, @PathParam("service-name") final String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    int instances = getInstances(request);
    if (!serviceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
    int currentInstances = getSystemServiceInstanceCount(serviceName);
    if (instances < serviceManager.getMinInstances() || instances > serviceManager.getMaxInstances()) {
        String response = String.format("Instance count should be between [%s,%s]", serviceManager.getMinInstances(), serviceManager.getMaxInstances());
        throw new BadRequestException(response);
    } else if (instances == currentInstances) {
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    serviceStore.setServiceInstance(serviceName, instances);
    if (serviceManager.setInstances(instances)) {
        responder.sendStatus(HttpResponseStatus.OK);
    } else {
        throw new BadRequestException("Operation did not succeed");
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 42 with AuditPolicy

use of co.cask.cdap.common.security.AuditPolicy in project cdap by caskdata.

the class ConsoleSettingsHttpHandler method set.

@Path("/")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void set(HttpRequest request, HttpResponder responder) throws Exception {
    String data = request.getContent().toString(Charsets.UTF_8);
    if (!isValidJSON(data)) {
        responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Invalid JSON in body");
        return;
    }
    //Configuration Layout for UserSettings:
    //Config ID : userId
    //Config Properties : Map (Key = CONFIG_PROPERTY, Value = Serialized JSON string of properties)
    //User Settings configurations are stored under empty NAMESPACE.
    Map<String, String> propMap = ImmutableMap.of(CONFIG_PROPERTY, data);
    String userId = Objects.firstNonNull(SecurityRequestContext.getUserId(), "");
    Config userConfig = new Config(userId, propMap);
    store.put(userConfig);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : Config(co.cask.cdap.config.Config) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 43 with AuditPolicy

use of co.cask.cdap.common.security.AuditPolicy in project cdap by caskdata.

the class ExploreExecutorHttpHandler method updateDataset.

/**
   * Enable ad-hoc exploration of a dataset instance.
   */
@POST
@Path("datasets/{dataset}/update")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void updateDataset(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("dataset") String datasetName) throws BadRequestException {
    final DatasetId datasetId = new DatasetId(namespace, datasetName);
    try {
        UpdateExploreParameters params = readUpdateParameters(request);
        final DatasetSpecification oldSpec = params.getOldSpec();
        final DatasetSpecification datasetSpec = params.getNewSpec();
        QueryHandle handle;
        if (oldSpec.equals(datasetSpec)) {
            handle = QueryHandle.NO_OP;
        } else {
            handle = impersonator.doAs(datasetId, new Callable<QueryHandle>() {

                @Override
                public QueryHandle call() throws Exception {
                    return exploreTableManager.updateDataset(datasetId, datasetSpec, oldSpec);
                }
            });
        }
        JsonObject json = new JsonObject();
        json.addProperty("handle", handle.getHandle());
        responder.sendJson(HttpResponseStatus.OK, json);
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (ExploreException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error updating explore on dataset " + datasetId);
    } catch (SQLException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "SQL exception while trying to update explore on dataset " + datasetId);
    } catch (UnsupportedTypeException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Schema for dataset " + datasetId + " is not supported for exploration: " + e.getMessage());
    } catch (Throwable e) {
        LOG.error("Got exception:", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : UpdateExploreParameters(co.cask.cdap.explore.client.UpdateExploreParameters) SQLException(java.sql.SQLException) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) JsonObject(com.google.gson.JsonObject) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) QueryHandle(co.cask.cdap.proto.QueryHandle) Callable(java.util.concurrent.Callable) DatasetId(co.cask.cdap.proto.id.DatasetId) ExploreException(co.cask.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 44 with AuditPolicy

use of co.cask.cdap.common.security.AuditPolicy in project cdap by caskdata.

the class NamespacedExploreQueryExecutorHttpHandler method query.

@POST
@Path("data/explore/queries")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void query(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") final String namespaceId) throws Exception {
    try {
        Map<String, String> args = decodeArguments(request);
        final String query = args.get("query");
        final Map<String, String> additionalSessionConf = new HashMap<>(args);
        additionalSessionConf.remove("query");
        LOG.trace("Received query: {}", query);
        QueryHandle queryHandle = impersonator.doAs(new NamespaceId(namespaceId), new Callable<QueryHandle>() {

            @Override
            public QueryHandle call() throws Exception {
                return exploreService.execute(new NamespaceId(namespaceId), query, additionalSessionConf);
            }
        }, ImpersonatedOpType.EXPLORE);
        responder.sendJson(HttpResponseStatus.OK, queryHandle);
    } catch (IllegalArgumentException e) {
        LOG.debug("Got exception:", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (SQLException e) {
        LOG.debug("Got exception:", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) QueryHandle(co.cask.cdap.proto.QueryHandle) SQLException(java.sql.SQLException) ExploreException(co.cask.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Aggregations

AuditPolicy (co.cask.cdap.common.security.AuditPolicy)44 Path (javax.ws.rs.Path)44 POST (javax.ws.rs.POST)24 PUT (javax.ws.rs.PUT)19 BadRequestException (co.cask.cdap.common.BadRequestException)12 ProgramId (co.cask.cdap.proto.id.ProgramId)11 NamespaceId (co.cask.cdap.proto.id.NamespaceId)9 IOException (java.io.IOException)8 NotFoundException (co.cask.cdap.common.NotFoundException)7 JsonSyntaxException (com.google.gson.JsonSyntaxException)7 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)6 ArtifactId (co.cask.cdap.proto.id.ArtifactId)6 ArrayList (java.util.ArrayList)6 Id (co.cask.cdap.proto.Id)5 StreamId (co.cask.cdap.proto.id.StreamId)5 Constraint (co.cask.cdap.internal.schedule.constraint.Constraint)4 ProtoConstraint (co.cask.cdap.proto.ProtoConstraint)4 DatasetId (co.cask.cdap.proto.id.DatasetId)4 InputStreamReader (java.io.InputStreamReader)4 Reader (java.io.Reader)4