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);
}
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;
}
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());
}
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;
}
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();
}
Aggregations