Search in sources :

Example 1 with AuditService

use of org.apache.nifi.admin.service.AuditService in project nifi by apache.

the class TestFlowController method setup.

@Before
public void setup() {
    flowFileEventRepo = Mockito.mock(FlowFileEventRepository.class);
    auditService = Mockito.mock(AuditService.class);
    final Map<String, String> otherProps = new HashMap<>();
    otherProps.put(NiFiProperties.PROVENANCE_REPO_IMPLEMENTATION_CLASS, MockProvenanceRepository.class.getName());
    otherProps.put("nifi.remote.input.socket.port", "");
    otherProps.put("nifi.remote.input.secure", "");
    nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, otherProps);
    encryptor = StringEncryptor.createEncryptor(nifiProperties);
    // use the system bundle
    systemBundle = SystemBundle.create(nifiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    User user1 = new User.Builder().identifier("user-id-1").identity("user-1").build();
    User user2 = new User.Builder().identifier("user-id-2").identity("user-2").build();
    Group group1 = new Group.Builder().identifier("group-id-1").name("group-1").addUser(user1.getIdentifier()).build();
    Group group2 = new Group.Builder().identifier("group-id-2").name("group-2").build();
    AccessPolicy policy1 = new AccessPolicy.Builder().identifier("policy-id-1").resource("resource1").action(RequestAction.READ).addUser(user1.getIdentifier()).addUser(user2.getIdentifier()).build();
    AccessPolicy policy2 = new AccessPolicy.Builder().identifier("policy-id-2").resource("resource2").action(RequestAction.READ).addGroup(group1.getIdentifier()).addGroup(group2.getIdentifier()).addUser(user1.getIdentifier()).addUser(user2.getIdentifier()).build();
    Set<Group> groups1 = new LinkedHashSet<>();
    groups1.add(group1);
    groups1.add(group2);
    Set<User> users1 = new LinkedHashSet<>();
    users1.add(user1);
    users1.add(user2);
    Set<AccessPolicy> policies1 = new LinkedHashSet<>();
    policies1.add(policy1);
    policies1.add(policy2);
    authorizer = new MockPolicyBasedAuthorizer(groups1, users1, policies1);
    variableRegistry = new FileBasedVariableRegistry(nifiProperties.getVariableRegistryPropertiesPaths());
    bulletinRepo = Mockito.mock(BulletinRepository.class);
    controller = FlowController.createStandaloneInstance(flowFileEventRepo, nifiProperties, authorizer, auditService, encryptor, bulletinRepo, variableRegistry, Mockito.mock(FlowRegistryClient.class));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProcessGroup(org.apache.nifi.groups.ProcessGroup) Group(org.apache.nifi.authorization.Group) BulletinRepository(org.apache.nifi.reporting.BulletinRepository) User(org.apache.nifi.authorization.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AccessPolicy(org.apache.nifi.authorization.AccessPolicy) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) MockPolicyBasedAuthorizer(org.apache.nifi.authorization.MockPolicyBasedAuthorizer) AuditService(org.apache.nifi.admin.service.AuditService) FileBasedVariableRegistry(org.apache.nifi.registry.variable.FileBasedVariableRegistry) Before(org.junit.Before)

Example 2 with AuditService

use of org.apache.nifi.admin.service.AuditService in project nifi by apache.

the class TestRemoteProcessGroupAuditor method updateProcessGroupConfiguration.

@SuppressWarnings("unchecked")
private Collection<Action> updateProcessGroupConfiguration(RemoteProcessGroupDTO inputRPGDTO, RemoteProcessGroup existingRPG) throws Throwable {
    final RemoteProcessGroupAuditor auditor = new RemoteProcessGroupAuditor();
    final ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
    final String remoteProcessGroupId = "remote-process-group-id";
    inputRPGDTO.setId(remoteProcessGroupId);
    final String targetUrl = "http://localhost:8080/nifi";
    when(existingRPG.getTargetUri()).thenReturn(targetUrl);
    final RemoteProcessGroupDAO remoteProcessGroupDAO = mock(RemoteProcessGroupDAO.class);
    when(remoteProcessGroupDAO.getRemoteProcessGroup(remoteProcessGroupId)).thenReturn(existingRPG);
    // Setup updatedRPG mock based on inputRPGDTO.
    final RemoteProcessGroup updatedRPG = mock(RemoteProcessGroup.class);
    when(updatedRPG.getIdentifier()).thenReturn(remoteProcessGroupId);
    when(updatedRPG.isTransmitting()).thenReturn(inputRPGDTO.isTransmitting());
    when(updatedRPG.getCommunicationsTimeout()).thenReturn(inputRPGDTO.getCommunicationsTimeout());
    when(updatedRPG.getYieldDuration()).thenReturn(inputRPGDTO.getYieldDuration());
    when(updatedRPG.getTransportProtocol()).thenReturn(SiteToSiteTransportProtocol.valueOf(inputRPGDTO.getTransportProtocol()));
    when(updatedRPG.getProxyHost()).thenReturn(inputRPGDTO.getProxyHost());
    when(updatedRPG.getProxyPort()).thenReturn(inputRPGDTO.getProxyPort());
    when(updatedRPG.getProxyUser()).thenReturn(inputRPGDTO.getProxyUser());
    when(updatedRPG.getProxyPassword()).thenReturn(inputRPGDTO.getProxyPassword());
    when(joinPoint.proceed()).thenReturn(updatedRPG);
    // Capture added actions so that those can be asserted later.
    final AuditService auditService = mock(AuditService.class);
    final AtomicReference<Collection<Action>> addedActions = new AtomicReference<>();
    doAnswer(invocation -> {
        Collection<Action> actions = invocation.getArgumentAt(0, Collection.class);
        addedActions.set(actions);
        return null;
    }).when(auditService).addActions(any());
    auditor.setAuditService(auditService);
    auditor.auditUpdateProcessGroupConfiguration(joinPoint, inputRPGDTO, remoteProcessGroupDAO);
    final Collection<Action> actions = addedActions.get();
    // Assert common action values.
    if (actions != null) {
        actions.forEach(action -> {
            assertEquals(remoteProcessGroupId, action.getSourceId());
            assertEquals("user-id", action.getUserIdentity());
            assertEquals(targetUrl, ((RemoteProcessGroupDetails) action.getComponentDetails()).getUri());
            assertNotNull(action.getTimestamp());
        });
    }
    return actions;
}
Also used : RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) Action(org.apache.nifi.action.Action) Collection(java.util.Collection) AtomicReference(java.util.concurrent.atomic.AtomicReference) RemoteProcessGroupDAO(org.apache.nifi.web.dao.RemoteProcessGroupDAO) AuditService(org.apache.nifi.admin.service.AuditService) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 3 with AuditService

