|
@@ -25,6 +25,7 @@ import java.net.URLConnection;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.security.AccessController;
|
|
|
import java.security.PrivilegedAction;
|
|
|
+import java.security.PrivilegedExceptionAction;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.ServiceLoader;
|
|
@@ -451,6 +452,41 @@ public class SecurityUtil {
|
|
|
return action.run();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Perform the given action as the daemon's login user. If an
|
|
|
+ * InterruptedException is thrown, it is converted to an IOException.
|
|
|
+ *
|
|
|
+ * @param action the action to perform
|
|
|
+ * @return the result of the action
|
|
|
+ * @throws IOException in the event of error
|
|
|
+ */
|
|
|
+ public static <T> T doAsLoginUser(PrivilegedExceptionAction<T> action)
|
|
|
+ throws IOException {
|
|
|
+ return doAsUser(UserGroupInformation.getLoginUser(), action);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Perform the given action as the daemon's current user. If an
|
|
|
+ * InterruptedException is thrown, it is converted to an IOException.
|
|
|
+ *
|
|
|
+ * @param action the action to perform
|
|
|
+ * @return the result of the action
|
|
|
+ * @throws IOException in the event of error
|
|
|
+ */
|
|
|
+ public static <T> T doAsCurrentUser(PrivilegedExceptionAction<T> action)
|
|
|
+ throws IOException {
|
|
|
+ return doAsUser(UserGroupInformation.getCurrentUser(), action);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static <T> T doAsUser(UserGroupInformation ugi,
|
|
|
+ PrivilegedExceptionAction<T> action) throws IOException {
|
|
|
+ try {
|
|
|
+ return ugi.doAs(action);
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
+ throw new IOException(ie);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Open a (if need be) secure connection to a URL in a secure environment
|