3

I need to get data from a server using LDAP. I'm using ldap3 and am getting all the attributes and responses that I need however, their stuck in the ldap.abstract.entry.entry class. I tried severall methods to get the data into a json or list form but it never worked the way I expected it to work.

For example I tried:

entry = conn.entries
current_data = (entry.entry_to_json())
print(type(current_data))
print(len(current_data))
>>> <class 'str'>
>>> 480

That however, caused all my data to be a single string of length 480 or more. I tried working with this but that just proved to be very difficult. So I tried the following to convert the string to a dict:

current_data = (entry[0].entry_to_json())
current_data = ast.literal_eval(current_data)
print(type(current_data))
print(len(current_data))
>>> <class 'dict'>
>>> 2

However, now I can't access all the different attributes seperately. My goal is to write all of the data that I get from the LDAP Query to a CSV so that other applications can work with it. But currently I'm stuck as I can't get the data into a JSON or CSV format.

How can I get and process the data from the conn.entries so that I can write each attribute to a seperate column in a CSV?

Best wishes,

C. Zerbe

Edit:

This is a sample of what I get when I use (entry.entry_to_json()):

{
    "attributes": {
        "dcxLDOUniqueCN": [
            "XXXX"
        ],
        "dcxPostalAddress": [
            "XXX"
        ],
        "dcxSupervisor": [
            "XXX"
        ],
        "mail": [
            "XXX"
        ],
        "mobile": [],
        "uid": [
            "XXX"
        ]
    },
    "dn": "XXX"
}
{
    "attributes": {
        "dcxLDOUniqueCN": [],
        "dcxPostalAddress": [
            "XXX"
        ],
        "dcxSupervisor": [
            "XXX"
        ],
        "mail": [],
        "mobile": [],
        "uid": [
            "XXX"
        ]
    },
    "dn": "XXX"
}
  • What would a sample of the data look like in `json` form? There are numerous questions transforming [json to csv](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – C.Nivs Aug 21 '19 at 13:34
  • The Problem I'm having is that the comand `current_data = (entry.entry_to_json())` doesn't actually give me a json class variable but instead gives me a string. – Christopher Zerbe Aug 21 '19 at 14:31
  • Right, and you are moving in the correct direction using `ast.literal_eval`. Once you have a functioning dictionary (there isn't really a json class object in python), there are ways to convert a dictionary to a csv. Can you add a sample of what your json looks like in the question? You can substitute potentially sensitive info (I'm guessing LDAP info may or may not have that) with dummy entries – C.Nivs Aug 21 '19 at 14:35

2 Answers2

6

I know it's late, but in case that someone else is looking for this...

From https://ldap3.readthedocs.io/en/latest/searches.html#entries

Attributes are stored in an internal dictionary with case insensitive access. You can even access the raw attribute with the entry_raw_attribute(attribute_name) to get an attribute raw value, or property entry_raw_attributes to get the whole raw attributes dictionary.

So entry.entry_raw_attributes will give you the dictionary you want.

Firula
  • 1,251
  • 10
  • 29
  • Just playing around with the ldap and ldap3 modules. I am exporting to LDIF, so that I'm fine but I was just curious. THX – runlevel0 Oct 27 '21 at 14:21
1

I needed to do something similar as I was working with the ldap3 library and returning responses in JSON form from a Flask API. I found this entry in my own search, so hopefully this may help others.

As you've already discovered, the entry_to_json() method converts the details in the ldap3.abstract.entry.Entry object to a JSON string. This can be combined with the json built-in library to be converted into a Python dictionary using json.loads().

Using your example in IDLE, but skipping the ldap3 components for brevity:

>>> my_entry = '''{
"attributes": {
    "dcxLDOUniqueCN": [
        "XXXX"
    ],
    "dcxPostalAddress": [
        "XXX"
    ],
    "dcxSupervisor": [
        "XXX"
    ],
    "mail": [
        "XXX"
    ],
    "mobile": [],
    "uid": [
        "XXX"
    ]
},
"dn": "XXX"
}'''
>>> print(type(my_entry))
<class 'str'>    
>>> import json
>>> my_entry_dict = json.loads(my_entry)
>>> print(type(my_entry_dict))
<class 'dict'>
print(my_entry_dict)
{'attributes': {'dcxLDOUniqueCN': ['XXXX'], 'dcxPostalAddress': ['XXX'], 'dcxSupervisor': ['XXX'], 'mail': ['XXX'], 'mobile': [], 'uid': ['XXX']}, 'dn': 'XXX'}    
gsfellis
  • 11
  • 3