mongo 2.14.1 → 2.15.0.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -1
  4. data/Rakefile +8 -15
  5. data/lib/mongo/auth/aws/conversation.rb +1 -4
  6. data/lib/mongo/auth/base.rb +13 -7
  7. data/lib/mongo/auth/conversation_base.rb +32 -0
  8. data/lib/mongo/auth/cr/conversation.rb +6 -29
  9. data/lib/mongo/auth/gssapi/conversation.rb +4 -15
  10. data/lib/mongo/auth/ldap/conversation.rb +3 -14
  11. data/lib/mongo/auth/sasl_conversation_base.rb +1 -13
  12. data/lib/mongo/auth/scram_conversation_base.rb +7 -34
  13. data/lib/mongo/auth/user/view.rb +16 -9
  14. data/lib/mongo/auth/x509/conversation.rb +4 -25
  15. data/lib/mongo/bulk_write.rb +21 -18
  16. data/lib/mongo/client.rb +82 -6
  17. data/lib/mongo/cluster/reapers/cursor_reaper.rb +6 -2
  18. data/lib/mongo/cluster.rb +19 -2
  19. data/lib/mongo/collection/view/aggregation.rb +1 -1
  20. data/lib/mongo/collection/view/change_stream.rb +1 -1
  21. data/lib/mongo/collection/view/iterable.rb +7 -17
  22. data/lib/mongo/collection/view/map_reduce.rb +2 -2
  23. data/lib/mongo/collection/view/readable.rb +42 -20
  24. data/lib/mongo/collection/view/writable.rb +14 -14
  25. data/lib/mongo/collection.rb +6 -6
  26. data/lib/mongo/cursor.rb +2 -12
  27. data/lib/mongo/database/view.rb +1 -1
  28. data/lib/mongo/database.rb +8 -3
  29. data/lib/mongo/error/bulk_write_error.rb +17 -3
  30. data/lib/mongo/error/internal_driver_error.rb +22 -0
  31. data/lib/mongo/error/operation_failure.rb +21 -2
  32. data/lib/mongo/error/parser.rb +65 -12
  33. data/lib/mongo/error/server_api_conflict.rb +23 -0
  34. data/lib/mongo/error/server_api_not_supported.rb +24 -0
  35. data/lib/mongo/error/unmet_dependency.rb +21 -0
  36. data/lib/mongo/error.rb +9 -1
  37. data/lib/mongo/index/view.rb +21 -11
  38. data/lib/mongo/monitoring/event/server_heartbeat_failed.rb +27 -16
  39. data/lib/mongo/monitoring/event/server_heartbeat_succeeded.rb +26 -15
  40. data/lib/mongo/monitoring.rb +13 -4
  41. data/lib/mongo/operation/collections_info/command.rb +2 -2
  42. data/lib/mongo/operation/collections_info.rb +18 -1
  43. data/lib/mongo/operation/context.rb +99 -0
  44. data/lib/mongo/operation/indexes.rb +15 -1
  45. data/lib/mongo/operation/insert/command.rb +2 -2
  46. data/lib/mongo/operation/insert/legacy.rb +2 -2
  47. data/lib/mongo/operation/insert/op_msg.rb +2 -2
  48. data/lib/mongo/operation/list_collections/result.rb +4 -1
  49. data/lib/mongo/operation/parallel_scan/command.rb +2 -1
  50. data/lib/mongo/operation/result.rb +2 -0
  51. data/lib/mongo/operation/shared/executable.rb +24 -14
  52. data/lib/mongo/operation/shared/executable_no_validate.rb +2 -2
  53. data/lib/mongo/operation/shared/op_msg_or_command.rb +1 -7
  54. data/lib/mongo/operation/shared/op_msg_or_find_command.rb +1 -7
  55. data/lib/mongo/operation/shared/polymorphic_operation.rb +39 -0
  56. data/lib/mongo/operation/shared/read_preference_supported.rb +36 -38
  57. data/lib/mongo/operation/shared/response_handling.rb +23 -23
  58. data/lib/mongo/operation/shared/sessions_supported.rb +15 -5
  59. data/lib/mongo/operation/shared/write.rb +8 -18
  60. data/lib/mongo/operation.rb +2 -2
  61. data/lib/mongo/protocol/compressed.rb +51 -5
  62. data/lib/mongo/protocol/message.rb +20 -2
  63. data/lib/mongo/protocol/msg.rb +38 -13
  64. data/lib/mongo/protocol/query.rb +11 -11
  65. data/lib/mongo/query_cache.rb +30 -0
  66. data/lib/mongo/retryable.rb +1 -1
  67. data/lib/mongo/server/app_metadata.rb +52 -18
  68. data/lib/mongo/server/connection.rb +5 -0
  69. data/lib/mongo/server/connection_base.rb +13 -10
  70. data/lib/mongo/server/connection_pool.rb +6 -2
  71. data/lib/mongo/server/description/features.rb +9 -8
  72. data/lib/mongo/server/description.rb +4 -0
  73. data/lib/mongo/server/monitor/app_metadata.rb +1 -1
  74. data/lib/mongo/server/monitor/connection.rb +9 -10
  75. data/lib/mongo/server/monitor.rb +20 -1
  76. data/lib/mongo/server/pending_connection.rb +24 -6
  77. data/lib/mongo/server/push_monitor.rb +11 -1
  78. data/lib/mongo/server.rb +7 -1
  79. data/lib/mongo/server_selector/secondary_preferred.rb +7 -2
  80. data/lib/mongo/session/session_pool.rb +4 -2
  81. data/lib/mongo/session.rb +2 -2
  82. data/lib/mongo/socket/ssl.rb +8 -0
  83. data/lib/mongo/socket.rb +29 -4
  84. data/lib/mongo/uri/options_mapper.rb +38 -0
  85. data/lib/mongo/utils.rb +15 -0
  86. data/lib/mongo/version.rb +1 -1
  87. data/lib/mongo.rb +23 -0
  88. data/spec/README.md +24 -1
  89. data/spec/integration/auth_spec.rb +25 -15
  90. data/spec/integration/bulk_write_error_message_spec.rb +41 -0
  91. data/spec/integration/change_stream_spec.rb +4 -4
  92. data/spec/integration/command_monitoring_spec.rb +2 -2
  93. data/spec/integration/connection_spec.rb +2 -0
  94. data/spec/integration/docs_examples_spec.rb +8 -1
  95. data/spec/integration/fork_reconnect_spec.rb +4 -1
  96. data/spec/integration/ocsp_verifier_spec.rb +13 -7
  97. data/spec/integration/operation_failure_code_spec.rb +1 -1
  98. data/spec/integration/operation_failure_message_spec.rb +90 -0
  99. data/spec/integration/query_cache_spec.rb +0 -45
  100. data/spec/integration/reconnect_spec.rb +1 -1
  101. data/spec/integration/snappy_compression_spec.rb +25 -0
  102. data/spec/integration/srv_monitoring_spec.rb +1 -1
  103. data/spec/integration/transactions_examples_spec.rb +6 -0
  104. data/spec/integration/zlib_compression_spec.rb +1 -1
  105. data/spec/integration/zstd_compression_spec.rb +26 -0
  106. data/spec/lite_spec_helper.rb +7 -1
  107. data/spec/mongo/address_spec.rb +15 -11
  108. data/spec/mongo/auth/ldap/conversation_spec.rb +1 -1
  109. data/spec/mongo/auth/ldap_spec.rb +5 -1
  110. data/spec/mongo/auth/scram_negotiation_spec.rb +1 -1
  111. data/spec/mongo/auth/scram_spec.rb +1 -1
  112. data/spec/mongo/auth/x509/conversation_spec.rb +3 -3
  113. data/spec/mongo/client_construction_spec.rb +207 -33
  114. data/spec/mongo/client_spec.rb +17 -0
  115. data/spec/mongo/cluster_spec.rb +1 -0
  116. data/spec/mongo/collection/view/explainable_spec.rb +1 -1
  117. data/spec/mongo/collection/view/readable_spec.rb +33 -19
  118. data/spec/mongo/collection_crud_spec.rb +4357 -0
  119. data/spec/mongo/collection_ddl_spec.rb +534 -0
  120. data/spec/mongo/collection_spec.rb +5 -4859
  121. data/spec/mongo/database_spec.rb +66 -4
  122. data/spec/mongo/error/bulk_write_error_spec.rb +3 -3
  123. data/spec/mongo/error/parser_spec.rb +37 -6
  124. data/spec/mongo/index/view_spec.rb +4 -0
  125. data/spec/mongo/monitoring/event/server_heartbeat_failed_spec.rb +1 -1
  126. data/spec/mongo/monitoring/event/server_heartbeat_succeeded_spec.rb +1 -1
  127. data/spec/mongo/operation/aggregate_spec.rb +2 -1
  128. data/spec/mongo/operation/collections_info_spec.rb +4 -1
  129. data/spec/mongo/operation/command_spec.rb +6 -3
  130. data/spec/mongo/operation/create_index_spec.rb +6 -3
  131. data/spec/mongo/operation/create_user_spec.rb +6 -3
  132. data/spec/mongo/operation/delete/bulk_spec.rb +9 -6
  133. data/spec/mongo/operation/delete_spec.rb +11 -7
  134. data/spec/mongo/operation/drop_index_spec.rb +6 -2
  135. data/spec/mongo/operation/find/legacy_spec.rb +3 -1
  136. data/spec/mongo/operation/get_more_spec.rb +3 -1
  137. data/spec/mongo/operation/indexes_spec.rb +5 -1
  138. data/spec/mongo/operation/insert/bulk_spec.rb +10 -7
  139. data/spec/mongo/operation/insert_spec.rb +15 -12
  140. data/spec/mongo/operation/map_reduce_spec.rb +5 -2
  141. data/spec/mongo/operation/read_preference_legacy_spec.rb +19 -9
  142. data/spec/mongo/operation/read_preference_op_msg_spec.rb +3 -3
  143. data/spec/mongo/operation/remove_user_spec.rb +6 -3
  144. data/spec/mongo/operation/result_spec.rb +1 -1
  145. data/spec/mongo/operation/update/bulk_spec.rb +9 -6
  146. data/spec/mongo/operation/update_spec.rb +10 -7
  147. data/spec/mongo/operation/update_user_spec.rb +4 -1
  148. data/spec/mongo/protocol/compressed_spec.rb +26 -12
  149. data/spec/mongo/query_cache_middleware_spec.rb +55 -0
  150. data/spec/mongo/retryable_spec.rb +3 -2
  151. data/spec/mongo/server/app_metadata_shared.rb +7 -33
  152. data/spec/mongo/server/app_metadata_spec.rb +2 -0
  153. data/spec/mongo/server/connection_pool/populator_spec.rb +3 -1
  154. data/spec/mongo/server/connection_pool_spec.rb +1 -1
  155. data/spec/mongo/server/connection_spec.rb +24 -17
  156. data/spec/mongo/server/monitor/connection_spec.rb +17 -7
  157. data/spec/mongo/server/monitor_spec.rb +9 -1
  158. data/spec/mongo/server_selector/secondary_preferred_spec.rb +6 -6
  159. data/spec/mongo/server_spec.rb +15 -2
  160. data/spec/mongo/socket/ssl_spec.rb +40 -0
  161. data/spec/mongo/socket_spec.rb +2 -2
  162. data/spec/mongo/tls_context_hooks_spec.rb +37 -0
  163. data/spec/runners/connection_string.rb +0 -4
  164. data/spec/runners/crud/requirement.rb +40 -3
  165. data/spec/runners/crud/verifier.rb +8 -0
  166. data/spec/runners/transactions/operation.rb +1 -1
  167. data/spec/runners/transactions/test.rb +1 -0
  168. data/spec/runners/unified/assertions.rb +249 -0
  169. data/spec/runners/unified/change_stream_operations.rb +26 -0
  170. data/spec/runners/unified/crud_operations.rb +199 -0
  171. data/spec/runners/unified/ddl_operations.rb +96 -0
  172. data/spec/runners/unified/entity_map.rb +39 -0
  173. data/spec/runners/unified/error.rb +25 -0
  174. data/spec/runners/unified/event_subscriber.rb +91 -0
  175. data/spec/runners/unified/exceptions.rb +21 -0
  176. data/spec/runners/unified/grid_fs_operations.rb +55 -0
  177. data/spec/runners/unified/support_operations.rb +250 -0
  178. data/spec/runners/unified/test.rb +393 -0
  179. data/spec/runners/unified/test_group.rb +28 -0
  180. data/spec/runners/unified/using_hash.rb +31 -0
  181. data/spec/runners/unified.rb +96 -0
  182. data/spec/shared/lib/mrss/cluster_config.rb +0 -3
  183. data/spec/shared/lib/mrss/docker_runner.rb +0 -3
  184. data/spec/shared/lib/mrss/lite_constraints.rb +0 -16
  185. data/spec/shared/lib/mrss/server_version_registry.rb +0 -3
  186. data/spec/shared/lib/mrss/spec_organizer.rb +0 -3
  187. data/spec/shared/shlib/server.sh +1 -1
  188. data/spec/spec_helper.rb +4 -1
  189. data/spec/spec_tests/crud_unified_spec.rb +10 -0
  190. data/spec/spec_tests/data/change_streams/change-streams.yml +0 -1
  191. data/spec/spec_tests/data/crud_unified/estimatedDocumentCount.yml +267 -0
  192. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-4.9.yml +60 -0
  193. data/spec/spec_tests/data/retryable_reads/{estimatedDocumentCount.yml → estimatedDocumentCount-pre4.9.yml} +2 -0
  194. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-serverErrors-4.9.yml +146 -0
  195. data/spec/spec_tests/data/retryable_reads/{estimatedDocumentCount-serverErrors.yml → estimatedDocumentCount-serverErrors-pre4.9.yml} +2 -0
  196. data/spec/spec_tests/data/retryable_reads/listIndexNames.yml +1 -1
  197. data/spec/spec_tests/data/unified/valid-fail/operation-failure.yml +31 -0
  198. data/spec/spec_tests/data/unified/valid-pass/poc-change-streams.yml +220 -0
  199. data/spec/spec_tests/data/unified/valid-pass/poc-command-monitoring.yml +102 -0
  200. data/spec/spec_tests/data/unified/valid-pass/poc-crud.yml +184 -0
  201. data/spec/spec_tests/data/unified/valid-pass/poc-gridfs.yml +155 -0
  202. data/spec/spec_tests/data/unified/valid-pass/poc-retryable-reads.yml +193 -0
  203. data/spec/spec_tests/data/unified/valid-pass/poc-retryable-writes.yml +210 -0
  204. data/spec/spec_tests/data/unified/valid-pass/poc-sessions.yml +215 -0
  205. data/spec/spec_tests/data/unified/valid-pass/poc-transactions-convenient-api.yml +235 -0
  206. data/spec/spec_tests/data/unified/valid-pass/poc-transactions-mongos-pin-auto.yml +169 -0
  207. data/spec/spec_tests/data/unified/valid-pass/poc-transactions.yml +170 -0
  208. data/spec/spec_tests/data/uri_options/compression-options.yml +1 -1
  209. data/spec/spec_tests/data/versioned_api/crud-api-version-1-strict.yml +416 -0
  210. data/spec/spec_tests/data/versioned_api/crud-api-version-1.yml +409 -0
  211. data/spec/spec_tests/data/versioned_api/runcommand-helper-no-api-version-declared.yml +67 -0
  212. data/spec/spec_tests/data/versioned_api/test-commands-deprecation-errors.yml +47 -0
  213. data/spec/spec_tests/data/versioned_api/test-commands-strict-mode.yml +44 -0
  214. data/spec/spec_tests/data/versioned_api/transaction-handling.yml +180 -0
  215. data/spec/spec_tests/unified_spec.rb +15 -0
  216. data/spec/spec_tests/uri_options_spec.rb +16 -0
  217. data/spec/spec_tests/versioned_api_spec.rb +10 -0
  218. data/spec/support/client_registry.rb +4 -8
  219. data/spec/support/client_registry_macros.rb +4 -4
  220. data/spec/support/common_shortcuts.rb +15 -1
  221. data/spec/support/shared/session.rb +2 -2
  222. data/spec/support/spec_config.rb +42 -11
  223. data/spec/support/utils.rb +64 -3
  224. data.tar.gz.sig +0 -0
  225. metadata +1005 -915
  226. metadata.gz.sig +0 -0
  227. data/lib/mongo/operation/shared/collections_info_or_list_collections.rb +0 -58
  228. data/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb +0 -47
  229. data/spec/integration/secondary_reads_spec.rb +0 -102
  230. data/spec/support/cluster_config.rb +0 -207
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02e085995ec501175b551369fff92fd59b44787da27cb9f84b93dafef4d0762b
4
- data.tar.gz: f7ac041c7d89ad98207a6038ab44263aed36ae4fbdc925107a0f74772889ff48
3
+ metadata.gz: a6312ea7c8c11a1e4f3dd355691d19bacf67bb3862395e69156133dace4b3503
4
+ data.tar.gz: 7a5f97640136a0f32e6e3d8a08f6fdb3a8b726342e7105391a49d18427c41016
5
5
  SHA512:
