001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hdfs.web; 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.Enumeration; 023import java.util.HashMap; 024import java.util.Iterator; 025import java.util.List; 026import java.util.Map; 027import java.util.Properties; 028 029import javax.servlet.FilterChain; 030import javax.servlet.FilterConfig; 031import javax.servlet.ServletException; 032import javax.servlet.ServletRequest; 033import javax.servlet.ServletResponse; 034import javax.servlet.http.HttpServletRequest; 035import javax.servlet.http.HttpServletRequestWrapper; 036 037import org.apache.hadoop.hdfs.web.resources.DelegationParam; 038import org.apache.hadoop.security.UserGroupInformation; 039import org.apache.hadoop.security.authentication.server.AuthenticationFilter; 040import org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler; 041import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler; 042import org.apache.hadoop.util.StringUtils; 043 044/** 045 * Subclass of {@link AuthenticationFilter} that 046 * obtains Hadoop-Auth configuration for webhdfs. 047 */ 048public class AuthFilter extends AuthenticationFilter { 049 private static final String CONF_PREFIX = "dfs.web.authentication."; 050 051 /** 052 * Returns the filter configuration properties, 053 * including the ones prefixed with {@link #CONF_PREFIX}. 054 * The prefix is removed from the returned property names. 055 * 056 * @param prefix parameter not used. 057 * @param config parameter contains the initialization values. 058 * @return Hadoop-Auth configuration properties. 059 * @throws ServletException 060 */ 061 @Override 062 protected Properties getConfiguration(String prefix, FilterConfig config) 063 throws ServletException { 064 final Properties p = super.getConfiguration(CONF_PREFIX, config); 065 // set authentication type 066 p.setProperty(AUTH_TYPE, UserGroupInformation.isSecurityEnabled()? 067 KerberosAuthenticationHandler.TYPE: PseudoAuthenticationHandler.TYPE); 068 // if not set, enable anonymous for pseudo authentication 069 if (p.getProperty(PseudoAuthenticationHandler.ANONYMOUS_ALLOWED) == null) { 070 p.setProperty(PseudoAuthenticationHandler.ANONYMOUS_ALLOWED, "true"); 071 } 072 //set cookie path 073 p.setProperty(COOKIE_PATH, "/"); 074 return p; 075 } 076 077 @Override 078 public void doFilter(ServletRequest request, ServletResponse response, 079 FilterChain filterChain) throws IOException, ServletException { 080 final HttpServletRequest httpRequest = toLowerCase((HttpServletRequest)request); 081 final String tokenString = httpRequest.getParameter(DelegationParam.NAME); 082 if (tokenString != null) { 083 //Token is present in the url, therefore token will be used for 084 //authentication, bypass kerberos authentication. 085 filterChain.doFilter(httpRequest, response); 086 return; 087 } 088 super.doFilter(httpRequest, response, filterChain); 089 } 090 091 private static HttpServletRequest toLowerCase(final HttpServletRequest request) { 092 @SuppressWarnings("unchecked") 093 final Map<String, String[]> original = (Map<String, String[]>)request.getParameterMap(); 094 if (!ParamFilter.containsUpperCase(original.keySet())) { 095 return request; 096 } 097 098 final Map<String, List<String>> m = new HashMap<String, List<String>>(); 099 for(Map.Entry<String, String[]> entry : original.entrySet()) { 100 final String key = StringUtils.toLowerCase(entry.getKey()); 101 List<String> strings = m.get(key); 102 if (strings == null) { 103 strings = new ArrayList<String>(); 104 m.put(key, strings); 105 } 106 for(String v : entry.getValue()) { 107 strings.add(v); 108 } 109 } 110 111 return new HttpServletRequestWrapper(request) { 112 private Map<String, String[]> parameters = null; 113 114 @Override 115 public Map<String, String[]> getParameterMap() { 116 if (parameters == null) { 117 parameters = new HashMap<String, String[]>(); 118 for(Map.Entry<String, List<String>> entry : m.entrySet()) { 119 final List<String> a = entry.getValue(); 120 parameters.put(entry.getKey(), a.toArray(new String[a.size()])); 121 } 122 } 123 return parameters; 124 } 125 126 @Override 127 public String getParameter(String name) { 128 final List<String> a = m.get(name); 129 return a == null? null: a.get(0); 130 } 131 132 @Override 133 public String[] getParameterValues(String name) { 134 return getParameterMap().get(name); 135 } 136 137 @Override 138 public Enumeration<String> getParameterNames() { 139 final Iterator<String> i = m.keySet().iterator(); 140 return new Enumeration<String>() { 141 @Override 142 public boolean hasMoreElements() { 143 return i.hasNext(); 144 } 145 @Override 146 public String nextElement() { 147 return i.next(); 148 } 149 }; 150 } 151 }; 152 } 153}