mongo 2.26.0 → 2.26.2

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mongo/address/validator.rb +14 -9
  3. data/lib/mongo/auth.rb +8 -3
  4. data/lib/mongo/client.rb +40 -9
  5. data/lib/mongo/client_encryption.rb +47 -1
  6. data/lib/mongo/cluster/topology.rb +3 -3
  7. data/lib/mongo/collection/view/aggregation.rb +9 -9
  8. data/lib/mongo/collection/view/change_stream.rb +1 -5
  9. data/lib/mongo/collection/view/iterable.rb +2 -9
  10. data/lib/mongo/collection/view/map_reduce.rb +5 -4
  11. data/lib/mongo/collection/view/readable.rb +3 -3
  12. data/lib/mongo/crypt/auto_encrypter.rb +15 -1
  13. data/lib/mongo/crypt/context.rb +30 -4
  14. data/lib/mongo/crypt/encryption_io.rb +103 -17
  15. data/lib/mongo/crypt/explicit_encrypter.rb +6 -2
  16. data/lib/mongo/csot_timeout_holder.rb +5 -2
  17. data/lib/mongo/cursor.rb +13 -0
  18. data/lib/mongo/database/view.rb +7 -6
  19. data/lib/mongo/database.rb +7 -10
  20. data/lib/mongo/error/invalid_read_option.rb +9 -2
  21. data/lib/mongo/error/invalid_uri.rb +14 -6
  22. data/lib/mongo/index/view.rb +3 -2
  23. data/lib/mongo/monitoring/event/secure.rb +6 -1
  24. data/lib/mongo/operation/map_reduce/result.rb +6 -2
  25. data/lib/mongo/operation/shared/result/aggregatable.rb +3 -3
  26. data/lib/mongo/options/redacted.rb +230 -30
  27. data/lib/mongo/protocol/serializers.rb +1 -9
  28. data/lib/mongo/server/app_metadata.rb +1 -2
  29. data/lib/mongo/server/connection.rb +7 -0
  30. data/lib/mongo/server/connection_pool.rb +60 -9
  31. data/lib/mongo/session.rb +7 -0
  32. data/lib/mongo/socket/ssl.rb +32 -6
  33. data/lib/mongo/srv/resolver.rb +38 -6
  34. data/lib/mongo/srv/result.rb +8 -6
  35. data/lib/mongo/tracing/open_telemetry/command_tracer.rb +142 -103
  36. data/lib/mongo/tracing/open_telemetry/operation_tracer.rb +56 -32
  37. data/lib/mongo/tracing/open_telemetry/tracer.rb +16 -30
  38. data/lib/mongo/uri/options_mapper.rb +44 -22
  39. data/lib/mongo/uri/srv_protocol.rb +18 -11
  40. data/lib/mongo/uri.rb +174 -41
  41. data/lib/mongo/version.rb +1 -1
  42. metadata +545 -537
@@ -40,6 +40,10 @@ module Mongo
40
40
  # @param [ String ] key_vault_namespace The key vault namespace in the format
41
41
  # db_name.collection_name.
42
42
  # @param [ Hash ] mongocryptd_options Options related to mongocryptd.
43
+ # @param [ Proc | nil ] kms_connect_callback Optional callable that
44
+ # establishes the TCP connection to a KMS provider. It is invoked with
45
+ # the host, port, and operation timeout in milliseconds (or nil), and
46
+ # must return an IO connected to the host.
43
47
  #
44
48
  # @option mongocryptd_options [ Boolean ] :mongocryptd_bypass_spawn
45
49
  # @option mongocryptd_options [ String ] :mongocryptd_spawn_path
@@ -52,10 +56,13 @@ module Mongo
52
56
  # @note This class expects that the key_vault_client and key_vault_namespace
53
57
  # options are not nil and are in the correct format.
54
58
  def initialize(
55
- key_vault_namespace:, key_vault_client:, metadata_client:, client: nil, mongocryptd_client: nil, mongocryptd_options: {}
59
+ key_vault_namespace:, key_vault_client:, metadata_client:, client: nil, mongocryptd_client: nil, mongocryptd_options: {}, kms_connect_callback: nil
56
60
  )