6
- metadata.gz: 61512bc1653f1b2d9e5d73db7acbfd0725931cf7e234aad7b709086e76037f287da12e6c893c6ae84429113fd83cf195a296e0f3f01fa3974670106d1b19e8d4
7
- data.tar.gz: 198f03ef56d8f280962ca212e411bf97a04cd518d7e1f82628f3bf4c274cf897c872e1d109af1ee94627803de2d3d8b37afbd239f87fe1c6a74e6813a2f65201
6
+ metadata.gz: 84fd33ac8d290e04c5ad2ca1c1dfa4022d9123d7ab7f8c3c9a621cf29dac04336f8dde8f73c8ff281107def7667ee0f3d5f3b596c90cee0ca8a88845754216f8
7
+ data.tar.gz: ccd825f5fba748bc45f75adef77f93f1a340862d32daa53145cb60e64a5e9b6dfd216c5eca033d1d3e6144a8931f548316315f3eb49b1dafed1dd63de978aa1a
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -14,7 +14,10 @@ Documentation
14
14
  High level documentation and usage examples are located
15
15
  [here](http://docs.mongodb.org/ecosystem/drivers/ruby/).
16
16
 
17
- API documentation can be found [here](https://api.mongodb.org/ruby/).
17
+ API documentation for the most recent release can be found
18
+ [here](https://docs.mongodb.com/ruby-driver/current/api/).
19
+ To build API documentation for the master branch, check out the
20
+ repository locally and run `rake docs`.
18
21
 
19
22
 
20
23
  Support
data/Rakefile CHANGED
@@ -1,26 +1,19 @@
1
- #!/usr/bin/env ruby
1
+ # -*- mode: ruby -*-
2
2
 
3
- require 'rubygems'
4
-
5
- begin
6
- require 'bundler'
7
- require 'bundler/gem_tasks'
8
- rescue LoadError
9
- raise '[FAIL] Bundler not found! Install it with ' +
10
- '`gem install bundler; bundle install`.'
11
- end
12
-
13
- default_groups = [:default, :testing]
14
- Bundler.require(*default_groups)
3
+ require 'bundler'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+ # TODO move the mongo require into the individual tasks that actually need it
7
+ require 'mongo'
15
8
 
16
9
  ROOT = File.expand_path(File.join(File.dirname(__FILE__)))
17
10
 
18
11
  $: << File.join(ROOT, 'spec/shared/lib')
19
12
 
20
- require 'rspec/core/rake_task'
21
13
  require 'mrss/spec_organizer'
22
14
 
23
15
  CLASSIFIERS = [
16
+ [%r,^mongo/server,, :unit_server],
24
17
  [%r,^mongo,, :unit],
25
18
  [%r,^kerberos,, :unit],
26
19
  [%r,^integration/sdam_error_handling,, :sdam_integration],
@@ -34,7 +27,7 @@ CLASSIFIERS = [
34
27
 
35
28
  RUN_PRIORITY = %i(
36
29
  tx_examples
37
- unit
30
+ unit unit_server
38
31
  integration sdam_integration cursor_reaping query_cache
39
32
  spec spec_sdam_integration
40
33
  )
@@ -49,10 +49,7 @@ module Mongo
49
49
  payload: BSON::Binary.new(client_final_payload),
50
50
  conversationId: conversation_id,
51
51
  )
52
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
53
- cluster_time = connection.mongos? && connection.cluster_time
54
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
55
- Protocol::Msg.new([], {}, selector)
52
+ build_message(connection, user.auth_source, selector)
56
53
  end
57
54
 
58
55
  private
@@ -100,7 +100,13 @@ module Mongo
100
100
  end
101
101
 
102
102
  def dispatch_msg(connection, conversation, msg)
103
- reply = connection.dispatch([msg])
103
+ context = Operation::Context.new(options: {
104
+ server_api: connection.options[:server_api],
105
+ })
106
+ if server_api = context.server_api
107
+ msg = msg.maybe_add_server_api(server_api)
108
+ end
109
+ reply = connection.dispatch([msg], context)
104
110
  reply_document = reply.documents.first
105
111
  validate_reply!(connection, conversation, reply_document)
106
112
  result = Operation::Result.new(reply, connection.description)
@@ -112,14 +118,14 @@ module Mongo
112
118
  # raises Unauthorized if not.
113
119
  def validate_reply!(connection, conversation, doc)
114
120
  if doc[:ok] != 1
115
- extra = [doc[:code], doc[:codeName]].compact.join(': ')
116
- msg = doc[:errmsg]
117
- unless extra.empty?
118
- msg += " (#{extra})"
119
- end
121
+ message = Error::Parser.build_message(
122
+ code: doc[:code],
123
+ code_name: doc[:codeName],
124
+ message: doc[:errmsg],
125
+ )
120
126
  raise Unauthorized.new(user,
121
127
  used_mechanism: self.class.const_get(:MECHANISM),
122
- message: msg,
128
+ message: message,
123
129
  server: connection.server,
124
130
  )
125
131
  end
@@ -47,6 +47,38 @@ module Mongo
47
47
  def speculative_auth_document
48
48
  nil
49
49
  end
50
+
51
+ # @return [ Protocol::Message ] The message to send.
52
+ def build_message(connection, auth_source, selector)
53
+ if connection && connection.features.op_msg_enabled?
54
+ selector = selector.dup
55
+ selector[Protocol::Msg::DATABASE_IDENTIFIER] = auth_source
56
+ cluster_time = connection.mongos? && connection.cluster_time
57
+ if cluster_time
58
+ selector[Operation::CLUSTER_TIME] = cluster_time
59
+ end
60
+ Protocol::Msg.new([], {}, selector)
61
+ else
62
+ Protocol::Query.new(
63
+ auth_source,
64
+ Database::COMMAND,
65
+ selector,
66
+ limit: -1,
67
+ )
68
+ end
69
+ end
70
+
71
+ def validate_external_auth_source
72
+ if user.auth_source != '$external'
73
+ user_name_msg = if user.name
74
+ " #{user.name}"
75
+ else
76
+ ''
77
+ end
78
+ mechanism = user.mechanism
79
+ raise Auth::InvalidConfiguration, "User#{user_name_msg} specifies auth source '#{user.auth_source}', but the only valid auth source for #{mechanism} is '$external'"
80
+ end
81
+ end
50
82
  end
51
83
  end
52
84
  end
@@ -43,22 +43,12 @@ module Mongo
43
43
  # @param [ Server::Connection ] connection The connection being
44
44
  # authenticated.
45
45
  #
46
- # @return [ Protocol::Query ] The first CR conversation message.
46
+ # @return [ Protocol::Message ] The first CR conversation message.
47
47
  #
48
48
  # @since 2.0.0
49
49
  def start(connection)
50
- if connection && connection.features.op_msg_enabled?
51
- selector = Auth::GET_NONCE.merge(Protocol::Msg::DATABASE_IDENTIFIER => user.auth_source)
52
- cluster_time = connection.mongos? && connection.cluster_time
53
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
54
- Protocol::Msg.new([], {}, selector)
55
- else
56
- Protocol::Query.new(
57
- user.auth_source,
58
- Database::COMMAND,
59
- Auth::GET_NONCE,
60
- limit: -1)
61
- end
50
+ selector = Auth::GET_NONCE
51
+ build_message(connection, user.auth_source, selector)
62
52
  end
63
53
 
64
54
  # Continue the CR conversation. This sends the client final message
@@ -70,26 +60,13 @@ module Mongo
70
60
  # @param [ Mongo::Server::Connection ] connection The connection being
71
61
  # authenticated.
72
62
  #
73
- # @return [ Protocol::Query ] The next message to send.
63
+ # @return [ Protocol::Message ] The next message to send.
74
64
  #
75
65
  # @since 2.0.0
76
66
  def continue(reply_document, connection)
77
67
  @nonce = reply_document[Auth::NONCE]
78
-
79
- if connection && connection.features.op_msg_enabled?
80
- selector = LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce))
81
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
82
- cluster_time = connection.mongos? && connection.cluster_time
83
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
84
- Protocol::Msg.new([], {}, selector)
85
- else
86
- Protocol::Query.new(
87
- user.auth_source,
88
- Database::COMMAND,
89
- LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce)),
90
- limit: -1
91
- )
92
- end
68
+ selector = LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce))
69
+ build_message(connection, user.auth_source, selector)
93
70
  end
