Search in sources :

Example 36 with AuditPolicy

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

the class StreamHandler method create.

@PUT
@Path("/{stream}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("stream") String stream) throws Exception {
    // Check for namespace existence. Throws NotFoundException if namespace doesn't exist
    namespaceQueryAdmin.get(new NamespaceId(namespaceId));
    StreamId streamId = validateAndGetStreamId(namespaceId, stream);
    Properties props = new Properties();
    StreamProperties streamProperties;
    // If the request to create a stream contains a non-empty body, then construct and set StreamProperties
    if (request.getContent().readable()) {
        streamProperties = getAndValidateConfig(request);
        if (streamProperties.getTTL() != null) {
            props.put(Constants.Stream.TTL, Long.toString(streamProperties.getTTL()));
        }
        if (streamProperties.getNotificationThresholdMB() != null) {
            props.put(Constants.Stream.NOTIFICATION_THRESHOLD, Integer.toString(streamProperties.getNotificationThresholdMB()));
        }
        if (streamProperties.getDescription() != null) {
            props.put(Constants.Stream.DESCRIPTION, streamProperties.getDescription());
        }
        if (streamProperties.getFormat() != null) {
            props.put(Constants.Stream.FORMAT_SPECIFICATION, GSON.toJson(streamProperties.getFormat()));
        }
        if (streamProperties.getOwnerPrincipal() != null) {
            props.put(Constants.Security.PRINCIPAL, streamProperties.getOwnerPrincipal());
        }
    }
    streamAdmin.create(streamId, props);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamProperties(co.cask.cdap.proto.StreamProperties) NamespaceId(co.cask.cdap.proto.id.NamespaceId) StreamProperties(co.cask.cdap.proto.StreamProperties) Properties(java.util.Properties) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 37 with AuditPolicy

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

the class NotificationFeedHttpHandler method createFeed.

@PUT
@Path("/feeds/categories/{feed-category}/names/{feed-name}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void createFeed(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("feed-category") String category, @PathParam("feed-name") String name) {
    try {
        NotificationFeedInfo feedInfo;
        try {
            Map<String, String> body = parseBody(request, MAP_TYPE);
            String description = body == null ? null : body.get("description");
            feedInfo = new NotificationFeedInfo(namespaceId, category, name, description);
        } catch (IllegalArgumentException e) {
            responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("Could not create Notification Feed. %s", e.getMessage()));
            return;
        }
        if (feedManager.createFeed(feedInfo)) {
            responder.sendString(HttpResponseStatus.OK, "Notification Feed created successfully");
        } else {
            LOG.trace("Notification Feed already exists.");
            responder.sendString(HttpResponseStatus.OK, "Notification Feed already exists.");
        }
    } catch (NotificationFeedException e) {
        LOG.error("Could not create notification feed.", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (JsonSyntaxException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Invalid json object provided in request body.");
    } catch (IOException e) {
        LOG.error("Failed to read Notification feed request body.", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (Throwable t) {
        LOG.debug("Error in creating notification feed.", t);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) JsonSyntaxException(com.google.gson.JsonSyntaxException) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) IOException(java.io.IOException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 38 with AuditPolicy

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

the class ProgramLifecycleHttpHandler method saveProgramRuntimeArgs.

/**
   * Save program runtime args.
   */
@PUT
@Path("/apps/{app-name}/{program-type}/{program-name}/runtimeargs")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void saveProgramRuntimeArgs(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("program-type") String type, @PathParam("program-name") String programName) throws Exception {
    ProgramType programType = getProgramType(type);
    ProgramId programId = new ProgramId(namespaceId, appName, programType, programName);
    saveProgramIdRuntimeArgs(programId, request, responder);
}
Also used : ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 39 with AuditPolicy

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

the class ProgramLifecycleHttpHandler method getInstances.

/**
   * Returns the number of instances for all program runnables that are passed into the data. The data is an array of
   * Json objects where each object must contain the following three elements: appId, programType, and programId
   * (flow name, service name). Retrieving instances only applies to flows, and user
   * services. For flows, another parameter, "runnableId", must be provided. This corresponds to the
   * flowlet/runnable for which to retrieve the instances.
   * <p>
   * Example input:
   * <pre><code>
   * [{"appId": "App1", "programType": "Service", "programId": "Service1", "runnableId": "Runnable1"},
   *  {"appId": "App1", "programType": "Mapreduce", "programId": "Mapreduce2"},
   *  {"appId": "App2", "programType": "Flow", "programId": "Flow1", "runnableId": "Flowlet1"}]
   * </code></pre>
   * </p><p>
   * The response will be an array of JsonObjects each of which will contain the three input parameters
   * as well as 3 fields:
   * <ul>
   * <li>"provisioned" which maps to the number of instances actually provided for the input runnable;</li>
   * <li>"requested" which maps to the number of instances the user has requested for the input runnable; and</li>
   * <li>"statusCode" which maps to the http status code for the data in that JsonObjects (200, 400, 404).</li>
   * </ul>
   * </p><p>
   * If an error occurs in the input (for the example above, Flowlet1 does not exist), then all JsonObjects for
   * which the parameters have a valid instances will have the provisioned and requested fields status code fields
   * but all JsonObjects for which the parameters are not valid will have an error message and statusCode.
   * </p><p>
   * For example, if there is no Flowlet1 in the above data, then the response could be 200 OK with the following data:
   * </p>
   * <pre><code>
   * [{"appId": "App1", "programType": "Service", "programId": "Service1", "runnableId": "Runnable1",
   *   "statusCode": 200, "provisioned": 2, "requested": 2},
   *  {"appId": "App1", "programType": "Mapreduce", "programId": "Mapreduce2", "statusCode": 400,
   *   "error": "Program type 'Mapreduce' is not a valid program type to get instances"},
   *  {"appId": "App2", "programType": "Flow", "programId": "Flow1", "runnableId": "Flowlet1", "statusCode": 404,
   *   "error": "Program": Flowlet1 not found"}]
   * </code></pre>
   */
@POST
@Path("/instances")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void getInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws IOException, BadRequestException {
    List<BatchRunnable> runnables = validateAndGetBatchInput(request, BATCH_RUNNABLES_TYPE);
    // cache app specs to perform fewer store lookups
    Map<ApplicationId, ApplicationSpecification> appSpecs = new HashMap<>();
    List<BatchRunnableInstances> output = new ArrayList<>(runnables.size());
    for (BatchRunnable runnable : runnables) {
        // cant get instances for things that are not flows, services, or workers
        if (!canHaveInstances(runnable.getProgramType())) {
            output.add(new BatchRunnableInstances(runnable, HttpResponseStatus.BAD_REQUEST.getCode(), String.format("Program type '%s' is not a valid program type to get instances", runnable.getProgramType().getPrettyName())));
            continue;
        }
        ApplicationId appId = new ApplicationId(namespaceId, runnable.getAppId());
        // populate spec cache if this is the first time we've seen the appid.
        if (!appSpecs.containsKey(appId)) {
            appSpecs.put(appId, store.getApplication(appId));
        }
        ApplicationSpecification spec = appSpecs.get(appId);
        if (spec == null) {
            output.add(new BatchRunnableInstances(runnable, HttpResponseStatus.NOT_FOUND.getCode(), String.format("App: %s not found", appId)));
            continue;
        }
        ProgramId programId = appId.program(runnable.getProgramType(), runnable.getProgramId());
        output.add(getProgramInstances(runnable, spec, programId));
    }
    responder.sendJson(HttpResponseStatus.OK, output);
}
Also used : BatchRunnable(co.cask.cdap.proto.BatchRunnable) ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BatchRunnableInstances(co.cask.cdap.proto.BatchRunnableInstances) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 40 with AuditPolicy

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

the class ProgramLifecycleHttpHandler method setFlowletInstances.

/**
   * Increases number of instance for a flowlet within a flow.
   */
@PUT
@Path("/apps/{app-id}/flows/{flow-id}/flowlets/{flowlet-id}/instances")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public synchronized void setFlowletInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("flow-id") String flowId, @PathParam("flowlet-id") String flowletId) throws Exception {
    int instances = getInstances(request);
    try {
        lifecycleService.setInstances(new ProgramId(namespaceId, appId, ProgramType.FLOW, flowId), instances, flowletId);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (Throwable e) {
        if (respondIfElementNotFound(e, responder)) {
            return;
        }
        throw e;
    }
}
Also used : ProgramId(co.cask.cdap.proto.id.ProgramId) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

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