KNOX-1369 - Default dispatch whitelist sometimes includes port information
authorPhil Zampino <pzampino@apache.org>
Thu, 28 Jun 2018 20:25:50 +0000 (16:25 -0400)
committerPhil Zampino <pzampino@apache.org>
Thu, 28 Jun 2018 20:25:50 +0000 (16:25 -0400)
gateway-spi/src/main/java/org/apache/knox/gateway/util/WhitelistUtils.java
gateway-spi/src/test/java/org/apache/knox/gateway/util/WhitelistUtilsTest.java

index 42e6eb2..e1f32be 100644 (file)
@@ -76,6 +76,11 @@ public class WhitelistUtils {
       int domainIndex = thisHost.indexOf('.');
       if (domainIndex > 0) {
         String domain = thisHost.substring(thisHost.indexOf('.'));
+        // Sometimes, the server name includes port details, which need to be stripped
+        int portIndex = domain.indexOf(":");
+        if (portIndex > 0) {
+          domain = domain.substring(0, portIndex);
+        }
         String domainPattern = ".+" + domain.replaceAll("\\.", "\\\\.");
         defaultWhitelist = String.format(DEFAULT_DISPATCH_WHITELIST_TEMPLATE, domainPattern);
       }
index 172979a..34a1c6c 100644 (file)
@@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest;
 import java.util.Collections;
 import java.util.List;
 
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -64,6 +65,21 @@ public class WhitelistUtilsTest {
     assertTrue(whitelist.contains("localhost"));
   }
 
+  /**
+   * KNOX-1369
+   */
+  @Test
+  public void testDomainBasedDefaultForAffectedServiceRoleWhenServerNameIncludesPort() throws Exception {
+    final String serviceRole = "TEST";
+
+    GatewayConfig config = createMockGatewayConfig(Collections.singletonList(serviceRole), null);
+
+    // Check localhost by loopback address
+    String whitelist = doTestGetDispatchWhitelist(config, "host.test.com:1234", serviceRole);
+    assertNotNull(whitelist);
+    assertTrue(whitelist.contains(".+\\.test\\.com"));
+    assertFalse(whitelist.contains(":1234"));
+  }
 
   @Test
   public void testDefaultDomainWhitelist() throws Exception {