94
71
  end
95
72
  end
@@ -70,20 +70,14 @@ module Mongo
70
70
  # @param [ BSON::Document ] reply_document The reply document of the
71
71
  # previous message.
72
72
  #
73
- # @return [ Protocol::Query ] The next query to execute.
73
+ # @return [ Protocol::Message ] The next query to execute.
74
74
  def continue(reply_document, connection)
75
75
  @id = reply_document['conversationId']
76
76
  payload = reply_document['payload']
77
77
 
78
78
  continue_token = authenticator.evaluate_challenge(payload)
79
79
  selector = CONTINUE_MESSAGE.merge(payload: continue_token, conversationId: id)
80
-
81
- Protocol::Query.new(
82
- Auth::EXTERNAL,
83
- Database::COMMAND,
84
- selector,
85
- limit: 1,
86
- )
80
+ build_message(connection, '$external', selector)
87
81
  end
88
82
 
89
83
  def process_continue_response(reply_document)
@@ -92,15 +86,10 @@ module Mongo
92
86
  @continue_token = authenticator.evaluate_challenge(payload)
93
87
  end
94
88
 
89
+ # @return [ Protocol::Message ] The next query to execute.
95
90
  def finalize(connection)
96
91
  selector = CONTINUE_MESSAGE.merge(payload: @continue_token, conversationId: id)
