001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with this
004 * work for additional information regarding copyright ownership. The ASF
005 * licenses this file to you under the Apache License, Version 2.0 (the
006 * "License"); you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 * 
009 * http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations under
015 * the License.
016 */
017package org.apache.hadoop.hdfs.server.namenode;
018
019import java.io.IOException;
020import java.security.PrivilegedExceptionAction;
021
022import javax.servlet.ServletContext;
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
031import org.apache.hadoop.hdfs.security.token.delegation.DelegationUtilsClient;
032import org.apache.hadoop.security.UserGroupInformation;
033import org.apache.hadoop.security.token.Token;
034
035/**
036 * Cancel delegation tokens over http for use in hftp.
037 */
038@SuppressWarnings("serial")
039public class CancelDelegationTokenServlet extends DfsServlet {
040  private static final Log LOG = LogFactory.getLog(CancelDelegationTokenServlet.class);
041  @Override
042  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
043      throws ServletException, IOException {
044    final UserGroupInformation ugi;
045    final ServletContext context = getServletContext();
046    final Configuration conf = NameNodeHttpServer.getConfFromContext(context);
047    try {
048      ugi = getUGI(req, conf);
049    } catch(IOException ioe) {
050      LOG.info("Request for token received with no authentication from "
051          + req.getRemoteAddr(), ioe);
052      resp.sendError(HttpServletResponse.SC_FORBIDDEN, 
053          "Unable to identify or authenticate user");
054      return;
055    }
056    final NameNode nn = NameNodeHttpServer.getNameNodeFromContext(
057        context);
058    String tokenString = req.getParameter(DelegationUtilsClient.TOKEN);
059    if (tokenString == null) {
060      resp.sendError(HttpServletResponse.SC_MULTIPLE_CHOICES,
061                     "Token to renew not specified");
062    }
063    final Token<DelegationTokenIdentifier> token = 
064      new Token<DelegationTokenIdentifier>();
065    token.decodeFromUrlString(tokenString);
066    
067    try {
068      ugi.doAs(new PrivilegedExceptionAction<Void>() {
069        @Override
070        public Void run() throws Exception {
071          nn.getRpcServer().cancelDelegationToken(token);
072          return null;
073        }
074      });
075    } catch(Exception e) {
076      LOG.info("Exception while cancelling token. Re-throwing. ", e);
077      resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
078                     e.getMessage());
079    }
080  }
081}