Esempio n. 1
0
  /**
   * Generates an audit record for the creation of a processor.
   *
   * @param processor processor
   * @param operation operation
   * @param actionDetails details
   * @return action
   */
  public Action generateAuditRecord(
      ProcessorNode processor, Operation operation, ActionDetails actionDetails) {
    FlowChangeAction action = null;

    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();

    // ensure the user was found
    if (user != null) {
      // create the processor details
      FlowChangeExtensionDetails processorDetails = new FlowChangeExtensionDetails();
      processorDetails.setType(processor.getProcessor().getClass().getSimpleName());

      // create the processor action for adding this processor
      action = new FlowChangeAction();
      action.setUserIdentity(user.getIdentity());
      action.setUserName(user.getUserName());
      action.setOperation(operation);
      action.setTimestamp(new Date());
      action.setSourceId(processor.getIdentifier());
      action.setSourceName(processor.getName());
      action.setSourceType(Component.Processor);
      action.setComponentDetails(processorDetails);

      if (actionDetails != null) {
        action.setActionDetails(actionDetails);
      }
    }

    return action;
  }
  /**
   * Gets the update actions for all specified referencing components.
   *
   * @param user user
   * @param actions actions
   * @param visitedServices services
   * @param referencingComponents components
   */
  private void getUpdateActionsForReferencingComponents(
      final NiFiUser user,
      final Collection<Action> actions,
      final Collection<String> visitedServices,
      final Set<ConfiguredComponent> referencingComponents) {
    // consider each component updates
    for (final ConfiguredComponent component : referencingComponents) {
      if (component instanceof ProcessorNode) {
        final ProcessorNode processor = ((ProcessorNode) component);

        // create the processor details
        FlowChangeExtensionDetails processorDetails = new FlowChangeExtensionDetails();
        processorDetails.setType(processor.getProcessor().getClass().getSimpleName());

        // create a processor action
        FlowChangeAction processorAction = new FlowChangeAction();
        processorAction.setUserIdentity(user.getDn());
        processorAction.setUserName(user.getUserName());
        processorAction.setTimestamp(new Date());
        processorAction.setSourceId(processor.getIdentifier());
        processorAction.setSourceName(processor.getName());
        processorAction.setSourceType(Component.Processor);
        processorAction.setComponentDetails(processorDetails);
        processorAction.setOperation(
            ScheduledState.RUNNING.equals(processor.getScheduledState())
                ? Operation.Start
                : Operation.Stop);
        actions.add(processorAction);
      } else if (component instanceof ReportingTask) {
        final ReportingTaskNode reportingTask = ((ReportingTaskNode) component);

        // create the reporting task details
        FlowChangeExtensionDetails processorDetails = new FlowChangeExtensionDetails();
        processorDetails.setType(reportingTask.getReportingTask().getClass().getSimpleName());

        // create a reporting task action
        FlowChangeAction reportingTaskAction = new FlowChangeAction();
        reportingTaskAction.setUserIdentity(user.getDn());
        reportingTaskAction.setUserName(user.getUserName());
        reportingTaskAction.setTimestamp(new Date());
        reportingTaskAction.setSourceId(reportingTask.getIdentifier());
        reportingTaskAction.setSourceName(reportingTask.getName());
        reportingTaskAction.setSourceType(Component.ReportingTask);
        reportingTaskAction.setComponentDetails(processorDetails);
        reportingTaskAction.setOperation(
            ScheduledState.RUNNING.equals(reportingTask.getScheduledState())
                ? Operation.Start
                : Operation.Stop);
        actions.add(reportingTaskAction);
      } else if (component instanceof ControllerServiceNode) {
        final ControllerServiceNode controllerService = ((ControllerServiceNode) component);

        // create the controller service details
        FlowChangeExtensionDetails serviceDetails = new FlowChangeExtensionDetails();
        serviceDetails.setType(
            controllerService.getControllerServiceImplementation().getClass().getSimpleName());

        // create a controller service action
        FlowChangeAction serviceAction = new FlowChangeAction();
        serviceAction.setUserIdentity(user.getDn());
        serviceAction.setUserName(user.getUserName());
        serviceAction.setTimestamp(new Date());
        serviceAction.setSourceId(controllerService.getIdentifier());
        serviceAction.setSourceName(controllerService.getName());
        serviceAction.setSourceType(Component.ControllerService);
        serviceAction.setComponentDetails(serviceDetails);
        serviceAction.setOperation(
            isDisabled(controllerService) ? Operation.Disable : Operation.Enable);
        actions.add(serviceAction);

        // need to consider components referencing this controller service (transitive)
        if (!visitedServices.contains(controllerService.getIdentifier())) {
          getUpdateActionsForReferencingComponents(
              user,
              actions,
              visitedServices,
              controllerService.getReferences().getReferencingComponents());
        }
      }
    }
  }
