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

Jean-Noël Rouvignac
15.22.2016 9be9796ea612b8b8892c9304aef737bf2b89be8a
Converters.java: fixed bad conversion from SDK entry to server Entry

Problem happened when converting objectclass names if an attribute type exists with the exact same name.
In that case, the converted entry end up with an object class which has the correct name,
but incorrectly references the attribute type OID.
This case happens in 02-config.ldif for "ds-cfg-backend", "ds-cfg-certificate-mapper", etc.
2 files modified
49 ■■■■ changed files
opendj-server-legacy/src/main/java/org/forgerock/opendj/adapter/server3x/Converters.java 33 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/forgerock/opendj/adapter/server3x/ConvertersTestCase.java 16 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/forgerock/opendj/adapter/server3x/Converters.java
@@ -50,6 +50,7 @@
import org.forgerock.util.Reject;
import org.opends.server.core.BindOperation;
import org.opends.server.core.CompareOperation;
import org.opends.server.core.DirectoryServer;
import org.opends.server.core.ExtendedOperation;
import org.opends.server.protocols.ldap.LDAPAttribute;
import org.opends.server.protocols.ldap.LDAPControl;
@@ -58,6 +59,7 @@
import org.opends.server.types.AttributeBuilder;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.LDAPException;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.Operation;
import org.opends.server.types.SearchFilter;
import org.opends.server.util.ServerConstants;
@@ -85,7 +87,22 @@
                new org.opends.server.types.Entry(sdkEntry.getName(), null, null, null);
            List<ByteString> duplicateValues = new ArrayList<>();
            for (org.opends.server.types.Attribute attribute : toAttributes(sdkEntry.getAllAttributes())) {
                entry.addAttribute(attribute, duplicateValues);
                if (attribute.getAttributeDescription().getAttributeType().isObjectClass()) {
                    for (ByteString attrName : attribute) {
                        try {
                            final String ocName = attrName.toString();
                            ObjectClass oc = DirectoryServer.getObjectClass(ocName);
                            if (oc == null) {
                                oc = DirectoryServer.getDefaultObjectClass(ocName);
                            }
                            entry.addObjectClass(oc);
                        } catch (DirectoryException e) {
                            throw new IllegalStateException(e.getMessage(), e);
                        }
                    }
                } else {
                    entry.addAttribute(attribute, duplicateValues);
                }
            }
            return entry;
        }
@@ -125,13 +142,11 @@
     * @return the converted value
     */
    public static org.opends.server.types.RawFilter to(final org.forgerock.opendj.ldap.Filter filter) {
        org.opends.server.protocols.ldap.LDAPFilter ldapFilter = null;
        try {
            ldapFilter = LDAPFilter.decode(filter.toString());
            return LDAPFilter.decode(filter.toString());
        } catch (LDAPException e) {
            throw new IllegalStateException(e);
        }
        return ldapFilter;
    }
    /**
@@ -143,13 +158,11 @@
     * @return the converted value
     */
    public static SearchFilter toSearchFilter(final org.forgerock.opendj.ldap.Filter filter) {
        SearchFilter ldapFilter = null;
        try {
            ldapFilter = SearchFilter.createFilterFromString(filter.toString());
            return SearchFilter.createFilterFromString(filter.toString());
        } catch (DirectoryException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
        return ldapFilter;
    }
    /**
@@ -633,17 +646,15 @@
     */
    public static ByteString getCredentials(final byte[] authenticationValue) {
        final ASN1Reader reader = ASN1.getReader(authenticationValue);
        ByteString saslCred = ByteString.empty();
        try {
            reader.readOctetStringAsString(); // Reads SASL Mechanism - RFC 4511 4.2
            if (reader.hasNextElement()) {
                saslCred = reader.readOctetString(); // Reads credentials.
                return reader.readOctetString().toByteString();
            }
        } catch (IOException e) {
            // Nothing to do.
        }
        return saslCred.toByteString();
        return ByteString.empty();
    }
    /**
opendj-server-legacy/src/test/java/org/forgerock/opendj/adapter/server3x/ConvertersTestCase.java
@@ -20,6 +20,7 @@
import static org.mockito.Mockito.*;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -64,6 +65,7 @@
import org.opends.server.types.DirectoryException;
import org.opends.server.types.FilterType;
import org.opends.server.types.LDAPException;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.Operation;
import org.opends.server.types.SearchResultEntry;
import org.opends.server.types.SearchResultReference;
@@ -134,6 +136,20 @@
        assertThat(result.getAttributes()).hasSize(2);
    }
    @Test
    public final void testToEntryDoesNotMixObjectClassAndAttributeTypeOIDs() throws Exception {
        org.forgerock.opendj.ldap.Entry entry =
            new LinkedHashMapEntry(DN.valueOf("uid=scarter,ou=People,dc=example,dc=com"));
        entry.addAttribute(new LinkedAttribute("objectClass", "ds-cfg-backend", "ds-cfg-create-placeholder-for-me"));
        org.opends.server.types.Entry result = to(entry);
        assertThat(result.getName().toString()).isEqualTo(entry.getName().toString());
        List<ObjectClass> ocs = new ArrayList<>(result.getObjectClasses().keySet());
        assertThat(ocs).hasSize(2);
        assertThat(ocs.get(0).getOID()).isEqualTo(DirectoryServer.getObjectClass("ds-cfg-backend").getOID());
        assertThat(ocs.get(1).getOID()).as("This should be a placeholder").endsWith("-oid");
    }
    /**
     * Converts a SDK control to a LDAP server control.
     *