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.io.PrintStream; 021import java.security.PrivilegedExceptionAction; 022 023import javax.servlet.ServletContext; 024import javax.servlet.ServletException; 025import javax.servlet.http.HttpServletRequest; 026import javax.servlet.http.HttpServletResponse; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier; 032import org.apache.hadoop.security.UserGroupInformation; 033import org.apache.hadoop.security.token.Token; 034 035/** 036 * Renew delegation tokens over http for use in hftp. 037 */ 038@SuppressWarnings("serial") 039public class RenewDelegationTokenServlet extends DfsServlet { 040 private static final Log LOG = LogFactory.getLog(RenewDelegationTokenServlet.class); 041 public static final String PATH_SPEC = "/renewDelegationToken"; 042 public static final String TOKEN = "token"; 043 044 @Override 045 protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) 046 throws ServletException, IOException { 047 final UserGroupInformation ugi; 048 final ServletContext context = getServletContext(); 049 final Configuration conf = NameNodeHttpServer.getConfFromContext(context); 050 try { 051 ugi = getUGI(req, conf); 052 } catch(IOException ioe) { 053 LOG.info("Request for token received with no authentication from " 054 + req.getRemoteAddr(), ioe); 055 resp.sendError(HttpServletResponse.SC_FORBIDDEN, 056 "Unable to identify or authenticate user"); 057 return; 058 } 059 final NameNode nn = NameNodeHttpServer.getNameNodeFromContext(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 long result = ugi.doAs(new PrivilegedExceptionAction<Long>() { 071 public Long run() throws Exception { 072 return nn.getRpcServer().renewDelegationToken(token); 073 } 074 }); 075 PrintStream os = new PrintStream(resp.getOutputStream()); 076 os.println(result); 077 os.close(); 078 } catch(Exception e) { 079 // transfer exception over the http 080 String exceptionClass = e.getClass().getName(); 081 String exceptionMsg = e.getLocalizedMessage(); 082 String strException = exceptionClass + ";" + exceptionMsg; 083 LOG.info("Exception while renewing token. Re-throwing. s=" + strException, e); 084 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, strException); 085 } 086 } 087}