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 */
018 package org.apache.oozie.servlet;
019
020 import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
021 import org.apache.hadoop.security.SecurityUtil;
022 import org.apache.hadoop.conf.Configuration;
023 import org.apache.oozie.service.Services;
024
025 import javax.servlet.FilterChain;
026 import javax.servlet.FilterConfig;
027 import javax.servlet.ServletException;
028 import javax.servlet.ServletRequest;
029 import javax.servlet.ServletResponse;
030 import javax.servlet.http.HttpServlet;
031 import javax.servlet.http.HttpServletRequest;
032 import java.io.IOException;
033 import java.net.InetAddress;
034 import java.net.UnknownHostException;
035 import java.util.Map;
036 import java.util.Properties;
037
038 /**
039 * Authentication filter that extends Hadoop-auth AuthenticationFilter to override
040 * the configuration loading.
041 */
042 public class AuthFilter extends AuthenticationFilter {
043 private static final String OOZIE_PREFIX = "oozie.authentication.";
044 private static final String KERBEROS_PRINCIPAL_CONFIG = "kerberos.principal";
045
046 private HttpServlet optionsServlet;
047
048 /**
049 * Initialize the filter.
050 *
051 * @param filterConfig filter configuration.
052 * @throws ServletException thrown if the filter could not be initialized.
053 */
054 @Override
055 public void init(FilterConfig filterConfig) throws ServletException {
056 super.init(filterConfig);
057 optionsServlet = new HttpServlet() {};
058 optionsServlet.init();
059 }
060
061 /**
062 * Destroy the filter.
063 */
064 @Override
065 public void destroy() {
066 optionsServlet.destroy();
067 super.destroy();
068 }
069
070 /**
071 * Returns the configuration from Oozie configuration to be used by the authentication filter.
072 * <p/>
073 * All properties from Oozie configuration which name starts with {@link #OOZIE_PREFIX} will
074 * be returned. The keys of the returned properties are trimmed from the {@link #OOZIE_PREFIX}
075 * prefix, for example the Oozie configuration property name 'oozie.authentication.type' will
076 * be just 'type'.
077 *
078 * @param configPrefix configuration prefix, this parameter is ignored by this implementation.
079 * @param filterConfig filter configuration, this parameter is ignored by this implementation.
080 * @return all Oozie configuration properties prefixed with {@link #OOZIE_PREFIX}, without the
081 * prefix.
082 */
083 @Override
084 protected Properties getConfiguration(String configPrefix, FilterConfig filterConfig) {
085 Properties props = new Properties();
086 Configuration conf = Services.get().getConf();
087
088 //setting the cookie path to root '/' so it is used for all resources.
089 props.setProperty(AuthenticationFilter.COOKIE_PATH, "/");
090
091 for (Map.Entry<String, String> entry : conf) {
092 String name = entry.getKey();
093 if (name.startsWith(OOZIE_PREFIX)) {
094 String value = conf.get(name);
095 name = name.substring(OOZIE_PREFIX.length());
096 if (name.equals(KERBEROS_PRINCIPAL_CONFIG)) {
097 String hostName = "localhost";
098 String principal = value;
099 try {
100 hostName = InetAddress.getLocalHost().getCanonicalHostName();
101 principal = SecurityUtil.getServerPrincipal(value, hostName);
102 } catch (IOException ioe) {
103 // ignore.
104 }
105 props.setProperty(name, principal);
106 }
107 else {
108 props.setProperty(name, value);
109 }
110 }
111 }
112
113 return props;
114 }
115
116 /**
117 * Enforces authentication using Hadoop-auth AuthenticationFilter.
118 * <p/>
119 * This method is overriden to respond to HTTP OPTIONS requests for authenticated calls, regardless
120 * of the target servlet supporting OPTIONS or not and to inject the authenticated user name as
121 * request attribute for Oozie to retrieve the user id.
122 *
123 * @param request http request.
124 * @param response http response.
125 * @param filterChain filter chain.
126 * @throws IOException thrown if an IO error occurs.
127 * @throws ServletException thrown if a servlet error occurs.
128 */
129 @Override
130 public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain)
131 throws IOException, ServletException {
132
133 FilterChain filterChainWrapper = new FilterChain() {
134 @Override
135 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
136 throws IOException, ServletException {
137 HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
138 if (httpRequest.getMethod().equals("OPTIONS")) {
139 optionsServlet.service(request, response);
140 }
141 else {
142 httpRequest.setAttribute(JsonRestServlet.USER_NAME, httpRequest.getRemoteUser());
143 filterChain.doFilter(servletRequest, servletResponse);
144 }
145 }
146 };
147
148 super.doFilter(request, response, filterChainWrapper);
149 }
150
151 }