client_options.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright 2014-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. """Tools to parse mongo client options."""
  15. from bson.codec_options import _parse_codec_options
  16. from pymongo.auth import _build_credentials_tuple
  17. from pymongo.common import validate_boolean
  18. from pymongo import common
  19. from pymongo.compression_support import CompressionSettings
  20. from pymongo.errors import ConfigurationError
  21. from pymongo.monitoring import _EventListeners
  22. from pymongo.pool import PoolOptions
  23. from pymongo.read_concern import ReadConcern
  24. from pymongo.read_preferences import (make_read_preference,
  25. read_pref_mode_from_name)
  26. from pymongo.server_selectors import any_server_selector
  27. from pymongo.ssl_support import get_ssl_context
  28. from pymongo.write_concern import WriteConcern
  29. def _parse_credentials(username, password, database, options):
  30. """Parse authentication credentials."""
  31. mechanism = options.get('authmechanism', 'DEFAULT' if username else None)
  32. source = options.get('authsource')
  33. if username or mechanism:
  34. return _build_credentials_tuple(
  35. mechanism, source, username, password, options, database)
  36. return None
  37. def _parse_read_preference(options):
  38. """Parse read preference options."""
  39. if 'read_preference' in options:
  40. return options['read_preference']
  41. name = options.get('readpreference', 'primary')
  42. mode = read_pref_mode_from_name(name)
  43. tags = options.get('readpreferencetags')
  44. max_staleness = options.get('maxstalenessseconds', -1)
  45. return make_read_preference(mode, tags, max_staleness)
  46. def _parse_write_concern(options):
  47. """Parse write concern options."""
  48. concern = options.get('w')
  49. wtimeout = options.get('wtimeoutms')
  50. j = options.get('journal')
  51. fsync = options.get('fsync')
  52. return WriteConcern(concern, wtimeout, j, fsync)
  53. def _parse_read_concern(options):
  54. """Parse read concern options."""
  55. concern = options.get('readconcernlevel')
  56. return ReadConcern(concern)
  57. def _parse_ssl_options(options):
  58. """Parse ssl options."""
  59. use_ssl = options.get('ssl')
  60. if use_ssl is not None:
  61. validate_boolean('ssl', use_ssl)
  62. certfile = options.get('ssl_certfile')
  63. keyfile = options.get('ssl_keyfile')
  64. passphrase = options.get('ssl_pem_passphrase')
  65. ca_certs = options.get('ssl_ca_certs')
  66. cert_reqs = options.get('ssl_cert_reqs')
  67. match_hostname = options.get('ssl_match_hostname', True)
  68. crlfile = options.get('ssl_crlfile')
  69. check_ocsp_endpoint = options.get('ssl_check_ocsp_endpoint', True)
  70. ssl_kwarg_keys = [k for k in options
  71. if k.startswith('ssl_') and options[k]]
  72. if use_ssl is False and ssl_kwarg_keys:
  73. raise ConfigurationError("ssl has not been enabled but the "
  74. "following ssl parameters have been set: "
  75. "%s. Please set `ssl=True` or remove."
  76. % ', '.join(ssl_kwarg_keys))
  77. if ssl_kwarg_keys and use_ssl is None:
  78. # ssl options imply ssl = True
  79. use_ssl = True
  80. if use_ssl is True:
  81. ctx = get_ssl_context(
  82. certfile,
  83. keyfile,
  84. passphrase,
  85. ca_certs,
  86. cert_reqs,
  87. crlfile,
  88. match_hostname,
  89. check_ocsp_endpoint)
  90. return ctx, match_hostname
  91. return None, match_hostname
  92. def _parse_pool_options(options):
  93. """Parse connection pool options."""
  94. max_pool_size = options.get('maxpoolsize', common.MAX_POOL_SIZE)
  95. min_pool_size = options.get('minpoolsize', common.MIN_POOL_SIZE)
  96. max_idle_time_seconds = options.get(
  97. 'maxidletimems', common.MAX_IDLE_TIME_SEC)
  98. if max_pool_size is not None and min_pool_size > max_pool_size:
  99. raise ValueError("minPoolSize must be smaller or equal to maxPoolSize")
  100. connect_timeout = options.get('connecttimeoutms', common.CONNECT_TIMEOUT)
  101. socket_keepalive = options.get('socketkeepalive', True)
  102. socket_timeout = options.get('sockettimeoutms')
  103. wait_queue_timeout = options.get(
  104. 'waitqueuetimeoutms', common.WAIT_QUEUE_TIMEOUT)
  105. wait_queue_multiple = options.get('waitqueuemultiple')
  106. event_listeners = options.get('event_listeners')
  107. appname = options.get('appname')
  108. driver = options.get('driver')
  109. server_api = options.get('server_api')
  110. compression_settings = CompressionSettings(
  111. options.get('compressors', []),
  112. options.get('zlibcompressionlevel', -1))
  113. ssl_context, ssl_match_hostname = _parse_ssl_options(options)
  114. load_balanced = options.get('loadbalanced')
  115. return PoolOptions(max_pool_size,
  116. min_pool_size,
  117. max_idle_time_seconds,
  118. connect_timeout, socket_timeout,
  119. wait_queue_timeout, wait_queue_multiple,
  120. ssl_context, ssl_match_hostname, socket_keepalive,
  121. _EventListeners(event_listeners),
  122. appname,
  123. driver,
  124. compression_settings,
  125. server_api=server_api,
  126. load_balanced=load_balanced)
  127. class ClientOptions(object):
  128. """ClientOptions"""
  129. def __init__(self, username, password, database, options):
  130. self.__options = options
  131. self.__codec_options = _parse_codec_options(options)
  132. self.__credentials = _parse_credentials(
  133. username, password, database, options)
  134. self.__direct_connection = options.get('directconnection')
  135. self.__local_threshold_ms = options.get(
  136. 'localthresholdms', common.LOCAL_THRESHOLD_MS)
  137. # self.__server_selection_timeout is in seconds. Must use full name for
  138. # common.SERVER_SELECTION_TIMEOUT because it is set directly by tests.
  139. self.__server_selection_timeout = options.get(
  140. 'serverselectiontimeoutms', common.SERVER_SELECTION_TIMEOUT)
  141. self.__pool_options = _parse_pool_options(options)
  142. self.__read_preference = _parse_read_preference(options)
  143. self.__replica_set_name = options.get('replicaset')
  144. self.__write_concern = _parse_write_concern(options)
  145. self.__read_concern = _parse_read_concern(options)
  146. self.__connect = options.get('connect')
  147. self.__heartbeat_frequency = options.get(
  148. 'heartbeatfrequencyms', common.HEARTBEAT_FREQUENCY)
  149. self.__retry_writes = options.get('retrywrites', common.RETRY_WRITES)
  150. self.__retry_reads = options.get('retryreads', common.RETRY_READS)
  151. self.__server_selector = options.get(
  152. 'server_selector', any_server_selector)
  153. self.__auto_encryption_opts = options.get('auto_encryption_opts')
  154. self.__load_balanced = options.get('loadbalanced')
  155. @property
  156. def _options(self):
  157. """The original options used to create this ClientOptions."""
  158. return self.__options
  159. @property
  160. def connect(self):
  161. """Whether to begin discovering a MongoDB topology automatically."""
  162. return self.__connect
  163. @property
  164. def codec_options(self):
  165. """A :class:`~bson.codec_options.CodecOptions` instance."""
  166. return self.__codec_options
  167. @property
  168. def credentials(self):
  169. """A :class:`~pymongo.auth.MongoCredentials` instance or None."""
  170. return self.__credentials
  171. @property
  172. def direct_connection(self):
  173. """Whether to connect to the deployment in 'Single' topology."""
  174. return self.__direct_connection
  175. @property
  176. def local_threshold_ms(self):
  177. """The local threshold for this instance."""
  178. return self.__local_threshold_ms
  179. @property
  180. def server_selection_timeout(self):
  181. """The server selection timeout for this instance in seconds."""
  182. return self.__server_selection_timeout
  183. @property
  184. def server_selector(self):
  185. return self.__server_selector
  186. @property
  187. def heartbeat_frequency(self):
  188. """The monitoring frequency in seconds."""
  189. return self.__heartbeat_frequency
  190. @property
  191. def pool_options(self):
  192. """A :class:`~pymongo.pool.PoolOptions` instance."""
  193. return self.__pool_options
  194. @property
  195. def read_preference(self):
  196. """A read preference instance."""
  197. return self.__read_preference
  198. @property
  199. def replica_set_name(self):
  200. """Replica set name or None."""
  201. return self.__replica_set_name
  202. @property
  203. def write_concern(self):
  204. """A :class:`~pymongo.write_concern.WriteConcern` instance."""
  205. return self.__write_concern
  206. @property
  207. def read_concern(self):
  208. """A :class:`~pymongo.read_concern.ReadConcern` instance."""
  209. return self.__read_concern
  210. @property
  211. def retry_writes(self):
  212. """If this instance should retry supported write operations."""
  213. return self.__retry_writes
  214. @property
  215. def retry_reads(self):
  216. """If this instance should retry supported read operations."""
  217. return self.__retry_reads
  218. @property
  219. def auto_encryption_opts(self):
  220. """A :class:`~pymongo.encryption.AutoEncryptionOpts` or None."""
  221. return self.__auto_encryption_opts
  222. @property
  223. def load_balanced(self):
  224. """True if the client was configured to connect to a load balancer."""
  225. return self.__load_balanced