JspHelper { public static UserGroupInformation getUGI(HttpServletRequest request, Configuration conf ) throws IOException { UserGroupInformation ugi = null; if(UserGroupInformation.isSecurityEnabled()) { String user = request.getRemoteUser(); String tokenString = request.getParameter(DELEGATION_PARAMETER_NAME); if (tokenString != null) { Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>(); token.decodeFromUrlString(tokenString); InetSocketAddress serviceAddr = NameNode.getAddress(conf); LOG.info("Setting service in token: " + new Text(serviceAddr.getAddress().getHostAddress() + ":" + serviceAddr.getPort())); token.setService(new Text(serviceAddr.getAddress().getHostAddress() + ":" + serviceAddr.getPort())); ByteArrayInputStream buf = new ByteArrayInputStream(token.getIdentifier()); DataInputStream in = new DataInputStream(buf); DelegationTokenIdentifier id = new DelegationTokenIdentifier(); id.readFields(in); ugi = id.getUser(); ugi.addToken(token); ugi.setAuthenticationMethod(AuthenticationMethod.TOKEN); } else { if(user == null) { throw new IOException("Security enabled but user not " + "authenticated by filter"); } ugi = UserGroupInformation.createRemoteUser(user); ugi.setAuthenticationMethod(AuthenticationMethod.KERBEROS_SSL); } } else { String user = request.getParameter("ugi"); if(user == null) { ugi = getDefaultWebUser(conf); } else { ugi = UserGroupInformation.createRemoteUser(user.split(",")[0]); } ugi.setAuthenticationMethod(AuthenticationMethod.SIMPLE); } if(LOG.isDebugEnabled()) LOG.debug("getUGI is returning: " + ugi.getShortUserName()); return ugi; } JspHelper(); DatanodeID randomNode(); DatanodeInfo bestNode(LocatedBlock blk); void streamBlockInAscii(InetSocketAddress addr, long blockId, 
               Token<BlockTokenIdentifier> accessToken, long genStamp, 
                                 long blockSize, 
                                 long offsetIntoBlock, long chunkSizeToView, 
                                 JspWriter out,
                                 Configuration conf); void DFSNodesStatus(ArrayList<DatanodeDescriptor> live,
                             ArrayList<DatanodeDescriptor> dead); void addTableHeader(JspWriter out); void addTableHeadingRow(JspWriter out, String[] columns); void addTableRow(JspWriter out, String[] columns); void addTableFooter(JspWriter out); String getSafeModeText(); static String getWarningText(FSNamesystem fsn); String getInodeLimitText(); String getUpgradeStatusText(); void sortNodeList(ArrayList<DatanodeDescriptor> nodes,
                           String field, String order); static void printPathWithLinks(String dir, JspWriter out, 
                                        int namenodeInfoPort,
                                        String tokenString
                                       ); static void printGotoForm(JspWriter out,
                                   int namenodeInfoPort,
                                   String tokenString,
                                   String file); static void createTitle(JspWriter out, 
                                 HttpServletRequest req, 
                                 String  file); static UserGroupInformation getDefaultWebUser(Configuration conf
                                                       ); static UserGroupInformation getUGI(HttpServletRequest request,
                                            Configuration conf
                                           ); static DFSClient getDFSClient(final UserGroupInformation user,
                                       final InetSocketAddress addr,
                                       final Configuration conf
                                       ); static String getDelegationTokenUrlParam(String tokenString); static int string2ChunkSizeToView(String s, int defaultValue); static int getDefaultChunkSize(Configuration conf); static String getVersionTable(); static final String CURRENT_CONF; final static public String WEB_UGI_PROPERTY_NAME; static final String DELEGATION_PARAMETER_NAME; static InetSocketAddress nameNodeAddr; }
@Test public void testGetUgi() throws IOException { conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "hdfs: HttpServletRequest request = mock(HttpServletRequest.class); String user = "TheDoctor"; Text userText = new Text(user); DelegationTokenIdentifier dtId = new DelegationTokenIdentifier(userText, userText, null); Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>( dtId, new DummySecretManager(0, 0, 0, 0)); String tokenString = token.encodeToUrlString(); when(request.getParameter(JspHelper.DELEGATION_PARAMETER_NAME)).thenReturn( tokenString); when(request.getRemoteUser()).thenReturn(user); conf.set(DFSConfigKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos"); UserGroupInformation.setConfiguration(conf); InetSocketAddress serviceAddr = NameNode.getAddress(conf); Text tokenService = new Text(serviceAddr.getAddress().getHostAddress() + ":" + serviceAddr.getPort()); UserGroupInformation ugi = JspHelper.getUGI(request, conf); Token<? extends TokenIdentifier> tokenInUgi = ugi.getTokens().iterator() .next(); Assert.assertEquals(tokenInUgi.getService(), tokenService); }