Search code examples
spring-bootspring-testopenldapspring-ldapapacheds

Ldap attribues for testing in Spring Boot with ApacheDS


I am trying to simulate a real Microsoft LDAP tree. The application is working against a real LDAP, but using an inmemory LDAP server (ApacheDS) for testing in Spring Boot but it doesn't work. I can't replicate some attributes to make it work. For example, my dn base is ou=users,dc=oficinas,dc=com:

dn: cn=user01,ou=users,dc=oficinas,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
cn: user01
sn: user

I need attributes like memberOf: , distinguishedname: or objectCategory: , and ApacheDS doesn't recognize them when loading the .ldif file.

I'm trying to create the schema but apacheDS doens't allow me.

dn: ou=schema
objectClass: organizationalUnit
objectClass: top
ou: schema

dn: m-oid=1.3.6.1.4.1.42.2.27.32.1.1,ou=schema
m-collective: FALSE
m-singlevalue: TRUE
m-oid: 1.3.6.1.4.1.42.2.27.32.1.1
m-obsolete: FALSE
m-description: Custom Attribute
m-nousermodification: FALSE
objectclass: metaAttributeType
objectclass: metaTop
objectclass: top
m-syntax: 1.3.6.1.4.1.1466.115.121.1.15
m-usage: USER_APPLICATIONS
m-name: memberOf

dn: m-oid=1.3.6.1.4.1.42.2.27.32.1,ou=schema
m-oid: 1.3.6.1.4.1.42.2.27.32.1
m-obsolete: FALSE
m-supobjectclass: inetOrgPerson
m-description: -
objectclass: metaObjectClass
objectclass: metaTop
objectclass: top
m-name: customPerson
m-typeobjectclass: STRUCTURAL
m-may: memberOf
m-equality: objectIdentifierMatch

And runnig I see this error:

'm-oid=1.3.6.1.4.1.42.2.27.32.1.1,ou=attributeTypes,ou=schema,dc=oficinas,dc=sepg,dc=minhac,dc=age' because it violates the provided schema:  The entry contains object class metaAttributeType which is not defined in the schema.  The entry contains object class metaTop which is not defined in the schema.  The entry contains attribute m-oid which is not defined in the schema.  The entry contains attribute m-syntax which is not defined in the schema.  The entry contains attribute m-obsolete which is not defined in the schema.  The entry contains attribute m-collective which is not defined in the schema.  The entry contains attribute m-usage which is not defined in the schema.  The entry contains attribute m-name which is not defined in the schema.  The entry contains attribute m-nousermodification which is not defined in the schema.  The entry contains attribute m-singlevalue which is not defined in the schema.  The entry contains attribute m-description which is not defined in the schema.  The entry's RDN contains attribute m-oid which is not defined in the schema.]
    at java.naming/com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3291) ~[na:na]

I need those attributes to simulate the real operations (I try bitnami as a local server and I find the same problem). How can I add custom attributes in the schema? Can I do it using Spring Boot in the .ldif file?


Solution

  • Another way has been loading data as add or modify

    dn: cn=schema
    changetype: modify
    add: attributeTypes
    attributeTypes: ( 1.3.6.1.4.1.4203.666.11.222.1 NAME 'memberOf'
      DESC 'Custom attribute memberOf'
      EQUALITY caseIgnoreMatch
      SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
      SINGLE-VALUE )
    
    dn: cn=schema
    changetype: modify
    add: attributeTypes
    attributeTypes: ( 1.3.6.1.4.1.4203.666.11.222.2 NAME 'objectCategory'
      DESC 'Custom attribute objectCategory'
      EQUALITY caseIgnoreMatch
      SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
      SINGLE-VALUE )
    
    dn: cn=schema
    changetype: modify
    add: attributeTypes
    attributeTypes: ( 1.3.6.1.4.1.4203.666.11.222.3 NAME 'distinguishedname'
      DESC 'Custom attribute objectCategory'
      EQUALITY caseIgnoreMatch
      SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
      SINGLE-VALUE )
    
    dn: cn=schema
    changetype: modify
    add: objectClasses
    objectClasses: ( 1.3.6.1.4.1.4203.666.11.333.2 NAME 'customPerson'
      DESC 'Custom object person'
      SUP top
      STRUCTURAL
      MUST ( cn $ sn )
      MAY ( memberOf $ objectCategory $ distinguishedname ) )
    
    dn: cn=user01,ou=users,dc=oficinas,dc=com
    changetype: add
    objectClass: customPerson
    objectClass: top
    cn: John Doe
    sn: Doe
    memberOf: cn=COFFEEIEC_GRP1,ou=group,dc=oficinas,dc=com
    objectCategory: user
    distinguishedname: cn=user01,ou=users,dc=oficinas,dc=com
    

    And load the data with something like this

      LDAPConnection connection = new LDAPConnection(ldapHost, ldapPort, bindDN, password);
    
      // Create an LDIF reader
        try (LDIFReader ldifReader = new LDIFReader(resourceLoader.getResource(env.getRequiredProperty("ldap.ldiffile")).getInputStream())) {
          LDIFChangeRecord changeRecord;
          // Read each entry or change record in the LDIF file
          while ((changeRecord = ldifReader.readChangeRecord()) != null) {
              if (changeRecord instanceof LDIFAddChangeRecord) {
                  LDIFAddChangeRecord addRecord = (LDIFAddChangeRecord) changeRecord;
                  AddRequest addRequest = addRecord.toAddRequest();
                  connection.add(addRequest);
              } else if (changeRecord instanceof LDIFModifyChangeRecord) {
                  LDIFModifyChangeRecord modifyRecord = (LDIFModifyChangeRecord) changeRecord;
                  ModifyRequest modifyRequest = modifyRecord.toModifyRequest();
                  connection.modify(modifyRequest);
              }
          }
        } catch (Exception e) {
            throw new RuntimeException();
        }