97
-
98
- Protocol::Query.new(
99
- Auth::EXTERNAL,
100
- Database::COMMAND,
101
- selector,
102
- limit: 1,
103
- )
92
+ build_message(connection, '$external', selector)
104
93
  end
105
94
  end
106
95
  end
@@ -38,20 +38,9 @@ module Mongo
38
38
  #
39
39
  # @since 2.0.0
40
40
  def start(connection)
41
- if connection && connection.features.op_msg_enabled?
42
- selector = LOGIN.merge(payload: payload, mechanism: LDAP::MECHANISM)
43
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = Auth::EXTERNAL
44
- cluster_time = connection.mongos? && connection.cluster_time
45
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
46
- Protocol::Msg.new([], {}, selector)
47
- else
48
- Protocol::Query.new(
49
- Auth::EXTERNAL,
50
- Database::COMMAND,
51
- LOGIN.merge(payload: payload, mechanism: LDAP::MECHANISM),
52
- limit: -1
53
- )
54
- end
41
+ validate_external_auth_source
42
+ selector = LOGIN.merge(payload: payload, mechanism: LDAP::MECHANISM)
43
+ build_message(connection, '$external', selector)
55
44
  end
56
45
 
57
46
  private
@@ -35,19 +35,7 @@ module Mongo
35
35
  # @return [ Protocol::Message ] The first SASL conversation message.
