client_options.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Copyright 2014-2015 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 bson.py3compat import iteritems
  17. from pymongo.auth import _build_credentials_tuple
  18. from pymongo.common import validate, validate_boolean
  19. from pymongo import common
  20. from pymongo.errors import ConfigurationError
  21. from pymongo.pool import PoolOptions
  22. from pymongo.read_preferences import make_read_preference
  23. from pymongo.ssl_support import get_ssl_context
  24. from pymongo.write_concern import WriteConcern
  25. def _parse_credentials(username, password, database, options):
  26. """Parse authentication credentials."""
  27. if username is None:
  28. return None
  29. mechanism = options.get('authmechanism', 'DEFAULT')
  30. source = options.get('authsource', database or 'admin')
  31. return _build_credentials_tuple(
  32. mechanism, source, username, password, options)
  33. def _parse_read_preference(options):
  34. """Parse read preference options."""
  35. if 'read_preference' in options:
  36. return options['read_preference']
  37. mode = options.get('readpreference', 0)
  38. tags = options.get('readpreferencetags')
  39. return make_read_preference(mode, tags)
  40. def _parse_write_concern(options):
  41. """Parse write concern options."""
  42. concern = options.get('w')
  43. wtimeout = options.get('wtimeout')
  44. j = options.get('j', options.get('journal'))
  45. fsync = options.get('fsync')
  46. return WriteConcern(concern, wtimeout, j, fsync)
  47. def _parse_ssl_options(options):
  48. """Parse ssl options."""
  49. use_ssl = options.get('ssl')
  50. if use_ssl is not None:
  51. validate_boolean('ssl', use_ssl)
  52. certfile = options.get('ssl_certfile')
  53. keyfile = options.get('ssl_keyfile')
  54. ca_certs = options.get('ssl_ca_certs')
  55. cert_reqs = options.get('ssl_cert_reqs')
  56. match_hostname = options.get('ssl_match_hostname', True)
  57. ssl_kwarg_keys = [k for k in options
  58. if k.startswith('ssl_') and options[k]]
  59. if use_ssl == False and ssl_kwarg_keys:
  60. raise ConfigurationError("ssl has not been enabled but the "
  61. "following ssl parameters have been set: "
  62. "%s. Please set `ssl=True` or remove."
  63. % ', '.join(ssl_kwarg_keys))
  64. if ssl_kwarg_keys and use_ssl is None:
  65. # ssl options imply ssl = True
  66. use_ssl = True
  67. if use_ssl is True:
  68. ctx = get_ssl_context(certfile, keyfile, ca_certs, cert_reqs)
  69. return ctx, match_hostname
  70. return None, match_hostname
  71. def _parse_pool_options(options):
  72. """Parse connection pool options."""
  73. max_pool_size = options.get('maxpoolsize', common.MAX_POOL_SIZE)
  74. connect_timeout = options.get('connecttimeoutms', common.CONNECT_TIMEOUT)
  75. socket_keepalive = options.get('socketkeepalive', False)
  76. socket_timeout = options.get('sockettimeoutms')
  77. wait_queue_timeout = options.get('waitqueuetimeoutms')
  78. wait_queue_multiple = options.get('waitqueuemultiple')
  79. ssl_context, ssl_match_hostname = _parse_ssl_options(options)
  80. return PoolOptions(max_pool_size,
  81. connect_timeout, socket_timeout,
  82. wait_queue_timeout, wait_queue_multiple,
  83. ssl_context, ssl_match_hostname, socket_keepalive)
  84. class ClientOptions(object):
  85. """ClientOptions"""
  86. def __init__(self, username, password, database, options):
  87. options = dict([validate(opt, val) for opt, val in iteritems(options)])
  88. self.__codec_options = _parse_codec_options(options)
  89. self.__credentials = _parse_credentials(
  90. username, password, database, options)
  91. self.__local_threshold_ms = options.get(
  92. 'localthresholdms', common.LOCAL_THRESHOLD_MS)
  93. # self.__server_selection_timeout is in seconds. Must use full name for
  94. # common.SERVER_SELECTION_TIMEOUT because it is set directly by tests.
  95. self.__server_selection_timeout = options.get(
  96. 'serverselectiontimeoutms', common.SERVER_SELECTION_TIMEOUT)
  97. self.__pool_options = _parse_pool_options(options)
  98. self.__read_preference = _parse_read_preference(options)
  99. self.__replica_set_name = options.get('replicaset')
  100. self.__write_concern = _parse_write_concern(options)
  101. @property
  102. def codec_options(self):
  103. """A :class:`~bson.codec_options.CodecOptions` instance."""
  104. return self.__codec_options
  105. @property
  106. def credentials(self):
  107. """A :class:`~pymongo.auth.MongoCredentials` instance or None."""
  108. return self.__credentials
  109. @property
  110. def local_threshold_ms(self):
  111. """The local threshold for this instance."""
  112. return self.__local_threshold_ms
  113. @property
  114. def server_selection_timeout(self):
  115. """The server selection timeout for this instance in seconds."""
  116. return self.__server_selection_timeout
  117. @property
  118. def pool_options(self):
  119. """A :class:`~pymongo.pool.PoolOptions` instance."""
  120. return self.__pool_options
  121. @property
  122. def read_preference(self):
  123. """A read preference instance."""
  124. return self.__read_preference
  125. @property
  126. def replica_set_name(self):
  127. """Replica set name or None."""
  128. return self.__replica_set_name
  129. @property
  130. def write_concern(self):
  131. """A :class:`~pymongo.write_concern.WriteConcern` instance."""
  132. return self.__write_concern