Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check(self, instance):
url, username, password, ssl_params, custom_queries, tags = self._get_instance_params(instance)
server = ldap3.Server(url, tls=self._get_tls_object(ssl_params))
conn = ldap3.Connection(server, username, password, collect_usage=True)
# Try binding to the server
try:
res = conn.bind()
if not res:
raise ldap3.core.exceptions.LDAPBindError("Error binding to server: {}".format(conn.result))
except ldap3.core.exceptions.LDAPExceptionError as e:
self.log.exception("Could not connect to server at %s: %s", url, e)
self.service_check(self.SERVICE_CHECK_CONNECT, self.CRITICAL, tags=tags)
raise
self.service_check(self.SERVICE_CHECK_CONNECT, self.OK, tags=tags)
bind_time = self._get_query_time(conn)
self.gauge("{}.bind_time".format(self.METRIC_PREFIX), bind_time, tags=tags)
try:
# Search Monitor database to get all metrics
conn.search(self.SEARCH_BASE, self.SEARCH_FILTER, attributes=self.ATTRS)
self._collect_monitor_metrics(conn, tags)
# Get additional custom metrics
self._perform_custom_queries(conn, custom_queries, tags, instance)
finally:
class LDAPInvalidTlsSpecificationError(LDAPExceptionError):
pass
class LDAPInvalidHashAlgorithmError(LDAPExceptionError, ValueError):
pass
# connection exceptions
class LDAPBindError(LDAPExceptionError):
pass
class LDAPInvalidServerError(LDAPExceptionError):
pass
class LDAPSASLMechanismNotSupportedError(LDAPExceptionError):
pass
class LDAPConnectionIsReadOnlyError(LDAPExceptionError):
pass
class LDAPChangeError(LDAPExceptionError, ValueError):
pass
class LDAPServerPoolError(LDAPExceptionError):
class LDAPReferralError(LDAPCommunicationError):
pass
# pooling exceptions
class LDAPConnectionPoolNameIsMandatoryError(LDAPExceptionError):
pass
class LDAPConnectionPoolNotStartedError(LDAPExceptionError):
pass
# restartable strategy
class LDAPMaximumRetriesError(LDAPExceptionError):
def __str__(self):
s = []
if self.args:
if isinstance(self.args, tuple):
if len(self.args) > 0:
s.append('LDAPMaximumRetriesError: ' + str(self.args[0]))
if len(self.args) > 1:
s.append('Exception history:')
prev_exc = ''
for i, exc in enumerate(self.args[1]): # args[1] contains exception history
# if str(exc[1]) != prev_exc:
# s.append((str(i).rjust(5) + ' ' + str(exc[0]) + ': ' + str(exc[1]) + ' - ' + str(exc[2])))
# prev_exc = str(exc[1])
if str(exc) != prev_exc:
s.append((str(i).rjust(5) + ' ' + str(type(exc)) + ': ' + str(exc)))
prev_exc = str(exc)
class LDAPInvalidDnError(LDAPExceptionError):
pass
class LDAPResponseTimeoutError(LDAPExceptionError):
pass
class LDAPTransactionError(LDAPExceptionError):
pass
# communication exceptions
class LDAPCommunicationError(LDAPExceptionError):
pass
class LDAPSocketOpenError(LDAPCommunicationError):
pass
class LDAPSocketCloseError(LDAPCommunicationError):
pass
class LDAPSocketReceiveError(LDAPCommunicationError, socket.error):
pass
class LDAPSocketSendError(LDAPCommunicationError, socket.error):
pass
class LDAPControlError(LDAPExceptionError, ValueError):
pass
class LDAPExtensionError(LDAPExceptionError, ValueError):
pass
class LDAPLDIFError(LDAPExceptionError):
pass
class LDAPSchemaError(LDAPExceptionError):
pass
class LDAPSASLPrepError(LDAPExceptionError):
pass
class LDAPSASLBindInProgressError(LDAPExceptionError):
pass
class LDAPMetricsError(LDAPExceptionError):
pass
class LDAPObjectClassError(LDAPExceptionError):
class LDAPCursorError(LDAPExceptionError):
pass
class LDAPObjectDereferenceError(LDAPExceptionError):
pass
# security exceptions
class LDAPSSLNotSupportedError(LDAPExceptionError, ImportError):
pass
class LDAPInvalidTlsSpecificationError(LDAPExceptionError):
pass
class LDAPInvalidHashAlgorithmError(LDAPExceptionError, ValueError):
pass
# connection exceptions
class LDAPBindError(LDAPExceptionError):
pass
class LDAPInvalidServerError(LDAPExceptionError):
pass
class LDAPSASLMechanismNotSupportedError(LDAPExceptionError):
pass
pass
class LDAPInvalidFilterError(LDAPExceptionError):
pass
class LDAPInvalidScopeError(LDAPExceptionError, ValueError):
pass
class LDAPInvalidDereferenceAliasesError(LDAPExceptionError, ValueError):
pass
class LDAPInvalidValueError(LDAPExceptionError, ValueError):
pass
class LDAPControlError(LDAPExceptionError, ValueError):
pass
class LDAPExtensionError(LDAPExceptionError, ValueError):
pass
class LDAPLDIFError(LDAPExceptionError):
pass
class LDAPSchemaError(LDAPExceptionError):
class LDAPInvalidHashAlgorithmError(LDAPExceptionError, ValueError):
pass
# connection exceptions
class LDAPBindError(LDAPExceptionError):
pass
class LDAPInvalidServerError(LDAPExceptionError):
pass
class LDAPSASLMechanismNotSupportedError(LDAPExceptionError):
pass
class LDAPConnectionIsReadOnlyError(LDAPExceptionError):
pass
class LDAPChangeError(LDAPExceptionError, ValueError):
pass
class LDAPServerPoolError(LDAPExceptionError):
pass
class LDAPServerPoolExhaustedError(LDAPExceptionError):
pass
class LDAPSchemaError(LDAPExceptionError):
pass
class LDAPSASLPrepError(LDAPExceptionError):
pass
class LDAPSASLBindInProgressError(LDAPExceptionError):
pass
class LDAPMetricsError(LDAPExceptionError):
pass
class LDAPObjectClassError(LDAPExceptionError):
pass
class LDAPInvalidDnError(LDAPExceptionError):
pass
class LDAPResponseTimeoutError(LDAPExceptionError):
pass
class LDAPTransactionError(LDAPExceptionError):
counter, message_type, request, controls = pool.request_queue.get(block=True, timeout=self.master_connection.strategy.pool.keepalive)
except Empty: # issue an Abandon(0) operation to keep the connection live - Abandon(0) is a harmless operation
if not self.worker.connection.closed:
self.worker.connection.abandon(0)
continue
with self.worker.worker_lock:
self.worker.busy = True
if counter == TERMINATE_REUSABLE:
terminate = True
if self.worker.connection.bound:
try:
self.worker.connection.unbind()
if log_enabled(BASIC):
log(BASIC, 'thread terminated')
except LDAPExceptionError:
pass
else:
if (datetime.now() - self.worker.creation_time).seconds >= self.master_connection.strategy.pool.lifetime: # destroy and create a new connection
try:
self.worker.connection.unbind()
except LDAPExceptionError:
pass
self.worker.new_connection()
if log_enabled(BASIC):
log(BASIC, 'thread respawn')
if message_type not in ['bindRequest', 'unbindRequest']:
try:
if pool.open_pool and self.worker.connection.closed:
self.worker.connection.open(read_server_info=False)
if pool.tls_pool and not self.worker.connection.tls_started:
self.worker.connection.start_tls(read_server_info=False)