[java-identity-provider] 02/04: IDP-1429 Regexp Filters take a Pattern, not a string

Rod Widdowson rdw at steadingsoftware.com
Thu Apr 11 11:19:39 EDT 2019


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=cdc183f09612a9fe1260c8c200d25029167a8dbb

commit cdc183f09612a9fe1260c8c200d25029167a8dbb
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Apr 11 15:19:56 2019 +0100

    IDP-1429 Regexp Filters take a Pattern, not a string
    
    https://issues.shibboleth.net/jira/browse/IDP-1429
    
    We leave setRegex in place for now to allow the Spring code to work
---
 .../matcher/impl/AbstractRegexpStringMatcher.java  | 13 +++++++++++
 .../policyrule/impl/AbstractRegexpPolicyRule.java  | 15 +++++++++++++
 .../impl/AbstractRegexpStringMatcherTest.java      |  6 +++--
 .../impl/AttributeScopeRegexpMatcherTest.java      |  4 +++-
 .../impl/AttributeValueRegexpMatcherTest.java      | 11 ++++++++-
 .../attribute/filter/matcher/impl/DataSources.java |  2 ++
 .../impl/AttributeIssuerRegexpPolicyRuleTest.java  | 26 +++++++++++++++++-----
 .../AttributeRequesterRegexpPolicyRuleTest.java    |  4 +++-
 .../impl/PrincipalNameRegexpPolicyRuleTest.java    |  4 +++-
 .../impl/ProxiedRequesterRegexpPolicyRuleTest.java |  3 ++-
 10 files changed, 75 insertions(+), 13 deletions(-)

diff --git a/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcher.java b/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcher.java
index 616a963..efc9ad6 100644
--- a/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcher.java
+++ b/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcher.java
@@ -19,11 +19,13 @@ package net.shibboleth.idp.attribute.filter.matcher.impl;
 
 import java.util.regex.Pattern;
 
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /**
  * General {@link net.shibboleth.idp.attribute.filter.Matcher} for regexp comparison of strings in Attribute
@@ -47,11 +49,22 @@ public abstract class AbstractRegexpStringMatcher extends AbstractMatcher {
      * Sets the regular expression to match.
      * 
      * @param expression regular expression to match
+     * @deprecated
      */
     public void setRegularExpression(final String expression) {
         ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
         regex = Pattern.compile(expression);
     }
+    
+    /**
+     * Sets the {@link Pattern} for matching to match.
+     * 
+     * @param pattern the pattern to match
+     */
+    public void setPattern(@Nonnull final Pattern pattern) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        regex = Constraint.isNotNull(pattern, "Pattern supplied to setPattern but not be null");
+    }
 
     /**
      * Matches the given value against the provided regular expression.
diff --git a/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/policyrule/impl/AbstractRegexpPolicyRule.java b/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/policyrule/impl/AbstractRegexpPolicyRule.java
index c8e2087..f4ba66a 100644
--- a/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/policyrule/impl/AbstractRegexpPolicyRule.java
+++ b/idp-attribute-filter-impl/src/main/java/net/shibboleth/idp/attribute/filter/policyrule/impl/AbstractRegexpPolicyRule.java
@@ -19,11 +19,13 @@ package net.shibboleth.idp.attribute.filter.policyrule.impl;
 
 import java.util.regex.Pattern;
 
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /**
  * General {@link net.shibboleth.idp.attribute.filter.Matcher} for regexp comparison of strings in Attribute Filters.
@@ -46,11 +48,24 @@ public abstract class AbstractRegexpPolicyRule extends AbstractPolicyRule {
      * Sets the regular expression to match.
      * 
      * @param expression regular expression to match
+     * @deprecated
      */
     public void setRegularExpression(final String expression) {
         ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
         regex = Pattern.compile(expression);
     }
