mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Chris Ridd
10.30.2012 4a2f584d59872a5af42434de64f39755acb7a148
Fix OPENDJ-562 Country String syntax should validate ISO 3166 codes

Thanks to Manuel Gaupp for this contribution!

The default mode for the syntax handler is strict.
3 files added
4 files modified
327 ■■■■■ changed files
opends/resource/config/config.ldif 3 ●●●●● patch | view | raw | blame | history
opends/resource/schema/02-config.ldif 7 ●●●●● patch | view | raw | blame | history
opends/src/admin/defn/org/opends/server/admin/std/CountryStringAttributeSyntaxConfiguration.xml 76 ●●●●● patch | view | raw | blame | history
opends/src/admin/messages/CountryStringAttributeSyntaxCfgDefn.properties 7 ●●●●● patch | view | raw | blame | history
opends/src/messages/messages/schema.properties 3 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/schema/CountryStringSyntax.java 77 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/schema/CountryStringSyntaxTest.java 154 ●●●●● patch | view | raw | blame | history
opends/resource/config/config.ldif
@@ -22,6 +22,7 @@
#
#      Copyright 2006-2010 Sun Microsystems, Inc.
#      Portions Copyright 2010-2012 ForgeRock AS.
#      Portions Copyright 2012 Manuel Gaupp
#
#
# This file contains the primary Directory Server configuration.  It must not
@@ -2067,9 +2068,11 @@
dn: cn=Country String,cn=Syntaxes,cn=config
objectClass: top
objectClass: ds-cfg-attribute-syntax
objectClass: ds-cfg-country-string-attribute-syntax
cn: Country String
ds-cfg-java-class: org.opends.server.schema.CountryStringSyntax
ds-cfg-enabled: true
ds-cfg-strict-format: true
dn: cn=Delivery Method,cn=Syntaxes,cn=config
objectClass: top
opends/resource/schema/02-config.ldif
@@ -24,6 +24,7 @@
#      Copyright 2006-2010 Sun Microsystems, Inc.
#      Portions Copyright 2010-2012 ForgeRock AS.
#      Portions Copyright 2011 profiq, s.r.o.
#      Portions Copyright 2012 Manuel Gaupp
#
#
# This file contains the attribute type and objectclass definitions for use
@@ -5154,3 +5155,9 @@
  STRUCTURAL
  MAY ds-cfg-strict-format
  X-ORIGIN 'OpenDJ Directory Server' )
objectClasses: ( 1.3.6.1.4.1.36733.2.1.2.12
  NAME 'ds-cfg-country-string-attribute-syntax'
  SUP ds-cfg-attribute-syntax
  STRUCTURAL
  MAY ds-cfg-strict-format
  X-ORIGIN 'OpenDJ Directory Server' )
opends/src/admin/defn/org/opends/server/admin/std/CountryStringAttributeSyntaxConfiguration.xml
New file
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
  ! CDDL HEADER START
  !
  ! The contents of this file are subject to the terms of the
  ! Common Development and Distribution License, Version 1.0 only
  ! (the "License").  You may not use this file except in compliance
  ! with the License.
  !
  ! You can obtain a copy of the license at
  ! trunk/opends/resource/legal-notices/CDDLv1_0.txt
  ! or http://forgerock.org/license/CDDLv1.0.html.
  ! See the License for the specific language governing permissions
  ! and limitations under the License.
  !
  ! When distributing Covered Code, include this CDDL HEADER in each
  ! file and include the License file at
  ! trunk/opends/resource/legal-notices/CDDLv1_0.txt.  If applicable,
  ! add the following below this CDDL HEADER, with the fields enclosed
  ! by brackets "[]" replaced with your own identifying information:
  !      Portions Copyright [yyyy] [name of copyright owner]
  !
  ! CDDL HEADER END
  !
  !
  !      Copyright 2012 ForgeRock AS.
  !      Portions Copyright 2012 Manuel Gaupp
  ! -->
<adm:managed-object name="country-string-attribute-syntax"
  plural-name="country-string-attribute-syntaxes"
  extends="attribute-syntax" package="org.opends.server.admin.std"
  xmlns:adm="http://www.opends.org/admin"
  xmlns:ldap="http://www.opends.org/admin-ldap">
  <adm:synopsis>
    <adm:user-friendly-plural-name />
    define an attribute syntax for storing country codes.
  </adm:synopsis>
  <adm:profile name="ldap">
    <ldap:object-class>
      <ldap:name>ds-cfg-country-string-attribute-syntax</ldap:name>
      <ldap:superior>ds-cfg-attribute-syntax</ldap:superior>
    </ldap:object-class>
  </adm:profile>
  <adm:property-override name="java-class" advanced="true">
    <adm:default-behavior>
      <adm:defined>
        <adm:value>
          org.opends.server.schema.CountryStringSyntax
        </adm:value>
      </adm:defined>
    </adm:default-behavior>
  </adm:property-override>
  <adm:property name="strict-format" advanced="true">
    <adm:synopsis>
      Indicates whether or not country code values are required to
      strictly comply with the standard definition for this syntax.
    </adm:synopsis>
    <adm:description>
      When set to false, country codes will not be validated and, as
      a result any string containing 2 characters will be acceptable.
    </adm:description>
    <adm:default-behavior>
      <adm:defined>
        <adm:value>true</adm:value>
      </adm:defined>
    </adm:default-behavior>
    <adm:syntax>
      <adm:boolean />
    </adm:syntax>
    <adm:profile name="ldap">
      <ldap:attribute>
        <ldap:name>ds-cfg-strict-format</ldap:name>
      </ldap:attribute>
    </adm:profile>
  </adm:property>
