HDFS-4415. HostnameFilter should handle hostname resolution failures and continue processing. Contributed by Robert Kanter.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1434952 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
707a53c5af
commit
6bf895b378
|
@ -29,6 +29,9 @@ import javax.servlet.ServletRequest;
|
|||
import javax.servlet.ServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Filter that resolves the requester hostname.
|
||||
|
@ -36,6 +39,7 @@ import java.net.InetAddress;
|
|||
@InterfaceAudience.Private
|
||||
public class HostnameFilter implements Filter {
|
||||
static final ThreadLocal<String> HOSTNAME_TL = new ThreadLocal<String>();
|
||||
private static final Logger log = LoggerFactory.getLogger(HostnameFilter.class);
|
||||
|
||||
/**
|
||||
* Initializes the filter.
|
||||
|
@ -66,7 +70,19 @@ public class HostnameFilter implements Filter {
|
|||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
try {
|
||||
String hostname = InetAddress.getByName(request.getRemoteAddr()).getCanonicalHostName();
|
||||
String hostname;
|
||||
try {
|
||||
String address = request.getRemoteAddr();
|
||||
if (address != null) {
|
||||
hostname = InetAddress.getByName(address).getCanonicalHostName();
|
||||
} else {
|
||||
log.warn("Request remote address is NULL");
|
||||
hostname = "???";
|
||||
}
|
||||
} catch (UnknownHostException ex) {
|
||||
log.warn("Request remote address could not be resolved, {0}", ex.toString(), ex);
|
||||
hostname = "???";
|
||||
}
|
||||
HOSTNAME_TL.set(hostname);
|
||||
chain.doFilter(request, response);
|
||||
} finally {
|
||||
|
|
|
@ -64,4 +64,30 @@ public class TestHostnameFilter extends HTestCase {
|
|||
filter.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingHostname() throws Exception {
|
||||
ServletRequest request = Mockito.mock(ServletRequest.class);
|
||||
Mockito.when(request.getRemoteAddr()).thenReturn(null);
|
||||
|
||||
ServletResponse response = Mockito.mock(ServletResponse.class);
|
||||
|
||||
final AtomicBoolean invoked = new AtomicBoolean();
|
||||
|
||||
FilterChain chain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
|
||||
throws IOException, ServletException {
|
||||
assertTrue(HostnameFilter.get().contains("???"));
|
||||
invoked.set(true);
|
||||
}
|
||||
};
|
||||
|
||||
Filter filter = new HostnameFilter();
|
||||
filter.init(null);
|
||||
assertNull(HostnameFilter.get());
|
||||
filter.doFilter(request, response, chain);
|
||||
assertTrue(invoked.get());
|
||||
assertNull(HostnameFilter.get());
|
||||
filter.destroy();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -420,6 +420,9 @@ Release 2.0.3-alpha - Unreleased
|
|||
|
||||
HDFS-1245. Pluggable block id generation. (shv)
|
||||
|
||||
HDFS-4415. HostnameFilter should handle hostname resolution failures and
|
||||
continue processing. (Robert Kanter via atm)
|
||||
|
||||
BREAKDOWN OF HDFS-3077 SUBTASKS
|
||||
|
||||
HDFS-3077. Quorum-based protocol for reading and writing edit logs.
|
||||
|
|
Loading…
Reference in New Issue