knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pzamp...@apache.org
Subject knox git commit: KNOX-1157 - Scoped rewrite rules are treated as global rules in some cases (Wei Han via Phil Zampino)
Date Wed, 16 May 2018 18:47:32 GMT
Repository: knox
Updated Branches:
  refs/heads/master 925c879ae -> a1640b99e


KNOX-1157 - Scoped rewrite rules are treated as global rules in some cases (Wei Han via Phil
Zampino)


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

Branch: refs/heads/master
Commit: a1640b99e667687f370c4760e37959d99cc6a578
Parents: 925c879
Author: Wei Han <weihan@uber.com>
Authored: Thu May 10 11:10:22 2018 -0700
Committer: Phil Zampino <pzampino@apache.org>
Committed: Wed May 16 14:33:56 2018 -0400

----------------------------------------------------------------------
 .../filter/rewrite/ext/ScopedMatcher.java       | 24 ++++++++------------
 .../rewrite/api/UrlRewriteProcessorTest.java    | 20 ++++++++++++++++
 .../rewrite-with-same-rules-different-scope.xml |  8 +++++++
 3 files changed, 37 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/a1640b99/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
index 08efde4..f9bd0b1 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/ext/ScopedMatcher.java
@@ -23,6 +23,7 @@ import org.apache.knox.gateway.util.urltemplate.Template;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.HashMap;
 
 /**
  * A simple extension to the matcher that takes into account scopes for rules along with
the templates themselves.
@@ -33,12 +34,11 @@ public class ScopedMatcher extends Matcher<UrlRewriteRuleProcessorHolder>
{
 
   public static final String GLOBAL_SCOPE = "GLOBAL";
 
-  private List<Matcher<UrlRewriteRuleProcessorHolder>> matchers;
+  private HashMap<String, Matcher<UrlRewriteRuleProcessorHolder>> matchers;
 
   public ScopedMatcher() {
     super();
-    matchers = new ArrayList<>();
-    matchers.add(new Matcher<UrlRewriteRuleProcessorHolder>());
+    matchers = new HashMap<>();
   }
 
   @Override
@@ -59,7 +59,7 @@ public class ScopedMatcher extends Matcher<UrlRewriteRuleProcessorHolder>
{
 
   public Match match(Template input, String scope) {
     List<Match> matches = new ArrayList<>();
-    for (Matcher<UrlRewriteRuleProcessorHolder> matcher : matchers) {
+    for (Matcher<UrlRewriteRuleProcessorHolder> matcher : matchers.values()) {
       Match match = matcher.match(input);
       if (match != null) {
         matches.add(match);
@@ -113,17 +113,11 @@ public class ScopedMatcher extends Matcher<UrlRewriteRuleProcessorHolder>
{
    * @return a matcher
    */
   private Matcher<UrlRewriteRuleProcessorHolder> getMatcher(Template template, UrlRewriteRuleProcessorHolder
holder) {
-    for (Matcher<UrlRewriteRuleProcessorHolder> matcher : matchers) {
-      UrlRewriteRuleProcessorHolder matchersHolder = matcher.get(template);
-      if (matchersHolder == null) {
-        return matcher;
-      } else if (holder.getScope() == null && matchersHolder.getScope() == null)
{
-        return matcher;
-      }
+    String scope = holder.getScope();
+    if (!matchers.containsKey(scope)) {
+      matchers.put(scope, new Matcher<UrlRewriteRuleProcessorHolder>());
     }
-    Matcher<UrlRewriteRuleProcessorHolder> matcher = new Matcher<>();
-    matchers.add(matcher);
-    return matcher;
-  }
 
+    return matchers.get(scope);
+  }
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/a1640b99/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
index ff1093b..dd6fc53 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
@@ -184,6 +184,26 @@ public class UrlRewriteProcessorTest {
         "Expect rewrite to contain the correct path.",
         outputUrl.toString(), is( "input-mock-scheme-2://input-mock-host-2:42/test-input-path"
) );
 
+    //Test the scenario where input could match two different rules
+    inputUrl = Parser.parseLiteral( "/foo/bar" );
+
+    roles.remove(0);
+    roles.add("service-1");
+    outputUrl = processor.rewrite( environment, inputUrl, UrlRewriter.Direction.OUT, null);
+
+    assertThat(
+            "Expect rewrite to contain the correct path.",
+            outputUrl.toString(), is( "/foo/service-1" ) );
+
+    roles.remove(0);
+    roles.add("service-2");
+
+    outputUrl = processor.rewrite( environment, inputUrl, UrlRewriter.Direction.OUT, null);
+
+    assertThat(
+            "Expect rewrite to contain the correct path.",
+            outputUrl.toString(), is( "/foo/service-2" ) );
+
     processor.destroy();
   }
 

http://git-wip-us.apache.org/repos/asf/knox/blob/a1640b99/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite-with-same-rules-different-scope.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite-with-same-rules-different-scope.xml
b/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite-with-same-rules-different-scope.xml
index 6c27476..d1e76db 100644
--- a/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite-with-same-rules-different-scope.xml
+++ b/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite-with-same-rules-different-scope.xml
@@ -51,4 +51,12 @@
         <rewrite template="input-mock-scheme-2://input-mock-host-2:{port}/{path=**}" />
     </rule>
 
+    <rule name="service-1/test-rule-8" dir="OUT" pattern="/foo/{**}">
+        <rewrite template="/foo/service-1" />
+    </rule>
+
+    <rule name="service-2/test-rule-9" dir="OUT" pattern="/foo/bar">
+        <rewrite template="/foo/service-2" />
+    </rule>
+
 </rules>


Mime
View raw message