From 1e40cbba7f0ac402328819d3fb8f09e11595117f Mon Sep 17 00:00:00 2001 From: Zhijie Shen Date: Thu, 4 Sep 2014 15:01:01 -0700 Subject: [PATCH] YARN-2511. Allowed all origins by default when CrossOriginFilter is enabled. Contributed by Jonathan Eagles. (cherry picked from commit 51a4faf52199b181471d591a9fde900fc166fc48) --- hadoop-yarn-project/CHANGES.txt | 3 +++ .../server/timeline/webapp/CrossOriginFilter.java | 10 +++++++--- .../timeline/webapp/TestCrossOriginFilter.java | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 7f7f367f118..b7e092a85c2 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -155,6 +155,9 @@ Release 2.6.0 - UNRELEASED YARN-2509. Enable Cross Origin Filter for timeline server only and not all Yarn servers (Mit Desai via jeagles) + YARN-2511. Allowed all origins by default when CrossOriginFilter is + enabled. (Jonathan Eagles via zjshen) + OPTIMIZATIONS BUG FIXES diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/timeline/webapp/CrossOriginFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/timeline/webapp/CrossOriginFilter.java index a9fb3e875f5..d71175f96dc 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/timeline/webapp/CrossOriginFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/timeline/webapp/CrossOriginFilter.java @@ -76,6 +76,7 @@ public class CrossOriginFilter implements Filter { private List allowedMethods = new ArrayList(); private List allowedHeaders = new ArrayList(); private List allowedOrigins = new ArrayList(); + private boolean allowAllOrigins = true; private String maxAge; @Override @@ -171,7 +172,9 @@ public class CrossOriginFilter implements Filter { } allowedOrigins = Arrays.asList(allowedOriginsConfig.trim().split("\\s*,\\s*")); + allowAllOrigins = allowedOrigins.contains("*"); LOG.info("Allowed Origins: " + StringUtils.join(allowedOrigins, ',')); + LOG.info("Allow All Origins: " + allowAllOrigins); } private void initializeMaxAge(FilterConfig filterConfig) { @@ -199,8 +202,9 @@ public class CrossOriginFilter implements Filter { return origin != null; } - private boolean isOriginAllowed(String origin) { - return allowedOrigins.contains(origin); + @VisibleForTesting + boolean isOriginAllowed(String origin) { + return allowAllOrigins || allowedOrigins.contains(origin); } private boolean areHeadersAllowed(String accessControlRequestHeaders) { @@ -213,7 +217,7 @@ public class CrossOriginFilter implements Filter { private boolean isMethodAllowed(String accessControlRequestMethod) { if (accessControlRequestMethod == null) { - return false; + return true; } return allowedMethods.contains(accessControlRequestMethod); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestCrossOriginFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestCrossOriginFilter.java index a29e4a0aa6d..f666c21b1ef 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestCrossOriginFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/timeline/webapp/TestCrossOriginFilter.java @@ -31,6 +31,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.junit.Assert; import org.junit.Test; import static org.mockito.Mockito.when; @@ -65,6 +66,20 @@ public class TestCrossOriginFilter { verify(mockChain).doFilter(mockReq, mockRes); } + @Test + public void testAllowAllOrigins() throws ServletException, IOException { + + // Setup the configuration settings of the server + Map conf = new HashMap(); + conf.put(CrossOriginFilter.ALLOWED_ORIGINS, "*"); + FilterConfig filterConfig = new FilterConfigTest(conf); + + // Object under test + CrossOriginFilter filter = new CrossOriginFilter(); + filter.init(filterConfig); + Assert.assertTrue(filter.isOriginAllowed("example.org")); + } + @Test public void testDisallowedOrigin() throws ServletException, IOException {