Search in sources :

Example 36 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project nifi by apache.

the class ITProcessorAccessControl method createExecuteCodeRestrictedProcessor.

private void createExecuteCodeRestrictedProcessor(final NiFiTestUser user) throws Exception {
    String url = helper.getBaseUrl() + "/process-groups/root/processors";
    // create the processor
    ProcessorDTO processor = new ProcessorDTO();
    processor.setName("execute code restricted");
    processor.setType(ExecuteCodeRestrictedProcessor.class.getName());
    // create the revision
    final RevisionDTO revision = new RevisionDTO();
    revision.setClientId(READ_WRITE_CLIENT_ID);
    revision.setVersion(0L);
    // create the entity body
    ProcessorEntity entity = new ProcessorEntity();
    entity.setRevision(revision);
    entity.setComponent(processor);
    // perform the request as a user with read/write but no restricted access
    Response response = helper.getReadWriteUser().testPost(url, entity);
    // ensure the request is successful
    assertEquals(403, response.getStatus());
    // perform the request as a user with read/write and restricted access
    response = user.testPost(url, entity);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    final ProcessorEntity responseEntity = response.readEntity(ProcessorEntity.class);
    // remove the restricted component
    deleteRestrictedComponent(responseEntity, user);
}
Also used : Response(javax.ws.rs.core.Response) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ExecuteCodeRestrictedProcessor(org.apache.nifi.integration.util.ExecuteCodeRestrictedProcessor) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO)

Example 37 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project nifi by apache.

the class ITProcessorAccessControl method createProcessor.

public static ProcessorEntity createProcessor(final AccessControlHelper ach, final String name) throws Exception {
    String url = ach.getBaseUrl() + "/process-groups/root/processors";
    // create the processor
    ProcessorDTO processor = new ProcessorDTO();
    processor.setName(name);
    processor.setType(SourceTestProcessor.class.getName());
    // create the revision
    final RevisionDTO revision = new RevisionDTO();
    revision.setClientId(READ_WRITE_CLIENT_ID);
    revision.setVersion(0L);
    // create the entity body
    ProcessorEntity entity = new ProcessorEntity();
    entity.setRevision(revision);
    entity.setComponent(processor);
    // perform the request
    Response response = ach.getReadWriteUser().testPost(url, entity);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    // get the entity body
    entity = response.readEntity(ProcessorEntity.class);
    // verify creation
    processor = entity.getComponent();
    assertEquals(name, processor.getName());
    assertEquals("org.apache.nifi.integration.util.SourceTestProcessor", processor.getType());
    // get the processor
    return entity;
}
Also used : Response(javax.ws.rs.core.Response) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) SourceTestProcessor(org.apache.nifi.integration.util.SourceTestProcessor) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO)

Example 38 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project nifi by apache.

the class ITProcessorAccessControl method testWriteUserPutProcessor.

/**
 * Ensures the WRITE user can put a processor.
 *
 * @throws Exception ex
 */
@Test
public void testWriteUserPutProcessor() throws Exception {
    final ProcessorEntity entity = getRandomProcessor(helper.getWriteUser());
    assertFalse(entity.getPermissions().getCanRead());
    assertTrue(entity.getPermissions().getCanWrite());
    assertNull(entity.getComponent());
    final String updatedName = "Updated Name";
    // attempt to update the name
    final ProcessorDTO requestDto = new ProcessorDTO();
    requestDto.setId(entity.getId());
    requestDto.setName(updatedName);
    final long version = entity.getRevision().getVersion();
    final RevisionDTO requestRevision = new RevisionDTO();
    requestRevision.setVersion(version);
    requestRevision.setClientId(AccessControlHelper.WRITE_CLIENT_ID);
    final ProcessorEntity requestEntity = new ProcessorEntity();
    requestEntity.setId(entity.getId());
    requestEntity.setRevision(requestRevision);
    requestEntity.setComponent(requestDto);
    // perform the request
    final Response response = updateProcessor(helper.getWriteUser(), requestEntity);
    // ensure successful response
    assertEquals(200, response.getStatus());
    // get the response
    final ProcessorEntity responseEntity = response.readEntity(ProcessorEntity.class);
    // verify
    assertEquals(WRITE_CLIENT_ID, responseEntity.getRevision().getClientId());
    assertEquals(version + 1, responseEntity.getRevision().getVersion().longValue());
}
Also used : Response(javax.ws.rs.core.Response) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Test(org.junit.Test)

Example 39 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class AbstractNiFiControllerServicesRestClient method updateServiceAndReferencingComponents.

/**
 * Updates a controller service.
 *
 * <p>This will first stop all referencing components, disable the service, update the service, enable the service, then reset the state of the components back to their prior state.</p>
 *
 * @param controllerService the service to update with the updated properties
 * @return the updated service
 */
