Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

restartable.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. """
  2. """
  3. # Created on 2014.03.04
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from sys import exc_info
  25. from time import sleep
  26. import socket
  27. from datetime import datetime
  28. from .. import get_config_parameter
  29. from .sync import SyncStrategy
  30. from ..core.exceptions import LDAPSocketOpenError, LDAPOperationResult, LDAPMaximumRetriesError
  31. from ..utils.log import log, log_enabled, ERROR, BASIC
  32. # noinspection PyBroadException,PyProtectedMember
  33. class RestartableStrategy(SyncStrategy):
  34. def __init__(self, ldap_connection):
  35. SyncStrategy.__init__(self, ldap_connection)
  36. self.sync = True
  37. self.no_real_dsa = False
  38. self.pooled = False
  39. self.can_stream = False
  40. self.restartable_sleep_time = get_config_parameter('RESTARTABLE_SLEEPTIME')
  41. self.restartable_tries = get_config_parameter('RESTARTABLE_TRIES')
  42. self._restarting = False
  43. self._last_bind_controls = None
  44. self._current_message_type = None
  45. self._current_request = None
  46. self._current_controls = None
  47. self._restart_tls = None
  48. self.exception_history = []
  49. def open(self, reset_usage=False, read_server_info=True):
  50. SyncStrategy.open(self, reset_usage, read_server_info)
  51. def _open_socket(self, address, use_ssl=False, unix_socket=False):
  52. """
  53. Try to open and connect a socket to a Server
  54. raise LDAPExceptionError if unable to open or connect socket
  55. if connection is restartable tries for the number of restarting requested or forever
  56. """
  57. try:
  58. SyncStrategy._open_socket(self, address, use_ssl, unix_socket) # try to open socket using SyncWait
  59. self._reset_exception_history()
  60. return
  61. except Exception as e: # machinery for restartable connection
  62. if log_enabled(ERROR):
  63. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  64. self._add_exception_to_history()
  65. if not self._restarting: # if not already performing a restart
  66. self._restarting = True
  67. counter = self.restartable_tries
  68. while counter > 0: # includes restartable_tries == True
  69. if log_enabled(BASIC):
  70. log(BASIC, 'try #%d to open Restartable connection <%s>', self.restartable_tries - counter, self.connection)
  71. sleep(self.restartable_sleep_time)
  72. if not self.connection.closed:
  73. try: # resetting connection
  74. self.connection.unbind()
  75. except (socket.error, LDAPSocketOpenError): # don't trace catch socket errors because socket could already be closed
  76. pass
  77. except Exception:
  78. self._add_exception_to_history()
  79. try: # reissuing same operation
  80. if self.connection.server_pool:
  81. new_server = self.connection.server_pool.get_server(self.connection) # get a server from the server_pool if available
  82. if self.connection.server != new_server:
  83. self.connection.server = new_server
  84. if self.connection.usage:
  85. self.connection._usage.servers_from_pool += 1
  86. SyncStrategy._open_socket(self, address, use_ssl, unix_socket) # calls super (not restartable) _open_socket()
  87. if self.connection.usage:
  88. self.connection._usage.restartable_successes += 1
  89. self.connection.closed = False
  90. self._restarting = False
  91. self._reset_exception_history()
  92. return
  93. except Exception as e:
  94. if log_enabled(ERROR):
  95. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  96. self._add_exception_to_history()
  97. if self.connection.usage:
  98. self.connection._usage.restartable_failures += 1
  99. if not isinstance(self.restartable_tries, bool):
  100. counter -= 1
  101. self._restarting = False
  102. self.connection.last_error = 'restartable connection strategy failed while opening socket'
  103. if log_enabled(ERROR):
  104. log(ERROR, '<%s> for <%s>', self.connection.last_error, self.connection)
  105. raise LDAPMaximumRetriesError(self.connection.last_error, self.exception_history, self.restartable_tries)
  106. def send(self, message_type, request, controls=None):
  107. self._current_message_type = message_type
  108. self._current_request = request
  109. self._current_controls = controls
  110. if not self._restart_tls: # RFCs doesn't define how to stop tls once started
  111. self._restart_tls = self.connection.tls_started
  112. if message_type == 'bindRequest': # stores controls used in bind operation to be used again when restarting the connection
  113. self._last_bind_controls = controls
  114. try:
  115. message_id = SyncStrategy.send(self, message_type, request, controls) # tries to send using SyncWait
  116. self._reset_exception_history()
  117. return message_id
  118. except Exception as e:
  119. if log_enabled(ERROR):
  120. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  121. self._add_exception_to_history()
  122. if not self._restarting: # machinery for restartable connection
  123. self._restarting = True
  124. counter = self.restartable_tries
  125. while counter > 0:
  126. if log_enabled(BASIC):
  127. log(BASIC, 'try #%d to send in Restartable connection <%s>', self.restartable_tries - counter, self.connection)
  128. sleep(self.restartable_sleep_time)
  129. if not self.connection.closed:
  130. try: # resetting connection
  131. self.connection.unbind()
  132. except (socket.error, LDAPSocketOpenError): # don't trace socket errors because socket could already be closed
  133. pass
  134. except Exception as e:
  135. if log_enabled(ERROR):
  136. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  137. self._add_exception_to_history()
  138. failure = False
  139. try: # reopening connection
  140. self.connection.open(reset_usage=False, read_server_info=False)
  141. if self._restart_tls: # restart tls if start_tls was previously used
  142. self.connection.start_tls(read_server_info=False)
  143. if message_type != 'bindRequest':
  144. self.connection.bind(read_server_info=False, controls=self._last_bind_controls) # binds with previously used controls unless the request is already a bindRequest
  145. if not self.connection.server.schema and not self.connection.server.info:
  146. self.connection.refresh_server_info()
  147. else:
  148. self.connection._fire_deferred(read_info=False) # in case of lazy connection, not open by the refresh_server_info
  149. except Exception as e:
  150. if log_enabled(ERROR):
  151. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  152. self._add_exception_to_history()
  153. failure = True
  154. if not failure:
  155. try: # reissuing same operation
  156. ret_value = self.connection.send(message_type, request, controls)
  157. if self.connection.usage:
  158. self.connection._usage.restartable_successes += 1
  159. self._restarting = False
  160. self._reset_exception_history()
  161. return ret_value # successful send
  162. except Exception as e:
  163. if log_enabled(ERROR):
  164. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  165. self._add_exception_to_history()
  166. failure = True
  167. if failure and self.connection.usage:
  168. self.connection._usage.restartable_failures += 1
  169. if not isinstance(self.restartable_tries, bool):
  170. counter -= 1
  171. self._restarting = False
  172. self.connection.last_error = 'restartable connection failed to send'
  173. if log_enabled(ERROR):
  174. log(ERROR, '<%s> for <%s>', self.connection.last_error, self.connection)
  175. raise LDAPMaximumRetriesError(self.connection.last_error, self.exception_history, self.restartable_tries)
  176. def post_send_single_response(self, message_id):
  177. try:
  178. ret_value = SyncStrategy.post_send_single_response(self, message_id)
  179. self._reset_exception_history()
  180. return ret_value
  181. except Exception as e:
  182. if log_enabled(ERROR):
  183. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  184. self._add_exception_to_history()
  185. # if an LDAPExceptionError is raised then resend the request
  186. try:
  187. ret_value = SyncStrategy.post_send_single_response(self, self.send(self._current_message_type, self._current_request, self._current_controls))
  188. self._reset_exception_history()
  189. return ret_value
  190. except Exception as e:
  191. if log_enabled(ERROR):
  192. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  193. self._add_exception_to_history()
  194. exc = e
  195. if exc:
  196. if not isinstance(exc, LDAPOperationResult):
  197. self.connection.last_error = 'restartable connection strategy failed in post_send_single_response'
  198. if log_enabled(ERROR):
  199. log(ERROR, '<%s> for <%s>', self.connection.last_error, self.connection)
  200. raise exc
  201. def post_send_search(self, message_id):
  202. try:
  203. ret_value = SyncStrategy.post_send_search(self, message_id)
  204. self._reset_exception_history()
  205. return ret_value
  206. except Exception as e:
  207. if log_enabled(ERROR):
  208. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  209. self._add_exception_to_history()
  210. # if an LDAPExceptionError is raised then resend the request
  211. try:
  212. ret_value = SyncStrategy.post_send_search(self, self.connection.send(self._current_message_type, self._current_request, self._current_controls))
  213. self._reset_exception_history()
  214. return ret_value
  215. except Exception as e:
  216. if log_enabled(ERROR):
  217. log(ERROR, '<%s> while restarting <%s>', e, self.connection)
  218. self._add_exception_to_history()
  219. exc = e
  220. if exc:
  221. if not isinstance(exc, LDAPOperationResult):
  222. self.connection.last_error = exc.args
  223. if log_enabled(ERROR):
  224. log(ERROR, '<%s> for <%s>', self.connection.last_error, self.connection)
  225. raise exc
  226. def _add_exception_to_history(self):
  227. if not isinstance(self.restartable_tries, bool): # doesn't accumulate when restarting forever
  228. if not isinstance(exc_info()[1], LDAPMaximumRetriesError): # doesn't add the LDAPMaximumRetriesError exception
  229. self.exception_history.append((datetime.now(), exc_info()[0], exc_info()[1]))
  230. def _reset_exception_history(self):
  231. if self.exception_history:
  232. self.exception_history = []
  233. def get_stream(self):
  234. raise NotImplementedError
  235. def set_stream(self, value):
  236. raise NotImplementedError