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 */
017 package org.apache.hadoop.hdfs.server.namenode;
018
019 import java.io.IOException;
020 import java.security.PrivilegedExceptionAction;
021
022 import javax.servlet.ServletContext;
023 import javax.servlet.ServletException;
024 import javax.servlet.http.HttpServletRequest;
025 import javax.servlet.http.HttpServletResponse;
026
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029 import org.apache.hadoop.conf.Configuration;
030 import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
031 import org.apache.hadoop.security.UserGroupInformation;
032 import org.apache.hadoop.security.token.Token;
033
034 /**
035 * Cancel delegation tokens over http for use in hftp.
036 */
037 @SuppressWarnings("serial")
038 public class CancelDelegationTokenServlet extends DfsServlet {
039 private static final Log LOG = LogFactory.getLog(CancelDelegationTokenServlet.class);
040 public static final String PATH_SPEC = "/cancelDelegationToken";
041 public static final String TOKEN = "token";
042
043 @Override
044 protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
045 throws ServletException, IOException {
046 final UserGroupInformation ugi;
047 final ServletContext context = getServletContext();
048 final Configuration conf = NameNodeHttpServer.getConfFromContext(context);
049 try {
050 ugi = getUGI(req, conf);
051 } catch(IOException ioe) {
052 LOG.info("Request for token received with no authentication from "
053 + req.getRemoteAddr(), ioe);
054 resp.sendError(HttpServletResponse.SC_FORBIDDEN,
055 "Unable to identify or authenticate user");
056 return;
057 }
058 final NameNode nn = NameNodeHttpServer.getNameNodeFromContext(
059 context);
060 String tokenString = req.getParameter(TOKEN);
061 if (tokenString == null) {
062 resp.sendError(HttpServletResponse.SC_MULTIPLE_CHOICES,
063 "Token to renew not specified");
064 }
065 final Token<DelegationTokenIdentifier> token =
066 new Token<DelegationTokenIdentifier>();
067 token.decodeFromUrlString(tokenString);
068
069 try {
070 ugi.doAs(new PrivilegedExceptionAction<Void>() {
071 @Override
072 public Void run() throws Exception {
073 nn.getRpcServer().cancelDelegationToken(token);
074 return null;
075 }
076 });
077 } catch(Exception e) {
078 LOG.info("Exception while cancelling token. Re-throwing. ", e);
079 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
080 e.getMessage());
081 }
082 }
083 }