36
36
  def start(connection)
37
37
  selector = client_first_document
38
- if connection && connection.features.op_msg_enabled?
39
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
40
- cluster_time = connection.mongos? && connection.cluster_time
41
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
42
- Protocol::Msg.new([], {}, selector)
43
- else
44
- Protocol::Query.new(
45
- user.auth_source,
46
- Database::COMMAND,
47
- selector,
48
- limit: -1,
49
- )
50
- end
38
+ build_message(connection, user.auth_source, selector)
51
39
  end
52
40
 
53
41
  private
@@ -87,19 +87,7 @@ module Mongo
87
87
  payload: client_final_message,
88
88
  conversationId: id,
89
89
  )
90
- if connection && connection.features.op_msg_enabled?
91
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
92
- cluster_time = connection.mongos? && connection.cluster_time
93
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
94
- Protocol::Msg.new([], {}, selector)
95
- else
96
- Protocol::Query.new(
97
- user.auth_source,
98
- Database::COMMAND,
99
- selector,
100
- limit: -1,
101
- )
102
- end
90
+ build_message(connection, user.auth_source, selector)
103
91
  end
104
92
 
105
93
  # Processes the second response from the server.
@@ -116,28 +104,13 @@ module Mongo
116
104
  #
117
105
  # @param [ Server::Connection ] connection The connection being authenticated.
