Search in sources :

Example 1 with AuditProviderFactory

use of org.apache.ranger.audit.provider.AuditProviderFactory in project ranger by apache.

the class TestAuditQueue method testFileDestination.

@Test
public void testFileDestination() {
    logger.debug("testFileDestination()...");
    int messageToSend = 10;
    int batchSize = messageToSend / 3;
    int queueSize = messageToSend * 2;
    // Should be less than final sleep time
    int intervalMS = 500;
    String logFolderName = "target/testFileDestination";
    File logFolder = new File(logFolderName);
    String logFileName = "test_ranger_audit.log";
    File logFile = new File(logFolder, logFileName);
    Properties props = new Properties();
    // Destination
    String filePropPrefix = AuditProviderFactory.AUDIT_DEST_BASE + ".file";
    props.put(filePropPrefix, "enable");
    props.put(filePropPrefix + "." + AuditQueue.PROP_NAME, "file");
    props.put(filePropPrefix + "." + FileAuditDestination.PROP_FILE_LOCAL_DIR, logFolderName);
    props.put(filePropPrefix + "." + FileAuditDestination.PROP_FILE_LOCAL_FILE_NAME_FORMAT, "%app-type%_ranger_audit.log");
    props.put(filePropPrefix + "." + FileAuditDestination.PROP_FILE_FILE_ROLLOVER, "" + 10);
    props.put(filePropPrefix + "." + AuditQueue.PROP_QUEUE, "batch");
    String batchPropPrefix = filePropPrefix + "." + "batch";
    props.put(batchPropPrefix + "." + AuditQueue.PROP_BATCH_SIZE, "" + batchSize);
    props.put(batchPropPrefix + "." + AuditQueue.PROP_QUEUE_SIZE, "" + queueSize);
    props.put(batchPropPrefix + "." + AuditQueue.PROP_BATCH_INTERVAL, "" + intervalMS);
    // Enable File Spooling
    int destRetryMS = 10;
    props.put(batchPropPrefix + "." + AuditQueue.PROP_FILE_SPOOL_ENABLE, "" + true);
    props.put(batchPropPrefix + "." + AuditFileSpool.PROP_FILE_SPOOL_LOCAL_DIR, "target");
    props.put(batchPropPrefix + "." + AuditFileSpool.PROP_FILE_SPOOL_DEST_RETRY_MS, "" + destRetryMS);
    AuditProviderFactory factory = AuditProviderFactory.getInstance();
    factory.init(props, "test");
    // FileAuditDestination fileDest = new FileAuditDestination();
    // fileDest.init(props, filePropPrefix);
    // 
    // AuditBatchQueue queue = new AuditBatchQueue(fileDest);
    // queue.init(props, batchPropPrefix);
    // queue.start();
    AuditHandler queue = factory.getProvider();
    for (int i = 0; i < messageToSend; i++) {
        queue.log(createEvent());
    }
    // Let's wait for second
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // ignore
    }
    queue.waitToComplete();
    queue.stop();
    queue.waitToComplete();
    assertTrue("File created", logFile.exists());
    try {
        List<AuthzAuditEvent> eventList = new ArrayList<AuthzAuditEvent>();
        int totalSum = 0;
        BufferedReader br = new BufferedReader(new FileReader(logFile));
        String line;
        int lastSeq = -1;
        boolean outOfSeq = false;
        while ((line = br.readLine()) != null) {
            AuthzAuditEvent event = MiscUtil.fromJson(line, AuthzAuditEvent.class);
            eventList.add(event);
            totalSum += event.getEventCount();
            if (event.getSeqNum() <= lastSeq) {
                outOfSeq = true;
            }
        }
        br.close();
        assertEquals("Total count", messageToSend, eventList.size());
        assertEquals("Total sum", messageToSend, totalSum);
        assertFalse("Event not in sequnce", outOfSeq);
    } catch (Throwable e) {
        logger.error("Error opening file for reading.", e);
        assertTrue("Error reading file. fileName=" + logFile + ", error=" + e.toString(), true);
    }
}
Also used : AuditHandler(org.apache.ranger.audit.provider.AuditHandler) BaseAuditHandler(org.apache.ranger.audit.provider.BaseAuditHandler) ArrayList(java.util.ArrayList) Properties(java.util.Properties) AuthzAuditEvent(org.apache.ranger.audit.model.AuthzAuditEvent) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) AuditProviderFactory(org.apache.ranger.audit.provider.AuditProviderFactory) File(java.io.File) Test(org.junit.Test)