</adm:managed-object>
opends/src/admin/messages/CountryStringAttributeSyntaxCfgDefn.properties
New file
@@ -0,0 +1,7 @@
user-friendly-name=Country String Attribute Syntax
user-friendly-plural-name=Country String Attribute Syntaxes
synopsis=Country String Attribute Syntaxes define an attribute syntax for storing country codes.
property.enabled.synopsis=Indicates whether the Country String Attribute Syntax is enabled.
property.java-class.synopsis=Specifies the fully-qualified name of the Java class that provides the Country String Attribute Syntax implementation.
property.strict-format.synopsis=Indicates whether or not country code values are required to strictly comply with the standard definition for this syntax.
property.strict-format.description=When set to false, country codes will not be validated and, as a result any string containing 2 characters will be acceptable.
opends/src/messages/messages/schema.properties
@@ -22,6 +22,7 @@
#
#      Copyright 2006-2010 Sun Microsystems, Inc.
#      Portions Copyright 2011 ForgeRock AS
#      Portions Copyright 2012 Manuel Gaupp
#
@@ -1073,3 +1074,5 @@
 a valid X.509 Certificate because it contains an invalid version number (%d)
SEVERE_ERR_SYNTAX_CERTIFICATE_INVALID_DER_332=The provided value is not a valid \
 X.509 Certificate because it contains invalid DER encodings
MILD_ERR_ATTR_SYNTAX_COUNTRY_NO_VALID_ISO_CODE_333=The provided value "%s" \
 is not a valid ISO 3166 country code
opends/src/server/org/opends/server/schema/CountryStringSyntax.java
@@ -24,12 +24,15 @@
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2012 ForgeRock AS
 *      Portions Copyright 2012 Manuel Gaupp
 */
package org.opends.server.schema;
import java.util.List;
import org.opends.server.admin.std.server.AttributeSyntaxCfg;
import org.opends.server.admin.server.ConfigurationChangeListener;
import org.opends.server.admin.std.server.CountryStringAttributeSyntaxCfg;
import org.opends.server.api.ApproximateMatchingRule;
import org.opends.server.api.AttributeSyntax;
import org.opends.server.api.EqualityMatchingRule;
@@ -38,10 +41,13 @@
import org.opends.server.config.ConfigException;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.ByteSequence;
import org.opends.server.types.ConfigChangeResult;
import org.opends.server.types.ResultCode;
import static org.opends.server.loggers.ErrorLogger.*;
import static org.opends.messages.SchemaMessages.*;
import org.opends.messages.Message;
import org.opends.messages.MessageBuilder;
import static org.opends.server.schema.PrintableString.*;
import static org.opends.server.schema.SchemaConstants.*;
@@ -55,7 +61,8 @@
 * ways, it will behave like the directory string attribute syntax.
 */