use of org.apache.nifi.admin.service.AuditService in project nifi by apache.

the class TestStandardReportingContext method setup.

@Before
public void setup() {
    flowFileEventRepo = Mockito.mock(FlowFileEventRepository.class);
    auditService = Mockito.mock(AuditService.class);
    final Map<String, String> otherProps = new HashMap<>();
    otherProps.put(NiFiProperties.PROVENANCE_REPO_IMPLEMENTATION_CLASS, MockProvenanceRepository.class.getName());
    otherProps.put("nifi.remote.input.socket.port", "");
    otherProps.put("nifi.remote.input.secure", "");
    nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, otherProps);
    encryptor = StringEncryptor.createEncryptor(nifiProperties);
    // use the system bundle
    systemBundle = SystemBundle.create(nifiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    User user1 = new User.Builder().identifier("user-id-1").identity("user-1").build();
    User user2 = new User.Builder().identifier("user-id-2").identity("user-2").build();
    Group group1 = new Group.Builder().identifier("group-id-1").name("group-1").addUser(user1.getIdentifier()).build();
    Group group2 = new Group.Builder().identifier("group-id-2").name("group-2").build();
    AccessPolicy policy1 = new AccessPolicy.Builder().identifier("policy-id-1").resource("resource1").action(RequestAction.READ).addUser(user1.getIdentifier()).addUser(user2.getIdentifier()).build();
    AccessPolicy policy2 = new AccessPolicy.Builder().identifier("policy-id-2").resource("resource2").action(RequestAction.READ).addGroup(group1.getIdentifier()).addGroup(group2.getIdentifier()).addUser(user1.getIdentifier()).addUser(user2.getIdentifier()).build();
    Set<Group> groups1 = new LinkedHashSet<>();
    groups1.add(group1);
    groups1.add(group2);
    Set<User> users1 = new LinkedHashSet<>();
    users1.add(user1);
    users1.add(user2);
    Set<AccessPolicy> policies1 = new LinkedHashSet<>();
    policies1.add(policy1);
    policies1.add(policy2);
    authorizer = new MockPolicyBasedAuthorizer(groups1, users1, policies1);
    variableRegistry = new FileBasedVariableRegistry(nifiProperties.getVariableRegistryPropertiesPaths());
    flowRegistry = Mockito.mock(FlowRegistryClient.class);
    bulletinRepo = Mockito.mock(BulletinRepository.class);
    controller = FlowController.createStandaloneInstance(flowFileEventRepo, nifiProperties, authorizer, auditService, encryptor, bulletinRepo, variableRegistry, flowRegistry);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Group(org.apache.nifi.authorization.Group) BulletinRepository(org.apache.nifi.reporting.BulletinRepository) User(org.apache.nifi.authorization.User) HashMap(java.util.HashMap) FlowRegistryClient(org.apache.nifi.registry.flow.FlowRegistryClient) AccessPolicy(org.apache.nifi.authorization.AccessPolicy) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) MockPolicyBasedAuthorizer(org.apache.nifi.authorization.MockPolicyBasedAuthorizer) AuditService(org.apache.nifi.admin.service.AuditService) FileBasedVariableRegistry(org.apache.nifi.registry.variable.FileBasedVariableRegistry) Before(org.junit.Before)

