本文整理汇总了Python中ldap.set_option方法的典型用法代码示例。如果您正苦于以下问题:Python ldap.set_option方法的具体用法?Python ldap.set_option怎么用?Python ldap.set_option使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ldap
的用法示例。
在下文中一共展示了ldap.set_option方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _connect_to_ldap
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def _connect_to_ldap(self):
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
connection = ldap.initialize(self.server_uri)
if self.start_tls:
try:
connection.start_tls_s()
except ldap.LDAPError:
e = get_exception()
self.module.fail_json(msg="Cannot start TLS.", details=str(e))
try:
if self.bind_dn is not None:
connection.simple_bind_s(self.bind_dn, self.bind_pw)
else:
connection.sasl_interactive_bind_s('', ldap.sasl.external())
except ldap.LDAPError:
e = get_exception()
self.module.fail_json(
msg="Cannot bind to the server.", details=str(e))
return connection
示例2: _ldap_connect
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def _ldap_connect(self):
"""
Prepare ldap object for binding phase.
"""
try:
connection = ldap.initialize(self._ldap_uri)
connection.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
connection.set_option(ldap.OPT_REFERRALS,
int(self._chase_referrals))
if self._ldap_uri.startswith('ldaps://'):
# Require server certificate but ignore it's validity. (allow self-signed)
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
if self._use_tls:
# Require TLS connection.
ldap.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND)
# Require server certificate but ignore it's validity. (allow self-signed)
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
connection.start_tls_s()
LOG.debug('Connection now using TLS')
return connection
except ldap.LDAPError as e:
LOG.debug('(_ldap_connect) LDAP Error: %s : Type %s' % (str(e), type(e)))
return False
示例3: initialize
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def initialize(self):
"""Initialize a connection to the LDAP server.
:return: LDAP connection object.
"""
try:
conn = ldap.initialize('{0}://{1}:{2}'.format(
current_app.config['LDAP_SCHEMA'],
current_app.config['LDAP_HOST'],
current_app.config['LDAP_PORT']))
conn.set_option(ldap.OPT_NETWORK_TIMEOUT,
current_app.config['LDAP_TIMEOUT'])
conn = self._set_custom_options(conn)
conn.protocol_version = ldap.VERSION3
if current_app.config['LDAP_USE_TLS']:
conn.start_tls_s()
return conn
except ldap.LDAPError as e:
raise LDAPException(self.error(e.args))
示例4: _get_conn
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def _get_conn(self):
self._log.debug('Setting up LDAP connection')
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
try:
conn = ldap.initialize(self._url)
conn.set_option(ldap.OPT_NETWORK_TIMEOUT, 3)
conn.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
conn.simple_bind_s(self._binddn, self._bindpw)
except (
ldap.SERVER_DOWN,
ldap.NO_SUCH_OBJECT,
ldap.INVALID_CREDENTIALS
) as e:
if hasattr(e, 'message') and 'desc' in e.message:
msg = e.message['desc']
else:
msg = e.args[0]['desc']
self._log.debug('%s (%s)' % (msg, self._url))
return False
self._log.debug('LDAP connection established')
return conn
示例5: get_ldap
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def get_ldap(cls, global_options=None):
"""
Returns the configured ldap module.
"""
# Apply global LDAP options once
if not cls._ldap_configured and global_options is not None:
for opt, value in global_options.items():
ldap.set_option(opt, value)
cls._ldap_configured = True
return ldap
示例6: _ldap_get_con
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def _ldap_get_con():
if not _check_ldap_settings_present():
return None
con = ldap.initialize(fame_config.ldap_uri)
con.protocol_version = ldap.VERSION3
con.set_option(ldap.OPT_REFERRALS, 0)
return con
示例7: get_connection
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def get_connection(self, bind_dn=None, password=None):
"""Return an LDAP object."""
if not LdapFactory.is_enabled():
raise LdapNotEnabledException('Ldap has not been configured on this node')
ca_cert_exists = os.path.exists(self.ldap_ca_cert_path)
ldap_config = MCVirtConfig().get_config()['ldap']
ldap.set_option(
ldap.OPT_X_TLS_CACERTFILE,
self.ldap_ca_cert_path if ca_cert_exists else ''
)
if bind_dn is None and password is None:
bind_dn = ldap_config['bind_dn']
password = ldap_config['bind_pass']
try:
ldap_connection = ldap.initialize(uri=ldap_config['server_uri'])
try:
ldap_connection.bind_s(bind_dn, password)
except AttributeError:
# This is required for the mockldap server as part of the unit tests
ldap_connection.simple_bind_s(bind_dn, password)
except Exception:
raise LdapConnectionFailedException(
'Connection attempts to the LDAP server failed.'
)
return ldap_connection
示例8: ldap_conn
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def ldap_conn(ldap_server):
connect = ldap.initialize(ldap_server)
connect.set_option(ldap.OPT_REFERRALS, 0)
return connect
示例9: get_ldap_connection
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def get_ldap_connection(host):
conn = ldap.initialize("ldap://{}".format(host))
conn.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3)
conn.start_tls_s()
return conn
示例10: ldap_auth
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def ldap_auth(self, username, password):
if self.cert_path:
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, self.cert_path)
connection = ldap.initialize(self.ldap_url)
connection.set_option(ldap.OPT_REFERRALS, 0)
if not password:
return False
auth_user = username + self.user_suffix
try:
if self.bind_user:
# use search filter to find DN of username
connection.simple_bind_s(self.bind_user, self.bind_password)
sfilter = self.search_filter % username
result = connection.search_s(self.base_dn, ldap.SCOPE_SUBTREE, sfilter, ['dn'])
if len(result) < 1:
return False
auth_user = result[0][0]
connection.simple_bind_s(auth_user, password)
except ldap.INVALID_CREDENTIALS:
return False
except (ldap.SERVER_DOWN, ldap.INVALID_DN_SYNTAX) as err:
logger.warning("%s", err)
return None
return True
示例11: ldap_auth
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def ldap_auth(self, username, password):
if self.cert_path:
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, self.cert_path)
connection = ldap.initialize(self.ldap_url)
connection.set_option(ldap.OPT_REFERRALS, 0)
if not password:
return False
auth_user = username + self.user_suffix
try:
if self.bind_user:
# use search filter to find DN of username
connection.simple_bind_s(self.bind_user, self.bind_password)
sfilter = self.search_filter % username
result = connection.search_s(self.base_dn, ldap.SCOPE_SUBTREE, sfilter, ['dn'])
if len(result) < 1:
return False
auth_user = result[0][0]
connection.simple_bind_s(auth_user, password)
except ldap.INVALID_CREDENTIALS:
return False
except (ldap.SERVER_DOWN, ldap.INVALID_DN_SYNTAX) as err:
logger.warn("%s", err)
return None
return True
示例12: ldap_init_conn
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def ldap_init_conn(self):
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
conn = ldap.initialize(Setting().get('ldap_uri'))
conn.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
conn.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND)
conn.set_option(ldap.OPT_X_TLS_DEMAND, True)
conn.set_option(ldap.OPT_DEBUG_LEVEL, 255)
conn.protocol_version = ldap.VERSION3
return conn
示例13: initialise
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def initialise():
ldap.set_option(ldap.OPT_DEBUG_LEVEL, toolkit.config[u'ckanext.ldap.debug_level'])
示例14: init_ldap
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def init_ldap(ldap_server=server,
ldap_port=port,
ldap_basedn=base_dn,
ldap_mode=mode,
secure=secure,
cert_path=cert_path,
cert_file=cert_file):
"""
Inicialize ldap connection
"""
logger.info('[%s] Initialize ldap connection' % str(ldap_server))
if secure:
if not ldap_port:
ldap_port = 636
con = ldap.initialize(
"ldaps://" + ldap_server + ":" + str(ldap_port))
if cert_path:
con.set_option(ldap.OPT_X_TLS_CACERTDIR, cert_path)
if cert_file:
con.set_option(ldap.OPT_X_TLS_CACERTFILE, cert_file)
else:
if not ldap_port:
ldap_port = 389
con = ldap.initialize(
"ldap://" + ldap_server + ":" + str(ldap_port))
return con
示例15: _set_custom_options
# 需要导入模块: import ldap [as 别名]
# 或者: from ldap import set_option [as 别名]
def _set_custom_options(conn):
options = current_app.config['LDAP_CUSTOM_OPTIONS']
if options:
for k, v in options.items():
conn.set_option(k, v)
return conn