118
106
  #
119
- # @return [ Protocol::Query ] The next message to send.
107
+ # @return [ Protocol::Message ] The next message to send.
120
108
  def finalize(connection)
121
- if connection && connection.features.op_msg_enabled?
122
- selector = CLIENT_CONTINUE_MESSAGE.merge(
123
- payload: client_empty_message,
124
- conversationId: id,
125
- )
126
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source
127
- cluster_time = connection.mongos? && connection.cluster_time
128
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
129
- Protocol::Msg.new([], {}, selector)
130
- else
131
- Protocol::Query.new(
132
- user.auth_source,
133
- Database::COMMAND,
134
- CLIENT_CONTINUE_MESSAGE.merge(
135
- payload: client_empty_message,
136
- conversationId: id,
137
- ),
138
- limit: -1,
139
- )
140
- end
109
+ selector = CLIENT_CONTINUE_MESSAGE.merge(
110
+ payload: client_empty_message,
111
+ conversationId: id,
112
+ )
113
+ build_message(connection, user.auth_source, selector)
141
114
  end
142
115
 
143
116
  # Returns the hash to provide to the server in the handshake
@@ -44,13 +44,13 @@ module Mongo
44
44
  # @since 2.0.0
45
45
  def create(user_or_name, options = {})
46
46
  user = generate(user_or_name, options)
