Search in sources :

Example 6 with TemplateEntity

use of org.apache.nifi.web.api.entity.TemplateEntity in project nifi by apache.

the class ProcessGroupResource method createTemplate.

/**
 * Creates a new template based off of the specified template.
 *
 * @param httpServletRequest          request
 * @param requestCreateTemplateRequestEntity request to create the template
 * @return A templateEntity
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/templates")
@ApiOperation(value = "Creates a template and discards the specified snippet.", response = TemplateEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{uuid}"), @Authorization(value = "Read - /{component-type}/{uuid} - For each component in the snippet and their descendant components") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response createTemplate(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, @ApiParam(value = "The create template request.", required = true) final CreateTemplateRequestEntity requestCreateTemplateRequestEntity) {
    if (requestCreateTemplateRequestEntity.getSnippetId() == null) {
        throw new IllegalArgumentException("The snippet identifier must be specified.");
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.POST, requestCreateTemplateRequestEntity);
    }
    return withWriteLock(serviceFacade, requestCreateTemplateRequestEntity, lookup -> {
        authorizeSnippetUsage(lookup, groupId, requestCreateTemplateRequestEntity.getSnippetId(), true);
    }, () -> serviceFacade.verifyCanAddTemplate(groupId, requestCreateTemplateRequestEntity.getName()), createTemplateRequestEntity -> {
        // create the template and generate the json
        final TemplateDTO template = serviceFacade.createTemplate(createTemplateRequestEntity.getName(), createTemplateRequestEntity.getDescription(), createTemplateRequestEntity.getSnippetId(), groupId, getIdGenerationSeed());
        templateResource.populateRemainingTemplateContent(template);
        // build the response entity
        final TemplateEntity entity = new TemplateEntity();
        entity.setTemplate(template);
        // build the response
        return generateCreatedResponse(URI.create(template.getUri()), entity).build();
    });
}
Also used : TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with TemplateEntity

use of org.apache.nifi.web.api.entity.TemplateEntity in project nifi by apache.

the class ITProcessorAccessControl method testTemplateWithRestrictedProcessor.

/**
 * Tests attempting to use a template with a restricted processor.
 *
 * @throws Exception ex
 */
@Test
public void testTemplateWithRestrictedProcessor() throws Exception {
    final String createTemplateUrl = helper.getBaseUrl() + "/process-groups/root/templates";
    final String instantiateTemplateUrl = helper.getBaseUrl() + "/process-groups/root/template-instance";
    final Tuple<ProcessorEntity, SnippetEntity> tuple = createSnippetWithRestrictedComponent(RestrictedProcessor.class.getName(), helper.getPrivilegedUser());
    final SnippetEntity snippetEntity = tuple.getValue();
    // create the template
    final CreateTemplateRequestEntity createTemplateRequest = new CreateTemplateRequestEntity();
    createTemplateRequest.setSnippetId(snippetEntity.getSnippet().getId());
    createTemplateRequest.setName("test");
    // create the snippet
    Response response = helper.getWriteUser().testPost(createTemplateUrl, createTemplateRequest);
    // ensure the request failed... need read perms to the components in the snippet
    assertEquals(403, response.getStatus());
    response = helper.getReadWriteUser().testPost(createTemplateUrl, createTemplateRequest);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    final TemplateEntity templateEntity = response.readEntity(TemplateEntity.class);
    // build the template request
    final InstantiateTemplateRequestEntity instantiateTemplateRequest = new InstantiateTemplateRequestEntity();
    instantiateTemplateRequest.setTemplateId(templateEntity.getTemplate().getId());
    instantiateTemplateRequest.setOriginX(0.0);
    instantiateTemplateRequest.setOriginY(0.0);
    // create the snippet
    response = helper.getReadWriteUser().testPost(instantiateTemplateUrl, instantiateTemplateRequest);
    // ensure the request failed... need privileged user since the template is comprised of restricted components
    assertEquals(403, response.getStatus());
    // create the snippet
    response = helper.getExecuteCodeUser().testPost(instantiateTemplateUrl, instantiateTemplateRequest);
    // ensure the request failed... need privileged user since the template is comprised of restricted components
    assertEquals(403, response.getStatus());
    // create the snippet
    response = helper.getPrivilegedUser().testPost(instantiateTemplateUrl, instantiateTemplateRequest);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    final FlowEntity flowEntity = response.readEntity(FlowEntity.class);
    // clean up the resources created during this test
    deleteTemplate(templateEntity);
    deleteRestrictedComponent(tuple.getKey(), helper.getPrivilegedUser());
    deleteRestrictedComponent(flowEntity.getFlow().getProcessors().stream().findFirst().orElse(null), helper.getPrivilegedUser());
}
Also used : Response(javax.ws.rs.core.Response) InstantiateTemplateRequestEntity(org.apache.nifi.web.api.entity.InstantiateTemplateRequestEntity) SnippetEntity(org.apache.nifi.web.api.entity.SnippetEntity) ExecuteCodeRestrictedProcessor(org.apache.nifi.integration.util.ExecuteCodeRestrictedProcessor) RestrictedProcessor(org.apache.nifi.integration.util.RestrictedProcessor) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) CreateTemplateRequestEntity(org.apache.nifi.web.api.entity.CreateTemplateRequestEntity) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) ProcessGroupFlowEntity(org.apache.nifi.web.api.entity.ProcessGroupFlowEntity) FlowEntity(org.apache.nifi.web.api.entity.FlowEntity) Test(org.junit.Test)

