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.jupiter.api.Assertions.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.http.ServerConfigurationKeys;
034import org.apache.hadoop.hbase.http.lib.StaticUserWebFilter.StaticUserFilter;
035import org.apache.hadoop.hbase.testclassification.MiscTests;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039import org.mockito.ArgumentCaptor;
040
041@Tag(MiscTests.TAG)
042@Tag(SmallTests.TAG)
043public class TestStaticUserWebFilter {
044
045  private FilterConfig mockConfig(String username) {
046    FilterConfig mock = mock(FilterConfig.class);
047    doReturn(username).when(mock).getInitParameter(ServerConfigurationKeys.HBASE_HTTP_STATIC_USER);
048    return mock;
049  }
050
051  @Test
052  public void testFilter() throws Exception {
053    FilterConfig config = mockConfig("myuser");
054    StaticUserFilter suf = new StaticUserFilter();
055    suf.init(config);
056
057    ArgumentCaptor<HttpServletRequestWrapper> wrapperArg =
058      ArgumentCaptor.forClass(HttpServletRequestWrapper.class);
059
060    FilterChain chain = mock(FilterChain.class);
061
062    suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), chain);
063
064    verify(chain).doFilter(wrapperArg.capture(), any());
065
066    HttpServletRequestWrapper wrapper = wrapperArg.getValue();
067    assertEquals("myuser", wrapper.getUserPrincipal().getName());
068    assertEquals("myuser", wrapper.getRemoteUser());
069
070    suf.destroy();
071  }
072
073  @Test
074  public void testOldStyleConfiguration() {
075    Configuration conf = new Configuration();
076    conf.set("dfs.web.ugi", "joe,group1,group2");
077    assertEquals("joe", StaticUserWebFilter.getUsernameFromConf(conf));
078  }
079
080  @Test
081  public void testConfiguration() {
082    Configuration conf = new Configuration();
083    conf.set(CommonConfigurationKeys.HADOOP_HTTP_STATIC_USER, "dr.stack");
084    assertEquals("dr.stack", StaticUserWebFilter.getUsernameFromConf(conf));
085  }
086
087}