Esempio n. 3
0
  /** Extracts the values for the configured properties from the specified Processor. */
  private Map<String, String> extractConfiguredPropertyValues(
      ProcessorNode processor, ProcessorDTO processorDTO) {
    Map<String, String> values = new HashMap<>();

    if (processorDTO.getName() != null) {
      values.put(NAME, processor.getName());
    }
    if (processorDTO.getConfig() != null) {
      ProcessorConfigDTO newConfig = processorDTO.getConfig();
      if (newConfig.getConcurrentlySchedulableTaskCount() != null) {
        values.put(
            CONCURRENTLY_SCHEDULABLE_TASKS, String.valueOf(processor.getMaxConcurrentTasks()));
      }
      if (newConfig.getPenaltyDuration() != null) {
        values.put(PENALTY_DURATION, processor.getPenalizationPeriod());
      }
      if (newConfig.getYieldDuration() != null) {
        values.put(YIELD_DURATION, processor.getYieldPeriod());
      }
      if (newConfig.getBulletinLevel() != null) {
        values.put(BULLETIN_LEVEL, processor.getBulletinLevel().name());
      }
      if (newConfig.getAnnotationData() != null) {
        values.put(ANNOTATION_DATA, processor.getAnnotationData());
      }
      if (newConfig.getSchedulingPeriod() != null) {
        values.put(SCHEDULING_PERIOD, String.valueOf(processor.getSchedulingPeriod()));
      }
      if (newConfig.getAutoTerminatedRelationships() != null) {
        // get each of the auto terminated relationship names
        final Set<Relationship> autoTerminatedRelationships =
            processor.getAutoTerminatedRelationships();
        final List<String> autoTerminatedRelationshipNames =
            new ArrayList<>(autoTerminatedRelationships.size());
        for (final Relationship relationship : autoTerminatedRelationships) {
          autoTerminatedRelationshipNames.add(relationship.getName());
        }

        // sort them and include in the configuration
        Collections.sort(autoTerminatedRelationshipNames, Collator.getInstance(Locale.US));
        values.put(
            AUTO_TERMINATED_RELATIONSHIPS, StringUtils.join(autoTerminatedRelationshipNames, ", "));
      }
      if (newConfig.getProperties() != null) {
        // for each property specified, extract its configured value
        Map<String, String> properties = newConfig.getProperties();
        Map<PropertyDescriptor, String> configuredProperties = processor.getProperties();
        for (String propertyName : properties.keySet()) {
          // build a descriptor for getting the configured value
          PropertyDescriptor propertyDescriptor =
              new PropertyDescriptor.Builder().name(propertyName).build();
          String configuredPropertyValue = configuredProperties.get(propertyDescriptor);

          // if the configured value couldn't be found, use the default value from the actual
          // descriptor
          if (configuredPropertyValue == null) {
            propertyDescriptor =
                locatePropertyDescriptor(configuredProperties.keySet(), propertyDescriptor);
            configuredPropertyValue = propertyDescriptor.getDefaultValue();
          }
          values.put(propertyName, configuredPropertyValue);
        }
      }
      if (newConfig.getComments() != null) {
        values.put(COMMENTS, processor.getComments());
      }
      if (newConfig.getSchedulingStrategy() != null) {
        values.put(SCHEDULING_STRATEGY, processor.getSchedulingStrategy().toString());
      }
    }

    return values;
  }