public class CountryStringSyntax
       extends AttributeSyntax<AttributeSyntaxCfg>
       extends AttributeSyntax<CountryStringAttributeSyntaxCfg>
       implements ConfigurationChangeListener<CountryStringAttributeSyntaxCfg>
{
  // The default approximate matching rule for this syntax.
  private ApproximateMatchingRule defaultApproximateMatchingRule;
@@ -69,6 +76,9 @@
  // The default substring matching rule for this syntax.
  private SubstringMatchingRule defaultSubstringMatchingRule;
  // The current configuration
  private volatile CountryStringAttributeSyntaxCfg config;
  /**
@@ -87,7 +97,7 @@
  /**
   * {@inheritDoc}
   */
  public void initializeSyntax(AttributeSyntaxCfg configuration)
  public void initializeSyntax(CountryStringAttributeSyntaxCfg configuration)
         throws ConfigException
  {
    defaultApproximateMatchingRule =
@@ -121,11 +131,42 @@
      logError(ERR_ATTR_SYNTAX_UNKNOWN_SUBSTRING_MATCHING_RULE.get(
          SMR_CASE_IGNORE_OID, SYNTAX_COUNTRY_STRING_NAME));
    }
    this.config = configuration;
    config.addCountryStringChangeListener(this);
  }
  /**
   * {@inheritDoc}
   */
  public boolean isConfigurationChangeAcceptable(
      CountryStringAttributeSyntaxCfg configuration,
      List<Message> unacceptableReasons)
  {
    // The configuration is always acceptable.
    return true;
  }
  /**
   * {@inheritDoc}
   */
  public ConfigChangeResult applyConfigurationChange(
      CountryStringAttributeSyntaxCfg configuration)
  {
    this.config = configuration;
    return new ConfigChangeResult(ResultCode.SUCCESS, false);
  }
  /**
   * Retrieves the common name for this attribute syntax.
   *
   * @return  The common name for this attribute syntax.
@@ -236,7 +277,7 @@
  public boolean valueIsAcceptable(ByteSequence value,
                                   MessageBuilder invalidReason)
  {
    String stringValue = toLowerCase(value.toString());
    String stringValue = value.toString();
    if (stringValue.length() != 2)
    {
      invalidReason.append(
@@ -244,16 +285,28 @@
      return false;
    }
    if ((! isPrintableCharacter(stringValue.charAt(0))) ||
        (! isPrintableCharacter(stringValue.charAt(1))))
    if (config.isStrictFormat())
    {
      invalidReason.append(
              ERR_ATTR_SYNTAX_COUNTRY_STRING_NOT_PRINTABLE.get(stringValue));
      return false;
      // Check for a string containing [A-Z][A-Z]
      if (stringValue.charAt(0) < 'A' || stringValue.charAt(0) > 'Z' ||
          stringValue.charAt(1) < 'A' || stringValue.charAt(1) > 'Z')
        {
          invalidReason.append(ERR_ATTR_SYNTAX_COUNTRY_NO_VALID_ISO_CODE
                 .get(value.toString()));
          return false;
        }
    }
    else
    {
      // Just validate as string containing 2 printable characters
      if ((! isPrintableCharacter(stringValue.charAt(0))) ||
          (! isPrintableCharacter(stringValue.charAt(1))))
      {
        invalidReason.append(
                ERR_ATTR_SYNTAX_COUNTRY_STRING_NOT_PRINTABLE.get(stringValue));
        return false;
      }
    }
    return true;
  }
opends/tests/unit-tests-testng/src/server/org/opends/server/schema/CountryStringSyntaxTest.java
New file
@@ -0,0 +1,154 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2012 Forgerock AS
 *      Portions Copyright 2012 Manuel Gaupp
 */
package org.opends.server.schema;
import org.opends.server.api.AttributeSyntax;
import org.opends.server.admin.server.ConfigurationChangeListener;
import org.opends.server.admin.std.server.AttributeSyntaxCfg;
import org.opends.server.admin.std.server.CountryStringAttributeSyntaxCfg;
import org.opends.server.config.ConfigException;
import org.opends.server.types.DN;
import org.testng.annotations.DataProvider;
/**
 * Test the CountryStringSyntax.
 */
public class CountryStringSyntaxTest extends AttributeSyntaxTest
{
  /**
   * {@inheritDoc}
   */
  @Override
  protected AttributeSyntax getRule()
  {
    CountryStringSyntax syntax = new CountryStringSyntax();
    CountryStringAttributeSyntaxCfg cfg = new CountryStringAttributeSyntaxCfg()
    {
      public DN dn()
      {
        return null;
      }
      public void removeChangeListener(ConfigurationChangeListener<AttributeSyntaxCfg> listener)
      {
        // Stub.
      }
      public boolean isEnabled()
      {
        // Stub.
        return false;
      }
      public void addChangeListener(
          ConfigurationChangeListener<AttributeSyntaxCfg> listener)
      {
        // Stub.
      }
      public void removeCountryStringChangeListener(
          ConfigurationChangeListener<CountryStringAttributeSyntaxCfg> listener)
      {
        // Stub.
      }
      public boolean isStrictFormat()
      {
        return true;
      }
      public String getJavaClass()
      {
        // Stub.
        return null;
      }
      public Class<? extends CountryStringAttributeSyntaxCfg> configurationClass()
      {
        // Stub.
        return null;
      }
      public void addCountryStringChangeListener(
          ConfigurationChangeListener<CountryStringAttributeSyntaxCfg> listener)
      {
        // Stub.
      }
    };
    try
    {
      syntax.initializeSyntax(cfg);
    }
    catch (ConfigException e)
    {
      // Should never happen.
      throw new RuntimeException(e);
    }
    return syntax;
  }
  /**
   * {@inheritDoc}
   */
  @Override
  @DataProvider(name="acceptableValues")
  public Object[][] createAcceptableValues()
  {
    return new Object [][] {
        {"DE", true},
        {"de", false},
        {"SX", true},
        {"12", false},
        {"UK", true},
        {"Xf", false},
        {"ÖÄ", false},
    };
  }
}