Procházet zdrojové kódy

allow value factories to enforce setting parameters

jrivard@gmail.com před 6 roky
rodič
revize
be7a66544b
28 změnil soubory, kde provedl 69 přidání a 60 odebrání
  1. 1 1
      pom.xml
  2. 0 34
      server/pom.xml
  3. 1 1
      server/src/main/java/password/pwm/config/StoredValue.java
  4. 1 0
      server/src/main/java/password/pwm/config/value/ActionValue.java
  5. 1 1
      server/src/main/java/password/pwm/config/value/BooleanValue.java
  6. 2 1
      server/src/main/java/password/pwm/config/value/ChallengeValue.java
  7. 1 1
      server/src/main/java/password/pwm/config/value/CustomLinkValue.java
  8. 1 0
      server/src/main/java/password/pwm/config/value/EmailValue.java
  9. 1 1
      server/src/main/java/password/pwm/config/value/FileValue.java
  10. 1 1
      server/src/main/java/password/pwm/config/value/FormValue.java
  11. 2 2
      server/src/main/java/password/pwm/config/value/LocalizedStringArrayValue.java
  12. 1 1
      server/src/main/java/password/pwm/config/value/LocalizedStringValue.java
  13. 1 0
      server/src/main/java/password/pwm/config/value/NamedSecretValue.java
  14. 1 1
      server/src/main/java/password/pwm/config/value/NumericArrayValue.java
  15. 21 2
      server/src/main/java/password/pwm/config/value/NumericValue.java
  16. 3 3
      server/src/main/java/password/pwm/config/value/OptionListValue.java
  17. 1 0
      server/src/main/java/password/pwm/config/value/PasswordValue.java
  18. 1 1
      server/src/main/java/password/pwm/config/value/PrivateKeyValue.java
  19. 1 0
      server/src/main/java/password/pwm/config/value/RemoteWebServiceValue.java
  20. 1 1
      server/src/main/java/password/pwm/config/value/StringArrayValue.java
  21. 1 1
      server/src/main/java/password/pwm/config/value/StringValue.java
  22. 1 1
      server/src/main/java/password/pwm/config/value/UserPermissionValue.java
  23. 1 1
      server/src/main/java/password/pwm/config/value/ValueFactory.java
  24. 1 1
      server/src/main/java/password/pwm/config/value/VerificationMethodValue.java
  25. 1 1
      server/src/main/java/password/pwm/config/value/X509CertificateValue.java
  26. 7 2
      server/src/main/java/password/pwm/util/java/JavaHelper.java
  27. 14 0
      server/src/test/java/password/pwm/config/PwmSettingTest.java
  28. 0 1
      webapp/pom.xml

+ 1 - 1
pom.xml

@@ -188,7 +188,7 @@
                     <dependency>
                         <groupId>com.puppycrawl.tools</groupId>
                         <artifactId>checkstyle</artifactId>
-                        <version>8.12</version>
+                        <version>8.13</version>
                     </dependency>
                 </dependencies>
                 <executions>

+ 0 - 34
server/pom.xml

@@ -21,40 +21,6 @@
 
     <build>
         <plugins>