Example 8 with TemplateEntity

use of org.apache.nifi.web.api.entity.TemplateEntity in project nifi by apache.

the class ITProcessorAccessControl method templateWithExecuteCodeRestrictedProcessor.

private void templateWithExecuteCodeRestrictedProcessor(final NiFiTestUser user) throws Exception {
    final String createTemplateUrl = helper.getBaseUrl() + "/process-groups/root/templates";
    final String instantiateTemplateUrl = helper.getBaseUrl() + "/process-groups/root/template-instance";
    final Tuple<ProcessorEntity, SnippetEntity> tuple = createSnippetWithRestrictedComponent(ExecuteCodeRestrictedProcessor.class.getName(), helper.getPrivilegedUser());
    final SnippetEntity snippetEntity = tuple.getValue();
    // create the template
    final CreateTemplateRequestEntity createTemplateRequest = new CreateTemplateRequestEntity();
    createTemplateRequest.setSnippetId(snippetEntity.getSnippet().getId());
    createTemplateRequest.setName("test");
    // create the snippet
    Response response = helper.getWriteUser().testPost(createTemplateUrl, createTemplateRequest);
    // ensure the request failed... need read perms to the components in the snippet
    assertEquals(403, response.getStatus());
    response = helper.getReadWriteUser().testPost(createTemplateUrl, createTemplateRequest);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    final TemplateEntity templateEntity = response.readEntity(TemplateEntity.class);
    // build the template request
    final InstantiateTemplateRequestEntity instantiateTemplateRequest = new InstantiateTemplateRequestEntity();
    instantiateTemplateRequest.setTemplateId(templateEntity.getTemplate().getId());
    instantiateTemplateRequest.setOriginX(0.0);
    instantiateTemplateRequest.setOriginY(0.0);
    // create the snippet
    response = helper.getReadWriteUser().testPost(instantiateTemplateUrl, instantiateTemplateRequest);
    // ensure the request failed... need privileged user since the template is comprised of restricted components
    assertEquals(403, response.getStatus());
    // create the snippet
    response = user.testPost(instantiateTemplateUrl, instantiateTemplateRequest);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    final FlowEntity flowEntity = response.readEntity(FlowEntity.class);
    // clean up the resources created during this test
    deleteTemplate(templateEntity);
    deleteRestrictedComponent(tuple.getKey(), user);
    deleteRestrictedComponent(flowEntity.getFlow().getProcessors().stream().findFirst().orElse(null), user);
}
Also used : Response(javax.ws.rs.core.Response) InstantiateTemplateRequestEntity(org.apache.nifi.web.api.entity.InstantiateTemplateRequestEntity) SnippetEntity(org.apache.nifi.web.api.entity.SnippetEntity) ExecuteCodeRestrictedProcessor(org.apache.nifi.integration.util.ExecuteCodeRestrictedProcessor) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) CreateTemplateRequestEntity(org.apache.nifi.web.api.entity.CreateTemplateRequestEntity) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) ProcessGroupFlowEntity(org.apache.nifi.web.api.entity.ProcessGroupFlowEntity) FlowEntity(org.apache.nifi.web.api.entity.FlowEntity)

Aggregations

TemplateEntity (org.apache.nifi.web.api.entity.TemplateEntity)8 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 Consumes (javax.ws.rs.Consumes)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 TemplateDTO (org.apache.nifi.web.api.dto.TemplateDTO)4 POST (javax.ws.rs.POST)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Response (javax.ws.rs.core.Response)2 JAXBException (javax.xml.bind.JAXBException)2 Authorizable (org.apache.nifi.authorization.resource.Authorizable)2 ExecuteCodeRestrictedProcessor (org.apache.nifi.integration.util.ExecuteCodeRestrictedProcessor)2 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)2 ResourceNotFoundException (org.apache.nifi.web.ResourceNotFoundException)2 CreateTemplateRequestEntity (org.apache.nifi.web.api.entity.CreateTemplateRequestEntity)2 FlowEntity (org.apache.nifi.web.api.entity.FlowEntity)2