Search in sources :

Example 6 with EventAuthorizer

use of org.apache.nifi.provenance.authorization.EventAuthorizer in project nifi by apache.

the class TestPartitionedWriteAheadEventStore method testGetSpecificEventIds.

@Test
public void testGetSpecificEventIds() throws IOException {
    final RepositoryConfiguration config = createConfig();
    final PartitionedWriteAheadEventStore store = new PartitionedWriteAheadEventStore(config, writerFactory, readerFactory, EventReporter.NO_OP, new EventFileManager());
    store.initialize();
    final int numEvents = 20;
    final List<ProvenanceEventRecord> events = new ArrayList<>(numEvents);
    for (int i = 0; i < numEvents; i++) {
        final ProvenanceEventRecord event = createEvent();
        store.addEvents(Collections.singleton(event));
        events.add(event);
    }
    final EventAuthorizer allowEvenNumberedEventIds = new EventAuthorizer() {

        @Override
        public boolean isAuthorized(final ProvenanceEventRecord event) {
            return event.getEventId() % 2 == 0L;
        }

        @Override
        public void authorize(ProvenanceEventRecord event) throws AccessDeniedException {
            if (!isAuthorized(event)) {
                throw new AccessDeniedException();
            }
        }
    };
    final List<Long> evenEventIds = new ArrayList<>();
    final List<Long> oddEventIds = new ArrayList<>();
    final List<Long> allEventIds = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        final Long id = Long.valueOf(i);
        allEventIds.add(id);
        if (i % 2 == 0) {
            evenEventIds.add(id);
        } else {
            oddEventIds.add(id);
        }
    }
    final List<ProvenanceEventRecord> storedEvents = store.getEvents(evenEventIds, allowEvenNumberedEventIds, EventTransformer.EMPTY_TRANSFORMER);
    assertEquals(numEvents / 2, storedEvents.size());
    for (int i = 0; i < storedEvents.size(); i++) {
        assertEquals(events.get(i * 2), storedEvents.get(i));
    }
    assertTrue(store.getEvents(oddEventIds, allowEvenNumberedEventIds, EventTransformer.EMPTY_TRANSFORMER).isEmpty());
    final List<ProvenanceEventRecord> allStoredEvents = store.getEvents(allEventIds, EventAuthorizer.GRANT_ALL, EventTransformer.EMPTY_TRANSFORMER);
    assertEquals(events, allStoredEvents);
}
Also used : AccessDeniedException(org.apache.nifi.authorization.AccessDeniedException) EventAuthorizer(org.apache.nifi.provenance.authorization.EventAuthorizer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) StandardProvenanceEventRecord(org.apache.nifi.provenance.StandardProvenanceEventRecord) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) RepositoryConfiguration(org.apache.nifi.provenance.RepositoryConfiguration) Test(org.junit.Test)

Example 7 with EventAuthorizer

use of org.apache.nifi.provenance.authorization.EventAuthorizer in project nifi by apache.

the class TestPartitionedWriteAheadEventStore method testGetEventsWithMinIdAndCountWithAuthorizer.

@Test
public void testGetEventsWithMinIdAndCountWithAuthorizer() throws IOException {
    final RepositoryConfiguration config = createConfig();
    final PartitionedWriteAheadEventStore store = new PartitionedWriteAheadEventStore(config, writerFactory, readerFactory, EventReporter.NO_OP, new EventFileManager());
    store.initialize();
    final int numEvents = 20;
    final List<ProvenanceEventRecord> events = new ArrayList<>(numEvents);
    for (int i = 0; i < numEvents; i++) {
        final ProvenanceEventRecord event = createEvent();
        store.addEvents(Collections.singleton(event));
        events.add(event);
    }
    final EventAuthorizer allowEventNumberedEventIds = new EventAuthorizer() {

        @Override
        public boolean isAuthorized(final ProvenanceEventRecord event) {
            return event.getEventId() % 2 == 0L;
        }

        @Override
        public void authorize(ProvenanceEventRecord event) throws AccessDeniedException {
            if (!isAuthorized(event)) {
                throw new AccessDeniedException();
            }
        }
    };
    final List<ProvenanceEventRecord> storedEvents = store.getEvents(0, 20, allowEventNumberedEventIds, EventTransformer.EMPTY_TRANSFORMER);
    assertEquals(numEvents / 2, storedEvents.size());
    for (int i = 0; i < storedEvents.size(); i++) {
        assertEquals(events.get(i * 2), storedEvents.get(i));
    }
}
Also used : AccessDeniedException(org.apache.nifi.authorization.AccessDeniedException) EventAuthorizer(org.apache.nifi.provenance.authorization.EventAuthorizer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) StandardProvenanceEventRecord(org.apache.nifi.provenance.StandardProvenanceEventRecord) ArrayList(java.util.ArrayList) RepositoryConfiguration(org.apache.nifi.provenance.RepositoryConfiguration) Test(org.junit.Test)

