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.ArgumentMatchers.any; 022import static org.mockito.Mockito.doReturn; 023import static org.mockito.Mockito.mock; 024import static org.mockito.Mockito.verify; 025 026import javax.servlet.FilterChain; 027import javax.servlet.FilterConfig; 028import javax.servlet.ServletResponse; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletRequestWrapper; 031import org.apache.hadoop.conf.Configuration; 032import org.apache.hadoop.fs.CommonConfigurationKeys; 033import org.apache.hadoop.hbase.HBaseClassTestRule; 034import org.apache.hadoop.hbase.http.ServerConfigurationKeys; 035import org.apache.hadoop.hbase.http.lib.StaticUserWebFilter.StaticUserFilter; 036import org.apache.hadoop.hbase.testclassification.MiscTests; 037import org.apache.hadoop.hbase.testclassification.SmallTests; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041import org.mockito.ArgumentCaptor; 042 043@Category({ MiscTests.class, SmallTests.class }) 044public class TestStaticUserWebFilter { 045 046 @ClassRule 047 public static final HBaseClassTestRule CLASS_RULE = 048 HBaseClassTestRule.forClass(TestStaticUserWebFilter.class); 049 050 private FilterConfig mockConfig(String username) { 051 FilterConfig mock = mock(FilterConfig.class); 052 doReturn(username).when(mock).getInitParameter(ServerConfigurationKeys.HBASE_HTTP_STATIC_USER); 053 return mock; 054 } 055 056 @Test 057 public void testFilter() throws Exception { 058 FilterConfig config = mockConfig("myuser"); 059 StaticUserFilter suf = new StaticUserFilter(); 060 suf.init(config); 061 062 ArgumentCaptor<HttpServletRequestWrapper> wrapperArg = 063 ArgumentCaptor.forClass(HttpServletRequestWrapper.class); 064 065 FilterChain chain = mock(FilterChain.class); 066 067 suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), chain); 068 069 verify(chain).doFilter(wrapperArg.capture(), any()); 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}