Esempio n. 4
0
  /**
   * Audits the configuration of a single processor.
   *
   * @param proceedingJoinPoint join point
   * @param processorDTO dto
   * @param processorDAO dao
   * @return node
   * @throws Throwable ex
   */
  @Around(
      "within(org.apache.nifi.web.dao.ProcessorDAO+) && "
          + "execution(org.apache.nifi.controller.ProcessorNode updateProcessor(org.apache.nifi.web.api.dto.ProcessorDTO)) && "
          + "args(processorDTO) && "
          + "target(processorDAO)")
  public ProcessorNode updateProcessorAdvice(
      ProceedingJoinPoint proceedingJoinPoint, ProcessorDTO processorDTO, ProcessorDAO processorDAO)
      throws Throwable {
    // determine the initial values for each property/setting thats changing
    ProcessorNode processor = processorDAO.getProcessor(processorDTO.getId());
    final Map<String, String> values = extractConfiguredPropertyValues(processor, processorDTO);
    final ScheduledState scheduledState = processor.getScheduledState();

    // update the processor state
    final ProcessorNode updatedProcessor = (ProcessorNode) proceedingJoinPoint.proceed();

    // if no exceptions were thrown, add the processor action...
    // get the updated verbose state
    processor = processorDAO.getProcessor(updatedProcessor.getIdentifier());

    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();

    // ensure the user was found
    if (user != null) {
      // determine the updated values
      Map<String, String> updatedValues = extractConfiguredPropertyValues(processor, processorDTO);

      // create the processor details
      FlowChangeExtensionDetails processorDetails = new FlowChangeExtensionDetails();
      processorDetails.setType(processor.getProcessor().getClass().getSimpleName());

      // create a processor action
      Date actionTimestamp = new Date();
      Collection<Action> actions = new ArrayList<>();

      // go through each updated value
      for (String property : updatedValues.keySet()) {
        String newValue = updatedValues.get(property);
        String oldValue = values.get(property);
        Operation operation = null;

        // determine the type of operation
        if (oldValue == null || newValue == null || !newValue.equals(oldValue)) {
          operation = Operation.Configure;
        }

        // create a configuration action accordingly
        if (operation != null) {
          // clear the value if this property is sensitive
          final PropertyDescriptor propertyDescriptor =
              processor.getProcessor().getPropertyDescriptor(property);
          if (propertyDescriptor != null && propertyDescriptor.isSensitive()) {
            if (newValue != null) {
              newValue = "********";
            }
            if (oldValue != null) {
              oldValue = "********";
            }
          } else if (ANNOTATION_DATA.equals(property)) {
            if (newValue != null) {
              newValue = "<annotation data not shown>";
            }
            if (oldValue != null) {
              oldValue = "<annotation data not shown>";
            }
          }

          final FlowChangeConfigureDetails actionDetails = new FlowChangeConfigureDetails();
          actionDetails.setName(property);
          actionDetails.setValue(newValue);
          actionDetails.setPreviousValue(oldValue);

          // create a configuration action
          FlowChangeAction configurationAction = new FlowChangeAction();
          configurationAction.setUserIdentity(user.getIdentity());
          configurationAction.setUserName(user.getUserName());
          configurationAction.setOperation(operation);
          configurationAction.setTimestamp(actionTimestamp);
          configurationAction.setSourceId(processor.getIdentifier());
          configurationAction.setSourceName(processor.getName());
          configurationAction.setSourceType(Component.Processor);
          configurationAction.setComponentDetails(processorDetails);
          configurationAction.setActionDetails(actionDetails);
          actions.add(configurationAction);
        }
      }

      // determine the new executing state
      final ScheduledState updatedScheduledState = processor.getScheduledState();

      // determine if the running state has changed and its not disabled
      if (scheduledState != updatedScheduledState) {
        // create a processor action
        FlowChangeAction processorAction = new FlowChangeAction();
        processorAction.setUserIdentity(user.getIdentity());
        processorAction.setUserName(user.getUserName());
        processorAction.setTimestamp(new Date());
        processorAction.setSourceId(processor.getIdentifier());
        processorAction.setSourceName(processor.getName());
        processorAction.setSourceType(Component.Processor);
        processorAction.setComponentDetails(processorDetails);

        // set the operation accordingly
        if (ScheduledState.RUNNING.equals(updatedScheduledState)) {
          processorAction.setOperation(Operation.Start);
        } else if (ScheduledState.DISABLED.equals(updatedScheduledState)) {
          processorAction.setOperation(Operation.Disable);
        } else {
          // state is now stopped... consider the previous state
          if (ScheduledState.RUNNING.equals(scheduledState)) {
            processorAction.setOperation(Operation.Stop);
          } else if (ScheduledState.DISABLED.equals(scheduledState)) {
            processorAction.setOperation(Operation.Enable);
          }
        }
        actions.add(processorAction);
      }

      // ensure there are actions to record
      if (!actions.isEmpty()) {
        // save the actions
        saveActions(actions, logger);
      }
    }

    return updatedProcessor;
  }
 @Override
 public void clearState(ProcessorNode processor) {
   clearState(processor.getIdentifier());
 }
 @Override
 public StateMap getState(ProcessorNode processor, Scope scope) {
   return getState(processor.getIdentifier(), scope);
 }