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.fs.http.server; 019 020import org.apache.hadoop.classification.InterfaceAudience; 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.hdfs.web.WebHdfsConstants; 023import org.apache.hadoop.security.authentication.server.AuthenticationFilter; 024import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticationFilter; 025import org.apache.hadoop.security.token.delegation.web.KerberosDelegationTokenAuthenticationHandler; 026 027import javax.servlet.FilterConfig; 028import javax.servlet.ServletException; 029 030import java.io.FileInputStream; 031import java.io.IOException; 032import java.io.InputStreamReader; 033import java.io.Reader; 034import java.nio.charset.StandardCharsets; 035import java.util.Map; 036import java.util.Properties; 037 038/** 039 * Subclass of hadoop-auth <code>AuthenticationFilter</code> that obtains its configuration 040 * from HttpFSServer's server configuration. 041 */ 042@InterfaceAudience.Private 043public class HttpFSAuthenticationFilter 044 extends DelegationTokenAuthenticationFilter { 045 046 private static final String CONF_PREFIX = "httpfs.authentication."; 047 048 private static final String SIGNATURE_SECRET_FILE = SIGNATURE_SECRET + ".file"; 049 050 /** 051 * Returns the hadoop-auth configuration from HttpFSServer's configuration. 052 * <p> 053 * It returns all HttpFSServer's configuration properties prefixed with 054 * <code>httpfs.authentication</code>. The <code>httpfs.authentication</code> 055 * prefix is removed from the returned property names. 056 * 057 * @param configPrefix parameter not used. 058 * @param filterConfig parameter not used. 059 * 060 * @return hadoop-auth configuration read from HttpFSServer's configuration. 061 */ 062 @Override 063 protected Properties getConfiguration(String configPrefix, 064 FilterConfig filterConfig) throws ServletException{ 065 Properties props = new Properties(); 066 Configuration conf = HttpFSServerWebApp.get().getConfig(); 067 068 props.setProperty(AuthenticationFilter.COOKIE_PATH, "/"); 069 for (Map.Entry<String, String> entry : conf) { 070 String name = entry.getKey(); 071 if (name.startsWith(CONF_PREFIX)) { 072 String value = conf.get(name); 073 name = name.substring(CONF_PREFIX.length()); 074 props.setProperty(name, value); 075 } 076 } 077 078 String signatureSecretFile = props.getProperty(SIGNATURE_SECRET_FILE, null); 079 if (signatureSecretFile == null) { 080 throw new RuntimeException("Undefined property: " + SIGNATURE_SECRET_FILE); 081 } 082 083 try { 084 StringBuilder secret = new StringBuilder(); 085 Reader reader = new InputStreamReader(new FileInputStream( 086 signatureSecretFile), StandardCharsets.UTF_8); 087 int c = reader.read(); 088 while (c > -1) { 089 secret.append((char)c); 090 c = reader.read(); 091 } 092 reader.close(); 093 props.setProperty(AuthenticationFilter.SIGNATURE_SECRET, secret.toString()); 094 } catch (IOException ex) { 095 throw new RuntimeException("Could not read HttpFS signature secret file: " + signatureSecretFile); 096 } 097 setAuthHandlerClass(props); 098 props.setProperty(KerberosDelegationTokenAuthenticationHandler.TOKEN_KIND, 099 WebHdfsConstants.WEBHDFS_TOKEN_KIND.toString()); 100 return props; 101 } 102 103 protected Configuration getProxyuserConfiguration(FilterConfig filterConfig) { 104 Map<String, String> proxyuserConf = HttpFSServerWebApp.get().getConfig(). 105 getValByRegex("httpfs\\.proxyuser\\."); 106 Configuration conf = new Configuration(false); 107 for (Map.Entry<String, String> entry : proxyuserConf.entrySet()) { 108 conf.set(entry.getKey().substring("httpfs.".length()), entry.getValue()); 109 } 110 return conf; 111 } 112 113}