mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-04-24 10:03:36 +02:00
ldap.Connection: Support uri config option
Currently the configuration passed to the Connection constructor needs to contain an ldap_uri entry. Add "uri" as alias, because ldap_uri for the LDAP config in many contexts represents a tautology and is left out. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
c153448608
commit
988d420e44
1 changed files with 19 additions and 4 deletions
|
|
@ -24,7 +24,12 @@ class Config:
|
||||||
@property
|
@property
|
||||||
def ldap_uri(self):
|
def ldap_uri(self):
|
||||||
if self.__ldap_uri is None:
|
if self.__ldap_uri is None:
|
||||||
self.__ldap_uri = self.__get('ldap_uri', default='ldap://ldap.janware.com')
|
for key in ['ldap_uri', 'uri']:
|
||||||
|
self.__ldap_uri = self.__get(key, default=None)
|
||||||
|
if self.__ldap_uri is not None:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
self.__ldap_uri = 'ldap://ldap.janware.com'
|
||||||
return self.__ldap_uri
|
return self.__ldap_uri
|
||||||
@ldap_uri.setter
|
@ldap_uri.setter
|
||||||
def ldap_uri(self, rhs):
|
def ldap_uri(self, rhs):
|
||||||
|
|
@ -74,13 +79,23 @@ class Connection: # export
|
||||||
May = auto()
|
May = auto()
|
||||||
|
|
||||||
def __init__(self, conf: Config|BaseConfig|None=None, backtrace=False):
|
def __init__(self, conf: Config|BaseConfig|None=None, backtrace=False):
|
||||||
|
uri: str|None = None
|
||||||
c = conf if isinstance(conf, Config) else Config(conf)
|
c = conf if isinstance(conf, Config) else Config(conf)
|
||||||
ret = ldap.initialize(c.ldap_uri)
|
try:
|
||||||
|
uri = c.ldap_uri
|
||||||
|
except:
|
||||||
|
uri = c.uri
|
||||||
|
try:
|
||||||
|
ret = ldap.initialize(uri)
|
||||||
ret.start_tls_s()
|
ret.start_tls_s()
|
||||||
|
except Exception as e:
|
||||||
|
slog(ERR, f'Failed to initialize LDAP connection to "{uri}" ({str(e)})')
|
||||||
|
raise
|
||||||
try:
|
try:
|
||||||
rr = ret.bind_s(c.bind_dn, c.bind_pw) # method)
|
rr = ret.bind_s(c.bind_dn, c.bind_pw) # method)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f'Failed to bind to {c.ldap_uri} with dn {c.bind_dn} ({e})')
|
slog(ERR, f'Failed to bind to "{uri}" with dn "{c.bind_dn}" ({str(e)})')
|
||||||
|
raise
|
||||||
self.__ldap = ret
|
self.__ldap = ret
|
||||||
self.__backtrace = backtrace
|
self.__backtrace = backtrace
|
||||||
self.__object_classes_by_oid: dict[str, ObjectClass]|None = None
|
self.__object_classes_by_oid: dict[str, ObjectClass]|None = None
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue