RANGER-2815:Ranger HDFSAuditDestination flush call should be privileged one
diff --git a/agents-audit/src/main/java/org/apache/ranger/audit/destination/HDFSAuditDestination.java b/agents-audit/src/main/java/org/apache/ranger/audit/destination/HDFSAuditDestination.java
index 01fc5e7..906ff34 100644
--- a/agents-audit/src/main/java/org/apache/ranger/audit/destination/HDFSAuditDestination.java
+++ b/agents-audit/src/main/java/org/apache/ranger/audit/destination/HDFSAuditDestination.java
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
+import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.*;
@@ -188,21 +189,13 @@
@Override
public void flush() {
logger.info("Flush called. name=" + getName());
- if (ostream != null) {
- try {
- synchronized (this) {
- if (ostream != null)
- // 1) PrinterWriter does not have bufferring of its own so
- // we need to flush its underlying stream
- // 2) HDFS flush() does not really flush all the way to disk.
- ostream.hflush();
- logger.info("Flush HDFS audit logs completed.....");
- }
- } catch (IOException e) {
- logger.error("Error on flushing log writer: " + e.getMessage() +
- "\nException will be ignored. name=" + getName() + ", fileName=" + currentFileName);
+ MiscUtil.executePrivilegedAction(new PrivilegedAction<Void>() {
+ @Override
+ public Void run() {
+ hflush();
+ return null;
}
- }
+ });
}
/*
@@ -373,6 +366,24 @@
}
}
+ private void hflush() {
+ if (ostream != null) {
+ try {
+ synchronized (this) {
+ if (ostream != null)
+ // 1) PrinterWriter does not have bufferring of its own so
+ // we need to flush its underlying stream
+ // 2) HDFS flush() does not really flush all the way to disk.
+ ostream.hflush();
+ logger.info("Flush HDFS audit logs completed.....");
+ }
+ } catch (IOException e) {
+ logger.error("Error on flushing log writer: " + e.getMessage() +
+ "\nException will be ignored. name=" + getName() + ", fileName=" + currentFileName);
+ }
+ }
+ }
+
private Date rollOverByDuration() {
long rollOverTime = rollingTimeUtil.computeNextRollingTime(fileRolloverSec,nextRollOverTime);
return new Date(rollOverTime);