47
- client.send(:with_session, options) do |session|
47
+ execute_operation(options) do |session|
48
48
  Operation::CreateUser.new(
49
49
  user: user,
50
50
  db_name: database.name,
51
51
  session: session,
52
52
  write_concern: options[:write_concern] && WriteConcern.get(options[:write_concern]),
53
- ).execute(next_primary(nil, session), client: client)
53
+ )
54
54
  end
55
55
  end
56
56
 
@@ -81,13 +81,13 @@ module Mongo
81
81
  #
82
82
  # @since 2.0.0
83
83
  def remove(name, options = {})
84
- client.send(:with_session, options) do |session|
84
+ execute_operation(options) do |session|
85
85
  Operation::RemoveUser.new(
86
86
  user_name: name,
87
87
  db_name: database.name,
88
88
  session: session,
89
89
  write_concern: options[:write_concern] && WriteConcern.get(options[:write_concern]),
90
- ).execute(next_primary(nil, session), client: client)
90
+ )
91
91
  end
92
92
  end
93
93
 
@@ -106,14 +106,14 @@ module Mongo
106
106
  #
107
107
  # @since 2.0.0
108
108
  def update(user_or_name, options = {})
109
- client.send(:with_session, options) do |session|
110
- user = generate(user_or_name, options)
109
+ user = generate(user_or_name, options)
110
+ execute_operation(options) do |session|
111
111
  Operation::UpdateUser.new(
112
112
  user: user,
113
113
  db_name: database.name,
114
114
  session: session,
115
115
  write_concern: options[:write_concern] && WriteConcern.get(options[:write_concern]),
116
- ).execute(next_primary(nil, session), client: client)
116
+ )
117
117
  end
118
118
  end
119
119
 
@@ -137,18 +137,25 @@ module Mongo
137
137
  private
138
138
 
139
139
  def user_query(name, options = {})
140
- client.send(:with_session, options) do |session|
140
+ execute_operation(options) do |session|
141
141
  Operation::UsersInfo.new(
142
142
  user_name: name,
143
143
  db_name: database.name,
144
144
  session: session
145
- ).execute(next_primary(nil, session), client: client)
145
+ )
146
146
  end
147
147
  end
148
148
 
149
149
  def generate(user, options)
150
150
  user.is_a?(String) ? Auth::User.new({ user: user }.merge(options)) : user
151
151
  end
152
+
153
+ def execute_operation(options)
154
+ client.send(:with_session, options) do |session|
155
+ op = yield session
156
+ op.execute(next_primary(nil, session), context: Operation::Context.new(client: client, session: session))
157
+ end
158
+ end
152
159
  end
153
160
  end
154
161
  end
@@ -34,34 +34,13 @@ module Mongo
34
34
  # @param [ Server::Connection ] connection The connection being
35
35
  # authenticated.
36
36
  #
37
- # @return [ Protocol::Query ] The first X.509 conversation message.
37
+ # @return [ Protocol::Message ] The first X.509 conversation message.
38
38
  #
39
39
  # @since 2.0.0
40
40
  def start(connection)
41
- login = client_first_document
42
- if connection && connection.features.op_msg_enabled?
43
- selector = login
44
- # The only valid database for X.509 authentication is $external.
45
- if user.auth_source != '$external'
46
- user_name_msg = if user.name
47
- " #{user.name}"
48
- else
49
- ''
50
- end
51
- raise Auth::InvalidConfiguration, "User#{user_name_msg} specifies auth source '#{user.auth_source}', but the only valid auth source for X.509 is '$external'"
52
- end
53
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = '$external'
54
- cluster_time = connection.mongos? && connection.cluster_time
55
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
56
- Protocol::Msg.new([], {}, selector)
57
- else
58
- Protocol::Query.new(
59
- Auth::EXTERNAL,
60
- Database::COMMAND,
61
- login,
62
- limit: -1
63
- )
64
- end
41
+ validate_external_auth_source
42
+ selector = client_first_document
43
+ build_message(connection, '$external', selector)
65
44
  end
66
45
 
67
46
  # Returns the hash to provide to the server in the handshake
@@ -58,6 +58,7 @@ module Mongo
58
58
  operations = op_combiner.combine
59
59
 
60
60
  client.send(:with_session, @options) do |session|
61
+ context = Operation::Context.new(client: client, session: session)
61
62
  operations.each do |operation|
62
63
  if single_statement?(operation)
63
64
  write_concern = write_concern(session)
@@ -67,6 +68,7 @@ module Mongo
67
68
  operation.keys.first,
68
69
  operation.values.flatten,
69
70
  connection,
71
+ context,
70
72
  operation_id,
71
73
  result_combiner,
72
74
  session,
@@ -80,6 +82,7 @@ module Mongo
80
82
  operation.keys.first,
81
83
  operation.values.flatten,
82
84
  connection,
85
+ context,
83
86
  operation_id,
