Search in sources :

Example 11 with FlowChangeConfigureDetails

use of org.apache.nifi.action.details.FlowChangeConfigureDetails in project nifi by apache.

the class RelationshipAuditor method updateConnectionAdvice.

/**
 * Audits the creation and removal of relationships via updateConnection().
 *
 * @param proceedingJoinPoint join point
 * @param connectionDTO dto
 * @param connectionDAO dao
 * @return connection
 * @throws Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.ConnectionDAO+) && " + "execution(org.apache.nifi.connectable.Connection updateConnection(org.apache.nifi.web.api.dto.ConnectionDTO)) && " + "args(connectionDTO) && " + "target(connectionDAO)")
public Connection updateConnectionAdvice(ProceedingJoinPoint proceedingJoinPoint, ConnectionDTO connectionDTO, ConnectionDAO connectionDAO) throws Throwable {
    // get the previous configuration
    Connection connection = connectionDAO.getConnection(connectionDTO.getId());
    Connectable previousDestination = connection.getDestination();
    Collection<Relationship> previousRelationships = connection.getRelationships();
    Map<String, String> values = extractConfiguredPropertyValues(connection, connectionDTO);
    // perform the underlying operation
    connection = (Connection) proceedingJoinPoint.proceed();
    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();
    // ensure the user was found
    if (user != null) {
        Collection<Action> actions = new ArrayList<>();
        Map<String, String> updatedValues = extractConfiguredPropertyValues(connection, connectionDTO);
        // get the source
        Connectable source = connection.getSource();
        // get the current target
        Connectable destination = connection.getDestination();
        // determine if a new target was specified in the initial request
        if (destination != null && !previousDestination.getIdentifier().equals(destination.getIdentifier())) {
            // record the removal of all relationships from the previous target
            final ConnectDetails disconnectDetails = createConnectDetails(connection, source, previousRelationships, previousDestination);
            actions.add(generateAuditRecordForConnection(connection, Operation.Disconnect, disconnectDetails));
            // record the addition of all relationships to the new target
            final ConnectDetails connectDetails = createConnectDetails(connection, connection.getRelationships());
            actions.add(generateAuditRecordForConnection(connection, Operation.Connect, connectDetails));
        }
        // audit and relationships added/removed
        Collection<Relationship> newRelationships = connection.getRelationships();
        // identify any relationships that were added
        if (newRelationships != null) {
            List<Relationship> relationshipsToAdd = new ArrayList<>(newRelationships);
            if (previousRelationships != null) {
                relationshipsToAdd.removeAll(previousRelationships);
            }
            if (!relationshipsToAdd.isEmpty()) {
                final ConnectDetails connectDetails = createConnectDetails(connection, relationshipsToAdd);
                actions.add(generateAuditRecordForConnection(connection, Operation.Connect, connectDetails));
            }
        }
        // identify any relationships that were removed
        if (previousRelationships != null) {
            List<Relationship> relationshipsToRemove = new ArrayList<>(previousRelationships);
            if (newRelationships != null) {
                relationshipsToRemove.removeAll(newRelationships);
            }
            if (!relationshipsToRemove.isEmpty()) {
                final ConnectDetails connectDetails = createConnectDetails(connection, relationshipsToRemove);
                actions.add(generateAuditRecordForConnection(connection, Operation.Disconnect, connectDetails));
            }
        }
        // go through each updated value
        Date actionTimestamp = new Date();
        for (String property : updatedValues.keySet()) {
            String newValue = updatedValues.get(property);
            String oldValue = values.get(property);
            // ensure the value is changing
            if (oldValue == null || newValue == null || !newValue.equals(oldValue)) {
                // create the config details
                FlowChangeConfigureDetails configurationDetails = new FlowChangeConfigureDetails();
                configurationDetails.setName(property);
                configurationDetails.setValue(newValue);
                configurationDetails.setPreviousValue(oldValue);
                // create a configuration action
                FlowChangeAction configurationAction = new FlowChangeAction();
                configurationAction.setUserIdentity(user.getIdentity());
                configurationAction.setOperation(Operation.Configure);
                configurationAction.setTimestamp(actionTimestamp);
                configurationAction.setSourceId(connection.getIdentifier());
                configurationAction.setSourceName(connection.getName());
                configurationAction.setSourceType(Component.Connection);
                configurationAction.setActionDetails(configurationDetails);
                actions.add(configurationAction);
            }
        }
        // save the actions
        if (!actions.isEmpty()) {
            saveActions(actions, logger);
        }
    }
    return connection;
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ConnectDetails(org.apache.nifi.action.details.ConnectDetails) FlowChangeConnectDetails(org.apache.nifi.action.details.FlowChangeConnectDetails) Connection(org.apache.nifi.connectable.Connection) ArrayList(java.util.ArrayList) Date(java.util.Date) Connectable(org.apache.nifi.connectable.Connectable) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) Relationship(org.apache.nifi.processor.Relationship) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Around(org.aspectj.lang.annotation.Around)

Example 12 with FlowChangeConfigureDetails

use of org.apache.nifi.action.details.FlowChangeConfigureDetails in project nifi by apache.

the class UserGroupAuditor method updateUserAdvice.

/**
 * Audits the configuration of a single user.
 *
 * @param proceedingJoinPoint join point
 * @param userGroupDTO dto
 * @param userGroupDAO dao
 * @return node
 * @throws Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.UserGroupDAO+) && " + "execution(org.apache.nifi.authorization.Group updateUserGroup(org.apache.nifi.web.api.dto.UserGroupDTO)) && " + "args(userGroupDTO) && " + "target(userGroupDAO)")
public Group updateUserAdvice(ProceedingJoinPoint proceedingJoinPoint, UserGroupDTO userGroupDTO, UserGroupDAO userGroupDAO) throws Throwable {
    // determine the initial values for each property/setting that's changing
    Group user = userGroupDAO.getUserGroup(userGroupDTO.getId());
    final Map<String, String> values = extractConfiguredPropertyValues(user, userGroupDTO);
    // update the user state
    final Group updatedUserGroup = (Group) proceedingJoinPoint.proceed();
    // if no exceptions were thrown, add the user group action...
    user = userGroupDAO.getUserGroup(updatedUserGroup.getIdentifier());
    // get the current user
    NiFiUser niFiUser = NiFiUserUtils.getNiFiUser();
    // ensure the user was found
    if (niFiUser != null) {
        // determine the updated values
        Map<String, String> updatedValues = extractConfiguredPropertyValues(user, userGroupDTO);
        // create a user 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) {
                final FlowChangeConfigureDetails actionDetails = new FlowChangeConfigureDetails();
                actionDetails.setName(property);
                actionDetails.setValue(newValue);
                actionDetails.setPreviousValue(oldValue);
                // create a configuration action
                FlowChangeAction configurationAction = new FlowChangeAction();
                configurationAction.setUserIdentity(niFiUser.getIdentity());
                configurationAction.setOperation(operation);
                configurationAction.setTimestamp(actionTimestamp);
                configurationAction.setSourceId(user.getIdentifier());
                configurationAction.setSourceName(user.getName());
                configurationAction.setSourceType(Component.UserGroup);
                configurationAction.setActionDetails(actionDetails);
                actions.add(configurationAction);
            }
        }
        // ensure there are actions to record
        if (!actions.isEmpty()) {
            // save the actions
            saveActions(actions, logger);
        }
    }
    return updatedUserGroup;
}
Also used : Group(org.apache.nifi.authorization.Group) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) ArrayList(java.util.ArrayList) Operation(org.apache.nifi.action.Operation) Date(java.util.Date) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Around(org.aspectj.lang.annotation.Around)

Example 13 with FlowChangeConfigureDetails

use of org.apache.nifi.action.details.FlowChangeConfigureDetails in project nifi by apache.

the class StandardActionDAO method getConfigureDetails.

private ConfigureDetails getConfigureDetails(Integer actionId) throws DataAccessException {
    FlowChangeConfigureDetails configurationDetails = null;
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        // create the statement
        statement = connection.prepareStatement(SELECT_CONFIGURE_DETAILS_FOR_ACTION);
        statement.setInt(1, actionId);
        // execute the query
        rs = statement.executeQuery();
        // ensure results
        if (rs.next()) {
            configurationDetails = new FlowChangeConfigureDetails();
            configurationDetails.setName(rs.getString("NAME"));
            configurationDetails.setValue(rs.getString("VALUE"));
            configurationDetails.setPreviousValue(rs.getString("PREVIOUS_VALUE"));
        }
    } catch (SQLException sqle) {
        throw new DataAccessException(sqle);
    } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
    }
    return configurationDetails;
}
Also used : FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 14 with FlowChangeConfigureDetails

use of org.apache.nifi.action.details.FlowChangeConfigureDetails in project nifi by apache.

the class StandardNiFiWebConfigurationContext method saveActions.

@Override
public void saveActions(final NiFiWebRequestContext requestContext, final Collection<ConfigurationAction> configurationActions) {
    Objects.requireNonNull(configurationActions, "Actions cannot be null.");
    // ensure the path could be
    if (requestContext.getExtensionType() == null) {
        throw new IllegalArgumentException("The UI extension type must be specified.");
    }
    Component componentType = null;
    switch(requestContext.getExtensionType()) {
        case ProcessorConfiguration:
            // authorize access
            serviceFacade.authorizeAccess(lookup -> {
                final Authorizable authorizable = lookup.getProcessor(requestContext.getId()).getAuthorizable();
                authorizable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
            });
            componentType = Component.Processor;
            break;
        case ControllerServiceConfiguration:
            // authorize access
            serviceFacade.authorizeAccess(lookup -> {
                final Authorizable authorizable = lookup.getControllerService(requestContext.getId()).getAuthorizable();
                authorizable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
            });
            componentType = Component.ControllerService;
            break;
        case ReportingTaskConfiguration:
            // authorize access
            serviceFacade.authorizeAccess(lookup -> {
                final Authorizable authorizable = lookup.getReportingTask(requestContext.getId()).getAuthorizable();
                authorizable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
            });
            componentType = Component.ReportingTask;
            break;
    }
    if (componentType == null) {
        throw new IllegalArgumentException("UI extension type must support Processor, ControllerService, or ReportingTask configuration.");
    }
    // - when running standalone or cluster ncm - actions from custom UIs are stored locally
    // - clustered nodes do not serve custom UIs directly to users so they should never be invoking this method
    final Date now = new Date();
    final Collection<Action> actions = new HashSet<>(configurationActions.size());
    for (final ConfigurationAction configurationAction : configurationActions) {
        final FlowChangeExtensionDetails extensionDetails = new FlowChangeExtensionDetails();
        extensionDetails.setType(configurationAction.getType());
        final FlowChangeConfigureDetails configureDetails = new FlowChangeConfigureDetails();
        configureDetails.setName(configurationAction.getName());
        configureDetails.setPreviousValue(configurationAction.getPreviousValue());
        configureDetails.setValue(configurationAction.getValue());
        final FlowChangeAction action = new FlowChangeAction();
        action.setTimestamp(now);
        action.setSourceId(configurationAction.getId());
        action.setSourceName(configurationAction.getName());
        action.setSourceType(componentType);
        action.setOperation(Operation.Configure);
        action.setUserIdentity(getCurrentUserIdentity());
        action.setComponentDetails(extensionDetails);
        action.setActionDetails(configureDetails);
        actions.add(action);
    }
    if (!actions.isEmpty()) {
        try {
            // record the operations
            auditService.addActions(actions);
        } catch (final Throwable t) {
            logger.warn("Unable to record actions: " + t.getMessage());
            if (logger.isDebugEnabled()) {
                logger.warn(StringUtils.EMPTY, t);
            }
        }
    }
}
Also used : Action(org.apache.nifi.action.Action) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) RequestAction(org.apache.nifi.authorization.RequestAction) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) FlowChangeExtensionDetails(org.apache.nifi.action.component.details.FlowChangeExtensionDetails) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) Component(org.apache.nifi.action.Component) Date(java.util.Date) HashSet(java.util.HashSet) FlowChangeAction(org.apache.nifi.action.FlowChangeAction)

Example 15 with FlowChangeConfigureDetails

use of org.apache.nifi.action.details.FlowChangeConfigureDetails in project nifi by apache.

the class DtoFactory method createActionDetailsDto.

/**
 * Creates an ActionDetailsDTO for the specified ActionDetails.
 *
 * @param actionDetails details
 * @return dto
 */
