Skip to content

RFE: Support for add and delete operations in ldif.LDIFParser.parse_change_records() #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fager opened this issue May 15, 2024 · 4 comments

Comments

@fager
Copy link

fager commented May 15, 2024

Issue description:

LDIF files with multiple operations are not fully processed by ldif.LDIFParser.parse_change_records().

Operations of the change type "modify" are processed via handle_modify() but operations of the change type "add" or "delete" are counted but otherwise simply ignored.

Example of such an LDIF file:

version: 1

dn: cn=demo1,ou=groups,dc=example,dc=com
changetype: add
objectClass: groupOfNames
objectClass: nsMemberOf
objectClass: posixGroup
objectClass: top
cn: demo1
gidNumber: 99997


dn: cn=demo2,ou=groups,dc=example,dc=com
changetype: add
objectClass: groupOfNames
objectClass: nsMemberOf
objectClass: posixGroup
objectClass: top
cn: demo2
gidNumber: 99996


dn: cn=demo2,ou=groups,dc=example,dc=com
changetype: modify
add: description
description: Eine zweite Testgruppe


dn: cn=demo1,ou=groups,dc=example,dc=com
changetype: delete


Steps to reproduce:

Create your own LDIF parser and an import for the test file shown:

from pathlib import Path
from ldif import LDIFParser

class MyLDIFParser(LDIFParser):
    
    def __init__(self, input_file, ignored_attr_types=None, max_entries=0, process_url_schemes=None, line_sep='\n'):
        super().__init__(input_file, ignored_attr_types, max_entries, process_url_schemes, line_sep)

    def handle(self, dn, entry):
        print(f"MyLDIFParser::handle({dn})")
    
    def handle_modify(self, dn, modops, controls=None):
        print(f"MyLDIFParser::handle_modify({dn})")

def main():
    p_change = Path("test_change.ldif")
    ldifparser_change = MyLDIFParser(open(p_change), "r")
    ldifparser_change.parse_change_records()
    print(f"Changetype counter: {ldifparser_change.changetype_counter}")
    
    

if __name__ == '__main__':
    main()

Start this test:

$ ./demo.py 
MyLDIFParser::handle_modify(cn=demo2,ou=groups,dc=example,dc=com)
Changetype counter: {'add': 2, 'delete': 1, 'modify': 1, 'modrdn': 0}

Operating system:
Fedora 39

Python version:

  • Python 3.6
  • Python 3.12

python-ldap version:
3.4.4

@quanah
Copy link
Contributor

quanah commented May 15, 2024

Duplicate of #483

@quanah
Copy link
Contributor

quanah commented May 15, 2024

Also needs to handle modrdn IIRC

@fager
Copy link
Author

fager commented May 15, 2024

@quanah: Excuse me. I hadn't seen your issue. It's my first code contribution to larger projects and I was still focused on other things...

I first extended the LDIFParser method in the PR and added two new handlers (handler_add, handler_delete), which have to be implemented by a derived parser.

I also see the problem with the all_modify_changes list. Mainly because the order of operations in the LDIF file is relevant.

Currently I had no need for the modrdn operations. But of course they would also have to be taken into account for a complete implementation.

The PR is initially an implementation proposal. I'm open to suggestions for improvement...

@quanah
Copy link
Contributor

quanah commented May 17, 2024

@fager I was noting the duplicate bit for tracking purposes. :) Really great you took this work on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants