From 51a4faf52199b181471d591a9fde900fc166fc48 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. --- 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 f6f5c4bde2..1a5ea07914 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -178,6 +178,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 a9fb3e875f..d71175f96d 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 @@ private void initializeAllowedOrigins(FilterConfig filterConfig) { } 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 @@ static boolean isCrossOrigin(String origin) { 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 @@ private boolean areHeadersAllowed(String accessControlRequestHeaders) { 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 a29e4a0aa6..f666c21b1e 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.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 void testSameOrigin() throws ServletException, IOException { 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 {