private ActionDetailsDTO createActionDetailsDto(final ActionDetails actionDetails) {
    if (actionDetails == null) {
        return null;
    }
    if (actionDetails instanceof FlowChangeConfigureDetails) {
        final ConfigureDetailsDTO configureDetails = new ConfigureDetailsDTO();
        configureDetails.setName(((ConfigureDetails) actionDetails).getName());
        configureDetails.setPreviousValue(((ConfigureDetails) actionDetails).getPreviousValue());
        configureDetails.setValue(((ConfigureDetails) actionDetails).getValue());
        return configureDetails;
    } else if (actionDetails instanceof FlowChangeConnectDetails) {
        final ConnectDetailsDTO connectDetails = new ConnectDetailsDTO();
        connectDetails.setSourceId(((ConnectDetails) actionDetails).getSourceId());
        connectDetails.setSourceName(((ConnectDetails) actionDetails).getSourceName());
        connectDetails.setSourceType(((ConnectDetails) actionDetails).getSourceType().toString());
        connectDetails.setRelationship(((ConnectDetails) actionDetails).getRelationship());
        connectDetails.setDestinationId(((ConnectDetails) actionDetails).getDestinationId());
        connectDetails.setDestinationName(((ConnectDetails) actionDetails).getDestinationName());
        connectDetails.setDestinationType(((ConnectDetails) actionDetails).getDestinationType().toString());
        return connectDetails;
    } else if (actionDetails instanceof FlowChangeMoveDetails) {
        final MoveDetailsDTO moveDetails = new MoveDetailsDTO();
        moveDetails.setPreviousGroup(((MoveDetails) actionDetails).getPreviousGroup());
        moveDetails.setPreviousGroupId(((MoveDetails) actionDetails).getPreviousGroupId());
        moveDetails.setGroup(((MoveDetails) actionDetails).getGroup());
        moveDetails.setGroupId(((MoveDetails) actionDetails).getGroupId());
        return moveDetails;
    } else if (actionDetails instanceof FlowChangePurgeDetails) {
        final PurgeDetailsDTO purgeDetails = new PurgeDetailsDTO();
        purgeDetails.setEndDate(((PurgeDetails) actionDetails).getEndDate());
        return purgeDetails;
    } else {
        throw new WebApplicationException(new IllegalArgumentException(String.format("Unrecognized type of action details encountered %s during serialization.", actionDetails.toString())));
    }
}
Also used : FlowChangeConnectDetails(org.apache.nifi.action.details.FlowChangeConnectDetails) ConfigureDetailsDTO(org.apache.nifi.web.api.dto.action.details.ConfigureDetailsDTO) MoveDetailsDTO(org.apache.nifi.web.api.dto.action.details.MoveDetailsDTO) FlowChangePurgeDetails(org.apache.nifi.action.details.FlowChangePurgeDetails) WebApplicationException(javax.ws.rs.WebApplicationException) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) ConnectDetails(org.apache.nifi.action.details.ConnectDetails) FlowChangeConnectDetails(org.apache.nifi.action.details.FlowChangeConnectDetails) FlowChangeMoveDetails(org.apache.nifi.action.details.FlowChangeMoveDetails) FlowChangePurgeDetails(org.apache.nifi.action.details.FlowChangePurgeDetails) PurgeDetails(org.apache.nifi.action.details.PurgeDetails) PurgeDetailsDTO(org.apache.nifi.web.api.dto.action.details.PurgeDetailsDTO) ConnectDetailsDTO(org.apache.nifi.web.api.dto.action.details.ConnectDetailsDTO)

Aggregations

FlowChangeConfigureDetails (org.apache.nifi.action.details.FlowChangeConfigureDetails)15 FlowChangeAction (org.apache.nifi.action.FlowChangeAction)13 Date (java.util.Date)12 Action (org.apache.nifi.action.Action)12 ArrayList (java.util.ArrayList)11 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)11 Around (org.aspectj.lang.annotation.Around)11 Operation (org.apache.nifi.action.Operation)7 FlowChangeExtensionDetails (org.apache.nifi.action.component.details.FlowChangeExtensionDetails)5 ActionDetails (org.apache.nifi.action.details.ActionDetails)3 FlowChangeConnectDetails (org.apache.nifi.action.details.FlowChangeConnectDetails)3 FlowChangeMoveDetails (org.apache.nifi.action.details.FlowChangeMoveDetails)3 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)3 ScheduledState (org.apache.nifi.controller.ScheduledState)3 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 Component (org.apache.nifi.action.Component)2