Skip to content

Commit

Permalink
[apache#772] fix(kerberos): cache proxy user ugi to avoid memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
zuston committed Mar 28, 2023
1 parent e8d9909 commit 1d523e6
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import java.io.IOException;
import java.security.PrivilegedExceptionAction;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
Expand All @@ -38,6 +40,7 @@ public class HadoopSecurityContext implements SecurityContext {

private UserGroupInformation loginUgi;
private ScheduledExecutorService refreshScheduledExecutor;
private Map<String, UserGroupInformation> proxyUserUgiPool;

public HadoopSecurityContext(
String krb5ConfPath,
Expand Down Expand Up @@ -75,6 +78,7 @@ public HadoopSecurityContext(
refreshIntervalSec,
refreshIntervalSec,
TimeUnit.SECONDS);
proxyUserUgiPool = Maps.newConcurrentMap();
}

private void authRefresh() {
Expand All @@ -94,8 +98,10 @@ public <T> T runSecured(String user, Callable<T> securedCallable) throws Excepti

// Run with the proxy user.
if (!user.equals(loginUgi.getShortUserName())) {
UserGroupInformation proxyUserUgi =
proxyUserUgiPool.computeIfAbsent(user, x -> UserGroupInformation.createProxyUser(x, loginUgi));
return executeWithUgiWrapper(
UserGroupInformation.createProxyUser(user, loginUgi),
proxyUserUgi,
securedCallable
);
}
Expand All @@ -118,5 +124,9 @@ public void close() throws IOException {
if (refreshScheduledExecutor != null) {
refreshScheduledExecutor.shutdown();
}
if (proxyUserUgiPool != null) {
proxyUserUgiPool.clear();
proxyUserUgiPool = null;
}
}
}

0 comments on commit 1d523e6

Please sign in to comment.