Example 4 with AuditService

use of org.apache.nifi.admin.service.AuditService in project nifi by apache.

the class StandardFlowSerializerTest method setUp.

@Before
public void setUp() throws Exception {
    final FlowFileEventRepository flowFileEventRepo = Mockito.mock(FlowFileEventRepository.class);
    final AuditService auditService = Mockito.mock(AuditService.class);
    final Map<String, String> otherProps = new HashMap<>();
    otherProps.put(NiFiProperties.PROVENANCE_REPO_IMPLEMENTATION_CLASS, MockProvenanceRepository.class.getName());
    otherProps.put("nifi.remote.input.socket.port", "");
    otherProps.put("nifi.remote.input.secure", "");
    final NiFiProperties nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, otherProps);
    final StringEncryptor encryptor = StringEncryptor.createEncryptor(nifiProperties);
    // use the system bundle
    systemBundle = SystemBundle.create(nifiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    final AbstractPolicyBasedAuthorizer authorizer = new MockPolicyBasedAuthorizer();
    final VariableRegistry variableRegistry = new FileBasedVariableRegistry(nifiProperties.getVariableRegistryPropertiesPaths());
    final BulletinRepository bulletinRepo = Mockito.mock(BulletinRepository.class);
    controller = FlowController.createStandaloneInstance(flowFileEventRepo, nifiProperties, authorizer, auditService, encryptor, bulletinRepo, variableRegistry, Mockito.mock(FlowRegistryClient.class));
    serializer = new StandardFlowSerializer(encryptor);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) BulletinRepository(org.apache.nifi.reporting.BulletinRepository) HashMap(java.util.HashMap) FileBasedVariableRegistry(org.apache.nifi.registry.variable.FileBasedVariableRegistry) VariableRegistry(org.apache.nifi.registry.VariableRegistry) AbstractPolicyBasedAuthorizer(org.apache.nifi.authorization.AbstractPolicyBasedAuthorizer) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) MockPolicyBasedAuthorizer(org.apache.nifi.authorization.MockPolicyBasedAuthorizer) AuditService(org.apache.nifi.admin.service.AuditService) FileBasedVariableRegistry(org.apache.nifi.registry.variable.FileBasedVariableRegistry) Before(org.junit.Before)

Example 5 with AuditService

use of org.apache.nifi.admin.service.AuditService in project nifi by apache.

the class StandardNiFiServiceFacadeTest method setUp.

