Skip to content
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

ldapsearch command #4

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.
The format is based on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`_,
and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`_.

[0.1.1] - 2021-11-24
====================

Added
*****

- Added `ldapsearch` command. :pr:`4`

[0.1.0] - 2021-04-02
====================

Expand Down
22 changes: 22 additions & 0 deletions slapd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def _find_commands(self):
self.PATH_LDAPADD = self._find_command("ldapadd")
self.PATH_LDAPDELETE = self._find_command("ldapdelete")
self.PATH_LDAPMODIFY = self._find_command("ldapmodify")
self.PATH_LDAPSEARCH = self._find_command("ldapsearch")
self.PATH_LDAPWHOAMI = self._find_command("ldapwhoami")
self.PATH_SLAPADD = self._find_command("slapadd")
self.PATH_SLAPCAT = self._find_command("slapcat")
Expand Down Expand Up @@ -558,6 +559,27 @@ def ldapdelete(self, dn, recursive=False, extra_args=None, expected=0):
self.PATH_LDAPDELETE, extra_args=extra_args, expected=expected
)

def ldapsearch(self, filter, searchbase=None, extra_args=None, expected=0):
"""
Runs search on this slapd instance

:param filter: The search filter.
:param base: The starting point for the search.
:param extra_args: Extra argument to pass to *ldapdelete*.
:param expected: Expected return code. Defaults to `0`.
:type expected: An integer or a list of integers

:return: A :class:`subprocess.CompletedProcess` with the *ldapdelete* execution data.
"""
if extra_args is None:
extra_args = []
if searchbase:
extra_args.extend(["-b", searchbase])
extra_args.append(filter)
return self._cli_popen(
self.PATH_LDAPSEARCH, extra_args=extra_args, expected=expected
)

def slapadd(self, ldif, extra_args=None, expected=0):
"""
Runs slapadd on this slapd instance, passing it the ldif content
Expand Down
4 changes: 4 additions & 0 deletions tests/test_slapdobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def test_commands():
"dn:cn=manager,dc=slapd-test,dc=python-ldap,dc=org\n"
== server.ldapwhoami().stdout.decode("utf-8")
)
server.ldapsearch("ou=home", "dc=slapd-test,dc=python-ldap,dc=org", expected=32)

ldif = (
"dn: dc=slapd-test,dc=python-ldap,dc=org\n"
"objectClass: dcObject\n"
Expand All @@ -55,6 +57,8 @@ def test_commands():
in server.slapcat().stdout.decode("utf-8")
)

server.ldapsearch("ou=home", "dc=slapd-test,dc=python-ldap,dc=org")

ldif = (
"dn: ou=home,dc=slapd-test,dc=python-ldap,dc=org\n"
"changetype: modify\n"
Expand Down