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 019package org.apache.hadoop.hbase.rest; 020 021import java.io.IOException; 022import javax.servlet.ServletException; 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.security.UserGroupInformation; 028import org.apache.hadoop.security.authorize.AuthorizationException; 029import org.apache.hadoop.security.authorize.ProxyUsers; 030import org.apache.yetus.audience.InterfaceAudience; 031 032import org.apache.hbase.thirdparty.org.glassfish.jersey.server.ResourceConfig; 033import org.apache.hbase.thirdparty.org.glassfish.jersey.servlet.ServletContainer; 034import static org.apache.hadoop.hbase.http.ProxyUserAuthenticationFilter.toLowerCase; 035 036/** 037 * REST servlet container. It is used to get the remote request user 038 * without going through @HttpContext, so that we can minimize code changes. 039 */ 040@InterfaceAudience.Private 041public class RESTServletContainer extends ServletContainer { 042 private static final long serialVersionUID = -2474255003443394314L; 043 044 public RESTServletContainer(ResourceConfig config) { 045 super(config); 046 } 047 048 /** 049 * This container is used only if authentication and 050 * impersonation is enabled. The remote request user is used 051 * as a proxy user for impersonation in invoking any REST service. 052 */ 053 @Override 054 public void service(final HttpServletRequest request, 055 final HttpServletResponse response) throws ServletException, IOException { 056 final HttpServletRequest lowerCaseRequest = toLowerCase(request); 057 final String doAsUserFromQuery = lowerCaseRequest.getParameter("doas"); 058 RESTServlet servlet = RESTServlet.getInstance(); 059 if (doAsUserFromQuery != null) { 060 Configuration conf = servlet.getConfiguration(); 061 if (!servlet.supportsProxyuser()) { 062 throw new ServletException("Support for proxyuser is not configured"); 063 } 064 // Authenticated remote user is attempting to do 'doAs' proxy user. 065 UserGroupInformation ugi = UserGroupInformation.createRemoteUser(request.getRemoteUser()); 066 // create and attempt to authorize a proxy user (the client is attempting 067 // to do proxy user) 068 ugi = UserGroupInformation.createProxyUser(doAsUserFromQuery, ugi); 069 // validate the proxy user authorization 070 try { 071 ProxyUsers.authorize(ugi, request.getRemoteAddr(), conf); 072 } catch(AuthorizationException e) { 073 throw new ServletException(e.getMessage()); 074 } 075 servlet.setEffectiveUser(doAsUserFromQuery); 076 } else { 077 String effectiveUser = request.getRemoteUser(); 078 servlet.setEffectiveUser(effectiveUser); 079 } 080 super.service(request, response); 081 } 082}