84
87
  result_combiner,
85
88
  session)
@@ -177,19 +180,19 @@ module Mongo
177
180
  }
178
181
  end
179
182
 
180
- def execute_operation(name, values, connection, operation_id, result_combiner, session, txn_num = nil)
183
+ def execute_operation(name, values, connection, context, operation_id, result_combiner, session, txn_num = nil)
181
184
  validate_collation!(connection)
182
185
  validate_array_filters!(connection)
183
186
  validate_hint!(connection)
184
187
 
185
188
  unpin_maybe(session) do
186
189
  if values.size > connection.description.max_write_batch_size
187
- split_execute(name, values, connection, operation_id, result_combiner, session, txn_num)
190
+ split_execute(name, values, connection, context, operation_id, result_combiner, session, txn_num)
188
191
  else
189
- result = send(name, values, connection, operation_id, session, txn_num)
192
+ result = send(name, values, connection, context, operation_id, session, txn_num)
190
193
 
191
194
  add_server_diagnostics(connection) do
192
- add_error_labels(client, connection, session) do
195
+ add_error_labels(connection, context) do
193
196
  result_combiner.combine!(result, values.size)
194
197
  end
195
198
  end
@@ -205,7 +208,7 @@ module Mongo
205
208
  rescue Error::MaxBSONSize, Error::MaxMessageSize => e
206
209
  raise e if values.size <= 1
207
210
  unpin_maybe(session) do
208
- split_execute(name, values, connection, operation_id, result_combiner, session, txn_num)
211
+ split_execute(name, values, connection, context, operation_id, result_combiner, session, txn_num)
209
212
  end
210
213
  end
211
214
 
@@ -213,47 +216,47 @@ module Mongo
213
216
  @op_combiner ||= ordered? ? OrderedCombiner.new(requests) : UnorderedCombiner.new(requests)
214
217
  end
215
218
 
216
- def split_execute(name, values, connection, operation_id, result_combiner, session, txn_num)
217
- execute_operation(name, values.shift(values.size / 2), connection, operation_id, result_combiner, session, txn_num)
219
+ def split_execute(name, values, connection, context, operation_id, result_combiner, session, txn_num)
220
+ execute_operation(name, values.shift(values.size / 2), connection, context, operation_id, result_combiner, session, txn_num)
218
221
 
219
222
  txn_num = session.next_txn_num if txn_num
220
- execute_operation(name, values, connection, operation_id, result_combiner, session, txn_num)
223
+ execute_operation(name, values, connection, context, operation_id, result_combiner, session, txn_num)
221
224
  end
222
225
 
223
- def delete_one(documents, connection, operation_id, session, txn_num)
226
+ def delete_one(documents, connection, context, operation_id, session, txn_num)
224
227
  QueryCache.clear_namespace(collection.namespace)
225
228
 
226
229
  spec = base_spec(operation_id, session).merge(:deletes => documents, :txn_num => txn_num)
227
- Operation::Delete.new(spec).bulk_execute(connection, client: client)
230
+ Operation::Delete.new(spec).bulk_execute(connection, context: context)
228
231
  end
229
232
 
230
- def delete_many(documents, connection, operation_id, session, txn_num)
233
+ def delete_many(documents, connection, context, operation_id, session, txn_num)
231
234
  QueryCache.clear_namespace(collection.namespace)
232
235
 
233
236
  spec = base_spec(operation_id, session).merge(:deletes => documents)
234
- Operation::Delete.new(spec).bulk_execute(connection, client: client)
237
+ Operation::Delete.new(spec).bulk_execute(connection, context: context)
235
238
  end
236
239
 
237
- def insert_one(documents, connection, operation_id, session, txn_num)
240
+ def insert_one(documents, connection, context, operation_id, session, txn_num)
238
241
  QueryCache.clear_namespace(collection.namespace)
239
242
 
240
243
  spec = base_spec(operation_id, session).merge(:documents => documents, :txn_num => txn_num)
241
- Operation::Insert.new(spec).bulk_execute(connection, client: client)
244
+ Operation::Insert.new(spec).bulk_execute(connection, context: context)
242
245
  end
243
246
 
244
- def update_one(documents, connection, operation_id, session, txn_num)
247
+ def update_one(documents, connection, context, operation_id, session, txn_num)
245
248
  QueryCache.clear_namespace(collection.namespace)
246
249
 
247
250
  spec = base_spec(operation_id, session).merge(:updates => documents, :txn_num => txn_num)
248
- Operation::Update.new(spec).bulk_execute(connection, client: client)
251
+ Operation::Update.new(spec).bulk_execute(connection, context: context)
249
252
  end
250
253
  alias :replace_one :update_one
251
254
 
252
- def update_many(documents, connection, operation_id, session, txn_num)
255
+ def update_many(documents, connection, context, operation_id, session, txn_num)
253
256
  QueryCache.clear_namespace(collection.namespace)
254
257
 
255
258
  spec = base_spec(operation_id, session).merge(:updates => documents)
256
- Operation::Update.new(spec).bulk_execute(connection, client: client)
259
+ Operation::Update.new(spec).bulk_execute(connection, context: context)
257
260
  end
258
261
 
259
262
  private