Example 8 with EventAuthorizer

use of org.apache.nifi.provenance.authorization.EventAuthorizer in project nifi by apache.

the class PartitionedEventStore method getEvents.

private List<ProvenanceEventRecord> getEvents(final int maxRecords, final EventAuthorizer authorizer, final Function<EventStorePartition, EventIterator> eventIteratorFactory, final EventTransformer transformer) throws IOException {
    if (maxRecords < 1) {
        return Collections.emptyList();
    }
    final List<ProvenanceEventRecord> selectedEvents = new ArrayList<>();
    // Create a Map so that the key is the next record available from a partition and the value is the EventIterator from which
    // the record came. This sorted map is then used so that we are able to always get the first entry, which is the next
    // lowest record id among all partitions.
    final SortedMap<ProvenanceEventRecord, EventIterator> recordToIteratorMap = new TreeMap<>((o1, o2) -> Long.compare(o1.getEventId(), o2.getEventId()));
    try {
        // Seed our map with the first event in each Partition.
        for (final EventStorePartition partition : getPartitions()) {
            final EventAuthorizer nonNullAuthorizer = authorizer == null ? EventAuthorizer.GRANT_ALL : authorizer;
            final EventIterator partitionIterator = eventIteratorFactory.apply(partition);
            final EventIterator iterator = new AuthorizingEventIterator(partitionIterator, nonNullAuthorizer, transformer);
            final Optional<ProvenanceEventRecord> option = iterator.nextEvent();
            if (option.isPresent()) {
                recordToIteratorMap.put(option.get(), iterator);
            }
        }
        // If no records found, just return the empty list.
        if (recordToIteratorMap.isEmpty()) {
            return selectedEvents;
        }
        // Get the event with the next-lowest ID. Add it to the list of selected events,
        // then read the next event from the same EventIterator that this event came from.
        // This ensures that our map is always populated with the next event for each
        // EventIterator, which also ensures that the first key in our map is the event
        // with the lowest ID (since all events from a given EventIterator have monotonically
        // increasing Event ID's).
        ProvenanceEventRecord nextEvent = recordToIteratorMap.firstKey();
        while (nextEvent != null && selectedEvents.size() < maxRecords) {
            selectedEvents.add(nextEvent);
            final EventIterator iterator = recordToIteratorMap.remove(nextEvent);
            final Optional<ProvenanceEventRecord> nextRecordFromIterator = iterator.nextEvent();
            if (nextRecordFromIterator.isPresent()) {
                recordToIteratorMap.put(nextRecordFromIterator.get(), iterator);
            }
            nextEvent = recordToIteratorMap.isEmpty() ? null : recordToIteratorMap.firstKey();
        }
        return selectedEvents;
    } finally {
        // Ensure that we close all record readers that have been created
        for (final EventIterator iterator : recordToIteratorMap.values()) {
            try {
                iterator.close();
            } catch (final Exception e) {
                if (logger.isDebugEnabled()) {
                    logger.warn("Failed to close Record Reader {}", iterator, e);
                } else {
                    logger.warn("Failed to close Record Reader {}", iterator);
                }
            }
        }
    }
}
Also used : EventAuthorizer(org.apache.nifi.provenance.authorization.EventAuthorizer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ArrayList(java.util.ArrayList) AuthorizingEventIterator(org.apache.nifi.provenance.store.iterator.AuthorizingEventIterator) EventIterator(org.apache.nifi.provenance.store.iterator.EventIterator) TreeMap(java.util.TreeMap) AuthorizingEventIterator(org.apache.nifi.provenance.store.iterator.AuthorizingEventIterator) IOException(java.io.IOException)

Aggregations

ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)8 EventAuthorizer (org.apache.nifi.provenance.authorization.EventAuthorizer)8 ArrayList (java.util.ArrayList)6 RepositoryConfiguration (org.apache.nifi.provenance.RepositoryConfiguration)6 StandardProvenanceEventRecord (org.apache.nifi.provenance.StandardProvenanceEventRecord)6 Test (org.junit.Test)6 AccessDeniedException (org.apache.nifi.authorization.AccessDeniedException)5 IOException (java.io.IOException)4 List (java.util.List)3 Set (java.util.Set)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 IndexManager (org.apache.nifi.provenance.lucene.IndexManager)3 SimpleIndexManager (org.apache.nifi.provenance.lucene.SimpleIndexManager)3 Query (org.apache.nifi.provenance.search.Query)3 QuerySubmission (org.apache.nifi.provenance.search.QuerySubmission)3 ArrayListEventStore (org.apache.nifi.provenance.store.ArrayListEventStore)3 StorageResult (org.apache.nifi.provenance.store.StorageResult)3 File (java.io.File)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2