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 */ 018package org.apache.hadoop.hbase.http.lib; 019 020import static org.junit.Assert.assertEquals; 021import static org.mockito.Mockito.mock; 022 023import javax.servlet.FilterChain; 024import javax.servlet.FilterConfig; 025import javax.servlet.ServletResponse; 026import javax.servlet.http.HttpServletRequest; 027import javax.servlet.http.HttpServletRequestWrapper; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.fs.CommonConfigurationKeys; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.http.ServerConfigurationKeys; 032import org.apache.hadoop.hbase.http.lib.StaticUserWebFilter.StaticUserFilter; 033import org.apache.hadoop.hbase.testclassification.MiscTests; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.mockito.ArgumentCaptor; 039import org.mockito.Mockito; 040 041@Category({MiscTests.class, SmallTests.class}) 042public class TestStaticUserWebFilter { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestStaticUserWebFilter.class); 047 048 private FilterConfig mockConfig(String username) { 049 FilterConfig mock = Mockito.mock(FilterConfig.class); 050 Mockito.doReturn(username).when(mock).getInitParameter( 051 ServerConfigurationKeys.HBASE_HTTP_STATIC_USER); 052 return mock; 053 } 054 055 @Test 056 public void testFilter() throws Exception { 057 FilterConfig config = mockConfig("myuser"); 058 StaticUserFilter suf = new StaticUserFilter(); 059 suf.init(config); 060 061 ArgumentCaptor<HttpServletRequestWrapper> wrapperArg = 062 ArgumentCaptor.forClass(HttpServletRequestWrapper.class); 063 064 FilterChain chain = mock(FilterChain.class); 065 066 suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), 067 chain); 068 069 Mockito.verify(chain).doFilter(wrapperArg.capture(), Mockito.<ServletResponse>anyObject()); 070 071 HttpServletRequestWrapper wrapper = wrapperArg.getValue(); 072 assertEquals("myuser", wrapper.getUserPrincipal().getName()); 073 assertEquals("myuser", wrapper.getRemoteUser()); 074 075 suf.destroy(); 076 } 077 078 @Test 079 public void testOldStyleConfiguration() { 080 Configuration conf = new Configuration(); 081 conf.set("dfs.web.ugi", "joe,group1,group2"); 082 assertEquals("joe", StaticUserWebFilter.getUsernameFromConf(conf)); 083 } 084 085 @Test 086 public void testConfiguration() { 087 Configuration conf = new Configuration(); 088 conf.set(CommonConfigurationKeys.HADOOP_HTTP_STATIC_USER, "dr.stack"); 089 assertEquals("dr.stack", StaticUserWebFilter.getUsernameFromConf(conf)); 090 } 091 092}