Class: Mongo::URI::SRVProtocol
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
URI
|
|
|
Instance Chain:
self,
URI
|
|
| Inherits: |
URI
|
| Defined in: | lib/mongo/uri/srv_protocol.rb |
Overview
Parser for a ::Mongo::URI using the mongodb+srv protocol, which specifies a DNS to query for SRV records.
The driver will query the DNS server for SRV records on ::Mongo::Client.
The driver also queries for a TXT record providing default connection string options.
Only one TXT record is allowed, and only a subset of ::Mongo::Client options is allowed.
Please refer to the Initial DNS Seedlist Discovery spec for details.
Constant Summary
-
DOT_PARTITION =
# File 'lib/mongo/uri/srv_protocol.rb', line 76Deprecated.
'.' -
FORMAT =
# File 'lib/mongo/uri/srv_protocol.rb', line 102
'mongodb+srv://[username:password@]host[/[database][?options]]' -
INVALID_DOMAIN =
# File 'lib/mongo/uri/srv_protocol.rb', line 94Deprecated.
'The domain name must consist of at least two parts: the domain name, ' + 'and a TLD.'
-
INVALID_HOST =
# File 'lib/mongo/uri/srv_protocol.rb', line 83
'One and only one host is required in a connection string with the ' + "'#{MONGODB_SRV_SCHEME}' protocol."
-
INVALID_PORT =
# File 'lib/mongo/uri/srv_protocol.rb', line 88
'It is not allowed to specify a port in a connection string with the ' + "'#{MONGODB_SRV_SCHEME}' protocol."
-
NO_SRV_RECORDS =
# File 'lib/mongo/uri/srv_protocol.rb', line 99
"The DNS query returned no SRV records for '%s'" -
VALID_TXT_OPTIONS =
# File 'lib/mongo/uri/srv_protocol.rb', line 79
%w[replicaset authsource loadbalanced].freeze
Instance Attribute Summary
-
#query_hostname ⇒ String
readonly
Internal use only
Internal use only
The hostname that is specified in the
::Mongo::URIand used to look up SRV records. - #srv_records readonly
- #srv_result ⇒ Srv::Result readonly Internal use only Internal use only
Instance Method Summary
-
#client_options ⇒ Hash
Gets the options hash that needs to be passed to a
::Mongo::Clienton instantiation, so we don't have to merge the txt record options, credentials, and database in at that point - we only have a single point here. -
#get_txt_options(hostname) ⇒ Hash
private
Obtains the TXT options of a host.
-
#parse!(remaining)
private
Parses the credentials from the
::Mongo::URIand performs DNS queries to obtain the hosts and TXT options. -
#parse_txt_options!(string) ⇒ Hash
private
Parses the TXT record options into a hash and adds the options to set of all
::Mongo::URIoptions parsed. -
#raise_invalid_error!(details)
private
Raises an InvalidURI error.
-
#resolver ⇒ Mongo::Srv::Resolver
private
Gets the SRV resolver.
-
#scheme ⇒ String
private
Gets the MongoDB SRV
::Mongo::URIscheme. -
#validate_srv_hostname(hostname)
private
Validates the hostname used in an SRV URI.
- #validate_uri_options! private
Instance Attribute Details
#query_hostname ⇒ String (readonly)
The hostname that is specified in the ::Mongo::URI and used to look up
SRV records.
This attribute needs to be defined because SRVProtocol changes
#servers to be the result of the lookup rather than the hostname
specified in the ::Mongo::URI.
# File 'lib/mongo/uri/srv_protocol.rb', line 68
attr_reader :query_hostname
#srv_records (readonly)
# File 'lib/mongo/uri/srv_protocol.rb', line 35
attr_reader :srv_records
#srv_result ⇒ Srv::Result (readonly)
# File 'lib/mongo/uri/srv_protocol.rb', line 56
attr_reader :srv_result
Instance Method Details
#client_options ⇒ Hash
Gets the options hash that needs to be passed to a ::Mongo::Client on instantiation, so we
don't have to merge the txt record options, credentials, and database in at that point -
we only have a single point here.
# File 'lib/mongo/uri/srv_protocol.rb', line 47
def opts = @txt_options.merge(ssl: true) opts = opts.merge().merge(database: database) @user ? opts.merge(credentials) : opts end
#get_txt_options(hostname) ⇒ Hash (private)
Obtains the TXT options of a host.
# File 'lib/mongo/uri/srv_protocol.rb', line 193
def (hostname) = resolver.(hostname) if () else {} end end
#parse!(remaining) (private)
Parses the credentials from the ::Mongo::URI and performs DNS queries to obtain
the hosts and TXT options.
# File 'lib/mongo/uri/srv_protocol.rb', line 138
def parse!(remaining) super raise_invalid_error!(INVALID_HOST) if @servers.length != 1 hostname = @servers.first validate_srv_hostname(hostname) @query_hostname = hostname log_debug "attempting to resolve #{hostname}" @srv_result = resolver.get_records( hostname, [:srv_service_name] || [:srv_service_name], [:srv_max_hosts] || [:srv_max_hosts] ) raise Error::NoSRVRecords.new(NO_SRV_RECORDS % hostname) if srv_result.empty? @txt_options = (hostname) || {} records = srv_result.address_strs records.each do |record| validate_address_str!(record) end @servers = records rescue Error::InvalidAddress => e raise_invalid_error!(e.) end
#parse_txt_options!(string) ⇒ Hash (private)
Parses the TXT record options into a hash and adds the options to set of all ::Mongo::URI options
parsed.
# File 'lib/mongo/uri/srv_protocol.rb', line 211
def (string) string.split(INDIV_URI_OPTS_DELIM).each_with_object({}) do |opt, | raise Error::InvalidTXTRecord.new(INVALID_OPTS_VALUE_DELIM) unless opt.index(URI_OPTS_VALUE_DELIM) key, value = opt.split('=') unless VALID_TXT_OPTIONS.include?(key.downcase) msg = "TXT records can only specify the options [#{VALID_TXT_OPTIONS.join(', ')}]: #{string}" raise Error::InvalidTXTRecord.new(msg) end .add_uri_option(key, value, ) end end
#raise_invalid_error!(details) (private)
Raises an InvalidURI error.
# File 'lib/mongo/uri/srv_protocol.rb', line 116
def raise_invalid_error!(details) raise Error::InvalidURI.new(@string, details, FORMAT) end
#resolver ⇒ Mongo::Srv::Resolver (private)
Gets the SRV resolver. If domain verification fails or no SRV records are found, an error must not be raised per the spec; instead, a warning is logged.
#scheme ⇒ String (private)
Gets the MongoDB SRV ::Mongo::URI scheme.
# File 'lib/mongo/uri/srv_protocol.rb', line 107
def scheme MONGODB_SRV_SCHEME end
#validate_srv_hostname(hostname) (private)
Validates the hostname used in an SRV URI.
The hostname cannot include a port.
The hostname must not begin with a dot, end with a dot, or have consecutive dots. The hostname must have a minimum of 1 component
Raises Error::InvalidURI if validation fails.
# File 'lib/mongo/uri/srv_protocol.rb', line 173
def validate_srv_hostname(hostname) raise_invalid_error!(INVALID_PORT) if hostname.include?(HOST_PORT_DELIM) raise_invalid_error!("Hostname cannot start with a dot: #{hostname}") if hostname.start_with?('.') raise_invalid_error!("Hostname cannot end with a dot: #{hostname}") if hostname.end_with?('.') parts = hostname.split('.') raise_invalid_error!("Hostname cannot have consecutive dots: #{hostname}") if parts.any?(&:empty?) return unless parts.length < 1 raise_invalid_error!("Hostname cannot be empty: #{hostname}") end
#validate_uri_options! (private)
# File 'lib/mongo/uri/srv_protocol.rb', line 224
def if [:direct_connection] raise_invalid_error_no_fmt!('directConnection=true is incompatible with SRV URIs') end super end