SLIDER-36 delete custom IP filter and tests

Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/dcae064c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/dcae064c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/dcae064c

Branch: refs/heads/feature/SLIDER-149_Support_a_YARN_service_registry
Commit: dcae064caa2dc636560821adcc222f038c17270e
Parents: 265c478
Author: Steve Loughran <ste...@apache.org>
Authored: Thu Aug 28 15:49:40 2014 +0100
Committer: Steve Loughran <ste...@apache.org>
Committed: Thu Aug 28 15:49:40 2014 +0100

----------------------------------------------------------------------
 .../web/SliderAmFilterInitializer.java          |  72 -------
 .../server/appmaster/web/SliderAmIpFilter.java  | 161 ---------------
 .../appmaster/web/TestSliderAmFilter.java       | 201 -------------------
 3 files changed, 434 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/dcae064c/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmFilterInitializer.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmFilterInitializer.java
 
b/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmFilterInitializer.java
deleted file mode 100644
index df6cea4..0000000
--- 
a/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmFilterInitializer.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements.  See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership.  The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* "License"); you may not use this file except in compliance
-* with the License.  You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-package org.apache.slider.server.appmaster.web;
-
-import com.google.common.annotations.VisibleForTesting;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.http.FilterContainer;
-import org.apache.hadoop.http.FilterInitializer;
-import org.apache.hadoop.http.HttpConfig;
-import org.apache.hadoop.yarn.api.ApplicationConstants;
-import org.apache.hadoop.yarn.conf.YarnConfiguration;
-import org.apache.hadoop.yarn.webapp.util.WebAppUtils;
-
-import java.util.HashMap;
-import java.util.Map;
-
-@Deprecated
-public class SliderAmFilterInitializer extends FilterInitializer {
-  private static final String FILTER_NAME = "AM_PROXY_FILTER";
-  private static final String FILTER_CLASS = 
SliderAmIpFilter.class.getCanonicalName();
-  private static final String HTTPS_PREFIX = "https://";;
-  private static final String HTTP_PREFIX = "http://";;
-  private Configuration configuration;
-
-  public static final String NAME =
-    "org.apache.slider.server.appmaster.web.SliderAmFilterInitializer";
-
-  @Override
-  public void initFilter(FilterContainer container, Configuration conf) {
-    configuration = conf;
-    Map<String, String> params = new HashMap<String, String>();
-    String proxy = WebAppUtils.getProxyHostAndPort(conf);
-    String[] parts = proxy.split(":");
-    params.put(SliderAmIpFilter.PROXY_HOST, parts[0]);
-    // todo:  eventually call WebAppUtils.getHttpSchemePrefix
-    params.put(SliderAmIpFilter.PROXY_URI_BASE, getHttpSchemePrefix()
-        + proxy + getApplicationWebProxyBase());
-    params.put(SliderAmIpFilter.WS_CONTEXT_ROOT,
-               conf.get(SliderAmIpFilter.WS_CONTEXT_ROOT));
-    container.addFilter(FILTER_NAME, FILTER_CLASS, params);
-  }
-
-  @VisibleForTesting
-  protected String getApplicationWebProxyBase() {
-    return System.getenv(ApplicationConstants.APPLICATION_WEB_PROXY_BASE_ENV);
-  }
-
-  private String getHttpSchemePrefix() {
-    return HttpConfig.Policy.HTTPS_ONLY ==
-           HttpConfig.Policy.fromString(configuration
-                                          .get(
-                                            
YarnConfiguration.YARN_HTTP_POLICY_KEY,
-                                            
YarnConfiguration.YARN_HTTP_POLICY_DEFAULT))
-           ? HTTPS_PREFIX : HTTP_PREFIX;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/dcae064c/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmIpFilter.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmIpFilter.java
 
b/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmIpFilter.java
deleted file mode 100644
index 4ced0b1..0000000
--- 
a/slider-core/src/main/java/org/apache/slider/server/appmaster/web/SliderAmIpFilter.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/**
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements.  See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership.  The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* "License"); you may not use this file except in compliance
-* with the License.  You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-package org.apache.slider.server.appmaster.web;
-
-import org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet;
-import org.apache.hadoop.yarn.server.webproxy.amfilter.AmIpPrincipal;
-import 
org.apache.hadoop.yarn.server.webproxy.amfilter.AmIpServletRequestWrapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-/**
- * IP-based filter derived from
- * {@link org.apache.hadoop.yarn.server.webproxy.amfilter.AmFilterInitializer};
- * with the ability to open up a section for unauthed access from everywhere.
- */
-@Deprecated
-public class SliderAmIpFilter implements Filter {
-  protected static final Logger log =
-      LoggerFactory.getLogger(SliderAmIpFilter.class);
-  
-  public static final String PROXY_HOST = "PROXY_HOST";
-  public static final String PROXY_URI_BASE = "PROXY_URI_BASE";
-  //update the proxy IP list about every 5 min
-  private static final long updateInterval = 5 * 60 * 1000;
-  public static final String WS_CONTEXT_ROOT = "slider.rest.context.root";
-
-  @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized")
-  private String proxyHost;
-  private Set<String> proxyAddresses = null;
-  private long lastUpdate;
-  private String proxyUriBase;
-  private List<String> wsContextRoots;
-  
-  @Override
-  public void init(FilterConfig conf) throws ServletException {
-    proxyHost = conf.getInitParameter(PROXY_HOST);
-    proxyUriBase = conf.getInitParameter(PROXY_URI_BASE);
-    wsContextRoots = 
Arrays.asList(conf.getInitParameter(WS_CONTEXT_ROOT).split("\\|"));
-  }
-  
-  protected Set<String> getProxyAddresses() throws ServletException {
-    long now = System.currentTimeMillis();
-    synchronized(this) {
-      if(proxyAddresses == null || (lastUpdate + updateInterval) >= now) {
-        try {
-          proxyAddresses = new HashSet<String>();
-          for(InetAddress add : InetAddress.getAllByName(proxyHost)) {
-            if (log.isDebugEnabled()) {
-              log.debug("proxy address is: " + add.getHostAddress());
-            }
-            proxyAddresses.add(add.getHostAddress());
-          }
-          lastUpdate = now;
-        } catch (UnknownHostException e) {
-          throw new ServletException("Could not locate "+proxyHost, e);
-        }
-      }
-      return proxyAddresses;
-    }
-  }
-
-  @Override
-  public void destroy() {
-    //Empty
-  }
-
-  @Override
-  public void doFilter(ServletRequest req, ServletResponse resp,
-      FilterChain chain) throws IOException, ServletException {
-    if(!(req instanceof HttpServletRequest)) {
-      throw new ServletException("This filter only works for HTTP/HTTPS");
-    }
-    
-    HttpServletRequest httpReq = (HttpServletRequest)req;
-    HttpServletResponse httpResp = (HttpServletResponse)resp;
-    if (log.isDebugEnabled()) {
-      log.debug("Remote address for request is: " + httpReq.getRemoteAddr());
-    }
-    String requestURI = httpReq.getRequestURI();
-      if(!isWsRequest(requestURI) &&
-       !getProxyAddresses().contains(httpReq.getRemoteAddr())) {
-      String redirectUrl = httpResp.encodeRedirectURL(proxyUriBase +
-                                                      requestURI);
-      httpResp.sendRedirect(redirectUrl);
-      return;
-    }
-    
-    String user = null;
-    
-    if (httpReq.getCookies() != null) {
-      for(Cookie c: httpReq.getCookies()) {
-        if(WebAppProxyServlet.PROXY_USER_COOKIE_NAME.equals(c.getName())){
-          user = c.getValue();
-          break;
-        }
-      }
-    }
-    try {
-      if (user == null) {
-        log.debug("Could not find " + WebAppProxyServlet.PROXY_USER_COOKIE_NAME
-                 + " cookie, so user will not be set");
-        chain.doFilter(req, resp);
-      } else {
-        final AmIpPrincipal principal = new AmIpPrincipal(user);
-        ServletRequest requestWrapper = new AmIpServletRequestWrapper(httpReq,
-            principal);
-        chain.doFilter(requestWrapper, resp);
-      }
-// JKD7    } catch (IOException | ServletException e) {
-    } catch (IOException e) {
-      log.warn("When fetching {}: {}", requestURI, e);
-      throw e;
-    } catch (ServletException e) {
-      log.warn("When fetching {}: {}", requestURI, e);
-      throw e;
-    }
-  }
-
-  private boolean isWsRequest(String requestURI) {
-    boolean isWsReq = false;
-    for (String wsContext : wsContextRoots) {
-      isWsReq = requestURI.startsWith(wsContext);
-      if (isWsReq) break;
-    }
-
-    return isWsReq;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/dcae064c/slider-core/src/test/java/org/apache/slider/server/appmaster/web/TestSliderAmFilter.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/test/java/org/apache/slider/server/appmaster/web/TestSliderAmFilter.java
 
b/slider-core/src/test/java/org/apache/slider/server/appmaster/web/TestSliderAmFilter.java
deleted file mode 100644
index 4ce41a4..0000000
--- 
a/slider-core/src/test/java/org/apache/slider/server/appmaster/web/TestSliderAmFilter.java
+++ /dev/null
@@ -1,201 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.slider.server.appmaster.web;
-
-import org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet;
-import org.apache.hadoop.yarn.server.webproxy.amfilter.AmIpFilter;
-import 
org.apache.hadoop.yarn.server.webproxy.amfilter.AmIpServletRequestWrapper;
-import org.junit.Test;
-import org.mockito.Mockito;
-
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.ServletResponseWrapper;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpServletResponseWrapper;
-import java.io.IOException;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
-
-/**
- * Test AmIpFilter. Requests to a no declared hosts should has way through
- * proxy. Another requests can be filtered with (without) user name.
- * 
- */
-@Deprecated
-public class TestSliderAmFilter {
-
-  private String proxyHost = "localhost";
-  private String proxyUri = "http://bogus";;
-  private String doFilterRequest;
-  private AmIpServletRequestWrapper servletWrapper;
-  private String wsContextRoot = "/ws";
-
-  private class TestAmIpFilter extends AmIpFilter {
-
-    private Set<String> proxyAddresses = null;
-
-    protected Set<String> getProxyAddresses() {
-      if (proxyAddresses == null) {
-        proxyAddresses = new HashSet<String>();
-      }
-      proxyAddresses.add(proxyHost);
-      return proxyAddresses;
-    }
-  }
-
-  private static class DummyFilterConfig implements FilterConfig {
-    final Map<String, String> map;
-
-    DummyFilterConfig(Map<String, String> map) {
-      this.map = map;
-    }
-
-    @Override
-    public String getFilterName() {
-      return "dummy";
-    }
-
-    @Override
-    public String getInitParameter(String arg0) {
-      return map.get(arg0);
-    }
-
-    @Override
-    public Enumeration<String> getInitParameterNames() {
-      return Collections.enumeration(map.keySet());
-    }
-
-    @Override
-    public ServletContext getServletContext() {
-      return null;
-    }
-  }
-
-  /**
-   * Test AmIpFilter
-   */
-  @Test(timeout = 1000)
-  public void testFilter() throws Exception {
-    Map<String, String> params = new HashMap<String, String>();
-    params.put(SliderAmIpFilter.PROXY_HOST, proxyHost);
-    params.put(SliderAmIpFilter.PROXY_URI_BASE, proxyUri);
-    params.put(SliderAmIpFilter.WS_CONTEXT_ROOT, wsContextRoot);
-    FilterConfig config = new DummyFilterConfig(params);
-
-    // dummy filter
-    FilterChain chain = new FilterChain() {
-      @Override
-      public void doFilter(ServletRequest servletRequest,
-          ServletResponse servletResponse) throws IOException, 
ServletException {
-        doFilterRequest = servletRequest.getClass().getName();
-        if (servletRequest instanceof AmIpServletRequestWrapper) {
-          servletWrapper = (AmIpServletRequestWrapper) servletRequest;
-
-        }
-      }
-    };
-    SliderAmIpFilter testFilter = new SliderAmIpFilter();
-    testFilter.init(config);
-
-    HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
-    HttpServletResponseForTest response =
-        new HttpServletResponseForTest(mockResponse);
-    // Test request should implements HttpServletRequest
-
-    ServletRequest failRequest = Mockito.mock(ServletRequest.class);
-    try {
-      testFilter.doFilter(failRequest, response, chain);
-      fail();
-    } catch (ServletException e) {
-      assertEquals("This filter only works for HTTP/HTTPS", e.getMessage());
-    }
-
-    // request with HttpServletRequest
-    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
-    Mockito.when(request.getRemoteAddr()).thenReturn("redirect");
-    Mockito.when(request.getRequestURI()).thenReturn("/ws/v1/slider/agent");
-    testFilter.doFilter(request, response, chain);
-    // address "redirect" is not in host list
-    assertEquals(0, response.getRedirect().length());
-    Mockito.when(request.getRequestURI()).thenReturn("/redirect");
-    testFilter.doFilter(request, response, chain);
-    assertEquals("http://bogus/redirect";, response.getRedirect());
-    // "127.0.0.1" contains in host list. Without cookie
-    Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1");
-    testFilter.doFilter(request, response, chain);
-
-    assertTrue(doFilterRequest
-        .contains("javax.servlet.http.HttpServletRequest"));
-    // cookie added
-    Cookie[] cookies = new Cookie[1];
-    cookies[0] = new Cookie(WebAppProxyServlet.PROXY_USER_COOKIE_NAME, "user");
-
-    Mockito.when(request.getCookies()).thenReturn(cookies);
-    testFilter.doFilter(request, response, chain);
-
-    assertEquals(
-        
"org.apache.hadoop.yarn.server.webproxy.amfilter.AmIpServletRequestWrapper",
-        doFilterRequest);
-    // request contains principal from cookie
-    assertEquals("user", servletWrapper.getUserPrincipal().getName());
-    assertEquals("user", servletWrapper.getRemoteUser());
-    assertFalse(servletWrapper.isUserInRole(""));
-
-  }
-
-  private class HttpServletResponseForTest extends HttpServletResponseWrapper {
-    String redirectLocation = "";
-
-    public HttpServletResponseForTest(HttpServletResponse response) {
-      super(response);
-    }
-
-    public String getRedirect() {
-      return redirectLocation;
-    }
-
-    @Override
-    public void sendRedirect(String location) throws IOException {
-      redirectLocation = location;
-    }
-
-    @Override
-    public String encodeRedirectURL(String url) {
-      return url;
-    }
-
-  }
-
-}

Reply via email to