@Nonnull
@Override
public ControllerServiceDTO updateServiceAndReferencingComponents(@Nonnull final ControllerServiceDTO controllerService) {
    // Recursively get all references to this controller service. This will include processors, other controller services, and reporting tasks.
    final Optional<ControllerServiceReferencingComponentsEntity> referencesEntity = getReferences(controllerService.getId());
    final Set<ControllerServiceReferencingComponentEntity> references = referencesEntity.isPresent() ? flattenReferencingComponents(referencesEntity.get()).collect(Collectors.toSet()) : Collections.emptySet();
    // build the reference state and revision maps prior to making this update
    final Map<String, RevisionDTO> referencingSchedulableComponentRevisions = new HashMap<>();
    final Map<String, RevisionDTO> referencingServiceRevisions = new HashMap<>();
    references.forEach(reference -> {
        if (REFERENCE_TYPE_CONTROLLER_SERVICE.equals(reference.getComponent().getReferenceType())) {
            referencingServiceRevisions.put(reference.getId(), reference.getRevision());
        } else {
            referencingSchedulableComponentRevisions.put(reference.getId(), reference.getRevision());
        }
    });
    // Update service and referencing components
    ControllerServiceDTO updatedService = null;
    Exception updateException = null;
    try {
        // Stop the referencing processors and ensure they are in the stopped state
        log.info("Stopping all component references to controller service {} ", controllerService.getName());
        if (!referencingSchedulableComponentRevisions.isEmpty() && !updateReferencingSchedulableComponents(controllerService, referencingSchedulableComponentRevisions, false)) {
            // error unable to change the state of the references. ... error
            throw new NifiClientRuntimeException("Unable to stop processor references to this controller service " + controllerService.getName() + " before making the update");
        }
        // Disable any controller service references
        if (!referencingServiceRevisions.isEmpty() && !updateReferencingServices(controllerService, referencingServiceRevisions, false)) {
            // error unable to change the state of the references. ... error
            throw new NifiClientRuntimeException("Unable to disable other controller service references to this controller service " + controllerService.getName() + " before making the update");
        }
        // Update the service and mark it disabled. This will throw a RuntimeException if it is not successful.
        log.info("Disabling the controller service  {} ", controllerService.getName());
        updateStateByIdWithRetries(controllerService.getId(), State.DISABLED.name(), 5, 500, TimeUnit.MILLISECONDS);
        // Perform the update to this controller service
        log.info("Updating the controller service  {} ", controllerService.getName());
        updatedService = update(controllerService);
        // Enable the service
        updateStateById(controllerService.getId(), State.ENABLED);
    } catch (final Exception e) {
        log.error("Reverting changes after controller service {} [{}] failed to update: ", controllerService.getId(), controllerService.getName(), e);
        updateException = e;
    }
    // Enable any controller service references
    boolean servicesUpdate;
    try {
        log.info("Enabling other controller services referencing this controller service  {} ", controllerService.getName());
        servicesUpdate = updateReferencingServices(controllerService, referencingServiceRevisions, true);
    } catch (final Exception e) {
        log.debug("Failed to restore referencing service states for controller service {} [{}]: {}", controllerService.getId(), controllerService.getName(), e, e);
        servicesUpdate = false;
    }
    // Update references back to previous states
    boolean componentsUpdate;
    try {
        final Set<ControllerServiceReferencingComponentEntity> runningComponents = references.stream().filter(reference -> !REFERENCE_TYPE_CONTROLLER_SERVICE.equals(reference.getComponent().getReferenceType())).filter(reference -> NifiProcessUtil.PROCESS_STATE.RUNNING.name().equals(reference.getComponent().getState())).collect(Collectors.toSet());
        if (runningComponents.size() == referencingSchedulableComponentRevisions.size()) {
            log.info("Updating all component references to be RUNNING for controller service  {} ", controllerService.getName());
            componentsUpdate = updateReferencingSchedulableComponents(controllerService, referencingSchedulableComponentRevisions, true);
        } else {
            log.info("The controller service component references ({} total) had mixed states prior to updating. Going through each processor/component and setting its state back to" + " what it was prior to the update.", referencingSchedulableComponentRevisions.size());
            componentsUpdate = startSchedulableComponents(runningComponents);
            log.info("Successfully updated controller service {} and updated {} components back to their prior state ", controllerService.getName(), runningComponents.size());
        }
    } catch (final Exception e) {
        log.debug("Failed to restore referencing component states for controller service {} [{}]: {}", controllerService.getId(), controllerService.getName(), e, e);
        componentsUpdate = false;
    }
    // Verify final state
    if (updateException != null) {
        throw Throwables.propagate(updateException);
    }
    if (!servicesUpdate) {
        // error unable to change the state of the references. ... error
        throw new NifiClientRuntimeException("Kylo was unable to enable other controller service references to the " + controllerService.getName() + " controller service. Please visit NiFi" + " to reconcile and fix any controller service issues.");
    }
    if (!componentsUpdate) {
        // error unable to change the state of the references. ... error
        throw new NifiClientRuntimeException("Kylo was unable to update the state of the processors as RUNNING. Please visit NiFi to reconcile and fix any controller service issues. " + controllerService.getName());
    }
    log.info("Successfully updated controller service {}", controllerService.getName());
    return updatedService;
}
Also used : ControllerServiceReferencingComponentsEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) LoggerFactory(org.slf4j.LoggerFactory) NifiProcessUtil(com.thinkbiganalytics.nifi.rest.support.NifiProcessUtil) HashMap(java.util.HashMap) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) StringUtils(org.apache.commons.lang3.StringUtils) ClientErrorException(javax.ws.rs.ClientErrorException) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) Future(java.util.concurrent.Future) UpdateControllerServiceReferenceRequestEntity(org.apache.nifi.web.api.entity.UpdateControllerServiceReferenceRequestEntity) ControllerServiceReferencingComponentEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentEntity) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) NifiConstants(com.thinkbiganalytics.nifi.rest.support.NifiConstants) ExecutorService(java.util.concurrent.ExecutorService) BulletinDTO(org.apache.nifi.web.api.dto.BulletinDTO) ControllerServiceReferencingComponentsEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Logger(org.slf4j.Logger) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) ControllerServiceReferencingComponentDTO(org.apache.nifi.web.api.dto.ControllerServiceReferencingComponentDTO) ReportingTaskEntity(org.apache.nifi.web.api.entity.ReportingTaskEntity) Predicate(java.util.function.Predicate) Throwables(com.google.common.base.Throwables) Set(java.util.Set) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) Optional(java.util.Optional) ReportingTaskDTO(org.apache.nifi.web.api.dto.ReportingTaskDTO) Collections(java.util.Collections) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) ControllerServiceReferencingComponentEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentEntity) HashMap(java.util.HashMap) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) ClientErrorException(javax.ws.rs.ClientErrorException) Nonnull(javax.annotation.Nonnull)

