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;
022
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.glassfish.jersey.servlet.ServletContainer;
030
031import org.apache.hadoop.security.UserGroupInformation;
032import org.apache.hadoop.security.authorize.AuthorizationException;
033import org.apache.hadoop.security.authorize.ProxyUsers;
034import org.apache.hadoop.conf.Configuration;
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  /**
045   * This container is used only if authentication and
046   * impersonation is enabled. The remote request user is used
047   * as a proxy user for impersonation in invoking any REST service.
048   */
049  @Override
050  public void service(final HttpServletRequest request,
051      final HttpServletResponse response) throws ServletException, IOException {
052    final String doAsUserFromQuery = request.getParameter("doAs");
053    RESTServlet servlet = RESTServlet.getInstance();
054    if (doAsUserFromQuery != null) {
055      Configuration conf = servlet.getConfiguration();
056      if (!servlet.supportsProxyuser()) {
057        throw new ServletException("Support for proxyuser is not configured");
058      }
059      // Authenticated remote user is attempting to do 'doAs' proxy user.
060      UserGroupInformation ugi = UserGroupInformation.createRemoteUser(request.getRemoteUser());
061      // create and attempt to authorize a proxy user (the client is attempting
062      // to do proxy user)
063      ugi = UserGroupInformation.createProxyUser(doAsUserFromQuery, ugi);
064      // validate the proxy user authorization
065      try {
066        ProxyUsers.authorize(ugi, request.getRemoteAddr(), conf);
067      } catch(AuthorizationException e) {
068        throw new ServletException(e.getMessage());
069      }
070      servlet.setEffectiveUser(doAsUserFromQuery);
071    } else {
072      String effectiveUser = request.getRemoteUser();
073      servlet.setEffectiveUser(effectiveUser);
074    }
075    super.service(request, response);
076  }
077}