|
18 | 18 |
|
19 | 19 | package org.apache.hadoop.yarn.server.security.http; |
20 | 20 |
|
21 | | -import java.util.Properties; |
| 21 | +import java.io.IOException; |
22 | 22 |
|
| 23 | +import javax.servlet.FilterChain; |
23 | 24 | import javax.servlet.FilterConfig; |
24 | 25 | import javax.servlet.ServletException; |
| 26 | +import javax.servlet.ServletRequest; |
| 27 | +import javax.servlet.ServletResponse; |
| 28 | +import javax.servlet.http.HttpServletRequest; |
| 29 | +import javax.servlet.http.HttpServletRequestWrapper; |
25 | 30 |
|
26 | 31 | import org.apache.hadoop.classification.InterfaceAudience.Private; |
27 | 32 | import org.apache.hadoop.classification.InterfaceStability.Unstable; |
28 | | -import org.apache.hadoop.security.authentication.server.AuthenticationFilter; |
| 33 | +import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager; |
| 34 | +import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticationFilter; |
| 35 | +import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticator; |
29 | 36 |
|
30 | 37 | @Private |
31 | 38 | @Unstable |
32 | | -public class RMAuthenticationFilter extends AuthenticationFilter { |
| 39 | +public class RMAuthenticationFilter extends |
| 40 | + DelegationTokenAuthenticationFilter { |
33 | 41 |
|
| 42 | + static private AbstractDelegationTokenSecretManager<?> manager; |
34 | 43 | public static final String AUTH_HANDLER_PROPERTY = |
35 | 44 | "yarn.resourcemanager.authentication-handler"; |
| 45 | + private static final String OLD_HEADER = "Hadoop-YARN-Auth-Delegation-Token"; |
36 | 46 |
|
37 | 47 | public RMAuthenticationFilter() { |
38 | 48 | } |
39 | 49 |
|
40 | 50 | @Override |
41 | | - protected Properties getConfiguration(String configPrefix, |
42 | | - FilterConfig filterConfig) throws ServletException { |
43 | | - |
44 | | - // In yarn-site.xml, we can simply set type to "kerberos". However, we need |
45 | | - // to replace the name here to use the customized Kerberos + DT service |
46 | | - // instead of the standard Kerberos handler. |
47 | | - |
48 | | - Properties properties = super.getConfiguration(configPrefix, filterConfig); |
49 | | - String yarnAuthHandler = properties.getProperty(AUTH_HANDLER_PROPERTY); |
50 | | - if (yarnAuthHandler == null || yarnAuthHandler.isEmpty()) { |
51 | | - // if http auth type is simple, the default authentication filter |
52 | | - // will handle it, else throw an exception |
53 | | - if (!properties.getProperty(AUTH_TYPE).equals("simple")) { |
54 | | - throw new ServletException("Authentication handler class is empty"); |
| 51 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 52 | + filterConfig.getServletContext().setAttribute( |
| 53 | + DelegationTokenAuthenticationFilter.DELEGATION_TOKEN_SECRET_MANAGER_ATTR, |
| 54 | + manager); |
| 55 | + super.init(filterConfig); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritDoc} |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public void doFilter(ServletRequest request, ServletResponse response, |
| 63 | + FilterChain filterChain) throws IOException, ServletException { |
| 64 | + HttpServletRequest req = (HttpServletRequest) request; |
| 65 | + String newHeader = |
| 66 | + req.getHeader(DelegationTokenAuthenticator.DELEGATION_TOKEN_HEADER); |
| 67 | + if (newHeader == null || newHeader.isEmpty()) { |
| 68 | + // For backward compatibility, allow use of the old header field |
| 69 | + // only when the new header doesn't exist |
| 70 | + final String oldHeader = req.getHeader(OLD_HEADER); |
| 71 | + if (oldHeader != null && !oldHeader.isEmpty()) { |
| 72 | + request = new HttpServletRequestWrapper(req) { |
| 73 | + @Override |
| 74 | + public String getHeader(String name) { |
| 75 | + if (name |
| 76 | + .equals(DelegationTokenAuthenticator.DELEGATION_TOKEN_HEADER)) { |
| 77 | + return oldHeader; |
| 78 | + } |
| 79 | + return super.getHeader(name); |
| 80 | + } |
| 81 | + }; |
55 | 82 | } |
56 | 83 | } |
57 | | - if (properties.getProperty(AUTH_TYPE).equalsIgnoreCase("kerberos")) { |
58 | | - properties.setProperty(AUTH_TYPE, yarnAuthHandler); |
59 | | - } |
60 | | - return properties; |
| 84 | + super.doFilter(request, response, filterChain); |
61 | 85 | } |
62 | 86 |
|
| 87 | + public static void setDelegationTokenSecretManager( |
| 88 | + AbstractDelegationTokenSecretManager<?> manager) { |
| 89 | + RMAuthenticationFilter.manager = manager; |
| 90 | + } |
63 | 91 | } |
0 commit comments