57
61
  validate_key_vault_client!(key_vault_client)
58
62
  validate_key_vault_namespace!(key_vault_namespace)
63
+ if kms_connect_callback && !kms_connect_callback.respond_to?(:call)
64
+ raise ArgumentError, 'kms_connect_callback must respond to :call'
65
+ end
59
66
 
60
67
  @client = client
61
68
  @mongocryptd_client = mongocryptd_client
@@ -63,6 +70,7 @@ module Mongo
63
70
  @key_vault_client = key_vault_client
64
71
  @metadata_client = metadata_client
65
72
  @options = mongocryptd_options
73
+ @kms_connect_callback = kms_connect_callback
66
74
  end
67
75
 
68
76
  # Query for keys in the key vault collection using the provided
@@ -163,15 +171,25 @@ module Mongo
163
171
  # Must be a non-negative integer. An explicit value of 0 means infinite.
164
172
  # The default value is unset which means the feature is not enabled.
165
173
  def feed_kms(kms_context, tls_options, timeout_ms: nil)
166
- with_ssl_socket(kms_context.endpoint, tls_options) do |ssl_socket|
167
- Timeout.timeout(timeout_ms || SOCKET_TIMEOUT, Error::SocketTimeoutError,
174
+ # When a budget is in effect the TLS handshake, the request write, and
175
+ # every read chunk share a single deadline, so a slow, chunked response
176
+ # cannot run for a multiple of the budget. Without a budget each phase
177
+ # keeps the fixed SOCKET_TIMEOUT fallback. Timeout.timeout takes
178
+ # seconds, but timeout_ms is in milliseconds.
179
+ deadline = Utils.monotonic_time + (timeout_ms / 1_000.0) if timeout_ms
180
+ # Use the CSOT-appropriate error class when a timeout budget is
181
+ # in effect, otherwise preserve the historical SocketTimeoutError.
182
+ timeout_error = timeout_ms ? Error::TimeoutError : Error::SocketTimeoutError
183
+
184
+ with_ssl_socket(kms_context.endpoint, tls_options, timeout_ms: timeout_ms) do |ssl_socket|
185
+ Timeout.timeout(remaining_timeout(deadline), timeout_error,
168
186
  'Socket write operation timed out') do
169
187
  ssl_socket.syswrite(kms_context.message)
170
188
  end
171
189
 
172
190
  bytes_needed = kms_context.bytes_needed
173
191
  while bytes_needed > 0
174
- bytes = Timeout.timeout(timeout_ms || SOCKET_TIMEOUT, Error::SocketTimeoutError,
192
+ bytes = Timeout.timeout(remaining_timeout(deadline), timeout_error,
175
193
  'Socket read operation timed out') do
176
194
  ssl_socket.sysread(bytes_needed)
177
195
  end
@@ -252,6 +270,38 @@ module Mongo
252
270
 
253
271
  private
254
272
 
273
+ # The time left, in seconds, before the shared KMS exchange deadline.
274
+ # When no operation budget is in effect there is no deadline, so each
275
+ # phase falls back to the fixed SOCKET_TIMEOUT.
276
+ #
277
+ # @param [ Float, nil ] deadline The monotonic time the exchange must
278
+ # finish by, or nil when no budget is set.
279
+ #
280
+ # @return [ Float ] The number of seconds left before the deadline.
281
+ #
282
+ # @raise [ Error::TimeoutError ] If the deadline has already passed.
283
+ def remaining_timeout(deadline)
284
+ return SOCKET_TIMEOUT if deadline.nil?
285
+
286
+ remaining = deadline - Utils.monotonic_time
287
+ raise Error::TimeoutError, 'KMS request timed out' if remaining <= 0
288
+
289
+ remaining
290
+ end
291
+
292
+ # The budget for the connect phase, in seconds: the remaining operation
293
+ # timeout when a CSOT budget is in effect, otherwise the driver's fixed
294
+ # connect timeout. The connect callback and the TLS handshake that
295
+ # follows it share this budget, so neither can run past the deadline.
296
+ #
297
+ # @param [ Integer, nil ] timeout_ms The remaining operation timeout in
298
+ # milliseconds, or nil when no budget is set.
299
+ #
300
+ # @return [ Float ] The connect budget in seconds.
301
+ def connect_budget(timeout_ms)
302
+ timeout_ms ? timeout_ms / 1_000.0 : Server::CONNECT_TIMEOUT
303
+ end
304
+
255
305
  def validate_key_vault_client!(key_vault_client)
256
306
  raise ArgumentError.new('The :key_vault_client option cannot be nil') unless key_vault_client
257
307
 
@@ -345,24 +395,60 @@ module Mongo
345
395
  # executing
346
396
  def with_ssl_socket(endpoint, tls_options, timeout_ms: nil)
347
397
  csot = !timeout_ms.nil?
348
- address = begin
349
- host, port = endpoint.split(':')
350
- port ||= 443 # All supported KMS APIs use this port by default.
351
- Address.new([ host, port ].join(':'))
352
- end
353
- socket_options = { ssl: true, csot: csot }.tap do |opts|
354
- opts[:connect_timeout] = (timeout_ms / 1_000.0) if csot
355
- end
356
- mongo_socket = address.socket(
357
- SOCKET_TIMEOUT,
358
- tls_options.merge(socket_options)
359
- )
398
+ host, port = endpoint.split(':')
399
+ port = (port || 443).to_i
400
+
401
+ mongo_socket =
402
+ if @kms_connect_callback
403
+ # The callback runs before any socket exists, so it cannot inherit
404
+ # the connect timeout the socket gets below; bound it explicitly by
405
+ # the connect budget so a callback that blocks cannot outlive the
406
+ # operation deadline. A well-behaved callback should still honor
407
+ # timeout_ms itself, so it can fail cleanly and close its socket.
408
+ connect_budget_sec = connect_budget(timeout_ms)
409
+ connect_deadline = Utils.monotonic_time + connect_budget_sec
410
+ connect_error = csot ? Error::TimeoutError : Error::SocketTimeoutError
411
+ connected_socket =
412
+ Timeout.timeout(connect_budget_sec, connect_error,
413
+ 'KMS connect callback timed out') do
414
+ @kms_connect_callback.call(host, port, timeout_ms)
415
+ end
416
+ unless connected_socket.is_a?(IO)
417
+ raise Error::KmsError.new(
418
+ 'kms_connect_callback must return an IO connected to the KMS provider ' +
419
+ "at #{host}:#{port}, but it returned #{connected_socket.inspect}"
420
+ )
421
+ end
422
+
423
+ # The callback and the handshake share one connect budget: give the
424
+ # handshake only the time the callback left, so together they cannot
425
+ # run past the deadline.
426
+ remaining_connect = connect_deadline - Utils.monotonic_time
427
+ raise connect_error, 'KMS connect timed out' if remaining_connect <= 0
428
+
429
+ socket_options = { ssl: true, csot: csot, connected_socket: connected_socket }
430
+ socket_options[:connect_timeout] = remaining_connect
431
+ Mongo::Socket::SSL.new(
432
+ host, port, host, SOCKET_TIMEOUT, ::Socket::AF_UNSPEC,
433
+ tls_options.merge(socket_options)
434
+ )
435
+ else
436
+ address = Address.new([ host, port ].join(':'))
437
+ socket_options = { ssl: true, csot: csot }.tap do |opts|
438
+ opts[:connect_timeout] = (timeout_ms / 1_000.0) if csot
439
+ end
440
+ address.socket(SOCKET_TIMEOUT, tls_options.merge(socket_options))
441
+ end
442
+
360
443
  yield(mongo_socket.socket)
361
444
  rescue Error::KmsError
362
445
  raise
363
446
  rescue StandardError => e
447
+ # A timeout is not a transient network failure: retrying it only spends
448
+ # more of the operation budget, and under CSOT the deadline has already
449
+ # passed, so let it surface instead of letting feed_kms retry.
364
450
  raise Error::KmsError.new("Error when connecting to KMS provider: #{e.class}: #{e.message}",
365
- network_error: true)
451
+ network_error: !e.is_a?(Error::TimeoutError))
366
452
  ensure
367
453
  mongo_socket&.close
368
454
  end
@@ -40,9 +40,12 @@ module Mongo
40
40
  # @param [ Integer | nil ] key_expiration_ms The lifetime of the data
41
41
  # encryption key cache, in milliseconds. A value of 0 means the cache
42
42
  # never expires. When nil, libmongocrypt's default of 60000 is used.
43
+ # @param [ Proc | Method | #call | nil ] kms_connect_callback A callable
44
+ # invoked as +call(host, port, timeout_ms)+ when the driver needs to
45
+ # reach a KMS host. See Mongo::ClientEncryption for details.
43
46
  def initialize(
44
47
  key_vault_client, key_vault_namespace, kms_providers, kms_tls_options,
45
- timeout_ms = nil, key_expiration_ms = nil
48
+ timeout_ms = nil, key_expiration_ms = nil, kms_connect_callback = nil
46
49
  )
47
50
  Crypt.validate_ffi!
48
51
  @crypt_handle = Handle.new(
@@ -54,7 +57,8 @@ module Mongo
54
57
  @encryption_io = EncryptionIO.new(
55
58
  key_vault_client: key_vault_client,
56
59
  metadata_client: nil,
57
- key_vault_namespace: key_vault_namespace
60
+ key_vault_namespace: key_vault_namespace,
61
+ kms_connect_callback: kms_connect_callback
58
62
  )
59
63
  @timeout_ms = timeout_ms
60
64
  end
@@ -54,12 +54,15 @@ module Mongo
54
54
 
55
55
  # @return [ Integer | nil ] Returns the remaining milliseconds of the timeout
56
56
  # set for the operation; if no timeout is set, or the timeout is 0
57
- # (means unlimited) returns nil.
57
+ # (means unlimited) returns nil. A finite budget never rounds down to 0,
58
+ # since 0 means unlimited to callers: the last sub-millisecond before the
59
+ # deadline is reported as 1 ms instead.
58
60
  def remaining_timeout_ms
59
61
  seconds = remaining_timeout_sec
60
62
  return nil if seconds.nil?
61
63
 
62
- (seconds * 1_000).to_i
64
+ ms = (seconds * 1_000).to_i
65
+ ms.zero? ? 1 : ms
63
66
  end
64
67
 
65
68
  def remaining_timeout_ms!
data/lib/mongo/cursor.rb CHANGED
@@ -103,6 +103,19 @@ module Mongo
103
103
  self.class.finalize(kill_spec(@connection_global_id), cluster)
104
104
  )
105
105
  end
106
+ rescue Exception # rubocop:disable Lint/RescueException
107
+ # In load-balanced topology the connection of the initial result is
108
+ # checked out of the pool until the cursor is drained. If the cursor
109
+ # cannot be constructed, nothing will ever check the connection back
110
+ # in, so release it here before the error propagates. Exception (not
111
+ # StandardError) is rescued so that an interrupt does not permanently
112
+ # leak the connection. In other topologies the connection is not owned
113
+ # by the cursor and must not be touched here.
114
+ if server&.load_balancer? && result.is_a?(Operation::Result) &&
115
+ (connection = result.connection)
116
+ connection.connection_pool.check_in_if_checked_out(connection)
117
+ end
118
+ raise
106
119
  end
107
120
 
108
121
  # @api private
@@ -273,12 +273,13 @@ module Mongo
273
273
  execution_opts = {}
274
274
  execution_opts[:deserialize_as_bson] = opts.delete(:deserialize_as_bson) if opts.key?(:deserialize_as_bson)
275
275
  if server.load_balancer?
276
- connection = server.pool.check_out(context: context)
277
- initial_query_op(session, opts).execute_with_connection(
278
- connection,
279
- context: context,
280
- options: execution_opts
281
- )
276
+ server.pool.with_cursor_connection(context: context) do |connection|
277
+ initial_query_op(session, opts).execute_with_connection(
278
+ connection,
279
+ context: context,
280
+ options: execution_opts
281
+ )
282
+ end
282
283
  else
283
284
  initial_query_op(session, opts).execute(
284
285
  server,
@@ -339,15 +339,12 @@ module Mongo
339
339
  server = selector.select_server(cluster, nil, session)
340
340
  if server.load_balancer?
341
341
  # The connection is checked in by the cursor when it is drained.
342
- connection = check_out_cursor_command_connection(server, context)
343
- begin
344
- op.execute_with_connection(connection, context: context, options: execution_opts)
345
- rescue StandardError
346
- # Release the connection before the error propagates so that
347
- # a retried attempt checks out a fresh one.
348
- connection.connection_pool.check_in(connection) unless connection.pinned?
349
- connection = nil
350
- raise
342
+ # If the command fails, the connection is released before the
343
+ # error propagates so that a retried attempt checks out a
344
+ # fresh one.
345
+ server.pool.with_cursor_connection(context: context) do |conn|
346
+ connection = conn
347
+ op.execute_with_connection(conn, context: context, options: execution_opts)
351
348
  end
352
349
  else
353
350
  op.execute(server, context: context, options: execution_opts)
@@ -366,7 +363,7 @@ module Mongo
366
363
  # If the cursor was created it owns the session and connection;
367
364
  # otherwise (error or no cursor in the response) release them here.
368
365
  unless cursor
369
- connection.connection_pool.check_in(connection) if connection && !connection.pinned?
366
+ connection.connection_pool.check_in_if_checked_out(connection) if connection
370
367
  session.end_session if session && session.implicit?
371
368
  end
372
369
  end
@@ -23,12 +23,19 @@ module Mongo
23
23
  class InvalidReadOption < Error
24
24
  # Instantiate the new exception.
25
25
  #
26
+ # The value is not included in the message: it may come from the
27
+ # connection string and can contain text the user intended as a
28
+ # credential.
29
+ #
26
30
  # @example Instantiate the exception.
27
31
  # Mongo::Error::InvalidReadOption.new({:mode => 'bogus'})
28
32
  #
33
+ # @param [ Object ] _read_option The provided read option.
34
+ # @param [ String ] msg Description of the problem.
35
+ #
29
36
  # @since 2.6.0
30
- def initialize(read_option, msg)
31
- super("Invalid read preference value: #{read_option.inspect}: #{msg}")
37
+ def initialize(_read_option, msg)
38
+ super("Invalid read preference value: #{msg}")
32
39
  end
33
40
  end
34
41
  end
@@ -23,17 +23,25 @@ module Mongo
23
23
  class InvalidURI < Error
24
24
  # Instantiate the new exception.
25
25
  #
26
- # The URI is redacted via {Mongo::URI.redact} before being interpolated
27
- # so that any cleartext credentials in the original input do not end up
28
- # in logs, error reporters, or backtraces.
26
+ # The message deliberately contains no part of the connection string.
27
+ # A string that failed to parse cannot be redacted reliably: the parser
28
+ # and the redactor can disagree about where the userinfo ends, so
29
+ # masking the input may still disclose a credential. The details
30
+ # supplied by the parser identify the component at fault by name
31
+ # instead. See the "Disclosure of Sensitive Values" section of the
32
+ # connection string specification.
29
33
  #
30
34
  # @example Instantiate the exception.
31
35
  # Mongo::Error::InvalidURI.new(uri, details, format)
32
36
  #
37
+ # @param [ String | nil ] _uri The URI that failed to parse. Kept in the
38
+ # signature for compatibility; it is not included in the message.
39
+ # @param [ String ] details Description of the parse failure.
40
+ # @param [ String | nil ] format The expected connection string format.
41
+ #
33
42
  # @since 2.0.0
34
- def initialize(uri, details, format = nil)
35
- message = "Bad URI: #{Mongo::URI.redact(uri)}\n" +
36
- "#{details}\n"
43
+ def initialize(_uri, details, format = nil)
44
+ message = "#{details}\n"
37
45
  message += "MongoDB URI must be in the following format: #{format}\n" if format
38
46
  message += "Please see the following URL for more information: #{Mongo::URI::HELP}\n"
39
47
  super(message)
@@ -421,8 +421,9 @@ module Mongo
421
421
 
422
422
  def send_initial_query(op, server, _session, context)
423
423
  if server.load_balancer?
424
- connection = server.pool.check_out(context: context)
425
- op.execute_with_connection(connection, context: context)
424
+ server.pool.with_cursor_connection(context: context) do |connection|
425
+ op.execute_with_connection(connection, context: context)
426
+ end
426
427
  else
427
428
  op.execute(server, context: context)
428
429
  end
@@ -37,6 +37,11 @@ module Mongo
37
37
  copydb
38
38
  ].freeze
39
39
 
40
+ # The hello and legacy hello command names. A constant, because
41
+ # sensitive? runs on every command event and an array literal would
42
+ # allocate on each call.
43
+ HELLO_COMMANDS = %w[hello ismaster isMaster].freeze
44
+
40
45
  # Check whether the command is sensitive in terms of command monitoring
41
46
  # spec. A command is detected as sensitive if it is in the
42
47
  # list or if it is a hello/legacy hello command, and
@@ -53,7 +58,7 @@ module Mongo
53
58
  # when speculativeAuthenticate is present, their commands AND replies
54
59
  # MUST be redacted from the events.
55
60
  # See https://github.com/mongodb/specifications/blob/master/source/command-logging-and-monitoring/command-logging-and-monitoring.md#security
56
- %w[hello ismaster isMaster].include?(command_name.to_s) &&
61
+ HELLO_COMMANDS.include?(command_name.to_s) &&
57
62
  !!document['speculativeAuthenticate']
58
63
  end
59
64
 
@@ -80,7 +80,7 @@ module Mongo
80
80
  # @since 2.0.0
81
81
  # @api public
82
82
  def successful?
83
- !documents.nil?
83
+ !documents.nil? && parser.message.empty?
84
84
  end
85
85
 
86
86
  # Get the execution time of the map/reduce.
@@ -112,7 +112,11 @@ module Mongo
112
112
  # @since 2.0.0
113
113
  # @api private
114
114
  def validate!
115
- documents.nil? ? raise_operation_failure : self
115
+ if documents.nil? || !parser.message.empty?
116
+ raise_operation_failure
117
+ else
118
+ self
119
+ end
116
120
  end
117
121
 
118
122
  # Get the cursor id.
@@ -36,7 +36,7 @@ module Mongo
36
36
  return unless @replies
37
37
 
38
38
  @replies.reduce(nil) do |errors, reply|
39
- next unless (write_errors = reply.documents.first['writeErrors'])
39
+ next errors unless (write_errors = reply.documents.first['writeErrors'])
40
40
 
41
41
  wes = write_errors.collect do |we|
42
42
  we.merge!('index' => count + we['index'])
@@ -59,11 +59,11 @@ module Mongo
59
59
  return unless @replies
60
60
 
61
61
  @replies.each_with_index.reduce(nil) do |errors, (reply, _)|
62
- next unless (write_concern_errors = reply.documents.first['writeConcernErrors'])
62
+ next errors unless (write_concern_errors = reply.documents.first['writeConcernErrors'])
63
63
 
64
64
  (errors || []) << write_concern_errors.reduce(nil) do |errs, wce|
65
65
  wce.merge!('index' => count + wce['index'])
66
- (errs || []) << write_concern_error
66
+ (errs || []) << wce
67
67
  end
68
68
  end
69
69
  end