From eeadb2ce178320bc2832e17e2cbaf68f35c9b386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Sat, 25 May 2019 23:50:59 +0200 Subject: [PATCH 1/3] Create a LDIF parser --- doc/ldap.rst | 14 ++++++++++++++ moulinette/utils/filesystem.py | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/doc/ldap.rst b/doc/ldap.rst index ea459732..221b55da 100644 --- a/doc/ldap.rst +++ b/doc/ldap.rst @@ -372,3 +372,17 @@ Here how it looks like for domain and user: (:file:`None` ?) so you need to check it returns code. .. automethod:: moulinette.authenticators.ldap.Authenticator.remove + +Reading LDIF file +================= + +Reading parsing a ldif to be able to insert in the LDAP database is really easy. Here is how to get the content of a LDIF file + +:: + + from moulinette.utils.filesystem import read_ldif + + my_reslut = read_ldif("your_file.ldif") + + +Note that the main difference of what the auth object return with the search method is that this function return a 2-tuples with the "dn" and the LDAP entry. diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index 521f93c5..e981d94f 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -77,6 +77,31 @@ def read_yaml(file_path): return loaded_yaml +def read_ldif(file_path): + """ + Safely read a LDIF file and create struct in the same style than + what return the auth objet with the seach method + The main difference with the auth object is that this function return a 2-tuples + with the "dn" and the LDAP entry. + + Keyword argument: + file_path -- Path to the ldif file + """ + from ldif import LDIFRecordList + + # Open file and read content + try: + with open(file_path, "r") as f: + parser = LDIFRecordList(f) + parser.parse() + except IOError as e: + raise MoulinetteError('cannot_open_file', file=file_path, error=str(e)) + except Exception as e: + raise MoulinetteError('error_reading_file', file=file_path, error=str(e)) + + return parser.all_records + + def write_to_file(file_path, data, file_mode="w"): """ Write a single string or a list of string to a text file. From a951b6f9a235fc82159bcdaff54fa2d38ca29f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Sun, 26 May 2019 00:45:44 +0200 Subject: [PATCH 2/3] Add the possibility to filter the returned entry --- moulinette/utils/filesystem.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index e981d94f..941e5117 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -77,7 +77,7 @@ def read_yaml(file_path): return loaded_yaml -def read_ldif(file_path): +def read_ldif(file_path, filtred_entries=[]): """ Safely read a LDIF file and create struct in the same style than what return the auth objet with the seach method @@ -85,14 +85,22 @@ def read_ldif(file_path): with the "dn" and the LDAP entry. Keyword argument: - file_path -- Path to the ldif file + file_path -- Path to the ldif file + filtred_entries -- The entries to don't include in the result """ from ldif import LDIFRecordList + class LDIFPar(LDIFRecordList): + def handle(self,dn,entry): + for e in filtred_entries: + if e in entry: + entry.pop(e) + self.all_records.append((dn,entry)) + # Open file and read content try: with open(file_path, "r") as f: - parser = LDIFRecordList(f) + parser = LDIFPar(f) parser.parse() except IOError as e: raise MoulinetteError('cannot_open_file', file=file_path, error=str(e)) From e91ab96cb7c189e829daf4b4668b5d5d4530d4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Tue, 28 May 2019 22:13:26 +0200 Subject: [PATCH 3/3] Fix pep --- moulinette/utils/filesystem.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index 941e5117..02066757 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -91,11 +91,11 @@ def read_ldif(file_path, filtred_entries=[]): from ldif import LDIFRecordList class LDIFPar(LDIFRecordList): - def handle(self,dn,entry): + def handle(self, dn, entry): for e in filtred_entries: if e in entry: entry.pop(e) - self.all_records.append((dn,entry)) + self.all_records.append((dn, entry)) # Open file and read content try: