Compare commits
2 commits
651e1a1a6c
...
cb4b8d6974
Author | SHA1 | Date | |
---|---|---|---|
|
cb4b8d6974 | ||
|
c643fd30ac |
3 changed files with 36 additions and 14 deletions
|
@ -60,12 +60,14 @@ class LdapServer:
|
||||||
con = 0
|
con = 0
|
||||||
|
|
||||||
def __init__(self, uri, dn=None, pwd=None, v2=None,
|
def __init__(self, uri, dn=None, pwd=None, v2=None,
|
||||||
raiseOnError=False, logger=False, checkCert=True):
|
raiseOnError=False, logger=False, checkCert=True,
|
||||||
|
disableReferral=False):
|
||||||
self.uri = uri
|
self.uri = uri
|
||||||
self.dn = dn
|
self.dn = dn
|
||||||
self.pwd = pwd
|
self.pwd = pwd
|
||||||
self.raiseOnError = raiseOnError
|
self.raiseOnError = raiseOnError
|
||||||
self.checkCert = checkCert
|
self.checkCert = checkCert
|
||||||
|
self.disableReferral = disableReferral
|
||||||
if v2:
|
if v2:
|
||||||
self.v2 = True
|
self.v2 = True
|
||||||
if logger:
|
if logger:
|
||||||
|
@ -85,6 +87,9 @@ class LdapServer:
|
||||||
if not self.checkCert:
|
if not self.checkCert:
|
||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
|
||||||
|
if self.disableReferral:
|
||||||
|
# pylint: disable=no-member
|
||||||
|
ldap.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
|
||||||
con = ldap.initialize(self.uri)
|
con = ldap.initialize(self.uri)
|
||||||
if self.v2:
|
if self.v2:
|
||||||
con.protocol_version = ldap.VERSION2 # pylint: disable=no-member
|
con.protocol_version = ldap.VERSION2 # pylint: disable=no-member
|
||||||
|
@ -468,6 +473,9 @@ class LdapClient:
|
||||||
section.add_option(
|
section.add_option(
|
||||||
BooleanOption, 'checkcert', default=True,
|
BooleanOption, 'checkcert', default=True,
|
||||||
comment='Check LDAP certificate')
|
comment='Check LDAP certificate')
|
||||||
|
section.add_option(
|
||||||
|
BooleanOption, 'disablereferral', default=False,
|
||||||
|
comment='Disable referral following')
|
||||||
|
|
||||||
return section
|
return section
|
||||||
|
|
||||||
|
@ -480,7 +488,9 @@ class LdapClient:
|
||||||
log.info("Connect to LDAP server %s as %s", uri, binddn if binddn else 'annonymous')
|
log.info("Connect to LDAP server %s as %s", uri, binddn if binddn else 'annonymous')
|
||||||
self._conn = LdapServer(
|
self._conn = LdapServer(
|
||||||
uri, dn=binddn, pwd=self._get_option('bindpwd'),
|
uri, dn=binddn, pwd=self._get_option('bindpwd'),
|
||||||
checkCert=self._get_option('checkcert'), raiseOnError=True
|
checkCert=self._get_option('checkcert'),
|
||||||
|
disableReferral=self._get_option('disablereferral'),
|
||||||
|
raiseOnError=True
|
||||||
)
|
)
|
||||||
# Reset cache
|
# Reset cache
|
||||||
self._cached_objects = {}
|
self._cached_objects = {}
|
||||||
|
|
|
@ -125,7 +125,6 @@ class OracleDB:
|
||||||
log.debug("Just-try mode : do not really execute SQL query '%s'", sql)
|
log.debug("Just-try mode : do not really execute SQL query '%s'", sql)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
cursor = self._conn.cursor()
|
|
||||||
try:
|
try:
|
||||||
log.debug(
|
log.debug(
|
||||||
'Run SQL query "%s" %s',
|
'Run SQL query "%s" %s',
|
||||||
|
@ -135,10 +134,11 @@ class OracleDB:
|
||||||
for key, value in params.items()
|
for key, value in params.items()
|
||||||
]) if params else "without params"
|
]) if params else "without params"
|
||||||
)
|
)
|
||||||
if isinstance(params, dict):
|
with self._conn.cursor() as cursor:
|
||||||
cursor.execute(sql, **params)
|
if isinstance(params, dict):
|
||||||
else:
|
cursor.execute(sql, **params)
|
||||||
cursor.execute(sql)
|
else:
|
||||||
|
cursor.execute(sql)
|
||||||
self._conn.commit()
|
self._conn.commit()
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -164,7 +164,6 @@ class OracleDB:
|
||||||
:return: List of selected rows as dict on success, False otherwise
|
:return: List of selected rows as dict on success, False otherwise
|
||||||
:rtype: list, bool
|
:rtype: list, bool
|
||||||
"""
|
"""
|
||||||
cursor = self._conn.cursor()
|
|
||||||
try:
|
try:
|
||||||
log.debug(
|
log.debug(
|
||||||
'Run SQL SELECT query "%s" %s',
|
'Run SQL SELECT query "%s" %s',
|
||||||
|
@ -174,12 +173,15 @@ class OracleDB:
|
||||||
for key, value in params.items()
|
for key, value in params.items()
|
||||||
]) if params else "without params"
|
]) if params else "without params"
|
||||||
)
|
)
|
||||||
if isinstance(params, dict):
|
with self._conn.cursor() as cursor:
|
||||||
cursor.execute(sql, **params)
|
if isinstance(params, dict):
|
||||||
else:
|
cursor.execute(sql, **params)
|
||||||
cursor.execute(sql)
|
else:
|
||||||
cursor.rowfactory = lambda *args: dict(zip([d[0] for d in cursor.description], args))
|
cursor.execute(sql)
|
||||||
results = cursor.fetchall()
|
cursor.rowfactory = lambda *args: dict(
|
||||||
|
zip([d[0] for d in cursor.description], args)
|
||||||
|
)
|
||||||
|
results = cursor.fetchall()
|
||||||
return results
|
return results
|
||||||
except Exception:
|
except Exception:
|
||||||
log.error(
|
log.error(
|
||||||
|
|
|
@ -15,8 +15,10 @@ class FakeCXOracleCursor:
|
||||||
self.expected_return = expected_return
|
self.expected_return = expected_return
|
||||||
self.expected_just_try = expected_just_try
|
self.expected_just_try = expected_just_try
|
||||||
self.expected_exception = expected_exception
|
self.expected_exception = expected_exception
|
||||||
|
self.opened = True
|
||||||
|
|
||||||
def execute(self, sql, **params):
|
def execute(self, sql, **params):
|
||||||
|
assert self.opened
|
||||||
if self.expected_exception:
|
if self.expected_exception:
|
||||||
raise Exception("%s.execute(%s, %s): expected exception" % (self, sql, params))
|
raise Exception("%s.execute(%s, %s): expected exception" % (self, sql, params))
|
||||||
if self.expected_just_try and not sql.lower().startswith('select '):
|
if self.expected_just_try and not sql.lower().startswith('select '):
|
||||||
|
@ -26,8 +28,16 @@ class FakeCXOracleCursor:
|
||||||
return self.expected_return
|
return self.expected_return
|
||||||
|
|
||||||
def fetchall(self):
|
def fetchall(self):
|
||||||
|
assert self.opened
|
||||||
return self.expected_return
|
return self.expected_return
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.opened = True
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.opened = False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "FakeCXOracleCursor(%s, %s, %s, %s)" % (
|
return "FakeCXOracleCursor(%s, %s, %s, %s)" % (
|
||||||
self.expected_sql, self.expected_params,
|
self.expected_sql, self.expected_params,
|
||||||
|
|
Loading…
Reference in a new issue