-            <!--
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-pmd-plugin</artifactId>
-                <version>3.9.0</version>
-                <executions>
-                    <execution>
-                        <phase>test</phase>
-                        <goals>
-                            <goal>check</goal>
-                            <goal>cpd-check</goal>
-                        </goals>
-                    </execution>
-                </executions>
-                <configuration>
-                    <language>jsp</language>
-                    <rulesets>
-                        <ruleset>/category/jsp/bestpractices.xml</ruleset>
-                        <ruleset>/category/jsp/codestyle.xml</ruleset>
-                        <ruleset>/category/jsp/design.xml</ruleset>
-                        <ruleset>/category/jsp/errorprone.xml</ruleset>
-                        <ruleset>/category/jsp/security.xml</ruleset>
-                    </rulesets>
-                    <includes>
-                        <include>**/*.jsp</include>
-                    </includes>
-                    <compileSourceRoots>
-                        <compileSourceRoot>${basedir}/src/main/webapp</compileSourceRoot>
-                    </compileSourceRoots>
-                    <skip>false</skip>
-                </configuration>
-            </plugin>
-            -->
-
             <plugin>
                 <artifactId>maven-resources-plugin</artifactId>
                 <version>3.1.0</version>

+ 1 - 1
server/src/main/java/password/pwm/config/StoredValue.java

@@ -51,7 +51,7 @@ public interface StoredValue extends Serializable
     {
         StoredValue fromJson( String input );
 
-        StoredValue fromXmlElement( Element settingElement, PwmSecurityKey key )
+        StoredValue fromXmlElement( PwmSetting pwmSetting, Element settingElement, PwmSecurityKey key )
                 throws PwmException;
     }
 

+ 1 - 0
server/src/main/java/password/pwm/config/value/ActionValue.java

@@ -89,6 +89,7 @@ public class ActionValue extends AbstractValue implements StoredValue
             }
 
             public ActionValue fromXmlElement(
+                    final PwmSetting pwmSetting,
                     final Element settingElement,
                     final PwmSecurityKey pwmSecurityKey
             )

+ 1 - 1
server/src/main/java/password/pwm/config/value/BooleanValue.java

@@ -55,7 +55,7 @@ public class BooleanValue implements StoredValue
                 return new BooleanValue( JsonUtil.deserialize( value, Boolean.class ) );
             }
 
-            public BooleanValue fromXmlElement( final Element settingElement, final PwmSecurityKey input )
+            public BooleanValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey input )
             {
                 final Element valueElement = settingElement.getChild( "value" );
                 final String value = valueElement.getText();

+ 2 - 1
server/src/main/java/password/pwm/config/value/ChallengeValue.java

@@ -61,7 +61,7 @@ public class ChallengeValue extends AbstractValue implements StoredValue
             {
                 if ( input == null )
                 {
-                    return new ChallengeValue( Collections.<String, List<ChallengeItemConfiguration>>emptyMap() );
+                    return new ChallengeValue( Collections.emptyMap() );
                 }
                 else
                 {
@@ -77,6 +77,7 @@ public class ChallengeValue extends AbstractValue implements StoredValue
             }
 
             public ChallengeValue fromXmlElement(
+                    final PwmSetting pwmSetting,
                     final Element settingElement,
                     final PwmSecurityKey input
             )

+ 1 - 1
server/src/main/java/password/pwm/config/value/CustomLinkValue.java

@@ -71,7 +71,7 @@ public class CustomLinkValue extends AbstractValue implements StoredValue
                 }
             }
 
-            public CustomLinkValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public CustomLinkValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
                     throws PwmOperationalException
             {
                 final List valueElements = settingElement.getChildren( "value" );

+ 1 - 0
server/src/main/java/password/pwm/config/value/EmailValue.java

@@ -74,6 +74,7 @@ public class EmailValue extends AbstractValue implements StoredValue
             }
 
             public EmailValue fromXmlElement(
+                    final PwmSetting pwmSetting,
                     final Element settingElement,
                     final PwmSecurityKey input
             )

+ 1 - 1
server/src/main/java/password/pwm/config/value/FileValue.java

@@ -125,7 +125,7 @@ public class FileValue extends AbstractValue implements StoredValue
         return new StoredValueFactory()
         {
 
-            public FileValue fromXmlElement( final Element settingElement, final PwmSecurityKey input )
+            public FileValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey input )
                     throws PwmOperationalException
             {
                 final List valueElements = settingElement.getChildren( "value" );

+ 1 - 1
server/src/main/java/password/pwm/config/value/FormValue.java

@@ -76,7 +76,7 @@ public class FormValue extends AbstractValue implements StoredValue
                 }
             }
 
-            public FormValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public FormValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
                     throws PwmOperationalException
             {
                 final boolean oldType = PwmSettingSyntax.LOCALIZED_STRING_ARRAY.toString().equals(

+ 2 - 2
server/src/main/java/password/pwm/config/value/LocalizedStringArrayValue.java

@@ -64,12 +64,12 @@ public class LocalizedStringArrayValue extends AbstractValue implements StoredVa
                     Map<String, List<String>> srcMap = JsonUtil.deserialize( input, new TypeToken<Map<String, List<String>>>()
                     {
                     } );
-                    srcMap = srcMap == null ? Collections.<String, List<String>>emptyMap() : new TreeMap<>( srcMap );
+                    srcMap = srcMap == null ? Collections.emptyMap() : new TreeMap<>( srcMap );
                     return new LocalizedStringArrayValue( Collections.unmodifiableMap( srcMap ) );
                 }
             }
 
-            public LocalizedStringArrayValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public LocalizedStringArrayValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
             {
                 final List valueElements = settingElement.getChildren( "value" );
                 final Map<String, List<String>> values = new TreeMap<>();

+ 1 - 1
server/src/main/java/password/pwm/config/value/LocalizedStringValue.java

@@ -69,7 +69,7 @@ public class LocalizedStringValue extends AbstractValue implements StoredValue
                 }
             }
 
-            public LocalizedStringValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public LocalizedStringValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
             {
                 final List elements = settingElement.getChildren( "value" );
                 final Map<String, String> values = new TreeMap<>();

+ 1 - 0
server/src/main/java/password/pwm/config/value/NamedSecretValue.java

@@ -87,6 +87,7 @@ public class NamedSecretValue implements StoredValue
             }
 
             public NamedSecretValue fromXmlElement(
+                    final PwmSetting pwmSetting,
                     final Element settingElement,
                     final PwmSecurityKey key
             )

+ 1 - 1
server/src/main/java/password/pwm/config/value/NumericArrayValue.java

@@ -57,7 +57,7 @@ public class NumericArrayValue extends AbstractValue implements StoredValue
                 return new NumericArrayValue( list );
             }
 
-            public NumericArrayValue fromXmlElement( final Element settingElement, final PwmSecurityKey input )
+            public NumericArrayValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey input )
             {
                 final List<Long> returnList = new ArrayList<>(  );
                 final List<Element> valueElements = settingElement.getChildren( "value" );

+ 21 - 2
server/src/main/java/password/pwm/config/value/NumericValue.java

@@ -24,6 +24,7 @@ package password.pwm.config.value;
 
 import org.jdom2.Element;
 import password.pwm.config.PwmSetting;
+import password.pwm.config.PwmSettingProperty;
 import password.pwm.config.StoredValue;
 import password.pwm.util.java.JsonUtil;
 import password.pwm.util.secure.PwmSecurityKey;
@@ -49,15 +50,33 @@ public class NumericValue extends AbstractValue implements StoredValue
                 return new NumericValue( JsonUtil.deserialize( value, Long.class ) );
             }
 
-            public NumericValue fromXmlElement( final Element settingElement, final PwmSecurityKey input )
+            public NumericValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey input )
             {
                 final Element valueElement = settingElement.getChild( "value" );
                 final String value = valueElement.getText();
-                return new NumericValue( Long.parseLong( value ) );
+                return new NumericValue( normalizeValue( pwmSetting, Long.parseLong( value ) ) );
             }
         };
     }
 
+    private static long normalizeValue( final PwmSetting pwmSetting, final long value )
+    {
+        final long minValue = Long.parseLong( pwmSetting.getProperties().getOrDefault( PwmSettingProperty.Minimum, "0" ) );
+        final long maxValue = Long.parseLong( pwmSetting.getProperties().getOrDefault( PwmSettingProperty.Maximum, "0" ) );
+
+        if ( minValue > 0 && value < minValue )
+        {
+            return minValue;
+        }
+
+        if ( maxValue > 0 && value > maxValue )
+        {
+            return maxValue;
+        }
+
+        return value;
+    }
+
     @Override
     public List<Element> toXmlValues( final String valueElementName, final PwmSecurityKey pwmSecurityKey  )
     {

+ 3 - 3
server/src/main/java/password/pwm/config/value/OptionListValue.java

@@ -55,14 +55,14 @@ public class OptionListValue extends AbstractValue implements StoredValue
             {
                 if ( input == null )
                 {
-                    return new OptionListValue( Collections.<String>emptySet() );
+                    return new OptionListValue( Collections.emptySet() );
                 }
                 else
                 {
                     Set<String> srcList = JsonUtil.deserialize( input, new TypeToken<Set<String>>()
                     {
                     } );
-                    srcList = srcList == null ? Collections.<String>emptySet() : srcList;
+                    srcList = srcList == null ? Collections.emptySet() : srcList;
                     while ( srcList.contains( null ) )
                     {
                         srcList.remove( null );
@@ -71,7 +71,7 @@ public class OptionListValue extends AbstractValue implements StoredValue
                 }
             }
 
-            public OptionListValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public OptionListValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
                     throws PwmOperationalException
             {
                 final List valueElements = settingElement.getChildren( "value" );

+ 1 - 0
server/src/main/java/password/pwm/config/value/PasswordValue.java

@@ -79,6 +79,7 @@ public class PasswordValue implements StoredValue
             }
 
             public PasswordValue fromXmlElement(
+                    final PwmSetting pwmSetting,
                     final Element settingElement,
                     final PwmSecurityKey key
             )

+ 1 - 1
server/src/main/java/password/pwm/config/value/PrivateKeyValue.java

@@ -59,7 +59,7 @@ public class PrivateKeyValue extends AbstractValue
     {
         return new StoredValue.StoredValueFactory()
         {
-            public PrivateKeyValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public PrivateKeyValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
             {
                 if ( settingElement != null && settingElement.getChild( "value" ) != null )
                 {

+ 1 - 0
server/src/main/java/password/pwm/config/value/RemoteWebServiceValue.java

@@ -82,6 +82,7 @@ public class RemoteWebServiceValue extends AbstractValue implements StoredValue
             }
 
             public RemoteWebServiceValue fromXmlElement(
+                    final PwmSetting pwmSetting,
                     final Element settingElement,
                     final PwmSecurityKey pwmSecurityKey
             )

+ 1 - 1
server/src/main/java/password/pwm/config/value/StringArrayValue.java

@@ -68,7 +68,7 @@ public class StringArrayValue extends AbstractValue implements StoredValue
                 }
             }
 
-            public StringArrayValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public StringArrayValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
             {
                 final List valueElements = settingElement.getChildren( "value" );
                 final List<String> values = new ArrayList<>();

+ 1 - 1
server/src/main/java/password/pwm/config/value/StringValue.java

@@ -60,7 +60,7 @@ public class StringValue extends AbstractValue implements StoredValue
                 return new StringValue( newValue );
             }
 
-            public StringValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public StringValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
             {
                 final Element valueElement = settingElement.getChild( "value" );
                 return new StringValue( valueElement == null ? "" : valueElement.getText() );

+ 1 - 1
server/src/main/java/password/pwm/config/value/UserPermissionValue.java

@@ -74,7 +74,7 @@ public class UserPermissionValue extends AbstractValue implements StoredValue
                 }
             }
 
-            public UserPermissionValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public UserPermissionValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
                     throws PwmOperationalException
             {
                 final boolean newType = "2".equals(

+ 1 - 1
server/src/main/java/password/pwm/config/value/ValueFactory.java

@@ -62,7 +62,7 @@ public class ValueFactory
         try
         {
             final StoredValue.StoredValueFactory factory = setting.getSyntax().getStoredValueImpl();
-            return factory.fromXmlElement( settingElement, key );
+            return factory.fromXmlElement( setting, settingElement, key );
         }
         catch ( Exception e )
         {

+ 1 - 1
server/src/main/java/password/pwm/config/value/VerificationMethodValue.java

@@ -126,7 +126,7 @@ public class VerificationMethodValue extends AbstractValue implements StoredValu
                 }
             }
 
-            public VerificationMethodValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public VerificationMethodValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
                     throws PwmOperationalException
             {
                 final Element valueElement = settingElement.getChild( "value" );

+ 1 - 1
server/src/main/java/password/pwm/config/value/X509CertificateValue.java

@@ -54,7 +54,7 @@ public class X509CertificateValue extends AbstractValue implements StoredValue
     {
         return new StoredValueFactory()
         {
-            public X509CertificateValue fromXmlElement( final Element settingElement, final PwmSecurityKey key )
+            public X509CertificateValue fromXmlElement( final PwmSetting pwmSetting, final Element settingElement, final PwmSecurityKey key )
             {
                 final List<X509Certificate> certificates = new ArrayList<>();
                 final List<Element> valueElements = settingElement.getChildren( "value" );

+ 7 - 2
server/src/main/java/password/pwm/util/java/JavaHelper.java

@@ -575,10 +575,15 @@ public class JavaHelper
 
     public static boolean isEmpty( final Map map )
     {
-        return map == null ? true : map.isEmpty();
+        return map == null || map.isEmpty();
     }
 
     public static int rangeCheck( final int min, final int max, final int value )
+    {
+        return (int) rangeCheck( (long) min, (long) max, (long) value );
+    }
+
+    public static long rangeCheck( final long min, final long max, final long value )
     {
         if ( min > max )
         {
@@ -588,7 +593,7 @@ public class JavaHelper
         {
             throw new IllegalArgumentException( "max range is less than min range" );
         }
-        int returnValue = value;
+        long returnValue = value;
         if ( value < min )
         {
             returnValue = min;

+ 14 - 0
server/src/test/java/password/pwm/config/PwmSettingTest.java

@@ -111,4 +111,18 @@ public class PwmSettingTest {
             seenKeys.add(pwmSetting.getKey());
         }
     }
+
+    @Test
+    public void testMinMaxValueRanges()
+    {
+        for (PwmSetting pwmSetting : PwmSetting.values()) {
+            final long minValue = Long.parseLong( pwmSetting.getProperties().getOrDefault( PwmSettingProperty.Minimum, "0" ) );
+            final long maxValue = Long.parseLong( pwmSetting.getProperties().getOrDefault( PwmSettingProperty.Maximum, "0" ) );
+            if ( maxValue != 0 )
+            {
+                Assert.assertTrue( maxValue > minValue );
+            }
+        }
+
+    }
 }

+ 0 - 1
webapp/pom.xml

@@ -15,7 +15,6 @@
     <name>PWM Password Self Service: Server WAR</name>
 
     <properties>
-        <skipSpotbugs>true</skipSpotbugs>
         <project.root.basedir>${project.basedir}/..</project.root.basedir>
     </properties>