+    
+    /**
+     * Sets the {@link Pattern} for matching to match.
+     * 
+     * @param pattern the pattern to match
+     */
+    public void setPattern(@Nonnull final Pattern pattern) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        regex = Constraint.isNotNull(pattern, "Pattern supplied to setPattern but not be null");
+    }
+
+
 
     /**
      * Matches the given value against the provided regular expression.
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcherTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcherTest.java
index 5671457..6fb9685 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcherTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AbstractRegexpStringMatcherTest.java
@@ -21,6 +21,8 @@ import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
+import java.util.regex.Pattern;
+
 import org.testng.annotations.Test;
 
 import net.shibboleth.idp.attribute.IdPAttributeValue;
@@ -37,7 +39,7 @@ public class AbstractRegexpStringMatcherTest {
             protected boolean compareAttributeValue(IdPAttributeValue value) {
                 return false;
             }};
-        predicate.setRegularExpression(DataSources.TEST_REGEX);
+        predicate.setPattern(Pattern.compile(DataSources.TEST_REGEX));
         predicate.setId("od");
         predicate.initialize();
 
@@ -51,7 +53,7 @@ public class AbstractRegexpStringMatcherTest {
             protected boolean compareAttributeValue(IdPAttributeValue value) {
                 return false;
             }};
-        predicate.setRegularExpression("^p.*");
+        predicate.setPattern(Pattern.compile("^p.*"));
         predicate.setId("od");
         predicate.initialize();
         assertFalse(predicate.regexpCompare(DataSources.TEST_STRING));
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeScopeRegexpMatcherTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeScopeRegexpMatcherTest.java
index d38530d..8bdbc2b 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeScopeRegexpMatcherTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeScopeRegexpMatcherTest.java
@@ -20,6 +20,8 @@ package net.shibboleth.idp.attribute.filter.matcher.impl;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
+import java.util.regex.Pattern;
+
 import org.testng.annotations.Test;
 
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
@@ -31,7 +33,7 @@ public class AttributeScopeRegexpMatcherTest {
     
     @Test public void testApply() throws ComponentInitializationException {
         AttributeScopeRegexpMatcher matcher = new AttributeScopeRegexpMatcher();
-        matcher.setRegularExpression(DataSources.TEST_REGEX);
+        matcher.setPattern(Pattern.compile(DataSources.TEST_REGEX));
         matcher.setId("TestId");
         matcher.initialize();
         
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeValueRegexpMatcherTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeValueRegexpMatcherTest.java
index 0e58bfa..f0c309f 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeValueRegexpMatcherTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/AttributeValueRegexpMatcherTest.java
@@ -20,6 +20,8 @@ package net.shibboleth.idp.attribute.filter.matcher.impl;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
+import java.util.regex.Pattern;
+
 import org.testng.annotations.Test;
 
 import net.shibboleth.idp.attribute.EmptyAttributeValue;
@@ -32,11 +34,12 @@ public class AttributeValueRegexpMatcherTest {
     
     @Test public void testApply() throws ComponentInitializationException {
         AttributeValueRegexpMatcher matcher = new AttributeValueRegexpMatcher();
-        matcher.setRegularExpression(DataSources.TEST_REGEX);
+        matcher.setPattern(Pattern.compile(DataSources.TEST_REGEX));
         matcher.setId("Test");
         matcher.initialize();
         
         assertTrue(matcher.compareAttributeValue(DataSources.STRING_VALUE));
+        assertFalse(matcher.compareAttributeValue(DataSources.STRING_VALUE_UPPER));
         assertTrue(matcher.compareAttributeValue(DataSources.SCOPED_VALUE_VALUE_MATCH));
         assertFalse(matcher.compareAttributeValue(DataSources.SCOPED_VALUE_SCOPE_MATCH));
         assertFalse(matcher.compareAttributeValue(DataSources.BYTE_ATTRIBUTE_VALUE));
@@ -45,6 +48,12 @@ public class AttributeValueRegexpMatcherTest {
         assertFalse(matcher.compareAttributeValue(null));
         assertTrue(matcher.compareAttributeValue(DataSources.OTHER_VALUE));
         
+        matcher = new AttributeValueRegexpMatcher();
+        matcher.setPattern(Pattern.compile(DataSources.TEST_REGEX, Pattern.CASE_INSENSITIVE));
+        matcher.setId("Test");
+        matcher.initialize();
+        assertTrue(matcher.compareAttributeValue(DataSources.STRING_VALUE));
+        assertTrue(matcher.compareAttributeValue(DataSources.STRING_VALUE_UPPER));
     }
 
 }
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/DataSources.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/DataSources.java
index 80bd765..58d25e3 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/DataSources.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/matcher/impl/DataSources.java
@@ -43,6 +43,8 @@ public abstract class DataSources {
     public final static String TEST_REGEX = "^n.*g";
 
     public final static StringAttributeValue STRING_VALUE = new StringAttributeValue(TEST_STRING);
+    
+    public final static StringAttributeValue STRING_VALUE_UPPER = new StringAttributeValue(TEST_STRING.toUpperCase());
 
     public final static StringAttributeValue NON_MATCH_STRING_VALUE = new StringAttributeValue(NON_MATCH_STRING);
 
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeIssuerRegexpPolicyRuleTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeIssuerRegexpPolicyRuleTest.java
index 7229119..d12a903 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeIssuerRegexpPolicyRuleTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeIssuerRegexpPolicyRuleTest.java
@@ -20,6 +20,8 @@ package net.shibboleth.idp.attribute.filter.policyrule.filtercontext.impl;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 
+import java.util.regex.Pattern;
+
 import org.testng.annotations.Test;
 
 import net.shibboleth.idp.attribute.filter.PolicyRequirementRule.Tristate;
@@ -32,9 +34,15 @@ import net.shibboleth.utilities.java.support.component.UninitializedComponentExc
  */
 public class AttributeIssuerRegexpPolicyRuleTest {
 
-    private AttributeIssuerRegexpPolicyRule getMatcher() throws ComponentInitializationException {
-        AttributeIssuerRegexpPolicyRule matcher = new AttributeIssuerRegexpPolicyRule();
-        matcher.setRegularExpression("^issu.*");
+    private AttributeIssuerRegexpPolicyRule getMatcher(boolean caseSensitive) throws ComponentInitializationException {
+        final AttributeIssuerRegexpPolicyRule matcher = new AttributeIssuerRegexpPolicyRule();
+        final int flags;
+        if (caseSensitive) {
+            flags = 0;
+        } else {
+            flags = Pattern.CASE_INSENSITIVE;
+        }
+        matcher.setPattern(Pattern.compile("^issu.*", flags));
         matcher.setId("Test");
         matcher.initialize();
         return matcher;
@@ -48,21 +56,27 @@ public class AttributeIssuerRegexpPolicyRuleTest {
         } catch (UninitializedComponentException ex) {
             // OK
         }
-        AttributeIssuerRegexpPolicyRule matcher = getMatcher();
+        AttributeIssuerRegexpPolicyRule matcher = getMatcher(true);
 
         assertEquals(matcher.matches(DataSources.populatedFilterContext(null, "wibble", null)), Tristate.FALSE);
         assertEquals(matcher.matches(DataSources.populatedFilterContext(null, "ISSUER", null)), Tristate.FALSE);
         assertEquals(matcher.matches(DataSources.populatedFilterContext(null, "issuer", null)), Tristate.TRUE);
+        
+        matcher = getMatcher(false);
+
+        assertEquals(matcher.matches(DataSources.populatedFilterContext(null, "wibble", null)), Tristate.FALSE);
+        assertEquals(matcher.matches(DataSources.populatedFilterContext(null, "ISSUER", null)), Tristate.TRUE);
+        assertEquals(matcher.matches(DataSources.populatedFilterContext(null, "issuer", null)), Tristate.TRUE);
     }
 
     @Test public void testUnpopulated()
             throws ComponentInitializationException {
-        assertEquals(getMatcher().matches(DataSources.unPopulatedFilterContext()), Tristate.FAIL);;
+        assertEquals(getMatcher(true).matches(DataSources.unPopulatedFilterContext()), Tristate.FAIL);;
     }
 
     @Test  public void testNoIssuer()
             throws ComponentInitializationException{
-        assertEquals(getMatcher().matches(DataSources.populatedFilterContext(null, null, null)), Tristate.FAIL);;
+        assertEquals(getMatcher(false).matches(DataSources.populatedFilterContext(null, null, null)), Tristate.FAIL);;
     }
 
 }
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeRequesterRegexpPolicyRuleTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeRequesterRegexpPolicyRuleTest.java
index 70333ed..ea94787 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeRequesterRegexpPolicyRuleTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/AttributeRequesterRegexpPolicyRuleTest.java
@@ -20,6 +20,8 @@ package net.shibboleth.idp.attribute.filter.policyrule.filtercontext.impl;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 
+import java.util.regex.Pattern;
+
 import org.testng.annotations.Test;
 
 import net.shibboleth.idp.attribute.filter.PolicyRequirementRule.Tristate;
@@ -34,7 +36,7 @@ public class AttributeRequesterRegexpPolicyRuleTest {
     
     private AttributeRequesterRegexpPolicyRule getMatcher() throws ComponentInitializationException {
         AttributeRequesterRegexpPolicyRule matcher = new AttributeRequesterRegexpPolicyRule();
-        matcher.setRegularExpression("^requ.*");
+        matcher.setPattern(Pattern.compile("^requ.*"));
         matcher.setId("Test");
         matcher.initialize();
         return matcher;
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/PrincipalNameRegexpPolicyRuleTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/PrincipalNameRegexpPolicyRuleTest.java
index d5cf2e9..cefb6d6 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/PrincipalNameRegexpPolicyRuleTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/PrincipalNameRegexpPolicyRuleTest.java
@@ -20,6 +20,8 @@ package net.shibboleth.idp.attribute.filter.policyrule.filtercontext.impl;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 
+import java.util.regex.Pattern;
+
 import org.testng.annotations.Test;
 
 import net.shibboleth.idp.attribute.filter.PolicyRequirementRule.Tristate;
@@ -34,7 +36,7 @@ public class PrincipalNameRegexpPolicyRuleTest {
     
     private PrincipalNameRegexpPolicyRule getMatcher() throws ComponentInitializationException {
         final PrincipalNameRegexpPolicyRule matcher = new PrincipalNameRegexpPolicyRule();
-        matcher.setRegularExpression("^p.*");
+        matcher.setPattern(Pattern.compile("^p.*"));
         matcher.setId("Test");
         matcher.initialize();
         return matcher;
diff --git a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/ProxiedRequesterRegexpPolicyRuleTest.java b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/ProxiedRequesterRegexpPolicyRuleTest.java
index 375f90f..1a4e956 100644
--- a/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/ProxiedRequesterRegexpPolicyRuleTest.java
+++ b/idp-attribute-filter-impl/src/test/java/net/shibboleth/idp/attribute/filter/policyrule/filtercontext/impl/ProxiedRequesterRegexpPolicyRuleTest.java
@@ -19,6 +19,7 @@ package net.shibboleth.idp.attribute.filter.policyrule.filtercontext.impl;import
 import static org.testng.Assert.fail;
 
 import java.util.Arrays;
+import java.util.regex.Pattern;
 
 import org.opensaml.messaging.context.navigate.ChildContextLookup;
 import org.opensaml.profile.context.ProxiedRequesterContext;
@@ -37,7 +38,7 @@ public class ProxiedRequesterRegexpPolicyRuleTest {
     
     private ProxiedRequesterRegexpPolicyRule getMatcher() throws ComponentInitializationException {
         final ProxiedRequesterRegexpPolicyRule matcher = new ProxiedRequesterRegexpPolicyRule();
-        matcher.setRegularExpression("^requ.*");
+        matcher.setPattern(Pattern.compile("^requ.*"));
         matcher.setId("Test");
         matcher.initialize();
         return matcher;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list