Example 2 with AuditProviderFactory

use of org.apache.ranger.audit.provider.AuditProviderFactory in project ranger by apache.

the class TestAuditQueue method testAuditSummaryByInfra.

@Test
public void testAuditSummaryByInfra() {
    logger.debug("testAuditSummaryByInfra()...");
    Properties props = new Properties();
    // Destination
    String propPrefix = AuditProviderFactory.AUDIT_DEST_BASE + ".test";
    props.put(propPrefix, "enable");
    props.put(BaseAuditHandler.PROP_DEFAULT_PREFIX + "." + "summary" + "." + "enabled", "true");
    props.put(propPrefix + "." + BaseAuditHandler.PROP_NAME, "test");
    props.put(propPrefix + "." + AuditQueue.PROP_QUEUE, "none");
    props.put(BaseAuditHandler.PROP_DEFAULT_PREFIX + "." + AuditSummaryQueue.PROP_SUMMARY_INTERVAL, "" + 300);
    props.put(propPrefix + "." + BaseAuditHandler.PROP_CLASS_NAME, TestConsumer.class.getName());
    AuditProviderFactory factory = AuditProviderFactory.getInstance();
    factory.init(props, "test");
    AuditQueue queue = (AuditQueue) factory.getProvider();
    BaseAuditHandler consumer = (BaseAuditHandler) queue.getConsumer();
    while (consumer != null && consumer instanceof AuditQueue) {
        AuditQueue cQueue = (AuditQueue) consumer;
        consumer = (BaseAuditHandler) cQueue.getConsumer();
    }
    assertTrue("Consumer should be TestConsumer. class=" + consumer.getClass().getName(), consumer instanceof TestConsumer);
    TestConsumer testConsumer = (TestConsumer) consumer;
    commonTestSummary(testConsumer, queue);
}
Also used : BaseAuditHandler(org.apache.ranger.audit.provider.BaseAuditHandler) AuditQueue(org.apache.ranger.audit.queue.AuditQueue) AuditProviderFactory(org.apache.ranger.audit.provider.AuditProviderFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with AuditProviderFactory

use of org.apache.ranger.audit.provider.AuditProviderFactory in project ranger by apache.

the class RangerConfiguration method initAudit.

public void initAudit(String appType) {
    AuditProviderFactory auditFactory = AuditProviderFactory.getInstance();
    if (auditFactory == null) {
        LOG.error("Unable to find the AuditProviderFactory. (null) found");
        return;
    }
    Properties props = getProps();
    if (props == null) {
        return;
    }
    if (!auditFactory.isInitDone()) {
        auditFactory.init(props, appType);
    }
}
Also used : AuditProviderFactory(org.apache.ranger.audit.provider.AuditProviderFactory) Properties(java.util.Properties)

Aggregations

Properties (java.util.Properties)3 AuditProviderFactory (org.apache.ranger.audit.provider.AuditProviderFactory)3 BaseAuditHandler (org.apache.ranger.audit.provider.BaseAuditHandler)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 AuthzAuditEvent (org.apache.ranger.audit.model.AuthzAuditEvent)1 AuditHandler (org.apache.ranger.audit.provider.AuditHandler)1 AuditQueue (org.apache.ranger.audit.queue.AuditQueue)1