@Before
public void setUp() throws Exception {
    // audit service
    final AuditService auditService = mock(AuditService.class);
    when(auditService.getAction(anyInt())).then(invocation -> {
        final Integer actionId = invocation.getArgumentAt(0, Integer.class);
        FlowChangeAction action = null;
        if (ACTION_ID_1.equals(actionId)) {
            action = getAction(actionId, PROCESSOR_ID_1);
        } else if (ACTION_ID_2.equals(actionId)) {
            action = getAction(actionId, PROCESSOR_ID_2);
        }
        return action;
    });
    when(auditService.getActions(any(HistoryQuery.class))).then(invocation -> {
        final History history = new History();
        history.setActions(Arrays.asList(getAction(ACTION_ID_1, PROCESSOR_ID_1), getAction(ACTION_ID_2, PROCESSOR_ID_2)));
        return history;
    });
    // authorizable lookup
    final AuthorizableLookup authorizableLookup = mock(AuthorizableLookup.class);
    when(authorizableLookup.getProcessor(Mockito.anyString())).then(getProcessorInvocation -> {
        final String processorId = getProcessorInvocation.getArgumentAt(0, String.class);
        // processor-2 is no longer part of the flow
        if (processorId.equals(PROCESSOR_ID_2)) {
            throw new ResourceNotFoundException("");
        }
        // component authorizable
        final ComponentAuthorizable componentAuthorizable = mock(ComponentAuthorizable.class);
        when(componentAuthorizable.getAuthorizable()).then(getAuthorizableInvocation -> {
            // authorizable
            final Authorizable authorizable = new Authorizable() {

                @Override
                public Authorizable getParentAuthorizable() {
                    return null;
                }

                @Override
                public Resource getResource() {
                    return ResourceFactory.getComponentResource(ResourceType.Processor, processorId, processorId);
                }
            };
            return authorizable;
        });
        return componentAuthorizable;
    });
    // authorizer
    authorizer = mock(Authorizer.class);
    when(authorizer.authorize(any(AuthorizationRequest.class))).then(invocation -> {
        final AuthorizationRequest request = invocation.getArgumentAt(0, AuthorizationRequest.class);
        AuthorizationResult result = AuthorizationResult.denied();
        if (request.getResource().getIdentifier().endsWith(PROCESSOR_ID_1)) {
            if (USER_1.equals(request.getIdentity())) {
                result = AuthorizationResult.approved();
            }
        } else if (request.getResource().equals(ResourceFactory.getControllerResource())) {
            if (USER_2.equals(request.getIdentity())) {
                result = AuthorizationResult.approved();
            }
        }
        return result;
    });
    // flow controller
    final FlowController controller = mock(FlowController.class);
    when(controller.getResource()).thenCallRealMethod();
    when(controller.getParentAuthorizable()).thenCallRealMethod();
    // controller facade
    final ControllerFacade controllerFacade = new ControllerFacade();
    controllerFacade.setFlowController(controller);
    serviceFacade = new StandardNiFiServiceFacade();
    serviceFacade.setAuditService(auditService);
    serviceFacade.setAuthorizableLookup(authorizableLookup);
    serviceFacade.setAuthorizer(authorizer);
    serviceFacade.setEntityFactory(new EntityFactory());
    serviceFacade.setDtoFactory(new DtoFactory());
    serviceFacade.setControllerFacade(controllerFacade);
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) AuthorizationRequest(org.apache.nifi.authorization.AuthorizationRequest) HistoryQuery(org.apache.nifi.history.HistoryQuery) ControllerFacade(org.apache.nifi.web.controller.ControllerFacade) History(org.apache.nifi.history.History) AuthorizationResult(org.apache.nifi.authorization.AuthorizationResult) AuthorizableLookup(org.apache.nifi.authorization.AuthorizableLookup) Authorizer(org.apache.nifi.authorization.Authorizer) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) FlowController(org.apache.nifi.controller.FlowController) AuditService(org.apache.nifi.admin.service.AuditService) EntityFactory(org.apache.nifi.web.api.dto.EntityFactory) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Before(org.junit.Before)

Aggregations

AuditService (org.apache.nifi.admin.service.AuditService)7 FlowFileEventRepository (org.apache.nifi.controller.repository.FlowFileEventRepository)4 BulletinRepository (org.apache.nifi.reporting.BulletinRepository)4 Before (org.junit.Before)4 HashMap (java.util.HashMap)3 MockPolicyBasedAuthorizer (org.apache.nifi.authorization.MockPolicyBasedAuthorizer)3 MockProvenanceRepository (org.apache.nifi.provenance.MockProvenanceRepository)3 FileBasedVariableRegistry (org.apache.nifi.registry.variable.FileBasedVariableRegistry)3 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Action (org.apache.nifi.action.Action)2 AccessPolicy (org.apache.nifi.authorization.AccessPolicy)2 AuthorizationRequest (org.apache.nifi.authorization.AuthorizationRequest)2 Authorizer (org.apache.nifi.authorization.Authorizer)2 Group (org.apache.nifi.authorization.Group)2 User (org.apache.nifi.authorization.User)2 FlowController (org.apache.nifi.controller.FlowController)2 StringEncryptor (org.apache.nifi.encrypt.StringEncryptor)2 RemoteProcessGroup (org.apache.nifi.groups.RemoteProcessGroup)2