use of org.apache.ranger.audit.model.AuthzAuditEvent in project ranger by apache.
the class RangerHiveAuditHandler method createAuditEvents.
List<AuthzAuditEvent> createAuditEvents(Collection<RangerAccessResult> results) {
Map<Long, AuthzAuditEvent> auditEvents = new HashMap<Long, AuthzAuditEvent>();
Iterator<RangerAccessResult> iterator = results.iterator();
AuthzAuditEvent deniedAuditEvent = null;
while (iterator.hasNext() && deniedAuditEvent == null) {
RangerAccessResult result = iterator.next();
if (result.getIsAudited()) {
if (!result.getIsAllowed()) {
deniedAuditEvent = createAuditEvent(result);
} else {
long policyId = result.getPolicyId();
if (auditEvents.containsKey(policyId)) {
// add this result to existing event by updating column values
AuthzAuditEvent auditEvent = auditEvents.get(policyId);
RangerHiveAccessRequest request = (RangerHiveAccessRequest) result.getAccessRequest();
RangerHiveResource resource = (RangerHiveResource) request.getResource();
String resourcePath = auditEvent.getResourcePath() + "," + resource.getColumn();
auditEvent.setResourcePath(resourcePath);
Set<String> tags = getTags(request);
if (tags != null) {
auditEvent.getTags().addAll(tags);
}
} else {
// new event as this approval was due to a different policy.
AuthzAuditEvent auditEvent = createAuditEvent(result);
if (auditEvent != null) {
auditEvents.put(policyId, auditEvent);
}
}
}
}
}
List<AuthzAuditEvent> result;
if (deniedAuditEvent == null) {
result = new ArrayList<>(auditEvents.values());
} else {
result = Lists.newArrayList(deniedAuditEvent);
}
return result;
}
use of org.apache.ranger.audit.model.AuthzAuditEvent 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);
}
}
use of org.apache.ranger.audit.model.AuthzAuditEvent in project ranger by apache.
the class TestAuditQueue method createEvent.
private AuthzAuditEvent createEvent() {
AuthzAuditEvent event = new AuthzAuditEvent();
event.setSeqNum(++seqNum);
return event;
}
use of org.apache.ranger.audit.model.AuthzAuditEvent in project ranger by apache.
the class TestConsumer method logJSON.
@Override
public boolean logJSON(String jsonStr) {
if (isDown) {
return false;
}
countTotal++;
AuthzAuditEvent event = MiscUtil.fromJson(jsonStr, AuthzAuditEvent.class);
sumTotal += event.getEventCount();
logger.info("JSON:" + jsonStr);
eventList.add(event);
return true;
}
use of org.apache.ranger.audit.model.AuthzAuditEvent in project ranger by apache.
the class TestConsumer method log.
/*
* (non-Javadoc)
*
* @see org.apache.ranger.audit.provider.AuditProvider#log(org.apache.ranger
* .audit.model.AuditEventBase)
*/
@Override
public boolean log(AuditEventBase event) {
if (isDown) {
return false;
}
countTotal++;
if (event instanceof AuthzAuditEvent) {
AuthzAuditEvent azEvent = (AuthzAuditEvent) event;
sumTotal += azEvent.getEventCount();
logger.info("EVENT:" + event);
eventList.add(azEvent);
}
return true;
}
Aggregations