Example 40 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class NiFiConnectionsRestClientV1 method update.

@Override
public Optional<ConnectionDTO> update(@Nonnull ConnectionDTO connectionDTO) {
    Optional<ConnectionEntity> current = findEntityById(connectionDTO.getId());
    if (current.isPresent()) {
        ConnectionEntity connectionEntity = new ConnectionEntity();
        ConnectionDTO updateDto = new ConnectionDTO();
        connectionEntity.setComponent(updateDto);
        updateDto.setId(connectionDTO.getId());
        updateDto.setSelectedRelationships(connectionDTO.getSelectedRelationships());
        if (connectionDTO.getSource() != null) {
            updateDto.setSource(new ConnectableDTO());
            updateDto.getSource().setGroupId(connectionDTO.getSource().getGroupId());
            updateDto.getSource().setId(connectionDTO.getSource().getId());
            updateDto.getSource().setType(connectionDTO.getSource().getType());
        }
        if (connectionDTO.getDestination() != null) {
            updateDto.setDestination(new ConnectableDTO());
            updateDto.getDestination().setGroupId(connectionDTO.getDestination().getGroupId());
            updateDto.getDestination().setId(connectionDTO.getDestination().getId());
            updateDto.getDestination().setType(connectionDTO.getDestination().getType());
        }
        final RevisionDTO revision = new RevisionDTO();
        revision.setVersion(current.get().getRevision().getVersion());
        connectionEntity.setRevision(revision);
        try {
            return Optional.of(client.put(CONNECTION_PATH + connectionDTO.getId(), connectionEntity, ConnectionEntity.class).getComponent());
        } catch (Exception e) {
            log.error("Error updating connection entry info for connection: {} {} ", connectionDTO, e.getMessage());
            return Optional.empty();
        }
    }
    return Optional.empty();
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ConnectionEntity(org.apache.nifi.web.api.entity.ConnectionEntity) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) NotFoundException(javax.ws.rs.NotFoundException) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException)

Aggregations

RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)96 Response (javax.ws.rs.core.Response)45 Authorizable (org.apache.nifi.authorization.resource.Authorizable)30 PermissionsDTO (org.apache.nifi.web.api.dto.PermissionsDTO)29 PortEntity (org.apache.nifi.web.api.entity.PortEntity)27 ProcessGroupEntity (org.apache.nifi.web.api.entity.ProcessGroupEntity)27 ProcessorEntity (org.apache.nifi.web.api.entity.ProcessorEntity)26 HashMap (java.util.HashMap)24 Set (java.util.Set)24 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)24 Map (java.util.Map)23 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)23 HashSet (java.util.HashSet)22 Collectors (java.util.stream.Collectors)22 ControllerServiceEntity (org.apache.nifi.web.api.entity.ControllerServiceEntity)22 ScheduledState (org.apache.nifi.controller.ScheduledState)21 ControllerServiceState (org.apache.nifi.controller.service.ControllerServiceState)21 VersionControlInformationDTO (org.apache.nifi.web.api.dto.VersionControlInformationDTO)21 Authorizer (org.apache.nifi.authorization.Authorizer)19 RequestAction (org.apache.nifi.authorization.RequestAction)19