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.
- checksums.yaml +4 -4
- data/lib/mongo/address/validator.rb +14 -9
- data/lib/mongo/auth.rb +8 -3
- data/lib/mongo/client.rb +40 -9
- data/lib/mongo/client_encryption.rb +47 -1
- data/lib/mongo/cluster/topology.rb +3 -3
- data/lib/mongo/collection/view/aggregation.rb +9 -9
- data/lib/mongo/collection/view/change_stream.rb +1 -5
- data/lib/mongo/collection/view/iterable.rb +2 -9
- data/lib/mongo/collection/view/map_reduce.rb +5 -4
- data/lib/mongo/collection/view/readable.rb +3 -3
- data/lib/mongo/crypt/auto_encrypter.rb +15 -1
- data/lib/mongo/crypt/context.rb +30 -4
- data/lib/mongo/crypt/encryption_io.rb +103 -17
- data/lib/mongo/crypt/explicit_encrypter.rb +6 -2
- data/lib/mongo/csot_timeout_holder.rb +5 -2
- data/lib/mongo/cursor.rb +13 -0
- data/lib/mongo/database/view.rb +7 -6
- data/lib/mongo/database.rb +7 -10
- data/lib/mongo/error/invalid_read_option.rb +9 -2
- data/lib/mongo/error/invalid_uri.rb +14 -6
- data/lib/mongo/index/view.rb +3 -2
- data/lib/mongo/monitoring/event/secure.rb +6 -1
- data/lib/mongo/operation/map_reduce/result.rb +6 -2
- data/lib/mongo/operation/shared/result/aggregatable.rb +3 -3
- data/lib/mongo/options/redacted.rb +230 -30
- data/lib/mongo/protocol/serializers.rb +1 -9
- data/lib/mongo/server/app_metadata.rb +1 -2
- data/lib/mongo/server/connection.rb +7 -0
- data/lib/mongo/server/connection_pool.rb +60 -9
- data/lib/mongo/session.rb +7 -0
- data/lib/mongo/socket/ssl.rb +32 -6
- data/lib/mongo/srv/resolver.rb +38 -6
- data/lib/mongo/srv/result.rb +8 -6
- data/lib/mongo/tracing/open_telemetry/command_tracer.rb +142 -103
- data/lib/mongo/tracing/open_telemetry/operation_tracer.rb +56 -32
- data/lib/mongo/tracing/open_telemetry/tracer.rb +16 -30
- data/lib/mongo/uri/options_mapper.rb +44 -22
- data/lib/mongo/uri/srv_protocol.rb +18 -11
- data/lib/mongo/uri.rb +174 -41
- data/lib/mongo/version.rb +1 -1
- metadata +545 -537
|
@@ -56,24 +56,28 @@ module Mongo
|
|
|
56
56
|
# @param [ String ] key URI option name.
|
|
57
57
|
# @param [ String ] value The value of the option.
|
|
58
58
|
# @param [ Hash ] uri_options The base option target.
|
|
59
|
-
|
|
59
|
+
# @param [ Integer | nil ] offset Position of the option within the
|
|
60
|
+
# connection options segment, if known. Used to make warnings about
|
|
61
|
+
# unknown options actionable without quoting the key.
|
|
62
|
+
def add_uri_option(key, value, uri_options, offset: nil)
|
|
60
63
|
strategy = URI_OPTION_MAP[key.downcase]
|
|
61
64
|
if strategy.nil?
|
|
62
|
-
log_warn(
|
|
65
|
+
log_warn(unsupported_option_message(offset))
|
|
63
66
|
return
|
|
64
67
|
end
|
|
65
68
|
|
|
69
|
+
canonical_key = URI_OPTION_CANONICAL_NAMES[key.downcase]
|
|
66
70
|
group = strategy[:group]
|
|
67
71
|
target = if group
|
|
68
72
|
uri_options[group] || {}
|
|
69
73
|
else
|
|
70
74
|
uri_options
|
|
71
75
|
end
|
|
72
|
-
value = apply_transform(
|
|
76
|
+
value = apply_transform(canonical_key, value, strategy[:type])
|
|
73
77
|
# Sometimes the value here would be nil, for example if we are processing
|
|
74
78
|
# read preference tags or auth mechanism properties and all of the
|
|
75
79
|
# data within is invalid. Ignore such options.
|
|
76
|
-
merge_uri_option(target, value, strategy[:name]) unless value.nil?
|
|
80
|
+
merge_uri_option(target, value, strategy[:name], canonical_key) unless value.nil?
|
|
77
81
|
|
|
78
82
|
return unless group && !target.empty? && !uri_options.key?(group)
|
|
79
83
|
|
|
@@ -86,10 +90,11 @@ module Mongo
|
|
|
86
90
|
opts.each do |key, value|
|
|
87
91
|
strategy = URI_OPTION_MAP[key.downcase]
|
|
88
92
|
if strategy.nil?
|
|
89
|
-
log_warn(
|
|
93
|
+
log_warn(unsupported_option_message)
|
|
90
94
|
next
|
|
91
95
|
end
|
|
92
96
|
|
|
97
|
+
canonical_key = URI_OPTION_CANONICAL_NAMES[key.downcase]
|
|
93
98
|
group = strategy[:group]
|
|
94
99
|
target = if group
|
|
95
100
|
uri_options[group] || {}
|
|
@@ -97,11 +102,11 @@ module Mongo
|
|
|
97
102
|
uri_options
|
|
98
103
|
end
|
|
99
104
|
|
|
100
|
-
value = apply_transform(
|
|
105
|
+
value = apply_transform(canonical_key, value, strategy[:type])
|
|
101
106
|
# Sometimes the value here would be nil, for example if we are processing
|
|
102
107
|
# read preference tags or auth mechanism properties and all of the
|
|
103
108
|
# data within is invalid. Ignore such options.
|
|
104
|
-
merge_uri_option(target, value, strategy[:name]) unless value.nil?
|
|
109
|
+
merge_uri_option(target, value, strategy[:name], canonical_key) unless value.nil?
|
|
105
110
|
|
|
106
111
|
uri_options[group] = target if group && !target.empty? && !uri_options.key?(group)
|
|
107
112
|
end
|
|
@@ -234,18 +239,35 @@ module Mongo
|
|
|
234
239
|
# @param [ Hash ] target The destination.
|
|
235
240
|
# @param [ Object ] value The value to be merged.
|
|
236
241
|
# @param [ Symbol ] name The name of the option.
|
|
237
|
-
|
|
242
|
+
# @param [ String ] canonical_key The canonical URI option name.
|
|
243
|
+
def merge_uri_option(target, value, name, canonical_key)
|
|
238
244
|
if target.key?(name)
|
|
239
245
|
if REPEATABLE_OPTIONS.include?(name)
|
|
240
246
|
target[name] += value
|
|
241
247
|
else
|
|
242
|
-
log_warn("Repeated option key: #{
|
|
248
|
+
log_warn("Repeated option key: #{canonical_key}.")
|
|
243
249
|
end
|
|
244
250
|
else
|
|
245
251
|
target.merge!(name => value)
|
|
246
252
|
end
|
|
247
253
|
end
|
|
248
254
|
|
|
255
|
+
# Builds a warning about an option the driver does not know. The key
|
|
256
|
+
# itself is not included: it comes from the connection string and may
|
|
257
|
+
# contain text the user intended as a credential.
|
|
258
|
+
#
|
|
259
|
+
# @param [ Integer | nil ] offset Position of the option within the
|
|
260
|
+
# connection options segment, if known.
|
|
261
|
+
#
|
|
262
|
+
# @return [ String ] The warning message.
|
|
263
|
+
def unsupported_option_message(offset = nil)
|
|
264
|
+
if offset
|
|
265
|
+
"Unsupported option at offset #{offset} in the query segment. It will be ignored."
|
|
266
|
+
else
|
|
267
|
+
'Unsupported option. It will be ignored.'
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
249
271
|
# Replica Set Options
|
|
250
272
|
uri_option 'replicaSet', :replica_set
|
|
251
273
|
|
|
@@ -329,7 +351,7 @@ module Mongo
|
|
|
329
351
|
when false, 'false', 'FALSE'
|
|
330
352
|
false
|
|
331
353
|
else
|
|
332
|
-
log_warn("
|
|
354
|
+
log_warn("Invalid boolean value for #{name}")
|
|
333
355
|
nil
|
|
334
356
|
end
|
|
335
357
|
end
|
|
@@ -443,7 +465,7 @@ module Mongo
|
|
|
443
465
|
# @return [ nil | Integer ] Converted value.
|
|
444
466
|
def convert_integer(name, value)
|
|
445
467
|
if value.is_a?(String) && /\A\d+\z/ !~ value
|
|
446
|
-
log_warn("#{
|
|
468
|
+
log_warn("#{name} is not a valid integer")
|
|
447
469
|
return nil
|
|
448
470
|
end
|
|
449
471
|
|
|
@@ -482,7 +504,7 @@ module Mongo
|
|
|
482
504
|
case value
|
|
483
505
|
when String
|
|
484
506
|
unless /\A-?\d+(\.\d+)?\z/.match?(value)
|
|
485
|
-
log_warn("Invalid
|
|
507
|
+
log_warn("Invalid value for #{name}")
|
|
486
508
|
return nil
|
|
487
509
|
end
|
|
488
510
|
if value.to_s[0] == '-'
|
|
@@ -573,10 +595,10 @@ module Mongo
|
|
|
573
595
|
# @param [ String ] value The authentication mechanism.
|
|
574
596
|
#
|
|
575
597
|
# @return [ Symbol ] The transformed authentication mechanism.
|
|
576
|
-
def convert_auth_mech(
|
|
598
|
+
def convert_auth_mech(name, value)
|
|
577
599
|
auth_mech = AUTH_MECH_MAP[value.upcase]
|
|
578
600
|
(auth_mech || value).tap do |_mech|
|
|
579
|
-
log_warn("#{
|
|
601
|
+
log_warn("#{name} is not a valid authentication mechanism") unless auth_mech
|
|
580
602
|
end
|
|
581
603
|
end
|
|
582
604
|
|
|
@@ -651,7 +673,7 @@ module Mongo
|
|
|
651
673
|
#
|
|
652
674
|
# @return [ Integer | nil ] The max staleness integer parsed out if it is valid, otherwise nil
|
|
653
675
|
# (and a warning will be logged).
|
|
654
|
-
def convert_max_staleness(
|
|
676
|
+
def convert_max_staleness(name, value)
|
|
655
677
|
int = if value.is_a?(String) && /\A-?\d+\z/ =~ value
|
|
656
678
|
value.to_i
|
|
657
679
|
elsif value.is_a?(Integer)
|
|
@@ -659,14 +681,14 @@ module Mongo
|
|
|
659
681
|
end
|
|
660
682
|
|
|
661
683
|
if int.nil?
|
|
662
|
-
log_warn("Invalid
|
|
684
|
+
log_warn("Invalid value for #{name}")
|
|
663
685
|
return nil
|
|
664
686
|
end
|
|
665
687
|
|
|
666
688
|
int = nil if int == -1
|
|
667
689
|
|
|
668
690
|
if int && ((int > 0 && int < 90) || int < 0)
|
|
669
|
-
log_warn("
|
|
691
|
+
log_warn("#{name} should be either 0 or greater than 90")
|
|
670
692
|
int = nil
|
|
671
693
|
end
|
|
672
694
|
|
|
@@ -717,12 +739,12 @@ module Mongo
|
|
|
717
739
|
# @param [ String ] value The server monitoring mode string value.
|
|
718
740
|
#
|
|
719
741
|
# @return [ Symbol | nil ] The server monitoring mode symbol.
|
|
720
|
-
def convert_server_monitoring_mode(
|
|
742
|
+
def convert_server_monitoring_mode(name, value)
|
|
721
743
|
mode = value.downcase
|
|
722
744
|
if SERVER_MONITORING_MODES.include?(mode)
|
|
723
745
|
mode.to_sym
|
|
724
746
|
else
|
|
725
|
-
log_warn("#{
|
|
747
|
+
log_warn("#{name} is not a valid server monitoring mode")
|
|
726
748
|
nil
|
|
727
749
|
end
|
|
728
750
|
end
|
|
@@ -828,7 +850,7 @@ module Mongo
|
|
|
828
850
|
#
|
|
829
851
|
# @return [ Integer | nil ] The compression level value if it is between -1 and 9 (inclusive),
|
|
830
852
|
# otherwise nil (and a warning will be logged).
|
|
831
|
-
def convert_zlib_compression_level(
|
|
853
|
+
def convert_zlib_compression_level(name, value)
|
|
832
854
|
i = if value.is_a?(String) && /\A-?\d+\z/ =~ value
|
|
833
855
|
value.to_i
|
|
834
856
|
elsif value.is_a?(Integer)
|
|
@@ -838,7 +860,7 @@ module Mongo
|
|
|
838
860
|
if i && i >= -1 && i <= 9
|
|
839
861
|
i
|
|
840
862
|
else
|
|
841
|
-
log_warn("#{
|
|
863
|
+
log_warn("#{name} is not a valid compression level")
|
|
842
864
|
nil
|
|
843
865
|
end
|
|
844
866
|
end
|
|
@@ -881,7 +903,7 @@ module Mongo
|
|
|
881
903
|
value.split(',').each do |tag|
|
|
882
904
|
k, v = tag.split(':', 2)
|
|
883
905
|
if v.nil?
|
|
884
|
-
log_warn("Invalid
|
|
906
|
+
log_warn("Invalid value for #{name}")
|
|
885
907
|
return nil if invalidate_on_error
|
|
886
908
|
|
|
887
909
|
next
|
|
@@ -38,14 +38,20 @@ module Mongo
|
|
|
38
38
|
# don't have to merge the txt record options, credentials, and database in at that point -
|
|
39
39
|
# we only have a single point here.
|
|
40
40
|
#
|
|
41
|
+
# The returned object is a Hash (with the same key types as before) whose
|
|
42
|
+
# string representations redact the credentials and the arguments of
|
|
43
|
+
# sensitive URI options. Accessing it returns the real values.
|
|
44
|
+
#
|
|
41
45
|
# @example Get the client options.
|
|
42
46
|
# uri.client_options
|
|
43
47
|
#
|
|
44
|
-
# @return [
|
|
48
|
+
# @return [ Mongo::Options::RedactedHash ] The options passed to the Mongo::Client
|
|
45
49
|
#
|
|
46
50
|
# @since 2.5.0
|
|
47
51
|
def client_options
|
|
48
|
-
|
|
52
|
+
ensure_not_redacted_serialization!
|
|
53
|
+
|
|
54
|
+
opts = Options::RedactedHash[@txt_options.merge(ssl: true)]
|
|
49
55
|
opts = opts.merge(uri_options).merge(database: database)
|
|
50
56
|
@user ? opts.merge(credentials) : opts
|
|
51
57
|
end
|
|
@@ -94,9 +100,10 @@ module Mongo
|
|
|
94
100
|
INVALID_DOMAIN = 'The domain name must consist of at least two parts: the domain name, ' +
|
|
95
101
|
'and a TLD.'
|
|
96
102
|
|
|
97
|
-
# @return [ String ] NO_SRV_RECORDS Error message
|
|
98
|
-
# were found.
|
|
99
|
-
|
|
103
|
+
# @return [ String ] NO_SRV_RECORDS Error message indicating that no SRV records
|
|
104
|
+
# were found. The hostname is not included: during initial parsing it may contain text the
|
|
105
|
+
# user intended as part of a credential.
|
|
106
|
+
NO_SRV_RECORDS = 'The DNS query returned no SRV records.'
|
|
100
107
|
|
|
101
108
|
# @return [ String ] FORMAT The expected SRV URI format.
|
|
102
109
|
FORMAT = 'mongodb+srv://[username:password@]host[/[database][?options]]'
|
|
@@ -143,14 +150,14 @@ module Mongo
|
|
|
143
150
|
validate_srv_hostname(hostname)
|
|
144
151
|
@query_hostname = hostname
|
|
145
152
|
|
|
146
|
-
log_debug
|
|
153
|
+
log_debug 'attempting to resolve SRV records'
|
|
147
154
|
|
|
148
155
|
@srv_result = resolver.get_records(
|
|
149
156
|
hostname,
|
|
150
157
|
uri_options[:srv_service_name] || options[:srv_service_name],
|
|
151
158
|
uri_options[:srv_max_hosts] || options[:srv_max_hosts]
|
|
152
159
|
)
|
|
153
|
-
raise Error::NoSRVRecords.new(NO_SRV_RECORDS
|
|
160
|
+
raise Error::NoSRVRecords.new(NO_SRV_RECORDS) if srv_result.empty?
|
|
154
161
|
|
|
155
162
|
@txt_options = get_txt_options(hostname) || {}
|
|
156
163
|
records = srv_result.address_strs
|
|
@@ -173,13 +180,13 @@ module Mongo
|
|
|
173
180
|
def validate_srv_hostname(hostname)
|
|
174
181
|
raise_invalid_error!(INVALID_PORT) if hostname.include?(HOST_PORT_DELIM)
|
|
175
182
|
|
|
176
|
-
raise_invalid_error!(
|
|
177
|
-
raise_invalid_error!(
|
|
183
|
+
raise_invalid_error!('Hostname cannot start with a dot.') if hostname.start_with?('.')
|
|
184
|
+
raise_invalid_error!('Hostname cannot end with a dot.') if hostname.end_with?('.')
|
|
178
185
|
parts = hostname.split('.')
|
|
179
|
-
raise_invalid_error!(
|
|
186
|
+
raise_invalid_error!('Hostname cannot have consecutive dots.') if parts.any?(&:empty?)
|
|
180
187
|
return unless parts.length < 1
|
|
181
188
|
|
|
182
|
-
raise_invalid_error!(
|
|
189
|
+
raise_invalid_error!('Hostname cannot be empty.')
|
|
183
190
|
end
|
|
184
191
|
|
|
185
192
|
# Obtains the TXT options of a host.
|
data/lib/mongo/uri.rb
CHANGED
|
@@ -36,16 +36,6 @@ module Mongo
|
|
|
36
36
|
# @since 2.0.0
|
|
37
37
|
attr_reader :options
|
|
38
38
|
|
|
39
|
-
# Mongo::Options::Redacted of the options specified in the uri.
|
|
40
|
-
#
|
|
41
|
-
# @since 2.1.0
|
|
42
|
-
attr_reader :uri_options
|
|
43
|
-
|
|
44
|
-
# The servers specified in the uri.
|
|
45
|
-
#
|
|
46
|
-
# @since 2.0.0
|
|
47
|
-
attr_reader :servers
|
|
48
|
-
|
|
49
39
|
# The mongodb connection string scheme.
|
|
50
40
|
#
|
|
51
41
|
# @deprecated Will be removed in 3.0.
|
|
@@ -141,10 +131,22 @@ module Mongo
|
|
|
141
131
|
# @since 2.5.0
|
|
142
132
|
SCHEME_DELIM = '://'
|
|
143
133
|
|
|
144
|
-
# Placeholder used in place of cleartext credentials
|
|
145
|
-
# rendered for display, logging, or error
|
|
134
|
+
# Placeholder used in place of cleartext credentials and sensitive URI
|
|
135
|
+
# option arguments when a URI is rendered for display, logging, or error
|
|
136
|
+
# reporting. The same marker is used for every sensitive value in a
|
|
137
|
+
# representation, as required by the connection string specification.
|
|
146
138
|
CREDENTIALS_PLACEHOLDER = '<credentials>'
|
|
147
139
|
|
|
140
|
+
# URI options whose values are sensitive and are replaced with
|
|
141
|
+
# {CREDENTIALS_PLACEHOLDER} in string representations.
|
|
142
|
+
#
|
|
143
|
+
# authMechanismProperties is hidden as a whole: any of its properties may
|
|
144
|
+
# carry a credential (for example the AWS session token), and the driver
|
|
145
|
+
# cannot know which properties a future mechanism will treat as secret.
|
|
146
|
+
#
|
|
147
|
+
# @since 2.26.2
|
|
148
|
+
SENSITIVE_URI_OPTIONS = %w[tlsCertificateKeyFilePassword authMechanismProperties].freeze
|
|
149
|
+
|
|
148
150
|
# Pattern matching the userinfo portion of a MongoDB connection string.
|
|
149
151
|
# Anchors at the start and is bounded to the authority component (stops at
|
|
150
152
|
# the first '/', '?', or '#'), but matches greedily up to the last '@' in
|
|
@@ -229,11 +231,15 @@ module Mongo
|
|
|
229
231
|
REPEATABLE_OPTIONS = %i[tag_sets ssl]
|
|
230
232
|
|
|
231
233
|
# Replace the userinfo portion of a MongoDB connection string with a
|
|
232
|
-
# placeholder so the result can
|
|
233
|
-
#
|
|
234
|
+
# placeholder so the result can be logged, displayed, or embedded in an
|
|
235
|
+
# exception message.
|
|
234
236
|
#
|
|
235
|
-
# The input is matched as a string, not parsed
|
|
236
|
-
#
|
|
237
|
+
# The input is matched as a string, not parsed. It is a best-effort
|
|
238
|
+
# helper for strings that are already known to be well-formed; it must not
|
|
239
|
+
# be used to make a parse failure safe, because a string the parser
|
|
240
|
+
# rejected may not have delimited its credentials with '@'. Diagnostics
|
|
241
|
+
# for parse failures must not include any part of the input; see
|
|
242
|
+
# {InvalidURI}.
|
|
237
243
|
#
|
|
238
244
|
# @param [ String ] string The raw URI string.
|
|
239
245
|
#
|
|
@@ -270,8 +276,7 @@ module Mongo
|
|
|
270
276
|
when MONGODB_SRV_SCHEME
|
|
271
277
|
SRVProtocol.new(string, opts)
|
|
272
278
|
else
|
|
273
|
-
raise Error::InvalidURI.new(string,
|
|
274
|
-
"Invalid scheme '#{scheme}'. Scheme must be '#{MONGODB_SCHEME}' or '#{MONGODB_SRV_SCHEME}'")
|
|
279
|
+
raise Error::InvalidURI.new(string, INVALID_SCHEME)
|
|
275
280
|
end
|
|
276
281
|
end
|
|
277
282
|
|
|
@@ -286,6 +291,8 @@ module Mongo
|
|
|
286
291
|
#
|
|
287
292
|
# @since 2.0.0
|
|
288
293
|
def client_options
|
|
294
|
+
ensure_not_redacted_serialization!
|
|
295
|
+
|
|
289
296
|
opts = uri_options.tap do |opts|
|
|
290
297
|
opts[:database] = @database if @database
|
|
291
298
|
end
|
|
@@ -318,7 +325,7 @@ module Mongo
|
|
|
318
325
|
@options = options
|
|
319
326
|
parsed_scheme, _, remaining = string.partition(SCHEME_DELIM)
|
|
320
327
|
unless parsed_scheme == scheme
|
|
321
|
-
raise_invalid_error!("Invalid scheme
|
|
328
|
+
raise_invalid_error!("Invalid scheme. Scheme must be '#{MONGODB_SCHEME}'. Use URI#get to parse SRV URIs.")
|
|
322
329
|
end
|
|
323
330
|
raise_invalid_error!('No hosts in the URI') if remaining.empty?
|
|
324
331
|
parse!(remaining)
|
|
@@ -336,9 +343,29 @@ module Mongo
|
|
|
336
343
|
#
|
|
337
344
|
# @since 2.0.0
|
|
338
345
|
def credentials
|
|
346
|
+
ensure_not_redacted_serialization!
|
|
347
|
+
|
|
339
348
|
{ user: @user, password: @password }
|
|
340
349
|
end
|
|
341
350
|
|
|
351
|
+
# Get the servers provided in the URI.
|
|
352
|
+
#
|
|
353
|
+
# @return [ Array<String> ] The servers.
|
|
354
|
+
def servers
|
|
355
|
+
ensure_not_redacted_serialization!
|
|
356
|
+
|
|
357
|
+
@servers
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Get the URI options.
|
|
361
|
+
#
|
|
362
|
+
# @return [ Mongo::Options::Redacted ] The URI options.
|
|
363
|
+
def uri_options
|
|
364
|
+
ensure_not_redacted_serialization!
|
|
365
|
+
|
|
366
|
+
@uri_options
|
|
367
|
+
end
|
|
368
|
+
|
|
342
369
|
# Get the database provided in the URI.
|
|
343
370
|
#
|
|
344
371
|
# @example Get the database.
|
|
@@ -348,21 +375,30 @@ module Mongo
|
|
|
348
375
|
#
|
|
349
376
|
# @since 2.0.0
|
|
350
377
|
def database
|
|
378
|
+
ensure_not_redacted_serialization!
|
|
379
|
+
|
|
351
380
|
@database || Database::ADMIN
|
|
352
381
|
end
|
|
353
382
|
|
|
354
|
-
# Get the uri as a string with
|
|
383
|
+
# Get the uri as a string with credentials and sensitive option arguments
|
|
384
|
+
# redacted.
|
|
355
385
|
#
|
|
356
|
-
#
|
|
357
|
-
#
|
|
358
|
-
#
|
|
386
|
+
# Every sensitive value, including the arguments of
|
|
387
|
+
# {SENSITIVE_URI_OPTIONS}, is replaced with the same
|
|
388
|
+
# {CREDENTIALS_PLACEHOLDER} marker, so the result is safe to log or
|
|
389
|
+
# display. The representation is built from the parsed components. Use
|
|
390
|
+
# {#credentials} and {#uri_options} to obtain the real values.
|
|
359
391
|
#
|
|
360
392
|
# @example Get the uri as a string.
|
|
361
393
|
# uri.to_s
|
|
362
394
|
#
|
|
363
395
|
# @return [ String ] The redacted uri string.
|
|
364
396
|
def to_s
|
|
365
|
-
|
|
397
|
+
if redacted_serialization?
|
|
398
|
+
@redacted_uri
|
|
399
|
+
else
|
|
400
|
+
reconstruct_uri
|
|
401
|
+
end
|
|
366
402
|
end
|
|
367
403
|
|
|
368
404
|
# Return a redacted, human-readable representation of the URI. The
|
|
@@ -371,24 +407,99 @@ module Mongo
|
|
|
371
407
|
#
|
|
372
408
|
# @return [ String ] The redacted inspect string.
|
|
373
409
|
def inspect
|
|
374
|
-
"#<#{self.class.name}: #{
|
|
410
|
+
"#<#{self.class.name}: #{self}>"
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
# Return the redacted URI for ActiveSupport's JSON encoding.
|
|
414
|
+
#
|
|
415
|
+
# ActiveSupport calls this method when encoding the URI (directly or as a
|
|
416
|
+
# value in a Hash or Array). The result is built from the parsed
|
|
417
|
+
# components and contains the credentials and sensitive option arguments
|
|
418
|
+
# as redaction markers.
|
|
419
|
+
#
|
|
420
|
+
# @param [ Hash | nil ] _options The ActiveSupport options.
|
|
421
|
+
#
|
|
422
|
+
# @return [ String ] The redacted URI.
|
|
423
|
+
def as_json(_options = nil)
|
|
424
|
+
to_s
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# Serialize the URI in a redacted form.
|
|
428
|
+
#
|
|
429
|
+
# Serialized URIs are redacted representations and are intentionally not
|
|
430
|
+
# restored as usable connection configurations: methods that would expose
|
|
431
|
+
# or use credentials and connection options raise {Error::InvalidURI}.
|
|
432
|
+
# The redacted string is still available through {#to_s} and {#inspect}.
|
|
433
|
+
#
|
|
434
|
+
# @param [ Psych::Coder ] coder The YAML coder.
|
|
435
|
+
def encode_with(coder)
|
|
436
|
+
coder.tag = "!ruby/object:#{self.class.name}"
|
|
437
|
+
coder['redacted_uri'] = to_s
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
# Initialize a redacted URI from YAML.
|
|
441
|
+
#
|
|
442
|
+
# @param [ Psych::Coder ] coder The YAML coder.
|
|
443
|
+
def init_with(coder)
|
|
444
|
+
initialize_redacted_serialization(coder['redacted_uri'])
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# Dump the URI for Marshal in a redacted form.
|
|
448
|
+
#
|
|
449
|
+
# @return [ Hash ] The serialized representation.
|
|
450
|
+
def marshal_dump
|
|
451
|
+
{ 'redacted_uri' => to_s }
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
# Load a redacted URI from Marshal.
|
|
455
|
+
#
|
|
456
|
+
# @param [ Hash ] hash The serialized representation.
|
|
457
|
+
def marshal_load(hash)
|
|
458
|
+
initialize_redacted_serialization(hash['redacted_uri'])
|
|
375
459
|
end
|
|
376
460
|
|
|
377
461
|
private
|
|
378
462
|
|
|
379
|
-
#
|
|
380
|
-
#
|
|
463
|
+
# Sets the state of a URI restored from a serialized representation.
|
|
464
|
+
#
|
|
465
|
+
# @param [ String | nil ] redacted_uri The redacted URI string.
|
|
466
|
+
def initialize_redacted_serialization(redacted_uri)
|
|
467
|
+
@redacted_serialization = true
|
|
468
|
+
@redacted_uri = redacted_uri || '<redacted connection string>'
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# @return [ true | false ] Whether this object was restored from a
|
|
472
|
+
# redacted serialized representation.
|
|
473
|
+
def redacted_serialization?
|
|
474
|
+
!@redacted_serialization.nil? && @redacted_serialization
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
# Raises when a redacted serialized URI is used as a connection
|
|
478
|
+
# configuration.
|
|
479
|
+
def ensure_not_redacted_serialization!
|
|
480
|
+
return unless redacted_serialization?
|
|
481
|
+
|
|
482
|
+
raise Error::InvalidURI.new(
|
|
483
|
+
nil,
|
|
484
|
+
'This connection string was redacted when it was serialized and cannot be used to connect.'
|
|
485
|
+
)
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
# Reconstruct the URI from its parts with credentials and sensitive option
|
|
489
|
+
# arguments redacted. Invalid options are dropped and options are converted
|
|
490
|
+
# to camelCase.
|
|
381
491
|
#
|
|
382
492
|
# @return [ String ] the uri.
|
|
383
493
|
def reconstruct_uri
|
|
384
494
|
servers = @servers.join(',')
|
|
385
495
|
options = options_mapper.ruby_to_string(@uri_options).map do |k, vs|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
496
|
+
next if vs.nil?
|
|
497
|
+
|
|
498
|
+
vs = CREDENTIALS_PLACEHOLDER if SENSITIVE_URI_OPTIONS.include?(k)
|
|
499
|
+
if vs.is_a?(Array)
|
|
500
|
+
vs.map { |v| "#{k}=#{v}" }.join('&')
|
|
501
|
+
else
|
|
502
|
+
"#{k}=#{vs}"
|
|
392
503
|
end
|
|
393
504
|
end.compact.join('&')
|
|
394
505
|
|
|
@@ -412,7 +523,7 @@ module Mongo
|
|
|
412
523
|
end
|
|
413
524
|
|
|
414
525
|
hosts, db = hosts_and_db.split('/', 2)
|
|
415
|
-
raise_invalid_error!(
|
|
526
|
+
raise_invalid_error!('Database name contains an unescaped slash (/).') if db && db.index('/')
|
|
416
527
|
|
|
417
528
|
if hosts.index('@')
|
|
418
529
|
creds, hosts = hosts.split('@', 2)
|
|
@@ -447,18 +558,37 @@ module Mongo
|
|
|
447
558
|
uri_options = {}
|
|
448
559
|
return uri_options unless string
|
|
449
560
|
|
|
561
|
+
offset = 0
|
|
450
562
|
string.split('&').each do |option_str|
|
|
563
|
+
current_offset = offset
|
|
564
|
+
offset += option_str.length + 1
|
|
451
565
|
next if option_str.empty?
|
|
452
566
|
|
|
453
567
|
key, value = option_str.split('=', 2)
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
568
|
+
decoded_key = decode(key)
|
|
569
|
+
if value.nil?
|
|
570
|
+
raise_invalid_error!(option_without_value_message(decoded_key))
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
options_mapper.add_uri_option(
|
|
574
|
+
decoded_key, decode(value), uri_options, offset: current_offset
|
|
575
|
+
)
|
|
458
576
|
end
|
|
459
577
|
uri_options
|
|
460
578
|
end
|
|
461
579
|
|
|
580
|
+
# Builds a diagnostic for an option that has no value. Only a canonical
|
|
581
|
+
# name of a key the driver knows may appear: an unknown key can contain
|
|
582
|
+
# text the user intended as a credential.
|
|
583
|
+
def option_without_value_message(key)
|
|
584
|
+
canonical = OptionsMapper::URI_OPTION_CANONICAL_NAMES[key.downcase]
|
|
585
|
+
if canonical
|
|
586
|
+
"Option #{canonical} has no value"
|
|
587
|
+
else
|
|
588
|
+
'Option has no value'
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
|
|
462
592
|
def parse_user!(string)
|
|
463
593
|
if string && (user = string.partition(AUTH_USER_PWD_DELIM)[0])
|
|
464
594
|
raise_invalid_error!(UNESCAPED_USER_PWD) if UNSAFE.match?(user)
|
|
@@ -543,14 +673,17 @@ module Mongo
|
|
|
543
673
|
if uri_options[:write_concern] && !uri_options[:write_concern].empty?
|
|
544
674
|
begin
|
|
545
675
|
WriteConcern.get(uri_options[:write_concern])
|
|
546
|
-
rescue Error::InvalidWriteConcern
|
|
547
|
-
|
|
676
|
+
rescue Error::InvalidWriteConcern
|
|
677
|
+
# The write concern error quotes the option arguments, which came
|
|
678
|
+
# from the connection string. Report a fixed message instead and
|
|
679
|
+
# drop the cause so the arguments cannot surface through it.
|
|
680
|
+
raise Error::InvalidURI.new(@string, 'Invalid write concern options.'), cause: nil
|
|
548
681
|
end
|
|
549
682
|
end
|
|
550
683
|
|
|
551
684
|
if uri_options[:direct_connection]
|
|
552
685
|
if uri_options[:connect] && uri_options[:connect].to_s != 'direct'
|
|
553
|
-
raise_invalid_error_no_fmt!(
|
|
686
|
+
raise_invalid_error_no_fmt!('directConnection=true cannot be used with a connect option other than direct')
|
|
554
687
|
end
|
|
555
688
|
raise_invalid_error_no_fmt!('directConnection=true cannot be used with multiple seeds') if servers.length > 1
|
|
556
689
|
elsif uri_options[:direct_connection] == false && uri_options[:connect].to_s == 'direct'
|
data/lib/mongo/version.rb
CHANGED