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

Allow for DN's to have {x} prefix on first RDN #5450

Merged
merged 5 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions changelogs/fragments/5450-allow-for-xordered-dns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ldap_attrs - allow for DNs to have ``{x}`` prefix on first RDN (https://github.com/ansible-collections/community.general/issues/977, https://github.com/ansible-collections/community.general/pull/5450).
21 changes: 20 additions & 1 deletion plugins/module_utils/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

try:
import ldap
import ldap.dn
import ldap.sasl

HAS_LDAP = True
Expand Down Expand Up @@ -48,7 +49,6 @@ def __init__(self, module):
self.module = module
self.bind_dn = self.module.params['bind_dn']
self.bind_pw = self.module.params['bind_pw']
self.dn = self.module.params['dn']
self.referrals_chasing = self.module.params['referrals_chasing']
self.server_uri = self.module.params['server_uri']
self.start_tls = self.module.params['start_tls']
Expand All @@ -58,13 +58,32 @@ def __init__(self, module):
# Establish connection
self.connection = self._connect_to_ldap()

# Try to find the X_ORDERed version of the DN
self.dn = self._find_dn()

def fail(self, msg, exn):
self.module.fail_json(
msg=msg,
details=to_native(exn),
exception=traceback.format_exc()
)

def _find_dn(self):
dn = self.module.params['dn']

explode_dn = ldap.dn.explode_dn(dn)

if len(explode_dn) > 1:
try:
dns = self.connection.search_s(','.join(explode_dn[1:]),
ldap.SCOPE_ONELEVEL, "(%s)" % explode_dn[0])
mrvanes marked this conversation as resolved.
Show resolved Hide resolved
if len(dns) == 1:
dn, dummy = dns[0]
except Exception:
pass

return dn

def _connect_to_ldap(self):
if not self.verify_cert:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
Expand Down